var thePictureArray = new Array();
var theVideoArray = new Array();
var theBannerArray = new Array();

var iPictureIndex = 0;
var iVideoIndex = 1;

var changeBannerPeriodInSeconds = 60;

var thumbnailURLprefix = "http://img.youtube.com/vi/"; // location of thumbnails on YouTube
var thumbnailURLsuffix = "/default.jpg"; // location of thumbnails on YouTube

function loadBanner()
{
	var myXMLHttpRequestUtil = new XMLHttpRequestUtil();
	myXMLHttpRequestUtil.returnType = 'xml';
	myXMLHttpRequestUtil.callback = processLoadBanner; 
	myXMLHttpRequestUtil.failedCallback = processFailedLoadBanner;
	
	myXMLHttpRequestUtil.makeRequest('data/banners.xml', '');
	return false;
}
function processLoadBanner(dataFromServer)
{
	var Banners = dataFromServer.getElementsByTagName("Banners");
	var theBanners = Banners[0].getElementsByTagName("Banner"); 
	
	var theName;
	
	var indexImage = Math.floor(theBanners.length*Math.random());
	var theImageToUse = convertXMLCheckForNull(theBanners[indexImage].getElementsByTagName("Url")[0].childNodes[0]);
	
	var newImage = "url(" + theImageToUse + ")";
    document.getElementById('banner').style.backgroundImage = newImage;
    
    for(var ii=0; ii < theBanners.length; ii++)
	{
		theBannerArray[ii] = convertXMLCheckForNull(theBanners[ii].getElementsByTagName("Url")[0].childNodes[0]);
	}
	
	var t=setTimeout("changeBanner()",changeBannerPeriodInSeconds*1000);
}
function processFailedLoadBanner(statusCode)
{
	alert("Failed in js method processFailedLoadBanner()" + statusCode);
}


function changeBanner()
{
	var indexImage = Math.floor(theBannerArray.length*Math.random());
	var theImageToUse = theBannerArray[indexImage];
	var newImage = "url(" + theImageToUse + ")";
    document.getElementById('banner').style.backgroundImage = newImage;
    var t=setTimeout("changeBanner()",changeBannerPeriodInSeconds*1000);
}




function loadPictures()
{
	var myXMLHttpRequestUtil = new XMLHttpRequestUtil();
	myXMLHttpRequestUtil.returnType = 'xml';
	myXMLHttpRequestUtil.callback = processLoadPictures; 
	myXMLHttpRequestUtil.failedCallback = processFailedLoadPictures;
	myXMLHttpRequestUtil.makeRequest('data/pictures.xml', '');
	return false;
}
function processLoadPictures(dataFromServer)
{
	var Pictures = dataFromServer.getElementsByTagName("Pictures");
	var thePictures = Pictures[0].getElementsByTagName("Picture"); 
	
	var theURL,theCaption,theDescription;
	
	for(var ii=0; ii < thePictures.length; ii++)
	{
		theURL = convertXMLCheckForNull(thePictures[ii].getElementsByTagName("Url")[0].childNodes[0]);
		theCaption = convertXMLCheckForNull(thePictures[ii].getElementsByTagName("Caption")[0].childNodes[0]);
		theDescription = convertXMLCheckForNull(thePictures[ii].getElementsByTagName("Description")[0].childNodes[0]);
		
		thePictureArray[ii] = {'url':'', 'caption':'', 'description':''};
		
		thePictureArray[ii].url = theURL;
		thePictureArray[ii].caption = theCaption;
		thePictureArray[ii].description = theDescription;
		
		document.getElementById('picturehomepage').src = thePictureArray[ii].url; // set the image src
		document.getElementById('hh_img_title').innerHTML = thePictureArray[ii].caption; 
	}
}
function processFailedLoadPictures(statusCode)
{
	alert("Failed in js method processFailedLoadPictures()" + statusCode);
}




function showPicture(iIndexStep)
{
	var tmpCaption;
	
	iPictureIndex = iPictureIndex + iIndexStep;
	if (iPictureIndex >= thePictureArray.length)
		iPictureIndex = 0;
	if (iPictureIndex < 0)
		iPictureIndex = thePictureArray.length - 1;
	
	document.getElementById('picturehomepage').src = thePictureArray[iPictureIndex].url; // set the image src
	
	tmpCaption = thePictureArray[iPictureIndex].caption;// set the caption (if any)
	if (tmpCaption.trim() == 0)
		tmpCaption = "&nbsp;"

	document.getElementById('hh_img_title').innerHTML = tmpCaption;
	return false; // return false so not to continue 
}




