// JavaScript Document




function FlashWriter(url, width, height)
{
	// Options here are "window", "opaque", and "transparent"
	var DEFAULT_WINDOW_SETTING = "window";

	// Defaults for optional stuff below
	var quality = "high";
	var id = "flash" + Math.floor(Math.random()*100000);
	var wmode = DEFAULT_WINDOW_SETTING;
	var script_access = "sameDomain";
	var allow_fullscreen = "false";
	var params = null;
	var has_flash = LookForFlashPlugin();

	this.SetQuality = function(new_quality)
	{
		quality = new_quality;
	}

	this.SetID = function(new_id)
	{
		id = new_id;
	}
	
	this.GetId = function()
	{
		return id;	
	}

	this.SetTransparent = function(is_transparent)
	{
		wmode = is_transparent ? "transparent" : DEFAULT_WINDOW_SETTING;
	}
	
	this.SetOpaque = function(is_opaque)
	{
		wmode = is_opaque ? "opaque" : DEFAULT_WINDOW_SETTING;
	}

	this.SetFullScreen = function(fullscreen)
	{
		allow_fullscreen = fullscreen ? "true" : "false";
	}

	this.SetScriptAccess = function(new_script_access)
	{
		script_access = new_script_access;
	}

	this.SetParams = function(new_params)
	{
		params = new_params;
	}

	this.ToString = function()
	{
		var str = "";

		if(has_flash)
		{
			str += '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="' + width + '" height="' + height + '" id="' + id + '">';

			str += '<param name="allowScriptAccess" value="' + script_access + '" />';
			str += '<param name="allowFullScreen" value="' + allow_fullscreen + '" />';
			str += '<param name="movie" value="' + url + '" /><param name="quality" value="' + quality + '" />';
			str += '<param name="wmode" value="' + wmode + '" />';

			if(params != null)
			{
				// IE needs this
				str += '<param name="flashvars" value="' + params + '" />';
			}

			str += '<embed src="' + url + '" quality="' + quality + '" ';

			if(params != null)
			{
				// Non-IE browsers need this
				str += 'flashvars="' + params + '" ';
			}

			str += 'wmode="' + wmode + '" width="' + width + '" height="' + height + '" name="' + id + '" allowScriptAccess="' + script_access + '" allowFullScreen="' + allow_fullscreen + '" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
			str += '</object>';
		}
		else
		{
			str += '<p style="text-align: center; margin-top: 2em; margin-bottom: 2em; padding-top: 3em; padding-bottom: 3em; background: #333333">You don\'t appear to have <a target="_blank" href="http://getflash.ngfiles.com">Flash</a> installed. <a target="_blank" href="http://getflash.ngfiles.com">Click here</a> to get it (it\'s free).</p>';
		}

		return(str);
	}

	this.Print = function()
	{
		document.write(this.ToString());
	}

	function LookForFlashPlugin()
	{
		var flash_versions = 12;

		// Code swiped from http://www.dangrossman.info/2007/01/03/detecting-flash-and-java-with-javascript/
		if (navigator.plugins && navigator.plugins.length) {
			// Netscape style plugin detection
			for (x = 0; x <navigator.plugins.length; x++) {
				if (navigator.plugins[x].name.indexOf('Shockwave Flash') != -1) {
					return(true);
				}
			}
		}
		else if (window.ActiveXObject) {
			// ActiveX style plugin detection
			for (x = 2; x <= flash_versions; x++) {
				try {
					oFlash = eval("new ActiveXObject('ShockwaveFlash.ShockwaveFlash." + x + "');");
					if (oFlash) {
						return(true);
					}
				}
				catch(e) { }
			}
		}
		
		return(false);
	}
}


var htmlBody;
var gameDiv;
var gameDiv_name = "holderdiv";
var gameClass_name = "GameWindow";


