

/*******************************************************************************/
/*   This loads the XML  for the content on the upper right part of homepage   */
/*******************************************************************************/

$(function()
{
	//if the mobile version of the site is in use, change out the homepage logo
	if (window.innerWidth<=800 || document.width<=800 || screen.width<=800)
	{
		$("img#logo").attr("src","/images/default/home_logo2.jpg");
	}
	
	
var timestamp = new Date();
var uri = "/xml/homepage.xml";

var uniqueURI = uri + (uri.indexOf("?") > 0 ? "&" : "?")+ "timestamp="+ timestamp.getTime();
	try
	{
		if (window.ActiveXObject)
		{
			var errorHappendHere = "Check Browser and security settings";
			xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.async=false;
			xmlDoc.onreadystatechange=verify;
			xmlDoc.load(uniqueURI);
			imageRotator();
		}
		else if(window.XMLHttpRequest)
		{
			var errorHappendHere = "Error handling XMLHttpRequest request";
			var d = new XMLHttpRequest();
			d.open("GET", uniqueURI, false);
			d.send(null);
			xmlDoc=d.responseXML;
			imageRotator();
		} else {
			var errorHappendHere = "Error.";
			xmlDoc = document.implementation.createDocument("","",null);
			xmlDoc.onreadystatechange=verify;
			xmlDoc.async=false;
			xmlDoc.load(uniqueURI);
			xmlDoc.onload=imageRotator();
		}
	}	
	catch(e)
	{
		alert(errorHappendHere); 
	}
});

function verify()
{
  // 0 Object is not initialized
  // 1 Loading object is loading data
  // 2 Loaded object has loaded data
  // 3 Data from object can be worked with
  // 4 Object completely initialized
  if (xmlDoc.readyState != 4)
  {
    return false;
  }
  return true;
}




/*********************************************************************/
/*    This function randomly grabs a photo from the file             */
/*    homepage.xml. It takes the info associated with it             */
/*    and creates innerHTML. All of this is sent to the 			 */
/*    image with a div labeled profile. This all happens             */ 
/*    on page refresh.                                               */
/*********************************************************************/

var imageRotator = function imageRotator()
{
	// create an object with all of the image nodes
	var xmlTopNodes=xmlDoc.getElementsByTagName("node")[0].getElementsByTagName("image"); 
	// find out how many top nodes there are
	var numTopNodes=xmlTopNodes.length; 
	//randomly choose a photo and display on the homepage from the XML 
	var randomNumber = (Math.floor(Math.random()*numTopNodes)); //This is a random number between 0 - (numTopNodes - 1)
	// get the value of the image source
	var newImage = xmlDoc.getElementsByTagName("node")[0].getElementsByTagName("image")[randomNumber].getElementsByTagName("source")[0].childNodes[0].nodeValue;
	
	// swap out the profile image
	document.getElementById('profile').src = newImage;
}