function loadVideos()
{
	var myXMLHttpRequestUtil = new XMLHttpRequestUtil();
	myXMLHttpRequestUtil.returnType = 'xml';
	myXMLHttpRequestUtil.callback = processLoadVideos; 
	myXMLHttpRequestUtil.failedCallback = processFailedLoadVideos;
	myXMLHttpRequestUtil.makeRequest('data/videos.xml', '');
	return false;
}
function processLoadVideos(dataFromServer)
{
	var Videos = dataFromServer.getElementsByTagName("Videos");
	var theVideos = Videos[0].getElementsByTagName("Video"); 
	
	
	
	if (theVideos.length > 3)
	{
		var theLeftvideoarrow = document.getElementById('leftvideoarrow');
		var theRightvideoarrow = document.getElementById('rightvideoarrow');
		theLeftvideoarrow.style.display = "block";
		theRightvideoarrow.style.display = "block";
	}
	
	var theURL,theCaption,theDescription,theThumbnailimage;
	
	for(var ii=0; ii < theVideos.length; ii++)
	{
		theURL = convertXMLCheckForNull(theVideos[ii].getElementsByTagName("Url")[0].childNodes[0]); // holds the YouTube VideoID
		theCaption = convertXMLCheckForNull(theVideos[ii].getElementsByTagName("Caption")[0].childNodes[0]);
		theDescription = convertXMLCheckForNull(theVideos[ii].getElementsByTagName("Description")[0].childNodes[0]);
		theThumbnailimage = convertXMLCheckForNull(theVideos[ii].getElementsByTagName("Thumbnailimage")[0].childNodes[0]);
		
		theVideoArray[ii] = {'url':'', 'caption':'', 'description':'', 'thumbnail':''};
		
		theVideoArray[ii].url = theURL;
		theVideoArray[ii].caption = theCaption;
		theVideoArray[ii].description = theDescription;
		theVideoArray[ii].thumbnail = theThumbnailimage;
		
		var theVideo1Thumbnail = document.getElementById('video1thumbnail');
		var theVideo2Thumbnail = document.getElementById('video2thumbnail');
		var theVideo3Thumbnail = document.getElementById('video3thumbnail');
		
		var thevideo1caption = document.getElementById('video1caption');
		var thevideo2caption = document.getElementById('video2caption');
		var thevideo3caption = document.getElementById('video3caption');
		
		// locations for thumbnails
		// http://img.youtube.com/vi/videoID/0.jpg (big picture)
		// http://i4.ytimg.com/vi/videoID/default.jpg (GGR found this one, may not always be true for all thumbnails)
		// http://img.youtube.com/vi/videoID/default.jpg (thumbnail)
		
		
		
		if (ii == 0) // set first video to be the first in the list, but not to play automatically
		{
			var movieDiv = document.getElementById('movieDiv');
			theVideo1Thumbnail.src = thumbnailURLprefix + theURL + thumbnailURLsuffix ; // set thumbnail
			movieDiv.innerHTML = '<embed src="http://www.youtube.com/v/' + theURL + '&autoplay=0&loop=1" type="application/x-shockwave-flash" width="425" wmode="transparent" height="350">';
			theVideo1Thumbnail.style.display = "block";
			thevideo1caption.innerHTML = theCaption;
		}
		else if (ii == 1) // set thumbnail
		{
			theVideo2Thumbnail.src = thumbnailURLprefix + theURL + thumbnailURLsuffix ; // set thumbnail
			theVideo2Thumbnail.style.display = "block";
			thevideo2caption.innerHTML = theCaption;
		}
		else if (ii == 2) // set thumbnail
		{
			theVideo3Thumbnail.src = thumbnailURLprefix + theURL + thumbnailURLsuffix ; // set thumbnail
			theVideo3Thumbnail.style.display = "block";
			thevideo3caption.innerHTML = theCaption;
		}
	}
}
function processFailedLoadVideos(statusCode)
{
	alert("Failed in js method processFailedLoadVideos()" + statusCode);
}