function openGame(url, width, height)
	{
	//create a child node with black background	

	htmlBody = document.getElementsByTagName("body").item(0);

	var flash = new FlashWriter(url, width, height);
	var strg = flash.ToString();
	
	var toadd = '';
	toadd += '<div id="'+gameDiv_name+'" class="'+gameClass_name+'" style="width:'+BodyWidth()+'px;height:'+BodyHeight()+'px">';

	toadd += '<center><table class="InnerGameWindowFrame" cellpadding="0" cellspacing="0"><tr><td class="InnerGameWindowTop" align="right">';
	toadd += '<a href="#" onClick="closeGame();return false;">Close</a>';
	toadd += '</td></tr><tr><td class="InnerGameWindowMiddle" align="center"><div class="DivTest" style="width:'+width+'px;height:'+height+'px;">';
	toadd += strg;
	toadd += '</div>';
//	toadd += '<div class="InnerGameMessage">My Message</div>';
	toadd += '</td></tr><tr><td class="InnerGameWindowBottom" align="center">';
	toadd += '<div class="Ad_728x90" style="height:102px;" id="TheSpace"></div>';
//	toadd += '<div class="Ad_728x90" style="height:102px;" id="TheSpace2"></div>';
//	toadd += '<form id="TheForm" method="post" target="TheFrame" action="adparser.php" style="height:0px;width:50px;padding:0px;margin:0xp;"><input type="hidden" value="TheSpace" name="thediv"><input type="hidden" value="1" name="thead" id="TheAdNumber"></form>';
//	toadd += '<iframe id="TheFrame" name="TheFrame" style="height:100px;width:50px;padding:0px;margin:0xp;border:0px;"></iframe>';
	toadd += '</td></tr></table></center>';

	toadd += '</div>';

	htmlBody.innerHTML += toadd;
	gameDiv = document.getElementById(gameDiv_name);
	STARTAD();
	}
function closeGame()
	{
	if(gameDiv != null)
		{
		gameDiv.innerHTML = '';
		htmlBody.removeChild(gameDiv);
		if(t != null)
			{
			clearTimeout(t);	
			}
		}
	}
var xmlhttp=null;

var looper = true;
if(navigator.appName == "Microsoft Internet Explorer")
	{
	looper = false;	
	}
var ads = 2;
var t = null;
function STARTAD()
	{/*
	if (window.XMLHttpRequest)
		{// code for IE7, Firefox, Opera, etc.
		xmlhttp=new XMLHttpRequest();
		}
	else if (window.ActiveXObject)
		{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
//	document.getElementById('TheSpace').innerHTML += '<table><tr><td>tried</td></tr></table>';
	var x = ADHERE();
	var r = new String (Math.random()*1000);
	xmlhttp.onreadystatechange=state_Change;
	xmlhttp.open("GET", "adparser.php?ad="+x+"&r="+r,true);
	xmlhttp.send(null);
//	document.getElementById('thead').value = x;
//	document.getElementById('TheForm').submit();
	if(looper)
		{
		t = window.setTimeout('STARTAD()', 40000);
		}*/
	}
	
function state_Change()
{
//	alert('something');
if (xmlhttp.readyState==4)
  {// 4 = "loaded"
  if (xmlhttp.status==200)
    {// 200 = "OK"
	var ptext = xmlhttp.responseText;
	
	document.getElementById('TheSpace').innerHTML = ptext;
	execJS(document.getElementById('TheSpace'), ptext);
    }
  else
    {
    alert("Problem retrieving XML data:" + xmlhttp.statusText + " "+xmlhttp.status);
    }
  }
}
var myspec;
function execJS(node, text)
	{
	var ff = text.split('ript\'>');
	var nf = ff[1].split('</scr');

	strExec = nf[0];
	strExec = strExec.replace("document.write(s);", "myspec = s;");

    try
		{
		eval(strExec);
//		myspec;
	  
		var y = document.createElement("script");
		y.language = "JavaScript";
		y.type = "text/javascript";

		var mmm = myspec.split('src =');
		var mm2 = mmm[1].split('>');

		y.src = mm2[0];
	
		document.write = function specailAlert(msg)
			{
			node.innerHTML += msg;
			}
		node.appendChild(y);
	
		return;
		}
	catch(e)
		{
		alert(e);
		}
		
	};

function ADHERE()
	{
	
	var x = readCookie('curad');
	if(x == null)
		{
		x = Math.floor(Math.random()*ads)// If ads = 2, then results would be 0-1, +1 would make it 1-2
		x++;//Add one, since 0 can't be an ad.
		}
	x++;
	if(x > ads)
		{//Reset ad to 1 if it's over the amount of ads we have
		x = 1;
		}
	createCookie('curad', x, 1);
	return x;
	//tell the <form> to submit a post to the <iframe> and let the iframe reutrn the stuff to the div...		
	}
function createCookie(name,value,days)
	{
	if (days)
		{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
		}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
	}
