opera.postError("Opera HbbTV user JS loading ..");

// Add onreadystatechange for XMLHttpRequests
(function(){
	var tmpInstance=new XMLHttpRequest();// because Opera doesn't support getter/setter lookup on prototype
	var orscGetter=tmpInstance.__lookupGetter__('onreadystatechange');
	var orscSetter=tmpInstance.__lookupSetter__('onreadystatechange');
	XMLHttpRequest.prototype.__defineGetter__('onreadystatechange', function(){
		return orscGetter.call(this);
	});
	XMLHttpRequest.prototype.__defineSetter__('onreadystatechange', function( actualListener ){
		var intermediateListener = function(){
			actualListener.call(this, {target: this});
		}
		return orscSetter.call(this, intermediateListener);
	});
})();

//set overflow:hidden needs to be done on both <html> and <body>
window.addEventListener('load', function() {
    if (document.getElementsByTagName('body')[0].style.overflow == 'hidden') {
        document.getElementsByTagName('html')[0].style.overflow = 'hidden';
    }
}, false);


/////////////////////////////////////////////////////////////////////////////////////////////
/////////        Local Key Set
LocalKeySet = {
	VK_RED : 66,
	VK_GREEN : 67,
	VK_YELLOW : 68,
	VK_BLUE : 69,

	/////////////////////////
	VK_PLAY : 81,
	VK_PAUSE : 10,
	VK_STOP : 82,

	VK_FAST : 90, //FASTFORWARD
	VK_SLOW : 78, //UNUSED
	VK_PREV:34, //P-
	VK_NEXT:33, //P+

	/////////////////////////
	VK_MENU : 18,
	VK_RECALL:120, //UNUSED
	
	VK_INFO:20,
	VK_PORTAL:50, //TEXT

	/////////////////////////
	VK_ENTER:13 //ENTER KEY
	//VK_ENTER:11, //OK KEY
	VK_LEFT:37,
	VK_UP:38,
	VK_RIGHT:39,
	VK_DOWN:40,

	VK_9:57,
	VK_8:56,
	VK_7:55,
	VK_6:54,
	VK_5:53,
	VK_4:52,
	VK_3:51,
	VK_2:50,
	VK_1:49,
	VK_0:48,

	/////////////////////////

	VK_MUTE:78,
	VK_VOLUME_ADD : 76,
	VK_VOLUME_SUB : 77,

	VK_EXIT : 36
}