//advance the thumbnails
// iIndexStep is set to 3 or -3 if to rewind or advance by 3 since three thumbnails holders are available
function showVideo(iIndexStep)
{
	var tmpCaption;
	
	iVideoIndex = iVideoIndex + iIndexStep; // incrament/decrament by stepSize
	
	if (iVideoIndex > theVideoArray.length) // reset VideoIndex of past length of array or negative
		iVideoIndex = 1;
	else if (iVideoIndex < 0)
	{
		if (theVideoArray.length % 3 == 0)
			iVideoIndex = theVideoArray.length - 2;
		else
			iVideoIndex = Math.ceil(theVideoArray.length/3)*3 - 2;
	}
	
	var leftPtr = iVideoIndex;
	var rightPtr = iVideoIndex + 2;
	
	var theVideo1Thumbnail = document.getElementById('video1thumbnail');
	var theVideo2Thumbnail = document.getElementById('video2thumbnail');
	var theVideo3Thumbnail = document.getElementById('video3thumbnail');
	
	var thevideo1caption = document.getElementById('video1caption');
	var thevideo2caption = document.getElementById('video2caption');
	var thevideo3caption = document.getElementById('video3caption');

	if (theVideoArray.length == (leftPtr+1)) // case to hide holder 3
	{
		theVideo3Thumbnail.style.display = "none"; // hide third video holder
		thevideo3caption.style.display = "none";
		theVideo1Thumbnail.src = thumbnailURLprefix + theVideoArray[iVideoIndex-1].url + thumbnailURLsuffix ; // set thumbnail
		theVideo2Thumbnail.src = thumbnailURLprefix + theVideoArray[iVideoIndex].url + thumbnailURLsuffix ; // set thumbnail
		thevideo1caption.innerHTML = theVideoArray[iVideoIndex-1].caption;
		thevideo2caption.innerHTML = theVideoArray[iVideoIndex].caption ;
		
	}
	else if (theVideoArray.length == (leftPtr+0)) // case to hide holder 2 & 3
	{
		theVideo2Thumbnail.style.display = "none";
		thevideo2caption.style.display = "none";
		theVideo3Thumbnail.style.display = "none";
		thevideo3caption.style.display = "none";
		theVideo1Thumbnail.src = thumbnailURLprefix + theVideoArray[iVideoIndex-1].url + thumbnailURLsuffix ; // set thumbnail
		thevideo1caption.innerHTML = theVideoArray[iVideoIndex-1].caption;
	}
	else // case to show all video thumbnails and captions
	{
		theVideo1Thumbnail.style.display = "block";
		theVideo2Thumbnail.style.display = "block";
		theVideo3Thumbnail.style.display = "block";
		thevideo1caption.style.display = "block";
		thevideo2caption.style.display = "block";
		thevideo3caption.style.display = "block";
		theVideo1Thumbnail.src = thumbnailURLprefix + theVideoArray[iVideoIndex-1].url + thumbnailURLsuffix ; // set thumbnail
		theVideo2Thumbnail.src = thumbnailURLprefix + theVideoArray[iVideoIndex].url + thumbnailURLsuffix ; // set thumbnail
		theVideo3Thumbnail.src = thumbnailURLprefix + theVideoArray[iVideoIndex + 1 ].url + thumbnailURLsuffix ; // set thumbnail
		thevideo1caption.innerHTML = theVideoArray[iVideoIndex-1].caption;
		thevideo2caption.innerHTML = theVideoArray[iVideoIndex].caption ;
		thevideo3caption.innerHTML = theVideoArray[iVideoIndex + 1 ].caption ;
	}
}


function playVideo(iIndexOffset)
{
	var tmpUrl = theVideoArray[iVideoIndex + iIndexOffset -1].url;
	movieDiv.innerHTML = '<embed src="http://www.youtube.com/v/' + tmpUrl+ '&autoplay=1&loop=1" type="application/x-shockwave-flash" width="425" height="350"> </embed>';
}


// Utility to check if the XML value is empty
function convertXMLCheckForNull(sXMLValue)
{
	var sReturnValue = "";
	try
	{
		sReturnValue = sXMLValue.nodeValue;
	}
	catch(e)
	{}
	return(sReturnValue);
}

// utility to trim white spaces
String.prototype.trim = function()
{
	return this.replace(/^\s+|\s+$/g, "");
}


