// JavaScript Document


/********************************************/
/* This is for the tabs on the homepage     */
/********************************************/
$(document).ready(function() { 	
	// set all content containers to invisible
	$("#tabWrapper > div").css("display","none");
	// set active content container to visible
	$("#tabWrapper > div.active").css("display","block");
	$("#tabs li a").click(function()
	{
		// remove active class from currently visible content area
		$("#tabWrapper > div.active").removeClass("active");
		// get href attribute value from clicked link to determine which content area to make visible and set active class to that div
		$(("#tabWrapper > div"+$(this).attr("href"))).addClass("active");
		// make all content areas invisible
		$("#tabWrapper > div").css("display","none");
		// make new active content area visible
		$("#tabWrapper > div.active").css("display","block");
		// remove active class from old tab
		$("#tabs li.active").removeClass("active");
		// set active class to new tab based on the click
		$(this).parent().addClass("active");
		// prevent browser from scrolling to the div anchor
		return false;
	});
	
	newsXML();
	
});




/*************************************/
/* intialize the thumbnail code      */
/*************************************/
var thumbCounter; // this variable will tell us how many thumbnails there are;
var thumbWidth = new Array();// create an array for the width of each image.
var arrowY = new Array('0', '78', '150', '225');//the positon of the Y coordinates for the thumbnail arrow.
var randomThumbs = new Array();
var randomLargeImg = new Array();
var visibleImgCounter = 0;
$(document).ready(function() {
	// open or close the whats new section on the homepage
	$('.expToggle').click(function() {
	  $('div.expander').slideToggle(1000);
	});	
});



/*************************************************************************/
/* functions for the thumbnail section of the homepage                   */
/*************************************************************************/

function loadXML()
{
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);//alert("first");
			getImages();
		}
		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;
			//alert("second");
			getImages();
			
		} else {
			var errorHappendHere = "Error.";
			xmlDoc = document.implementation.createDocument("","",null);
			xmlDoc.onreadystatechange=verify;
			xmlDoc.async=false;
			xmlDoc.load(uniqueURI);//alert("third");
			xmlDoc.onload=getImages();
		}
	}	
	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;
  }
}

var getImages = function getImages()
{
	// Make left and right buttons visible if javascript is on
	var xmlImages=xmlDoc.getElementsByTagName("node")[0].getElementsByTagName("image");
	var thumbCounter=xmlImages.length; // find out how many images there are
	var thumbMaker; // assign all img sourcecode to this variable and use it in innerHTML.
	var lgImgMaker; // assign all Large Image sourcecode to this variable and use it in innerHTML.
	var divWidth, divHeight; // decide how tall and wide to make the div holding the thumbnails
	var thumbSrc, imageAlt;
	// grab unique values from xml
	for(var i=0; i<4; i++)
	{			
		var randomNumber = (Math.floor(Math.random()*thumbCounter));
			
		if(i == 0)
		{ 
			randomThumbs[i] = randomNumber;
			randomLargeImg[i] = randomNumber;
		}
		else
		{
			while(randomThumbs.length <= i)
			{
				var unique = true;
				for(var j=0; j<=i-1; j++)
				{
					if(randomNumber == randomThumbs[j])
					{
						unique = false;
						randomNumber = (Math.floor(Math.random()*thumbCounter));
					}
				}
				if(unique)
				{
					randomThumbs[i] = randomNumber;
					randomLargeImg[i] = randomNumber;
				}
			}			
		}	
	}
	// loop through arrays of unique values
	for(var k = 0; k < 4; k++)
	{
		thumbSrc = xmlDoc.getElementsByTagName("image")[randomThumbs[k]].getElementsByTagName("thumbnail")[0].childNodes[0].nodeValue;
		imageAlt = xmlDoc.getElementsByTagName("image")[randomThumbs[k]].getElementsByTagName("altText")[0].childNodes[0].nodeValue;
		thumbWidth[i] = xmlDoc.getElementsByTagName("image")[randomThumbs[k]].getElementsByTagName("width")[0].childNodes[0].nodeValue;
		lgImgSrc = '<img src="'
			+ xmlDoc.getElementsByTagName("image")[randomThumbs[k]].getElementsByTagName("largeImage")[0].childNodes[0].nodeValue
			+ '" alt="' 
			+ xmlDoc.getElementsByTagName("image")[randomThumbs[k]].getElementsByTagName("altText")[0].childNodes[0].nodeValue
			+ '" id="lgImg'+k+'" />';
		//lgImgSrc = '<img src="images/home/portfolio/large/large' + randomLargeImg[k] + '.jpg" alt="portfolio image" width="478" id="lgImg'+ k + '" />';
		if(k == 0)
		{
			thumbMaker = '<img id="smImg'+ k + '" src="'+thumbSrc+'" alt="'+ imageAlt +'" onclick="portfolioArrowMover('+arrowY[k]+','+k+','+randomThumbs[k]+')" />';
			lgImgMaker = lgImgSrc;
		}
		else
		{
			thumbMaker += '<img id="smImg'+ k + '" src="'+thumbSrc+'" alt="'+ imageAlt +'" onclick="portfolioArrowMover('+arrowY[k]+','+k+','+randomThumbs[k]+')" />';
			lgImgMaker += lgImgSrc;
		}
	}
	// add the large images to the page
	document.getElementById('thumbnails').innerHTML = thumbMaker;
	document.getElementById('portfolioImageHolder').innerHTML = lgImgMaker;
	$("#rightCol3 #thumbnails img").css("opacity", "0.6");
	$("#rightCol3 #thumbnails #smImg0").css("opacity", "1");
	$("#portfolioImageHolder img").css("opacity", "0");
	$("#portfolioImageHolder #lgImg0").css("opacity", "1");
	timedCount();
}
function timedCount()
{	
	// time in between loops
	largeTimeOut = setTimeout("timedCount()",8000);
	portfolioArrowMover(arrowY[visibleImgCounter],visibleImgCounter);
}


function newsXML()
{
	$.ajax({
    type: "GET",
    url: "xml/news.xml",
    dataType: "xml",
    success: parseXML
  });
}

function parseXML(xml)
{
	$(xml).find("item").each(function()
	{
		$("#news").append("<p><strong><a target='_blank' href='"+$(this).find("link").text()+"'>"+$(this).find("title").text() + "</a></strong>  " + $(this).find("timePlace").text() + "</p>");
	});
}
