// JavaScript Document

function URLDecode( encoded ) {

 // Replace + with ' '
 // Replace %xx with equivalent character
 // Put [ERROR] in output if %xx is invalid.
 
 var HEXCHARS = "0123456789ABCDEFabcdef"; 
 var plaintext = "";
 var i = 0;
 
 while (i < encoded.length) {
  
  var ch = encoded.charAt(i);
  
	if (ch == "+") {
	 
   plaintext += " ";
	 i++;
	} else if (ch == "%") {
	
	 if (i < (encoded.length-2) 
			 && HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
			 && HEXCHARS.indexOf(encoded.charAt(i+2)) != -1) {
			
    plaintext += unescape( encoded.substr(i,3) );
		i += 3;
	 } else {
				
    alert( 'Bad escape combination near ...' + encoded.substr(i) );
		plaintext += "%[ERROR]";
		i++;
	 }
	} else {
		  
   plaintext += ch;
	 i++;
	}
 } // while
 
 return plaintext;
}


function startPlayer ( gamedata ) {

 var gamedata = URLDecode( gamedata );
 var gamedata_array = gamedata.split( "<:jp:>" );
 
 if ( gamedata_array[3] == "<:tags:>" ) {
 
  XMLDiv = "playercode";
  document.getElementById( XMLDiv ).style.background = "#FFFFFF";
  document.getElementById( XMLDiv ).style.padding = "4px";
  document.getElementById( XMLDiv ).style.border = "1px solid #f9dc66";
  loadXMLDoc( gamedata_array[0] + "&tagc=" + gamedata_array[2] + "&tagm=" + gamedata_array[1] );
  showIt( '<:tags:>' , '<:tags:>' );
 }
 else if ( gamedata_array[3] == "<:tags1:>" ) {
  
  XMLDiv = "playercode";
  loadXMLDoc( gamedata_array[0] + "&tagc=" + gamedata_array[2] + "&tagm=" + gamedata_array[1] );
 }
 else {
 
  // get adverts - games.
  XMLDiv = "adverts";
  loadXMLDoc( 'index.php?playerads=1' ); 

  player_code = '<br><object width="'+gamedata_array[2]+'" height="'+gamedata_array[1]+'"><param name="movie" value="';
  player_code += gamedata_array[0];
  player_code += '"></param><param name="wmode" value="transparent"></param><embed src="';
  player_code += gamedata_array[0];
  player_code += '"type="application/x-shockwave-flash" wmode="transparent" width="'+gamedata_array[2]+'" height="'+gamedata_array[1]+'"></embed></object>\n\n';
  
  showIt( gamedata_array[1] , gamedata_array[2] );
  document.getElementById('playercode').innerHTML = player_code;
 }
}


function getScroll () {

 scrOfX = 0;
 scrOfY = 0;

 if( typeof( window.pageYOffset ) == 'number' ) {
  //Netscape compliant
  scrOfY = window.pageYOffset;
  scrOfX = window.pageXOffset;
 }
 else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
  //DOM compliant
  scrOfY = document.body.scrollTop;
  scrOfX = document.body.scrollLeft;
 }
 else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
  //IE6 standards compliant mode
  scrOfY = document.documentElement.scrollTop;
  scrOfX = document.documentElement.scrollLeft;
 }
}


function hideIt( scrolltoy , scrolltox ) {

 document.getElementById('player').style.visibility = 'hidden';
 document.getElementById('playercode').innerHTML = "&nbsp;";
 document.getElementById('adverts').innerHTML = "&nbsp;";
 
 window.scrollTo( scrolltox , scrolltoy );
 
 document.getElementById('body').style.overflow = 'scroll';
 document.getElementById('hide').style.visibility = 'visible';
 layer_set = 0;
 
 // rewrite google ad
 if ( document.getElementById('googlead') ) { document.getElementById('googlead').innerHTML = googleAd; }
}


function showIt( player_height , player_width ) {

 getScroll(); 
 //document.getElementById('playerheader').innerHTML = '<a href="javascript:hideIt('+scrOfY+' , '+scrOfX+');" class="author">Close this Window</a> | To move this Window, first click inside this Area, then click anywhere in the Document to move the Window to the Click Position.';
 
 document.getElementById('playerheader').innerHTML = '<span style="background:red; padding:2px;"><a href="javascript:hideIt('+scrOfY+' , '+scrOfX+');"><font size=4 color="#FFFFFF">X</font></a></span> To move this Window, first click inside this Area, then click anywhere in the Document to move the Window to the Click Position.';
 
 // determine player placement.
 if ( (player_height == "<:tags:>") && (player_width == "<:tags:>") ) { pos_left = 5; }
 else {
 
  getDim();
  pos_left = (myWidth - player_width) / 2;
  if ( pos_left < 0 ) { pos_left = 0; }
  document.getElementById('body').style.overflow = 'hidden';
  
  //player_height = parseInt(player_height, 10);
  //player_height += 150; 
  //if ( player_height < myHeight ) { document.getElementById('body').style.overflow = 'hidden'; }
 }

 document.getElementById('player').style.top = 5; 
 document.getElementById('player').style.left = pos_left; 
 document.getElementById('player').style.visibility = 'visible';
 layer_set = 1;
 
 window.scrollTo( 0 , 0 ); 
 document.getElementById('hide').style.visibility = 'hidden';
 
 //hide google ad.
 if ( document.getElementById('googlead') ) {
 
  googleAd = document.getElementById('googlead').innerHTML;
  document.getElementById('googlead').innerHTML = "&nbsp;";
 }
}


