if (typeof(functionsOnload) == 'undefined')
	functionsOnload = Array(0); // for page-specific onload functions, push into this array
var W3CDOM = false;
var innerHTMLWorks = false;




// these functions are sometimes used even before the page has loaded, onmouseover, onclick and onsubmit:

function wnstt(str) // WiNdow STaTus
{
	window.status = str;
	return true;
}

function cst(area) // Click STatistics
{
	// csPagename is a global variable which should be set on all pages which use this function
	if (typeof(csPagename) == 'string' && csPagename != '')
	{
		var xhr;
		if (window.XMLHttpRequest)
			xhr = new XMLHttpRequest();
		else if (window.ActiveXObject)
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		if (xhr)
		{
			xhr.open("GET", "/clickstat.php?page=" + csPagename + "&area=" + area, true);
			xhr.send(null);
		}
	}
}
function cstr(prefix) // Click STatistics for Rotating placards
{
	cst(prefix + rotaNr);
}

function MagazineIssue(url)
{
	window.open(url, 'magazineissue', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=620,height=450');
	return false;
}

function AdvertisingInfo(url)
{
	window.open(url, 'advertise', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=620,height=450');
	return false;
}

function GRTVpopup(url)
{
	window.open(url, 'GRTVpopup', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=765,height=580');
	return false;
}

function Gullstikka()
{
	window.open('/gullstikka/', 'gullstikka', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=710,height=892');
	return false;
}

function SubmitWithoutXY(inputimage)
{
	// by using this function we can avoid the ugly x=11&y=13 in the querystring
	inputimage.form.submit();
	return false;
}

function SubmitForm(formId)
{
	// Kenneth created this to use <a> links as submit buttoms
	document.forms[formId].submit();
}

function Login()
{
	if (document.loginform)
	{
		if (!document.loginform.username.value)
		{
			document.loginform.username.focus();
			return false;
		}

		if (!document.loginform.password.value)
		{
			document.loginform.password.focus();
			return false;
		}
	}

	return true;
}
/*
$("document").ready(function () {
	console.log("Document is now ready");
});
*/
function pullDownSite()
{
	console.log("pulling down...");
	console.log(document.ready);
	$("#ontop").toggleClass("expanded");
	$("#ads_top2").toggleClass("expanded");
	if ($("#pagebody1st").length != 0)
	{
		$("#pagebody1st").toggleClass("expanded");
	}
	if ($("#pagebody").length != 0)
	{
		$("#pagebody").toggleClass("expanded");
	} 	    
}
function pullUpSite()
{
	//console.log("pulling up...");
	$("#ontop").toggleClass("expanded");
	$("#ads_top2").toggleClass("expanded");
	if ($("#pagebody1st").length != 0)
	{
		$("#pagebody1st").toggleClass("expanded");
	}
	if ($("#pagebody").length != 0)
	{
		$("#pagebody").toggleClass("expanded");
	}
}

function expandBanner(asdf)
{
	document.getElementById('ads_top2').style.height = "462px";
}

function retractBanner(asdf)
{
	document.getElementById('ads_top2').style.height = "180px";
}


function openDivLayerWithBanner(bannerId)
{
	//console.log("www: openDivLayerWithBanner");
	//console.log("www: typeof(bannerId)="+typeof(bannerId));
	//console.log("www: bannerId="+bannerId);

	// OpenX Interstitial or Floating DHTML Tag v2.8.7
	//old: alhack.php?zoneid=266
	var ox_u = 'http://openx.gamereactor.dk/www/delivery/alhack.php?bannerid='+bannerId+'&layerstyle=simple&align=center&valign=top&shifth=-6&padding=0&closebutton=f&nobg=t&noborder=t';
	if (document.context) ox_u += '&context=' + escape(document.context);

	//document.write('<'+'script type="text/javascript" src="' + ox_u + '"></scr'+'ipt>'); not a good idea, it replaces the whole page content

	var objContainer = document.getElementById('justacontainer');
	if (objContainer)
	{
		//objContainer.innerHTML += '<'+'script type="text/javascript" src="' + ox_u + '"></scr'+'ipt>'; doesn't work, script isn't loaded nor executed.
		var script = document.createElement("script");
		script.type = 'text/javascript';
		script.src = ox_u;
		objContainer.appendChild(script);
	}
}

var globalBannerDivId = false;

function writeBannerIntoGrDiv(bannerDivId, bannerHtml) // this function will be called from alhack.php
{
	//console.log("www: writeBannerIntoGrDiv");

	var objContainer = document.getElementById('justacontainer');
	if (objContainer)
	{
		globalBannerDivId = bannerDivId;
		objContainer.innerHTML += bannerHtml; // again, this won't execute javascript inside the string. We'll need more code for that:

		// override document.write so we capture output into a string instead
		var writtenOutput = '';
		document.write = function(s) { writtenOutput += s; };

		objContainer2 = document.getElementById(bannerDivId);
		if (objContainer2)
			objContainer = objContainer2;

		// loop through the container, find any <script> tags and execute them
		var d = objContainer.getElementsByTagName("script");
		var t = d.length;
		for (var i = 0; i < t; i++)
		{
			var newScript = document.createElement('script');
			newScript.type = "text/javascript";
			newScript.text = d[i].text;
			objContainer.appendChild(newScript);
		}

		// append anything that was document.written
		objContainer.innerHTML += writtenOutput;
	}
}

function closeDivLayerWithBanner()
{
	//console.log("closeDivLayerWithBanner");
	var objContainer = document.getElementById('bottomaddress');
	var objContainer2 = document.getElementById(globalBannerDivId);
	objContainer.removeChild(objContainer2);
}


// these functions are called onload or later:

function TimeThisPage()
{
	var pageTimeEnd = new Date();
	if (!W3CDOM)
		return;
	var pageTimeStart = getCookie('jsPageTimer');
	if (pageTimeStart)
	{
		pageTimeEnd = pageTimeEnd.getTime();
		var pageLoadTime = pageTimeEnd - pageTimeStart; // difference in milliseconds
		if (!isNaN(pageLoadTime))
		{
			var jsPageTimer = document.getElementById('jsPageTimer');
			if (jsPageTimer && jsPageTimer.innerHTML)
				jsPageTimer.innerHTML = (pageLoadTime / 1000);
		}
		deleteCookie('jsPageTimer');
	}
}

function unload()
{
	if (typeof(exitscript) == "string") // exitscript is only defined on a few pages
	{
		var xhr;
		if (window.XMLHttpRequest)
			xhr = new XMLHttpRequest();
		else if (window.ActiveXObject)
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		if (xhr)
		{
			xhr.open("GET", exitscript, true);
			xhr.send(null);
		}
	}
	StartPageTimer();
}

function StartPageTimer()
{
	var expires = new Date();
	var expiresSeconds = 60;
	expires.setMilliseconds(expiresSeconds * 1000);
	expires = expires.toGMTString();

	var pageTimeStart = new Date();
	setCookie('jsPageTimer', pageTimeStart.getTime(), expires, '/', false, false);
}

function setCookie(name, value, expires, path, domain, secure)
{
	var myCookie = name + "=" + escape(value) +
		((expires) ? "; expires=" + expires : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");

	document.cookie = myCookie;
}

function deleteCookie(name)
{
	var expires = new Date();
	expires.setTime(expires.getTime() - 1);
	document.cookie = name += "=; expires=" + expires.toGMTString();
}

function getCookie(name)
{
	var results = document.cookie.match(name + '=(.*)(;|$)');
	if (results)
		return unescape(results[1]);
	else
		return null;
}

/*
// from http://www.quirksmode.org/js/mouseov.html
var mouseOvers = new Array();
var mouseOuts = new Array();

function InitConsoleSwitch()
{
	var imgs = document.getElementById('topmenu');
	if (!imgs)
		return;
	imgs = imgs.getElementsByTagName('img');
	for (var i=0;i<imgs.length;i++)
	{
		if (imgs[i].src.indexOf('_on.gif') == -1) // no mouseover effect if it's already on!
		{
			mouseOuts[i] = new Image();
			mouseOuts[i].src = imgs[i].src;
			mouseOvers[i] = new Image();
			mouseOvers[i].src = imgs[i].src.substring(0,imgs[i].src.lastIndexOf('.')) + '_on.gif';
			imgs[i].number = i;
			imgs[i].onmouseover = mouseGoesOver;
			imgs[i].onmouseout = mouseGoesOut;
		}
	}
}

function mouseGoesOver()
{
	if (typeof(mouseOvers) == "object")
		this.src = mouseOvers[this.number].src;
}

function mouseGoesOut()
{
	if (typeof(mouseOvers) == "object")
		this.src = mouseOuts[this.number].src;
}
*/

var oldMenuSelection = -1;

function Submenu(objA, intIndex)
{
	if (oldMenuSelection == -1)
	{
		// gotta loop through all menu items and deselect them all
		var objAllMenus = document.getElementById('topmenu');
		objAllMenus = objAllMenus.getElementsByTagName('img');
		for (var i=0;i<objAllMenus.length;i++)
			if (objAllMenus[i].src.indexOf('_on.gif') != -1)
			{
				objAllMenus[i].src = objAllMenus[i].src.replace('_on.gif', '.gif');
				oldMenuSelection = i;
				break;
			}
	}
	else
	{
		var objTemp = document.getElementById('topmenu_'+oldMenuSelection);
		objTemp.src = objTemp.src.replace('_on.gif', '.gif');
	}

	if (oldMenuSelection != -1)
	{
		var objTemp = document.getElementById('submenu_'+oldMenuSelection);
		if (objTemp)
			objTemp.style.display = 'none';
	}


	oldMenuSelection = intIndex;

	if (objA.firstChild)
	{
		var objImg = objA.firstChild;
		objImg.src = objImg.src.substring(0,objImg.src.lastIndexOf('.')) + '_on.gif';
	}

	document.getElementById('submenu_'+intIndex).style.display = 'inline';

	objA.blur();

	return false;
}

function FixExplorerLabelBug()
{
	if (!document.getElementsByTagName)
		return;

	// official bug page here: http://support.microsoft.com/default.aspx?scid=kb;en-us;314279&Product=iep
	// suggested workaround here: http://splintor.blogspot.com/2005/09/ie-bug-with-label-related-to-select.html
	// Microsoft might have fixed this bug in IE 7, I haven't tested.
	// Either way, this workaround doesn't break anything, so we don't have to give IE 7 special treatment.
	// find all <label> elements on the page, and add an onclick function.
	var labels = document.getElementsByTagName('label');
	var nrSearched = 0;
	var nrAdded = 0;
	for (var i = 0; i < labels.length; i++)
	{
		if (labels[i].htmlFor && !labels[i].onclick)
		{
			labels[i].onclick = FocusFor;
			nrAdded++;
		}

		nrSearched++;
	}
//	alert('Lade till onclick i ' + nrAdded + ' av ' + nrSearched + ' labels');
}

function FocusFor()
{
	document.getElementById(this.htmlFor).focus();
	return false;
}

function jshtmlspecialchars(str)
{
	return str.replace('&','&amp;').replace('"','&quot;').replace('<','&lt;').replace('>','&gt;');
}

function jsurlencode(strArg)
{
	return escape(strArg).replace(/%20/g, '+').replace(/\//g, '%2F');
}


function onDomLoaded()
{
	onDomLoaded.done = true;

	W3CDOM = (document.createElement && document.getElementsByTagName);

	if (W3CDOM)
	{
		var container = document.getElementById("loginbox");
		if (container && typeof(container.innerHTML) == "string") // test if the browser supports innerHTML
		{
			// set a cookie just to show the server that the client supports modern javascript
			if (!getCookie('w3'))
				setCookie('w3', '1', false, '/', false, false);
			innerHTMLWorks = true;

			if (typeof(html_Loginbox) == "string") // coming from /js/gr_se.js
			{
				container.innerHTML = html_Loginbox;

				container = document.getElementById("pollbox");
				container.innerHTML = html_Pollbox;
			}
		}

		//InitConsoleSwitch();
	}
}

function onWindowLoaded()
{
	if (!onDomLoaded.done)
		onDomLoaded();

	if (typeof(doJsTiming) == 'number')
	{
		TimeThisPage();
		window.onunload = unload;
	}
	else if (typeof(exitscript) == "string") // exitscript is only defined on a few pages
		window.onunload = unload;

	if (navigator.userAgent.indexOf('MSIE') >= 0 && navigator.userAgent.indexOf('Opera') < 0)
	{
		FixExplorerLabelBug();

		try { document.execCommand("BackgroundImageCache", false, true); } catch(err) {}
	}

	if (innerHTMLWorks && typeof(banners) == "object") // coming from multi_w3.php on the ad server
	{
		for (var i = 0; i < banners.length; i++)
		{
			var banner = banners[i];
			setad(banner[0],banner[1]);
		}
	}

	if (typeof(functionsOnload) == 'object')
		for (var i = 0; i < functionsOnload.length; i++)
		{
			var runFunction = functionsOnload[i];
			runFunction();
		}
}

if (document.addEventListener)
    document.addEventListener("DOMContentLoaded", onDomLoaded, false);

window.onload = onWindowLoaded;