/////////////////////////////////////////////////////////////////////////////////////////////
/////////      Event AfterEvent.load
opera.addEventListener("AfterEvent.load", function (ev)
{
  if (location.href.indexOf("http://www.viewster.tv/") != -1) {
    if (ev.event.target instanceof Document) {
	   tvKey ={
		   KEY_UP: LocalKeySet.VK_UP,
		   KEY_DOWN: LocalKeySet.VK_DOWN,
		   KEY_RIGHT: LocalKeySet.VK_RIGHT,
		   KEY_LEFT: LocalKeySet.VK_LEFT,
		   KEY_ENTER: LocalKeySet.VK_ENTER,

		   KEY_RETURN : LocalKeySet.VK_MENU,
		   KEY_PLAY : LocalKeySet.VK_PLAY,
		   KEY_STOP : LocalKeySet.VK_STOP,
		   KEY_PAUSE : LocalKeySet.VK_PAUSE,
		   KEY_FF : LocalKeySet.VK_FAST,
		   KEY_RW : LocalKeySet.VK_SLOW
	   }
    }
  }

  if (location.href.indexOf("http://itv.mit-xperts.com/zdfmediathek") != -1) {
    if (ev.event.target instanceof Document) {
		VK_RED = LocalKeySet.VK_RED;
		VK_GREEN = LocalKeySet.VK_GREEN;
		VK_YELLOW = LocalKeySet.VK_YELLOW;
		VK_BLUE = LocalKeySet.VK_BLUE;

		VK_PLAY = LocalKeySet.VK_PLAY;
		VK_PAUSE = LocalKeySet.VK_PASUE;
		VK_STOP = LocalKeySet.VK_STOP;

		VK_FAST_FWD = LocalKeySet.VK_FAST;
		VK_REWIND = LocalKeySet.VK_SLOW;

		VK_BACK = LocalKeySet.VK_MENU;
    }
  }

  if (location.href.indexOf("http://tvapp.wetter.com/") != -1) {
	  if (ev.event.target instanceof Document) {
		   inputs = document.getElementsByTagName('input');
		   currentFocus = 1;
		   inputs[currentFocus].focus();
		   isFootFocus = true;

		   document.onkeydown = function(e){
			   e = e || window.event;
			   var keycode = e.which ? e.which : e.keyCode; 

			   if (keycode ==  LocalKeySet.VK_LEFT)
			   {
				   currentFocus = (currentFocus + inputs.length - 1) % inputs.length;
				   inputs[currentFocus].focus();
				   isFootFocus = true;
				   e.preventDefault();
			   }
			   else if (keycode ==  LocalKeySet.VK_RIGHT)
			   {
				   currentFocus = (currentFocus + 1) % inputs.length;
				   inputs[currentFocus].focus();
				   isFootFocus = true;
				   e.preventDefault();
			   }
			   else if (keycode ==  LocalKeySet.VK_UP || keycode ==  LocalKeySet.VK_DOWN )
			   {
				   if(document.getElementById("s1") == null || isFootFocus)
				   {
					   e.preventDefault();
				   }

				   if(isFootFocus)
				   {
					   document.getElementById("s1").focus();
					   isFootFocus = false;
				   }
			   }
		   };
	   };
   }

},false);

