// global stuff
var imgpath = '/images/blog/';

function pageload() {
	turnonfootnotes();
}

function turnonfootnotes() {
	var footnotelink;
	var footnotelinks = getElementsByClass("footnotelink", document, "a");
	for (footnotelink in footnotelinks) {
		footnotelinks[footnotelink].style.display = "inline";
	}
}

// special blog haiku functions
function haikurotator(which, n, rootname) {
	var rotatehaiku = '';
	var rot = '';
	rotatehaiku = eval('document.getElementById("' + rootname + '-' + which + '").src');
	var swapwhich = which;
	if (rotatehaiku.split(imgpath)[1].replace('.gif', '') == (rootname + '-' + which)) {
		rot = 'rot';
		swapwhich = n-which+1;
	}
	eval('document.getElementById("' + rootname + '-' + which + '").src = "' + imgpath + rootname + '-' + swapwhich + rot + '.gif"');
}														

function haikurotatorall(n, rootname) {
	for (var x = 1; x <= n; x++) {
		haikurotator(x, n, rootname);
	}
}

function haikurotatorreset(n, rootname) {
	for (var x = 1; x <= n; x++) {
		document.getElementById(rootname + '-' + x).src = imgpath + rootname + '-' + x + '.gif';
	}
}


// webcam functions
var autorefcamera;

function querycamera() {
        document.getElementById('livecamfat').src = "/cam/mushrushcam.jpg?" + new Date().getTime();
}

function autocamera(toggle) {
	AJAXautorefresh("camera", "livecamcontrols", toggle, 60000, "");
}													
	

// ajax stuff for 8 ball
var request8ball;

function query8ball() {
	AJAXmakerequest('request8ball', 'http://mushrush.com/appfat/8ballfetch.shtml', 'process8ball()');
}

function process8ball() {
	document.getElementById('eightballresults').innerHTML = request8ball.responseText;
}


// ajax stuff for now playing list
var requestnowplaying;
var autorefnowplaying;

function querynowplaying() {
	AJAXmakerequest('requestnowplaying', 'http://mushrush.com/includes/nowplaying/nowplaying.php', 'processnowplaying()');
}

function processnowplaying() {
	document.getElementById('nowplayinglist').innerHTML = requestnowplaying.responseText;
}

function autonowplaying(toggle) {
	AJAXautorefresh("nowplaying", "nowplayingcontrols", toggle, 60000, "");
}


// ajax stuff for twitter
var requesttweets;

function querytweets() {
	AJAXmakerequest('requesttweets', 'http://mushrush.com/includes/twitter/twitter.php', 'processtweets()');
}

function processtweets() {
	document.getElementById('tweetlist').innerHTML = requesttweets.responseText;
}

function autotweets(toggle) {
	AJAXautorefresh("tweets", "twittercontrols", toggle, 3600000, "");
}
		

// ajax stuff for pop-up help
var requesthelp;
var helpboxclass = "helpbox";
var helpboxwidth = 250;
var helpboxheight = 150;

function queryhelp(page, element) {
	var helpboxobj = document.getElementById(page + "help");
	if (helpboxobj == null) { // only hit the server if this pop-up isn't already open
		var helpURL = 'http://mushrush.com/includes/helploader/helploader.php?page=' + page;
		var positionarray = findPos(element);
		AJAXmakerequest("requesthelp", helpURL, "processhelp('" + page + "help', " + positionarray[0] + ", " + positionarray[1] + ")");
	}
}