function readCookie(name)
	{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
		{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
	return null;
	}

function login()
	{
	
	//When press login, it will do the login script in the iframe.
	//And replace the log in area with a LOADING...
	
	//If login worked, 
		//reload page, else
		//Display "wrong username"
    //window.location.reload(true);
	}


function BodyWidth()
	{
	var width = 0;

	if(window.innerHeight && window.scrollMaxY)
		{
		width = document.body.scrollWidth;
		}
	else if(document.body.scrollHeight > document.body.offsetHeight)
		{
		width = document.body.scrollWidth;
		}
	else
		{
		width = document.body.offsetWidth;
		}

	return width;
	}
	
function BodyHeight()
	{
	var height = 0;

	if(window.innerHeight && window.scrollMaxY)
		{
		height = window.innerHeight + window.scrollMaxY;
		}
	else if(document.body.scrollHeight > document.body.offsetHeight)
		{
		height = document.body.scrollHeight;
		}
	else
		{
		height = document.body.offsetHeight;
		}

	return height;
	}


function bookmark_us(url, title){
if (window.sidebar) // firefox
    window.sidebar.addPanel(title, url, "");
else if(window.opera && window.print){ // opera
    var elem = document.createElement('a');
    elem.setAttribute('href',url);
    elem.setAttribute('title',title);
    elem.setAttribute('rel','sidebar');
    elem.click();
}
else if(document.all)// ie
    window.external.AddFavorite(url, title);
}

function explain(obj, text)
	{
	var curleft = curtop = 0;
	if (obj.offsetParent)
		{
			
		do
			{
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
			}
		while (obj = obj.offsetParent);
//		return [curleft,curtop];
		}

	
	alert('position of clicked '+curleft+' '+curtop+' text: '+text);
	}
	
	
	
	
	




function inputTouch(element)
	{
	if(element.className == 'inputUnTouched')
		{ 
		element.className = 'inputTouched';
		element.value = '';
		}
	}

var ratetext = Array();
ratetext[0] = 'Please remove this game.';
ratetext[1] = 'One of the worst ever!';
ratetext[2] = 'Almost one of the worst.';
ratetext[3] = 'Bad.';
ratetext[4] = 'Below average.';
ratetext[5] = 'Average.';
ratetext[6] = 'Above average.';
ratetext[7] = 'Good.';
ratetext[8] = 'Almost one of the best.';
ratetext[9] = 'One of the best ever!';
var myrate = null;

function Rate(element)
	{
	oelement = element;
	objx = 0;
	objy = 0;
	do
		{
		objx += element.offsetLeft;
		objy += element.offsetTop;
		}while (element = element.offsetParent);


	myrate = Math.floor((mousex-objx-2)/10);
	if(myrate <= 9 && myrate >= 0)
		{
		tempClassName = 'Rank'+myrate;
		document.getElementById('RateText').innerHTML = ratetext[myrate];
		}
	else
		{
		tempClassName = currClassName;
//		document.getElementById('RateText').innerHTML = '';
		}
	if(hasRated == false)
		{
		oelement.className=tempClassName
		}
	}
function RateOn(e)
	{
	currClassName = e.className;
	hasRated = false;
	}
function RateOff(e)
	{
	oelement.className=currClassName;
	document.getElementById('RateText').innerHTML = '';
	}
var httprate=null;

function RateClick(e)
	{
	//check if logged in
	if(ggLoggedIn)
		{
		currClassName = tempClassName;
		hasRated = true;
		
		if (window.XMLHttpRequest)
			{// code for IE7, Firefox, Opera, etc.
			httprate=new XMLHttpRequest();
			}
		else if (window.ActiveXObject)
			{// code for IE6, IE5
			httprate=new ActiveXObject("Microsoft.XMLHTTP");
			}
	//	document.getElementById('TheSpace').innerHTML += '<table><tr><td>tried</td></tr></table>';
		var x = ADHERE();
		var r = new String (Math.random()*1000);
//		httprate.onreadystatechange=state_Change;
		httprate.open("GET", BASE_URL+"/games/rate.php?user_game_id="+user_game_id+"&num="+myrate,true);
		httprate.send(null);

		//que up the rating page
		//show thank you for voting popup
		alert('Thanks for rating this!');
		}
	else
		{
		//show the 'You need to login to vote!' popup
		alert('You need to login first!');
		}
	}
var currClassName = '';
var tempClassName = '';
var hasRated = false;
var mousex = 0;
var mousey = 0;
function init()
{
  document.onmousemove = getMouseXY; // update(event) implied on NS, update(null) implied on IE
  getMouseXY();
}
init();
function getMouseXY(e) // works on IE6,FF,Moz,Opera7
{ 
  if (!e) e = window.event; // works on IE, but not NS (we rely on NS passing us the event)

  if (e)
  { 
    if (e.pageX || e.pageY)
    { // this doesn't work on IE6!! (works on FF,Moz,Opera7)
      mousex = e.pageX;
      mousey = e.pageY;
      algor = '[e.pageX]';
      if (e.clientX || e.clientY) algor += ' [e.clientX] '
    }
    else if (e.clientX || e.clientY)
    { // works on IE6,FF,Moz,Opera7
      mousex = e.clientX + document.body.scrollLeft;
      mousey = e.clientY + document.body.scrollTop;
      algor = '[e.clientX]';
      if (e.pageX || e.pageY) algor += ' [e.pageX] '
    }  
  }
}
