// JavaScript Document
var xmlhttp;
var mySection="";
var picId=1;
var numSections=0;
var data;


function init(){
	loadXMLDoc('sections.xml');
	
	
}
function loadXMLDoc(url){

	if(xmlhttp!=null){
		xmlhttp.onreadystatechange=onResponse;
		xmlhttp.open("GET",url+'?cache='+new Date().getTime(),true);
		xmlhttp.send(null);
	}
}
function getXmlHttp(){
	xmlhttp=null;
	if (window.XMLHttpRequest){
		// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
		
  	}else{
		// code for IE5, IE6
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  	}
	return xmlhttp
}

function onResponse(){
	if(xmlhttp.readyState!=4) return;
	if(xmlhttp.status!=200){
		alert("Problem retrieving XML data");
		  return;
  	}
	
	parseXml(xmlhttp.responseXML);
	

}
function parseXml(txt){
	
	data=txt.documentElement.getElementsByTagName("section");
	
	numSections=data.length;
	
	for(var i=0;i<data.length;i++){
		removeTextNodes(document.getElementById('content').childNodes);
	}
	if(mySection==""){
	if(window.location.hash!=""){
		var id=window.location.hash.substring(1);
		if(parseInt(id)>=0 && parseInt(id)<numSections){
			setUpCanvas(parseInt(id));
		}
	}
	}
	
}

function removeTextNodes(node){
	
	index=node.length-1;
	while(index>=0){
		
		if(node[index].nodeType==3){
			node[index].parentNode.removeChild(node[index]);
			index--;
		}
		index--;
	}
	
}


function checkTargetUrl(){
	
}
function setUpCanvas(id){
	
	if(id!=mySection){
		picId=1;
		
	}
	mySection=id;
	
	
	
	document.getElementById('theTitle').innerHTML="<p>&#x2714; "+data[mySection].getElementsByTagName("title")[0].firstChild.nodeValue+" <a href='#"+mySection +"' onclick='prevPic()'class='projectsnav'>Prev</a> / <a href='#"+mySection +"' onclick='nextPic()'class='projectsnav'>Next</a></p>";
	document.getElementById('thePic').innerHTML="<a href='#"+mySection +"' onclick='nextPic()' ><img src='"+data[mySection].getElementsByTagName("location")[0].firstChild.nodeValue+"/pic"+picId+".jpg' alt='' width='676' height='475' ></img></a>";
	document.getElementById('theDesc').innerHTML="<p>&mdash;"+data[mySection].getElementsByTagName("description")[0].firstChild.nodeValue+"</p>";
	window.location.hash = mySection;
}
function nextPic(){
	picId++;
	if(picId>parseInt(data[mySection].getElementsByTagName("ammount")[0].firstChild.nodeValue)){
		picId=1;
	}
	setUpCanvas(mySection)
	window.location.hash = mySection;
}
function prevPic(){
	picId--;
	if(picId==0){
		picId=parseInt(data[mySection].getElementsByTagName("ammount")[0].firstChild.nodeValue);
	}
	setUpCanvas(mySection)
	window.location.hash = mySection;
}