// script to manage front page slideshow and navigation on thumbnails
// cookies keep track of preloaded images to avoid reloading if a visitor already has images chached in browser

// set a cookie
function setCookie(string,value,exdays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
	document.cookie=string + "=" + c_value;
}

// get a cookie
function getCookie(string)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0; i < ARRcookies.length; i++)
{
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x == string)
    {
    return unescape(y);
    }
  }
}

// check a cookie
function checkCookie()
{
var visitor=getCookie("visitor");
	if (visitor!=null && visitor!="")
	{
	  //alert("Welcome again " + visitor);
	  return 1;
	}
	else
	{
  		//visitor=prompt("Please enter your name:","");
  		visitor="return";
	  	if (visitor!=null && visitor!="")
	    {
		    setCookie("visitor",visitor,365);
			return 0;
	    }
  	}
}

// beginning values for variables to hold current, previous, and next photo index
var curPhoto, prevPhoto, nextPhoto;
var intervalID;
var pImg = new Array();
var pThumb = new Array();

// read paintings.xml file for random image links

var xmlDoc;
var xmlObj;

if(window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  	xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
	xmlhttp.open("GET","paintings.xml",false);
	xmlhttp.send();
	xmlDoc=xmlhttp.responseXML; 
	xmlObj=xmlDoc.documentElement;

//alert(xmlObj.getElementsByTagName("thumb")[0].childNodes[0].nodeValue);
var item_count=xmlObj.getElementsByTagName("thumb").length-1;

for(var i = 0; i < item_count; i++)
	{
		pImg[i] = xmlObj.getElementsByTagName('link')[i].childNodes[0].nodeValue
		pThumb[i] = xmlObj.getElementsByTagName('thumb')[i].childNodes[0].nodeValue
	}

$(function(){

// check cookie
var status = checkCookie();

// initialize index values and update first photo 
setDefaultValues();
intervalID = myTimer();

	if(status != 1)
	{
			// if new visitor then show loading animation
		    $("#picbox").append('<img src="Scripts/spacer.gif" />');
		    $("#picbox").append('<img src="Scripts/ajax-loader.gif" style="margin-left: 175px;"/><span class="pictitle" style="margin-left: 175px;">Loading ...</span>');
		    $("#picbox").append('<img src="Scripts/spacer.gif" />');
			$("#tbcolumn").hide();
	
			preLoadImg(pImg);
			preLoadImg(pThumb);
	
	}
	else
	{
			// this is a return visit so skip loading animation and image preload
			changePhoto(1);
	}

});

// preload images 
function preLoadImg(images)
{
	$(images).each(function(key, value) {
	
	var img = new Image();
	
	$(img)
	
	.attr('src', value)
	
		.error(function (){
		
		alert('Error preloading image ' . value);
		
		})
	});
}


function setDefaultValues()
{
	// initialize photo index for timed image slides
	curPhoto = - 1;
	prevPhoto = item_count - 1;
	nextPhoto = 0;
}

// setup a timer to trigger every 10 seconds (10000 milliseconds)

function myTimer()
{
	var intervalID = setInterval(function() {
	
		// if tbcolumn div is hidden then set to .show()
		if($("#tbcolumn").is(":hidden")) { $("#tbcolumn").show(); }
	
		// change photo to next
		changePhoto(1);
	
	}, 10000);
	return intervalID;
}

function updatePhotos()
{
	// change the photo to show next or prev image
	$("#tbnext").empty();
	$("#tbnext").append('<a href="#anchor" onclick="changePhoto(1)"><img src="'+xmlObj.getElementsByTagName('thumb')[nextPhoto].childNodes[0].nodeValue+'" title="'+xmlObj.getElementsByTagName('title')[nextPhoto].childNodes[0].nodeValue+' by Patricia Cole" alt="'+xmlObj.getElementsByTagName('title')[nextPhoto].childNodes[0].nodeValue+' by Patricia Cole" /></a>');     
	$("#tbprev").empty();
	$("#tbprev").append('<a href="#anchor" onclick="changePhoto(2)"><img src="'+xmlObj.getElementsByTagName('thumb')[prevPhoto].childNodes[0].nodeValue+'" title="'+xmlObj.getElementsByTagName('title')[prevPhoto].childNodes[0].nodeValue+' by Patricia Cole" alt="'+xmlObj.getElementsByTagName('title')[prevPhoto].childNodes[0].nodeValue+' by Patricia Cole" /></a>');
	$("#picbox").empty();
    $("#picbox").append('<img src="'+xmlObj.getElementsByTagName('link')[curPhoto].childNodes[0].nodeValue + '" title="'+xmlObj.getElementsByTagName('title')[curPhoto].childNodes[0].nodeValue+' by Patricia Cole" alt="'+xmlObj.getElementsByTagName('title')[curPhoto].childNodes[0].nodeValue+' by Patricia Cole" /><span class="pictitle">'+xmlObj.getElementsByTagName('title')[curPhoto].childNodes[0].nodeValue+'</span>');
}


function changePhoto(numPhoto)
{

// reset the timer
clearInterval(intervalID);
intervalID = myTimer();

	// roll photo index over if upper or lower bounds reach list limits creating a looping effect in either direction
	switch(numPhoto)
	{
		case 1:
			curPhoto = curPhoto + 1;
			if(curPhoto > item_count) { curPhoto = 0; }
	        break;
		case 2:
			curPhoto = curPhoto - 1;
			if(curPhoto < 0) { curPhoto = item_count; }
			break;
	}

	// setup previous and next links
	if(curPhoto == 0) {
		nextPhoto = 1; // set the next image to 1 and prev to last image in cycle of XML file
		prevPhoto = item_count;
	} 
	else if(curPhoto == item_count) 
	{
		nextPhoto = 0; // reset the image cycle to zero and start with first image in the XML file
		prevPhoto = curPhoto - 1; // current imgID is max, so prev would less by one
	} 
	else 
	{
		nextPhoto = curPhoto + 1; // add one for next image
		prevPhoto = curPhoto - 1; // subtract one for previous image
	}

	updatePhotos();
}