function processhelp(whichhelp, x, y) {
	var helpdiv;
	var helpxoffset;
	var helpyoffset;
	
	var browsersize = getbrowsersize();
	var pageposition = getpageposition();
	if (browsersize[0] - helpboxwidth > x) helpxoffset = x;
	else helpxoffset = x - helpboxwidth;
	if (pageposition[1] - (y - helpboxheight) > 0) helpyoffset = y;
	else helpyoffset = y - helpboxheight;

	helpdiv = document.createElement('div');
	helpdiv.id = whichhelp;
	helpdiv.innerHTML = "<p class=\"helpcloser\"><a href=\"javascript://\" onclick=\"destroyobj('" + whichhelp + "');\">close</a></p>";
	helpdiv.innerHTML += requesthelp.responseText;
	helpdiv.style.position = 'absolute';
	helpdiv.style.left = helpxoffset + 'px';
	helpdiv.style.top = helpyoffset + 'px';
	helpdiv.style.width = helpboxwidth + 'px';
	helpdiv.style.height = helpboxheight + 'px';
	helpdiv.className = helpboxclass;
	document.body.appendChild(helpdiv);
}


// universal ajax functions
function AJAXmakerequest(httprequestname, url, processfunction) {
	if (url.indexOf("?") == -1) url += "?";
	else url += "&";
	url += "fixfat=" + new Date().getTime(); // avoid IE7's cache by making the url change
	if (window.XMLHttpRequest) {
		eval(httprequestname + ' = new XMLHttpRequest()');
	}
	else if (window.ActiveXObject) {
		eval(httprequestname + ' = new ActiveXObject("Microsoft.XMLHTTP")');
	}				
	eval(httprequestname + '.open("GET", url, true)');
	eval(httprequestname + '.onreadystatechange = function() {AJAXprocessrequest("' + httprequestname + '", "' + processfunction + '");}');
	eval(httprequestname + '.send(null)');
}

function AJAXprocessrequest(httprequestname, processfunction) {
	if (eval(httprequestname + '.readyState == 4')) {
		if (eval(httprequestname + '.status == 200')) {
			eval(processfunction);
		}
		else {
			alert("error loading page\n" + eval(httprequestname + '.status') + " " + eval(httprequestname + '.statusText'));
		}
	}
}

function AJAXautorefresh(autorefappname, controlsname, toggle, interval, queryargs) {
	var controlshtml = ""
	if (toggle == "on") {
		eval("query" + autorefappname + "(" + queryargs + ");");
		controlshtml = "(<a href=\"javascript://\" onclick=\"auto" + autorefappname + "('off');\">Turn Auto Off</a>)";
		eval("autoref" + autorefappname + " = setInterval('query" + autorefappname + "(" + queryargs + ")'," + interval + ");");
	} else {
		controlshtml = "(<a href=\"javascript://\" onclick=\"query" + autorefappname + "(" + queryargs + ");\">Refresh</a>";
		controlshtml += "&nbsp;|&nbsp;";
		controlshtml += "<a href=\"javascript://\" onclick=\"auto" + autorefappname + "('on');\">Turn Auto On</a>)";
		eval("clearInterval(autoref" + autorefappname + ")");
	}
	document.getElementById(controlsname).innerHTML = controlshtml;
}


// universal helper functions
function findPos(obj) { // this script was lifted from http://www.quirksmode.org/js/findpos.html
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft, curtop];
}

function getbrowsersize() {
	var browserwidth = window.innerWidth;
	var browserheight = window.innerHeight;
	if (browserwidth == null) browserwidth = document.documentElement.clientWidth;
	if (browserheight == null) browserheight = document.documentElement.clientHeight;
	return [browserwidth, browserheight];
}

function getpageposition() {
	var pagexposition = window.pageXOffset;
	var pageyposition = window.pageYOffset;
	if (pagexposition == null) pagexposition = document.documentElement.scrollLeft;
	if (pageyposition == null) pageyposition = document.documentElement.scrollTop;
	return [pagexposition, pageyposition];
}

function destroyobj(objid) {
	var obj = document.getElementById(objid);
	document.body.removeChild(obj);
}

function getElementsByClass(searchClass, node, tag) { // I took this script from http://www.webscriptexpert.com
	var classElements = new Array();
	if (node == null) node = document;
	if (tag == null) tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	var j = 0;
	for (i = 0; i < elsLen; i++) {
		if (pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}
