//Load XML File
xmlDoc = loadXMLDoc('fac.xml');
facCounter = getNodeCount(xmlDoc);

//Variables for counter
var timer = 15;
var c = Math.floor(Math.random()*(facCounter+1)) * timer - 1;

//Create array's
var facImg = new Array();
var facName = new Array();
var facDept = new Array();
var facAbstract = new Array();
var facLink = new Array();

//Load Array's
for(var x = 0; x < facCounter; x++){

	facImg[x] = xmlDoc.getElementsByTagName("img")[x].childNodes[0].nodeValue;
	facName[x] = xmlDoc.getElementsByTagName("name")[x].childNodes[0].nodeValue;
	facDept[x] = xmlDoc.getElementsByTagName("dept")[x].childNodes[0].nodeValue;
	facAbstract[x] = xmlDoc.getElementsByTagName("abstract")[x].childNodes[0].nodeValue;
	facLink[x] = xmlDoc.getElementsByTagName("link")[x].childNodes[0].nodeValue;		
}	

//Function to Change Image, Name, Abstract and Link	
function facFocus(tempCounter){	
	
	document.getElementById("facImg").src = facImg[tempCounter % facCounter];
	document.getElementById("facName").innerHTML = facName[tempCounter % facCounter];
	document.getElementById("facDept").innerHTML = facDept[tempCounter % facCounter];
	document.getElementById("facAbstract").innerHTML = facAbstract[tempCounter % facCounter];
	document.getElementById("facLink").href = facLink[tempCounter % facCounter];

}

//Function to Load XML File
function loadXML(url){
	var xmlDoc;
	// code for IE
	if (window.ActiveXObject){
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
	}
	// code for Mozilla, Firefox, Opera, etc.
	else if (document.implementation && document.implementation.createDocument){
		xmlDoc=document.implementation.createDocument("","",null);
	}
	else{
		alert('Your browser cannot handle this script');
	}
	
	xmlDoc.async=false;
	xmlDoc.load(url);
	
	return xmlDoc;

}

//Function to create infinite loop to make changes
function timedCount(){
	c=c+1
	t=setTimeout("timedCount()",1000)
	if(c % timer == 0){
   		facFocus(c/timer);
   	}
}

//Function to return the number of child nodes for facList
function getNodeCount(xmlDoc){
	//Number of entries in XML file
	facList = xmlDoc.getElementsByTagName("facList")[0].childNodes.length;
	facListChildren = 0;
	
	//Removes entries for line breaks
	for(y = 0; y < facList; y++){
		if(xmlDoc.getElementsByTagName("facList")[0].childNodes[y].nodeValue == null)
			facListChildren++;
	}
	
	return facListChildren;
}


function loadXMLDoc(url){
	var xmlHttp = getXmlHttp();
	xmlHttp.open("GET", url,false);
	xmlHttp.send(null);
  	
  	temp = xmlHttp.responseText;

	// code for IE
	if (window.ActiveXObject){
		var doc=new ActiveXObject("Microsoft.XMLDOM");
		doc.async="false";
		doc.loadXML(temp);
	}
	// code for Mozilla, Firefox, Opera, etc.
	else{
		var parser=new DOMParser();
		var doc=parser.parseFromString(temp,"text/xml");
	}
  	
  	return doc;
}

function getXmlHttp(){
	var xmlHttp;
  	try {
    	// Firefox, Opera 8.0+, Safari
    	xmlHttp=new XMLHttpRequest();
	}
  	catch (e){
    	// Internet Explorer
    	try{
      		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      	}
    	catch (e){
      		try{
        		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        	}
      		catch (e){
        		alert("Your browser does not support AJAX!");
        		return false;
        	}
      	}
    }
    return xmlHttp;
}
