//lookupBannerStuff.js
//2008-03-01, Luke McKenzie theclockspot.com

//This .js uses lookupBannerStuff.php to get, and place onto index.ph
//controls dom @ <div id='bannerphoto'>, <p id='slogan1'>, <p id='slogan2'>
	//no swirly
	
//requires pathtohome
var bannerphotopathfromhome='bannerphotos/';

//requires ajax.js

//request object
var requestb = getAjaxRequestObj();

//as this is just a randomizer, we're not expecting any input - however, both image and slogan are done by this
function lookupBannerStuff() {
	//requires js pathtohome!
	var url = pathtohome+'data/lookupBannerStuff.php?dummy=' + new Date().getTime(); //to prevent browser caching
	requestb.open("GET", url, true);
	requestb.onreadystatechange = lookupBannerStuffUpdate;
	requestb.send(null); //there it gooooes
}

function lookupBannerStuffWithTimer() {
	//setTimeout('lookupBannerStuffWithTimer()',5000);
	lookupBannerStuff();
}

function lookupBannerStuffUpdate() {
	if(requestb.readyState == 4) { //when it's done
		if(requestb.status == 200) {
			var xmadoc = requestb.responseText; //Don't delete these two lines!
			//alert('hello: '+xmadoc); //They are good for debugging PHP errors (until you have firebug)
			
			var xmlDoc = requestb.responseXML; //as opposed to responseText, this is XML, which turns into a DOM tree
			//expecting: 
			//expecting: bannerstuff(slogan() image() )
			var xmlSlogan = xmlDoc.getElementsByTagName('slogan');
			var slogan = getNodeValue(xmlSlogan,0);
			var xmlImage = xmlDoc.getElementsByTagName('image');
			var image = getNodeValue(xmlImage,0);
			
			document.getElementById("bannerphoto").style.background = "url("+bannerphotopathfromhome+unescape(image)+") left top no-repeat";
			document.getElementById("slogan1").innerHTML=unescape(slogan);
			document.getElementById("slogan2").innerHTML=unescape(slogan); //two, because of an artificial shadow arrrgh
			
		} else { //if the status isn't 200, this will alert us to problems //FIX EVERYWHERE
			var msgs = 'Error! requestb came back with status '+requestb.status+'.';
			var bitsy = requestb.getResponseHeader("Status");
			if(bitsy) msgs += '\n\n'+bitsy;
			alert(msgs);
		} //close if requestb.status = 200
	}
}

function getNodeValue(x,i) {
	//x=object HTMLCollection (basically an array)
	//i=integer, the index to return
	//Separated out from the mains for simplicity,
	//and because if(!multishows), the checks inside (on nonexistent show thingies) still killed the process.
	if(x) { //make sure not null
		if(x[i].firstChild) { //make sure not null
			return x[i].firstChild.nodeValue;
		} else return null;
	} else return null;
}
