var imgObj = new Array;
var current=0;
//-----------------------------
window.onload=function(){
	turnOnButtonCurrentPage();
	splashImagesToDOM();
}
//-----------------------------
function turnOnButtonCurrentPage(){
	var fileName = location.href.substring(location.href.lastIndexOf('/') + 1);
	var anchorList = document.getElementById('navmenu').getElementsByTagName('a');
	for(var i=0; i<anchorList .length; ++i) {
		var anchorHref = anchorList[i].href;
		anchorHref = anchorHref.substring(anchorHref.lastIndexOf('/') + 1);
		if(anchorHref == fileName) {
			anchorList[i].className = 'buttonSelected';// change the class of this anchor
			break;
		}
	} 
}
//-----------------------------
function submitQueryToServer(theFormId,theBaseURL) {
	// build query string
	theForm = document.getElementById(theFormId);
	queryString = '';
	for (i=0; i < theForm.elements.length; i++) {
		element = theForm.elements[i];
		if (element.name){
			if (element.type == 'radio' || element.type == 'checkbox'){
				if (element.checked == true){
					queryString += element.name + '=' + element.value + "&";
				}
			}
			else{
				queryString += element.name + '=' + element.value + "&";
			}
		}
	}
	getResponse(theBaseURL + '?' + queryString);
	window.location.href = "#saved"; // don't put a slash after the # or it will try to load 'saved'. This just creates a history response if we press back.

}
//-----------------------------
function stateChanged() {
	idToChange = 'playerStatusText';
	//alert ("id to change: " + idToChange);
	if (xmlHttp.readyState==4) {
		replaceHtml(idToChange,xmlHttp.responseText)
		//document.getElementById('cityData').innerHTML=xmlHttp.responseText
		hideDiv('timer');
	}
	else {
		showDiv('timer');
		//alert ("ready state:" + xmlHttp.readyState);
	}
}
//-----------------------------
var xmlHttp
function getResponse(url){
	xmlHttp = GetXMLHttpObject()
	if (xmlHttp == null) {
		alert ("Browser does not support HTTP request")
		return
	}
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url, true)
	xmlHttp.send(null)
	//alert ("response " + xmlHttp.responseText);
}
//-----------------------------
function GetXMLHttpObject(){
	var objXMLHttp = null
	if (window.XMLHttpRequest) {
		objXMLHttp = new XMLHttpRequest(); //Mozilla
	}
	else if  (window.ActiveXObject) {
		objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP"); //Explorer
	}
	return objXMLHttp
}
//-----------------------------
function replaceHtml(el, html) {
	var oldEl = typeof el === "string" ? document.getElementById(el) : el;
	/*@cc_on // Pure innerHTML is slightly faster in IE
	oldEl.innerHTML = html;
	return oldEl;
	@*/
	var newEl = oldEl.cloneNode(false);
	newEl.innerHTML = html;
	oldEl.parentNode.replaceChild(newEl, oldEl);
	/* Since we just removed the old element from the DOM, return a reference
	to the new element, which can be used to restore variable references. */
	return newEl;
}
//-----------------------------
function clientSideInclude(id, url) {
	var req = false;
	if (window.XMLHttpRequest) {
		try {
			req = new XMLHttpRequest();
		}
		catch (e) {
			req = false;
		}
	}
	else if (window.ActiveXObject) {
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
				req = false;
			}
		}
	}
	var element = document.getElementById(id);
	if (!element) {
		alert("Bad id " + id + "passed to clientSideInclude." +	"You need a div or span element " + "with this id in your page.");
		return;
	}
	if (req) {
		req.open('GET', url, false);
		req.send(null);
		element.innerHTML = req.responseText;
	}
	else {
		element.innerHTML =
			"Sorry, your browser does not support XMLHTTPRequest objects. This page requires " +
			"Internet Explorer 5 or better for Windows, or Firefox for any system, or Safari. Other " +
			"compatible browsers may also exist.";
	}
}
//-----------------------------
function splashImagesToDOM(){
	// first create the images in divs, appended to existing image
	var parentDiv	= document.getElementById("splashFrame");
	for(i=0; i<imageArray.length; i++) { 
		var newDiv = document.createElement("DIV");
		parentDiv.appendChild(newDiv);
		newDiv.setAttribute('id','splash' + i);
		newDiv.style.width = "100%";
		newDiv.style.height = "100%";
		newDiv.style.position = "absolute";
		newDiv.style.top = "0";
		newDiv.style.background = "url(" + imageArray[i] + ")";
		newDiv.style.display = "none";
		imgObj.push(document.getElementById("splash" + i));
		imgObj[i].xOpacity = 0;
	}
	// now begin crossfading the images:
	imgObj[0].xOpacity = .99;
	crossFadeSplashImages(0);
}
//-----------------------------
function crossFadeSplashImages(){
	currentOpacity = imgObj[current].xOpacity;
	nextIndex = imgObj[current+1]?current+1:0;
	nextOpacity = imgObj[nextIndex].xOpacity;
	currentOpacity-=.05; 
	nextOpacity+=.05;
	imgObj[nextIndex].style.display = "block";
	imgObj[current].xOpacity = currentOpacity;
	imgObj[nextIndex].xOpacity = nextOpacity;

	setOpacity(imgObj[current]); 
	setOpacity(imgObj[nextIndex]);

	if(currentOpacity<=0) {
		imgObj[current].style.display = "none";
		current = nextIndex;
		setTimeout(crossFadeSplashImages,4000);
	} else {
		setTimeout(crossFadeSplashImages,40);
	}
	function setOpacity(obj) {
		if(obj.xOpacity>.99) {
			obj.xOpacity = .99;
			return;
		}
		obj.style.opacity = obj.xOpacity;
		obj.style.MozOpacity = obj.xOpacity;
		obj.style.filter = "alpha(opacity=" + (obj.xOpacity*100) + ")";
	}
}
//-----------------------------
