var responses = new Array(6);
function GetXmlHttpObject () 
{
	var XMLHttp = null;
	// active x internetexplorer ---------------------------------------------------------
	try
	{
		XMLHttp = new XMLHttpRequest();
	}
	catch (e)
	{
		try 
		{
			XMLHttp = new ActiveXObject('MSXML2.XMLHTTP.3.0'); // ie7
		}
		catch (e)
		{
			try	
			{
				XMLHttp = new ActiveXObject("Msxml2.XMLHTTP"); //ie 6
			}
			catch(e)
			{
				try	
				{
					XMLHttp = new ActiveXObject("Microsoft.XMLHTTP");//ie older versions
				} 
				catch(e) 
				{
				}
			}
		}
	}
	
	if (XMLHttp == null) 
	{
		alert("no xmlhttp object, your browser does not support this page.");	
	}	
	
	return XMLHttp;
}

function loadXMLPage(page)
{
	var http = GetXmlHttpObject();
	http.open('get', page+'?rand='+Math.random());
	http.onreadystatechange =	function()
					{
						handleXMLResponse(this);
					};
	http.send(null);
}

function loadSection(page,box,arguments) 
{
	var http = GetXmlHttpObject();
	http.open('get', page+'?rand='+Math.random()+'&'+arguments);
	http.onreadystatechange = 	function()
					{
						var myBox = box;
						handleResponse(myBox,this);
					};
	http.send(null);
}

function handleXMLResponse(http)
{
	if(http.readyState == 4 && http.status == 200) 
	{		
		// Text returned FROM the PHP script
		var responseDOC = http.responseXML;
		if(responseDOC) 
		{
			var url = '';
			var box = '';
			//Get all sections
			var sections = responseDOC.getElementsByTagName('section');
			//load each section using the information contained in it
			for(var i=0;i<sections.length;i++)
			{
				url = sections[i].getElementsByTagName('url')[0].childNodes[0].nodeValue;
				box = sections[i].getElementsByTagName('box')[0].childNodes[0].nodeValue;
				loadSection(url,box,"");
			}
			//Handle any javascript sections
			var scripts = responseDOC.getElementsByTagName('script');
			for(var i=0;i<scripts.length;i++)
			{
				eval(scripts[i].childNodes[0].nodeValue);
			}
		}
	}
}

function handleResponse(box,http)
{
	if(http.readyState == 4 && http.status == 200) 
	{		
		// Text returned FROM the PHP script
		var response = http.responseText;
		if(response) 
		{
			responses[box] = response;
			//Fade transition to new content (somehow)
			opacity(box,100,0,500);
			setTimeout("document.getElementById('"+box+"').innerHTML = responses["+box+"]",510);
			setTimeout("opacity('"+box+"',0,100,500)",520);
		}
	}
}

function opacity(id, opacStart, opacEnd, millisec) 
{
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) 
    {
        for(i = opacStart; i >= opacEnd; i--) 
        {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } 
    else if(opacStart < opacEnd) 
    {
        for(i = opacStart; i <= opacEnd; i++)
        {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
} 

//change the opacity for different browsers
function changeOpac(opacity, id) 
{
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
}

//Stuff for image slideshow
var running = false; //Tells us whether slideshow is running or not
var Pic = new Array();
Pic[0] = 'images/banner1.png';
Pic[1] = 'images/banner3.png';
Pic[2] = 'images/banner4.png';

var chinaRunTimer;
var chinaSwapTimer;
var chinaImage = null;
var chinaRunning = false;
var chinaCurrPicIndex = 1;
var chinaPics = new Array();
chinaPics[0] = 'images/china_banner1.png';
chinaPics[1] = 'images/china_banner2.png';
chinaPics[2] = 'images/china_banner3.png';

var image = null; //the image that is currently being shown
var currPicIndex = 1;
var numPictures = Pic.length;
var preLoad = new Array();
var chinaPreLoad = new Array();
for (i = 0; i < numPictures; i++)
{
   preLoad[i] = new Image();
   preLoad[i].src = Pic[i];
}
for (i = 0; i < chinaPics.length; i++)
{
   chinaPreLoad[i] = new Image();
   chinaPreLoad[i].src = chinaPics[i];
}

function startChinaSlideShow()
{
	if(chinaRunning==false)
	{
		chinaRunning = true;
		runChinaSlideShow()
	}
}

function startSlideShow()
{
	if(running==false)
	{
		running = true;
		runSlideShow()
	}
}

function runChinaSlideShow()
{
	chinaImage = document.getElementById("china_bannerImage");
	if(chinaImage!=null)
	{
		document.getElementById("china_slideshow").style.backgroundImage="url("+chinaImage.src+")";
		changeOpac(0, "china_bannerImage");
		chinaSwapTimer = setTimeout("swapChinaImages()", 3000);
	}
	chinaRunTimer = setTimeout("runChinaSlideShow()", 10000);
}

function runSlideShow()
{
	image = document.getElementById("bannerImage");
	if(image!=null)
	{
		document.getElementById("small_box_2").style.backgroundImage="url("+image.src+")";
		changeOpac(0, "bannerImage");
		setTimeout("swapImages()", 3000);
	}
	setTimeout("runSlideShow()", 10000);
}

//Swap the currently shown image to the background and start fading in the next one. Also update the link
function swapImages()
{
	image.src = preLoad[currPicIndex].src;
	opacity("bannerImage", 0, 100, 2500);
	//Update the link in the banner
	document.getElementById("bannerImageLink").href = "javascript: loadXMLPage('project"+(currPicIndex+1)+".xml');";
	currPicIndex = currPicIndex + 1; 
	//If currPicIndex exceeds the number of pictures then we need to reset it
	if (currPicIndex > (numPictures-1))
	{
   		currPicIndex = 0;
	}
}

function swapChinaImages()
{
	chinaImage.src = chinaPreLoad[chinaCurrPicIndex].src;
	opacity("china_bannerImage", 0, 100, 2500);
	chinaCurrPicIndex = chinaCurrPicIndex + 1; 
	//If currPicIndex exceeds the number of pictures then we need to reset it
	if (chinaCurrPicIndex > (chinaPics.length-1))
	{
   		chinaCurrPicIndex = 0;
	}
}

var colors = new Array();
colors[0] = "#000000";
colors[1] = "#303030";
colors[2] = "#838080";
colors[3] = "#636262";
function changeBackgroundColor()
{
	document.body.style.backgroundColor = colors[Math.floor(Math.random()*4)];
	//if china slideshow is running then change the background
	if(chinaRunning)
	{
		stopChinaSlideShow();
	}
}

function stopChinaSlideShow()
{
	clearTimeout(chinaRunTimer);
	clearTimeout(chinaSwapTimer);
	document.getElementById("china_slideshow").style.backgroundImage="none";
	chinaCurrPicIndex = 1;
	chinaRunning = false;
}



//deals with loading the correct page on first load of the site
function onLoadMethod()
{
	loadXMLPage('slideshow.xml');
	//Fetch the query string from the URL
	var querystring = window.location.search.substring(1);
	var query = querystring.split('=');
	if(query[0]=='page')
	{
		loadXMLPage(query[1]);
	}
	else
	{
		loadXMLPage('homepage.xml');
	}
}