function writeIt () {

 document.write('<div id="player" style="position:absolute; left:0px; top:0px; visibility:hidden;"><div id="playerheader" onclick="javascript: register();"></div><table border=0 width=100%><tr valign="top"><td><div id="playercode"></div></td><td><div id="embrace"><div id="adverts" style="width:200px;"></div></div></td></tr></table></div>');
}


function Mausklick (Ereignis) {

 if (!Ereignis) Ereignis = window.event;
 getScroll();

 if (document.getElementById) {
  document.getElementById("player").style.left  = Ereignis.clientX + scrOfX + "px";
  document.getElementById("player").style.top = Ereignis.clientY + scrOfY + "px";
 } else if (document.all) {
  document.all.player.style.left = Ereignis.clientX + scrOfX + "px";
  document.all.player.style.top = Ereignis.clientY + scrOfY + "px";
 }

 document.onmousedown = "";
}


function getDim() {

  myWidth = 0;
  myHeight = 0;
  
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
}


function showEmbedCode ( mode , layerid , link ) {

 var whichlayer = link+layerid; 
 var whichlink = link+'code'+layerid;
 var linktext = link+' Code';

 if ( mode == "close" ) { 
 
  document.getElementById(whichlayer).style.visibility = 'hidden';
  document.getElementById(whichlayer).style.position = 'absolute';
  document.getElementById(whichlink).href = "javascript:showEmbedCode('open' , '"+layerid+"' , '"+link+"');"; 
  document.getElementById(whichlink).innerHTML = linktext; 
 }
 
 if ( mode == "open" ) {
  
  document.getElementById(whichlink).href = "javascript:showEmbedCode('close' , '"+layerid+"' , '"+link+"');";
  document.getElementById(whichlink).innerHTML = 'hide the ' + linktext;
  document.getElementById(whichlayer).style.visibility = 'visible';
  document.getElementById(whichlayer).style.position = 'relative';
 }
}


function showHighScores ( mode , game_slug , layerid ) {

 var whichlayer = 'HighScores'+layerid;
 var whichlink = 'scoreslink'+layerid;

 if ( mode == "close" ) { 
 
  document.getElementById(whichlayer).style.visibility = 'hidden';
  document.getElementById(whichlayer).innerHTML = '';
  document.getElementById(whichlink).href = "javascript:showHighScores('open' , '"+game_slug+"' , '"+layerid+"');"; 
  document.getElementById(whichlink).innerHTML = 'High Scores'; 
 }
 
 if ( mode == "open" ) {
  
  document.getElementById(whichlink).href = "javascript:showHighScores('close' , '"+game_slug+"' , '"+layerid+"');";
  document.getElementById(whichlink).innerHTML = 'hide the High Scores';
  
  game_slug = URLDecode( game_slug );
  
  var leaderboard_data = '<embed src="http://xs.mochiads.com/static/pub/swf/LeaderboardWidget.swf?game='+game_slug+'" allowscriptaccess="always" allowmenu="false" quality="high" width="400" height="400" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>';
  
  document.getElementById(whichlayer).innerHTML = leaderboard_data;
  document.getElementById(whichlayer).style.visibility = 'visible';
 }
}


// Ajax
function loadXMLDoc( docUrl ) {

 xmlhttp = false;
 var url = "http://"+window.location.host+"/"+docUrl;
   
 if (window.XMLHttpRequest)     { xmlhttp = new XMLHttpRequest(); } // Mozilla Code, etc.
 else if (window.ActiveXObject) { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } // IE Code.

 if ( xmlhttp ) {

  xmlhttp.onreadystatechange=state_Change;
  xmlhttp.open("GET",url,true);
  xmlhttp.send(null);
 }
 else {

  printXML( "Error processing your Request.<br>Your Browser does not support XMLHTTP." );
 }
}


function state_Change() {

 // xmlhttp shows loaded
 if (xmlhttp.readyState == 4) {

  if ( xmlhttp.status == 200 ) { printXML( xmlhttp.responseText ); }
  else { printXML( "Problem retrieving Data: " + xmlhttp.statusText ); }
 }
}


function printXML ( pXML ) {

 document.getElementById( XMLDiv ).innerHTML = pXML;
 
 if ( XMLDiv == "playercode" ) {
 
  // get adverts - games.
  XMLDiv = "adverts";
  loadXMLDoc( 'index.php?playerads=1' );
 }
}


function gameInfo ( mode , gameurl ) {

 var gamedata_array = gameurl.split( "<:jp:>" );
 gamedata_array[0] = URLDecode( gamedata_array[0] );
 
 if ( document.getElementById(gamedata_array[1]) ) {
 
  if ( mode == "open" ) {
  
   divcontent = document.getElementById(gamedata_array[1]).innerHTML;
   XMLDiv = gamedata_array[1];
   loadXMLDoc( gamedata_array[0] );
  }
  
  if ( mode == "close" ) {
  
   document.getElementById(gamedata_array[1]).innerHTML = divcontent;
  }
 } 
}


function register () { document.onmousedown = Mausklick; }
writeIt();



