// javascript functions - RGA
var W3CDOM = (document.createElement && document.getElementsByTagName);

var aMouseOvers = new Array();
var aMouseOuts = new Array();
var aRollovers = new Array('nav');
var aEvents = new Array();

window.onload = init;

function init() {
	if (!W3CDOM) return;
	loadRollovers();
	loadEvents();
}

/* ******************************
 * rollover scripts
 */
function loadRollovers() {
	for (var x = 0; x < aRollovers.length; x++) {
		var oRollover = document.getElementById(aRollovers[x]);
		if (oRollover) {
			// images
			var oElem = oRollover.getElementsByTagName('img');
			addRollovers(oElem, x);
			// inputs
			oElem = oRollover.getElementsByTagName('input');
			addRollovers(oElem, x);
		}
	}
}

function addRollovers(oElem, x) {
	for (var i = 0; i < oElem.length; i++) {
		var j = (x * 100) + i;
		// check class for no rollover
		if (oElem[i].className && oElem[i].className == 'noRollover') continue;
		// check if image input
		if (oElem[i].type && oElem[i].type != 'image') continue;
		
		oElem[i].onmouseover = mouseOver;
		oElem[i].onmouseout = mouseOut;
		var suffix = oElem[i].src.substring(oElem[i].src.lastIndexOf('.'));
		
		aMouseOuts[j] = new Image();
		aMouseOuts[j].src = oElem[i].src;
		aMouseOvers[j] = new Image();
		aMouseOvers[j].src = oElem[i].src.substring(0,oElem[i].src.lastIndexOf('.')) + "-over" + suffix;
		
		oElem[i].n = j;
	}
}

function mouseOver() {
	this.src = aMouseOvers[this.n].src;
}

function mouseOut() {
	this.src = aMouseOuts[this.n].src;
}

/* ******************************
 * popup scripts
 */
function loadEvents() {
	var oEvent = document.getElementsByTagName('a');
	for (var i = 0; i < oEvent.length; i++) {
		if (oEvent[i].className && oEvent[i].className == 'popup') {
			aEvents[i] = new Object();
			aEvents[i].params = 'scrollbars=1,status=0,width=550,height=400';
			oEvent[i].onclick = popWin;
			
			oEvent[i].n = i;
		}
	}
}

function popWin() {
	window.open(this.href, 'popWin', aEvents[this.n].params);
	return false;
}

function popPicatronWin()
{
	window.open('picatron.html', 'myWin', 'toolbar=no, directories=no, location=no, status=yes, menubar=no, resizable=no, scrollbars=no, width=751, height=408');   
	return false;
}	

function displayPopup(url, windowName, height, width) {

	var topPos = (screen.height - height)/2;
	var leftPos = (screen.width - width)/2;
	
	window.open(url, windowName, "height=" + height + ",width=" + width + 
		", left=" + leftPos + ", top=" + topPos +
		", menubar=no, resizable=no, scrollbars=no, status=no, titlebar=no, toolbar=no");
}

var newWindow = null;

function closeWin(){
	if (newWindow != null){
		if(!newWindow.closed)
			newWindow.close();
	}
}

function popUpWin(url, type, strWidth, strHeight){
	
	closeWin();
	
	if (type == "fullScreen"){
		strWidth = screen.availWidth - 10;
		strHeight = screen.availHeight - 160;
	}
	
	var tools="";
	if (type == "standard" || type == "fullScreen") tools = "resizable,toolbar=yes,location=yes,scrollbars=yes,menubar=yes,width="+strWidth+",height="+strHeight+",top=0,left=0";
	if (type == "console") tools = "resizable=0,toolbar=no,location=no,scrollbars=no,width="+strWidth+",height="+strHeight+",left=0,top=0";
	newWindow = window.open(url, 'newWin', tools);
	newWindow.focus();
}


function openWorldCuppa(query) {
	/*
	var height = 500;
	var width = 900;
	var topPos = (screen.height - height)/2;
	var leftPos = (screen.width - width)/2;
	*/
	var fixHeight = 28;
	var fixWidth = 8;
	var width = screen.availWidth-fixWidth;
	var height = screen.availHeight-fixHeight;
	var topPos = 0;
	var leftPos = 0;
	var strGoTo = "/halftimegames/game_popup.htm"; 
	if (query != null && query != "") {
		strGoTo += "?"+query;
	}
	var strParams = "";
	if(navigator.appName.indexOf("Netscape") != -1) {
		strParams += "screenX=0,screenY=0";
	} else {
		strParams += "left=0,top=0,fullscreen=0";
	}
	window.open(strGoTo, "myGamePopup", "toolbar=no, directories=no, location=no, status=yes, menubar=no, "+strParams+", resizable=yes, scrollbars=no, left="+leftPos+", top="+topPos+", width="+width+", height="+height);
	return false;
}

function showAnswer(x) {
	topX = document.getElementById(x);
	y=x+"_bottom"
	bottomX = document.getElementById(y);
	topX.className="col-2 clearfix games answer";
	if( (x!='heyRiddle') && (x!='howMany') )	bottomX.className="bottom_a";
}

function noLink() {
	return false;
}

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 eraseCookie(name) {
	createCookie(name,"",-1);
}