/////////////////////////////////////////////////////////////////////////////////////////////
/////////      Event BeforeScript
opera.addEventListener("BeforeScript", function (ev)
{
  if (location.href.indexOf("http://mini.maxdome.de/") != -1) {
       isMsIe = function(){ return false; } 
       isOpera = function(){ return true; } 
	   isHumaxiCord = function(){ return true; }

	   newState = 0;
	   ev.element.text = ev.element.text.replace("var isCEHTML = false;","var isCEHTML = true;");
  }

  if( location.hostname.indexOf('stage.qtom.nettv.kbia.de') != -1 ){
	   ev.element.text = ev.element.text.replace('.Play','.play');
  }

  if( location.href.indexOf('http://hbbtv.ardmediathek.de/') != -1 ){

		defineKey = function(a,b){ window.KeyEvent[a]=window[a]=b; }
	    defineKey("VK_RED",LocalKeySet.VK_RED);
		defineKey("VK_GREEN",LocalKeySet.VK_GREEN);
		defineKey("VK_YELLOW",LocalKeySet.VK_YELLOW);
		defineKey("VK_BLUE",LocalKeySet.VK_BLUE);
		
		defineKey("VK_PLAYLIST_BACK",LocalKeySet.VK_SLOW);
		defineKey("VK_PLAYLIST_FORWARD",LocalKeySet.VK_FAST);
		defineKey("VK_PLAY",LocalKeySet.VK_PLAY);
		defineKey("VK_PAUSE",LocalKeySet.VK_PASUE);
//		defineKey("VK_PLAY_PAUSE",407);
		defineKey("VK_STOP",LocalKeySet.VK_STOP);
		defineKey("VK_BACK",LocalKeySet.VK_MENU);
  }

   if (location.href.indexOf("http://www.viewster.tv/") != -1) {
	   ev.element.text = ev.element.text.replace(
		   "document.getElementById(\"submenuGenre\").style.height = 34 * Data.getChannelsCount()",
		   "document.getElementById(\"submenuGenre\").style.height = 34 * Data.getChannelsCount() + 'px'"
	   );
   }

   if (location.href.indexOf("http://www.aupeo.com/") != -1) {
		VK_RED = LocalKeySet.VK_RED;
		VK_GREEN = LocalKeySet.VK_GREEN;
		VK_YELLOW = LocalKeySet.VK_YELLOW;
		VK_BLUE = LocalKeySet.VK_BLUE;

		VK_PLAY = LocalKeySet.VK_PLAY;
		VK_PAUSE = LocalKeySet.VK_PAUSE;
		VK_STOP = LocalKeySet.VK_STOP;
		VK_FAST_FWD = LocalKeySet.VK_FAST;
		VK_REWIND = LocalKeySet.VK_SLOW;

		VK_BACK = LocalKeySet.VK_MENU;

		VK_ENTER=LocalKeySet.VK_ENTER;
		VK_LEFT=LocalKeySet.VK_LEFT;
		VK_UP=LocalKeySet.VK_UP;
		VK_RIGHT=LocalKeySet.VK_RIGHT;
		VK_DOWN=LocalKeySet.VK_DOWN;

		VK_9=LocalKeySet.VK_9;
		VK_8=LocalKeySet.VK_8;
		VK_7=LocalKeySet.VK_7;
		VK_6=LocalKeySet.VK_6;
		VK_5=LocalKeySet.VK_5;
		VK_4=LocalKeySet.VK_4;
		VK_3=LocalKeySet.VK_3;
		VK_2=LocalKeySet.VK_2;
		VK_1=LocalKeySet.VK_1;
		VK_0=LocalKeySet.VK_0;
   }

   if(location.href.indexOf('http://www.yellowmap.de')>-1){   
	   for(var i=0; i<document.getElementById('Nav').children.length - 1 ;i++)
		{
			document.getElementById('Nav').children[i].children[0].style.fontSize = '18pt';
		}
   }

   if(location.href.indexOf('tvportal.humaxdigital.com')>-1){

	   if(location.href.indexOf('PortalService')>-1)       // Internet Radio
	   {
		   thisAppName = "Opera";
		   ev.element.text = ev.element.text.replace(new RegExp("window.navigator.appName","gm"),"thisAppName");

		   ev.element.text = ev.element.text.replace(
			   "window.onload = initPage;",
			   "setTimeout(initPage,3000);"
		   );

		   if(location.href.indexOf('Player')>-1)
		   {
			   ev.element.text = ev.element.text.replace(
				   "var appVersion = window.navigator.appVersion;",

				   "var audiotype= location.href.replace(/^.*mime=/,'').replace(/&.*$/,''); " +
				   "if(audiotype!= undefined && audiotype != null )" +
				   "videoElt.type='audio/' + location.href.replace(/^.*mime=/,'').replace(/&.*$/,'').toLowerCase() ;" + 
				   "var appVersion = window.navigator.appVersion;"
			   );

			   ev.element.text = ev.element.text.replace(
				   "case 0x13:", "case " + LocalKeySet.VK_PAUSE + ":"
			   );
			   ev.element.text = ev.element.text.replace(
				   "case 0x19D:", "case " + LocalKeySet.VK_STOP + ":"
			   );
			   ev.element.text = ev.element.text.replace(
				   "case 0x19F:", "case " + LocalKeySet.VK_PLAY + ":"
			   );
		   }

		   key_set = function() {
				// number
				this.keyNum0 = LocalKeySet.VK_0;
				this.keyNum1 = LocalKeySet.VK_1;
				this.keyNum2 = LocalKeySet.VK_2;
				this.keyNum3 = LocalKeySet.VK_3;
				this.keyNum4 = LocalKeySet.VK_4;
				this.keyNum5 = LocalKeySet.VK_5;
				this.keyNum6 = LocalKeySet.VK_6;
				this.keyNum7 = LocalKeySet.VK_7;
				this.keyNum8 = LocalKeySet.VK_8;
				this.keyNum9 = LocalKeySet.VK_9;
				this.keyPortal = LocalKeySet.VK_PORTAL;
				// play control
				this.keyPlay = LocalKeySet.VK_PLAY;
				this.keyPause = LocalKeySet.VK_PAUSE;
				this.keyStop = LocalKeySet.VK_STOP;
				this.keyBackward = LocalKeySet.VK_PREV;
				this.keyForward = LocalKeySet.VK_NEXT;
				// color
				this.keyRed = LocalKeySet.VK_RED;
				this.keyGreen = LocalKeySet.VK_GREEN;
				this.keyYellow = LocalKeySet.VK_YELLOW;
				this.keyBlue = LocalKeySet.VK_BLUE;
				// direction
				this.keyUp = LocalKeySet.VK_UP;
				this.keyDown = LocalKeySet.VK_DOWN;
				this.keyLeft = LocalKeySet.VK_LEFT;
				this.keyRight = LocalKeySet.VK_RIGHT;
				// controller
				this.keyOK = LocalKeySet.VK_ENTER;
				this.keyBack = LocalKeySet.VK_MENU;
				this.keyPageUp = LocalKeySet.VK_FAST;
				this.keyPageDown = LocalKeySet.VK_SLOW;
				// additional
				this.keyText = LocalKeySet.VK_INFO;
			}
	   }
   }

   if(location.href.indexOf('http://www.humaxtvportal.com')>-1){   
	   if(location.href.indexOf('YouTube')>-1)        // YouTube
	   {
            KeyEvent.VK_RED = LocalKeySet.VK_RED;
            KeyEvent.VK_GREEN = LocalKeySet.VK_GREEN;
            KeyEvent.VK_YELLOW = LocalKeySet.VK_YELLOW;
            KeyEvent.VK_BLUE = LocalKeySet.VK_BLUE;

            KeyEvent.VK_PLAY = LocalKeySet.VK_PLAY;
            KeyEvent.VK_PAUSE = LocalKeySet.VK_PAUSE;
            KeyEvent.VK_STOP = LocalKeySet.VK_STOP;

            KeyEvent.VK_FAST_FWD = LocalKeySet.VK_FAST;
            KeyEvent.VK_REWIND = LocalKeySet.VK_SLOW;

            KeyEvent.VK_BACK = LocalKeySet.VK_MENU;

            /////////////////////////
            KeyEvent.VK_ENTER=LocalKeySet.VK_ENTER;
            KeyEvent.VK_LEFT=LocalKeySet.VK_LEFT;
            KeyEvent.VK_UP=LocalKeySet.VK_UP;
            KeyEvent.VK_RIGHT=LocalKeySet.VK_RIGHT;
            KeyEvent.VK_DOWN=LocalKeySet.VK_DOWN;

            KeyEvent.VK_9=LocalKeySet.VK_9;
            KeyEvent.VK_8=LocalKeySet.VK_8;
            KeyEvent.VK_7=LocalKeySet.VK_7;
            KeyEvent.VK_6=LocalKeySet.VK_6;
            KeyEvent.VK_5=LocalKeySet.VK_5;
            KeyEvent.VK_4=LocalKeySet.VK_4;
            KeyEvent.VK_3=LocalKeySet.VK_3;
            KeyEvent.VK_2=LocalKeySet.VK_2;
            KeyEvent.VK_1=LocalKeySet.VK_1;
            KeyEvent.VK_0=LocalKeySet.VK_0;

           VK_INFO = LocalKeySet.VK_INFO;
           //VK_INFO1=???;
       }
   }

},false);

if(location.hostname.indexOf('spox.kbia.de')>-1){   
    opera.addEventListener('BeforeScript', function(ev){
        ev.element.text = ev.element.text.replace("<![CDATA[","");
        ev.element.text = ev.element.text.replace("]]>","");
        ev.element.text = ev.element.text.replace('.Play','.play');
        },
    false); 
}
