	/* define class for image library browser */
  
	//Connstructor for the gallery image 'Class'.
	//Instantiate with photo = new Photo(...);
	function Photo(title,imagePath,description,taken,pID,views) {
		this.title = title;
		this.imagePath = imagePath;
		this.description = description;
		this.taken = taken;
		this.pID = pID;
		this.views = views;
	}


	//create array of photos for javascript enabled gallery browsing
	var gallery = [];
	var admin = false;
	var fanAcct =false;
	//gallery variables
	var curPic = 0;
	var playing = false;

	//global fading vars
	var opac = 100;
	ie5  = (document.all && document.getElementById);
	ns6 = (!document.all && document.getElementById);
	
	//move to next image
	function moveNext() {
		//first check we are not already changing pictures.
		if(opac==100) { 
			if(curPic < gallery.length-1) 
				curPic++;
			else
				curPic = 0;
				
			//Fade image out
			//restore filter attribute so that image can fade out in IE
			document.getElementById("divPrint").setAttribute("filter","alpha(opacity=100)");
			fadeOut();
		}
	}
	
	//Move Back to Previous Imae
	function movePrevious() {
		//first check we are not already changing pictures.
		if(opac==100) { 
			if(curPic > 0)
					curPic--;
			else
				curPic=gallery.length-1;
			
			//restore filter attribute so that image can fade out in IE
			document.getElementById("divPrint").setAttribute("filter","alpha(opacity=100)");
			fadeOut();
		}
	}
	
	//function to play slideshow
	function playSlideShow() {		
		if(playing) {
			moveNext();
		} 
		else 
			clearTimeout;
	}
	
	function stopSlideShow() {
		clearTimeout();
	}
	
	function toggleSlideShow() {
		if(!playing) {
			document.getElementById("btnPlay").src = "images/layout/pause.gif";
			document.getElementById("btnPlay").alt = "Pause Slideshow";
			playing = true;
			playSlideShow();
		}
		else {
			document.getElementById("btnPlay").src = "images/layout/play.gif";
			document.getElementById("btnPlay").alt = "Play Slideshow";
			playing = false;
		}
	}
	
	//function to display picture
	function displayPic() {
		getExif();
		//set document title and others
		document.title = gallery[curPic].title + " - Photography Prints by Justin Kercher";
		document.getElementById("picTitle").innerHTML = gallery[curPic].title;
		//document.getElementById("picTitle").innerHTML += "<br><a href=\"buyprint.asp?pID=" + curPic + "\" onclick=\"addToCart(); return false;\">Buy this Photography Print</a>";
		document.getElementById("picDesc").innerHTML = gallery[curPic].description;
		document.getElementById("picDate").innerHTML = gallery[curPic].taken;
		document.getElementById("aComment").href = "contact.asp?pID=" + gallery[curPic].pID;
		document.getElementById("picViews").innerHTML = gallery[curPic].views;
		//getImage with AJAX
		fetchImageFile();
	}
	

	
	//fade out image
	function fadeOut() {
	//clear existing time out to ensure image has time to display during slideshow mode.
	clearTimeout();
		if(opac > 0){
	        opac-=2;
	        if(ie5) document.getElementById('divPrint').filters.alpha.opacity = opac;
	        if(ns6) document.getElementById('divPrint').style.MozOpacity = opac/100;
			document.getElementById("divPrint").style.KhtmlOpacity = opac / 100;
        	setTimeout('fadeOut()', 5);
    	} 
		else {
			//get the next image.
			displayPic();
		}
	}
	
	//getDetails 
	function displayDetails() {
	}
	
	//fade in
	function fadeIn() {	
		if(opac < 100){
	        opac+=1;
	        if(ie5) document.getElementById('divPrint').filters.alpha.opacity = opac;
	        if(ns6) document.getElementById('divPrint').style.MozOpacity = opac/100;
			document.getElementById("divPrint").style.KhtmlOpacity = opac / 100;
        	setTimeout('fadeIn()', 10);
    	}
		else {
			//remove filter attribute to remove missing pixels with IE bug
			document.getElementById("divPrint").removeAttribute("filter");
			//if we are in slideshow mode, restore timeout for moving to next image
			setTimeout('playSlideShow()',3000);
		}
	}
	
	
	//ajax to get exif data
	
	var xmlhttp,xmlhttp2;
	
	function getExif() {
		xmlhttp=null;
		if (window.XMLHttpRequest)  {// code for all new browsers
		  xmlhttp=new XMLHttpRequest();
	  	}
		else if (window.ActiveXObject) {
		  // code for IE5 and IE6
		  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		if (xmlhttp!=null)  {
		  xmlhttp.onreadystatechange=state_Change;
		  url = "getExif.asp?path=" + gallery[curPic].imagePath;
		  xmlhttp.open("GET",url,true);
		  xmlhttp.send(null);
		}
		else  {
		  alert("Your browser does not support XMLHTTP.");
	    }
	}
	
	function state_Change() {
		if (xmlhttp.readyState==4) {// 4 = "loaded"
		  if (xmlhttp.status==200) {// 200 = OK
		    document.getElementById("divExif").innerHTML = xmlhttp.responseText;
		  }
	  	  else {
		    document.getElementById("divExif").innerHTML = "Exif Data Not Available";
	      }
	  }
	}
	
	//function to get image
	function fetchImageFile() {
		xmlhttp2=null;
		if (window.XMLHttpRequest)  {// code for all new browsers
		  xmlhttp2=new XMLHttpRequest();
	  	}
		else if (window.ActiveXObject) {
		  // code for IE5 and IE6
		  xmlhttp2=new ActiveXObject("Microsoft.XMLHTTP");
		}
		if (xmlhttp2!=null)  {
		  xmlhttp2.onreadystatechange=loadImage;
		  var url;
		  url = "getImage.asp?path=" + gallery[curPic].imagePath + "&pID="+gallery[curPic].pID;
		  if(admin){
		  	url = url + "&admin=T";
		  }
		  if(fanAcct) {
		  	url = url + "&fanID="+fanID;
		  }
		  xmlhttp2.open("GET",url,true);
		  xmlhttp2.send(null);
		}
		else  {
		  alert("Your browser does not support XMLHTTP.");
	    }
	}
	
	function loadImage() {
		if (xmlhttp2.readyState==4) {// 4 = "loaded"
		  if (xmlhttp2.status==200) {// 200 = OK
		    document.getElementById("divPrint").innerHTML = xmlhttp2.responseText;
			fadeIn();
		  }
	  	  else {
		    alert("Failed to Retrieve Image");
	      }
	  }
	}

	//functions to expand and shrink thumbnails on mouse over
	var pos, posx, posy, twidth, theight;
	var locked=false;
	var curthumb = "";
	function enlargeThumb(thumbID) {
		if(!locked) {
			locked=true;
			curthumb = thumbID;
			var thumb;
			thumb = document.getElementById("thumb"+thumbID);
			//get coords of thumb
			pos = findPos(thumb);			
			posx = thumb.offsetLeft;
			posy = thumb.offsetTop;
			
			//only run this is in IE or FF. Opera and Safari support coming soon.	
			if(posx!=0 || posy!=0) {	
				thumb.style.height = "auto"; 
				thumb.style.width =  "auto"; 
				//store target width
				theight = thumb.height;
				twidth = thumb.width;
				
				//reset so we can animate it
				thumb.style.height = "40px"; 
				thumb.style.width =  "40px"; 
				
				//animate
				cwidth = 40; cheight = 40;
				
				setTimeout("sizeUp('"+thumbID+"')", 1);
			}
			else {	
				return false;
			}
		}
	}
	
	function sizeUp(thumbID) {
		if(thumbID==curthumb) {
			var thumb;
			thumb = document.getElementById("thumb"+thumbID);
			if(cwidth < twidth) {
				if(cwidth + 4 > twidth) 
					cwidth = twidth
				else
					cwidth+=4;
			}   
			if(cheight < theight) {
				if(cheight + 4 > theight) 
					cheight = theight
				else
					cheight+=4;
			}
			
			//calculate space to move thumb by
			width = cwidth;
			height = cheight;
			moveleft = width/2-20;
			movedown = height/2-20;
			
			thumb.style.left = posx-moveleft+"px";
			thumb.style.top = posy-movedown+"px";
			thumb.style.position="absolute";
			thumb.style.zIndex = "100";
			thumb.style.height = cheight+"px"; 
			thumb.style.width =  cwidth+"px"; 
			//set timeout again
			if(cwidth < twidth || cheight < theight) { 
				setTimeout("sizeUp('"+thumbID+"')",1);
			}
		}
	}
	
	function restoreThumb(thumbID) {
		locked = true;
		var thumb;
		thumb = document.getElementById("thumb"+thumbID);
		thumb.style.visbility="hidden";
		thumb.style.height ="40px";
		thumb.style.width = "40px";
		thumb.style.position="relative";
		thumb.style.left = "0px";
		thumb.style.top = "0px";
		thumb.style.zIndex = "1";
		thumb.style.visbility="visible";
		locked=false;
		curthumb="";
	}
	
	
	function findPos(obj) {
		var curleft = curtop = 0;
		if (obj.offsetParent) {
			do {
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
			} while (obj = obj.offsetParent);
			return [curleft,curtop];
		}	
	}
	
	//function to create category link.
	function createLink(pID,cID) {
		xmlhttp3=null;
		if (window.XMLHttpRequest)  {// code for all new browsers
		  xmlhttp3=new XMLHttpRequest();
	  	}
		else if (window.ActiveXObject) {
		  // code for IE5 and IE6
		  xmlhttp3=new ActiveXObject("Microsoft.XMLHTTP");
		}
		if (xmlhttp3!=null)  {
	      xmlhttp3.onreadystatechange=confirmLink;
		  var url;
		  url = "createLink.asp?pID="+pID+"&cID="+cID;
		  xmlhttp3.open("GET",url,true);
		  xmlhttp3.send(null);
		}
	}
	
	function confirmLink() {
		if (xmlhttp3.readyState==4) {// 4 = "loaded"
		  if (xmlhttp3.status==200) {// 200 = OK
			alert("Created Link");
		  }
	  	  else {
		    alert("Failed to create link.");
	      }
	  }
	}
	
	
	
	//function to fave a link to a users account
	function addFave(uID) {
		xmlhttp4=null;
		if (window.XMLHttpRequest)  {// code for all new browsers
		  xmlhttp4=new XMLHttpRequest();
	  	}
		else if (window.ActiveXObject) {
		  // code for IE5 and IE6
		  xmlhttp4=new ActiveXObject("Microsoft.XMLHTTP");
		}
		if (xmlhttp4!=null)  {
	      xmlhttp4.onreadystatechange=confirmFave;
		  var url;
		  url = "addFave.asp?pID="+gallery[curPic].pID+"&uID="+uID;
		  xmlhttp4.open("GET",url,true);
		  xmlhttp4.send(null);
		}
	}
	
	function confirmFave() {
		if (xmlhttp4.readyState==4) {// 4 = "loaded"
		  if (xmlhttp4.status==200) {// 200 = OK
			alert("Added to Favorites - Thank you.");
		  }
	  	  else {
		    alert("Failed to Add Fave.");
	      }
	  }
	}
	
	//new function to redirect to cart page to add new print to the shopping cart
	function addToCart() {
		window.location.href = "buyprint.asp?pID="+ gallery[curPic].pID;
	}
	
	function showTerms() {
		window.open("terms.asp","terms and conditions","width=300,height=500");
	}
	
	function blockClick(e) {
		var msg = "All images are copyright© 2008 by Justin Kercher.\nYou may not use these images for anything without prior consent from the photographer."
		alert(msg);
		return false;
	}



/*********************************************************
 Poll System Functions 
**********************************************************/

function castVote(pID, optID) {
	xmlhttp5=null;
	if (window.XMLHttpRequest)  {// code for all new browsers
	  xmlhttp5=new XMLHttpRequest();
  	}
	else if (window.ActiveXObject) {
	  // code for IE5 and IE6
	  xmlhttp5=new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (xmlhttp5!=null)  {
      xmlhttp5.onreadystatechange=showPollResults;
	  var url;
	  url = "pollvote.asp?pID=" + pID + "&optID=" + optID;
	  xmlhttp5.open("GET",url,true);
	  xmlhttp5.send(null);
	}
}

function getResults(pID) {
	xmlhttp5=null;
	if (window.XMLHttpRequest)  {// code for all new browsers
	  xmlhttp5=new XMLHttpRequest();
  	}
	else if (window.ActiveXObject) {
	  // code for IE5 and IE6
	  xmlhttp5=new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (xmlhttp5!=null)  {
      xmlhttp5.onreadystatechange=showPollResults;
	  var url;
	  url = "pollresults.asp?pID=" + pID;
	  xmlhttp5.open("GET",url,true);
	  xmlhttp5.send(null);
	}
}

function showPollResults() {
	if (xmlhttp5.readyState==4) {// 4 = "loaded"
		  if (xmlhttp5.status==200) {// 200 = OK
			document.getElementById("divPoll").innerHTML = xmlhttp5.responseText;
		  }
	  	  else {
		    alert("Failed to cast vote.");
	      }
	  }
}

	//this function is used to switch URLs
	//Google will wind up with an individual PERM url 
	//and javascript users will end up with the gallery url.
	function switchURL(lnkID,cID) {
		document.getElementById("lnk"+lnkID).href="viewprint.asp?cID=" + cID + "&pg=" + lnkID;
	}
	
	function switchURLMode(lnkID,mode) {
		document.getElementById("lnk"+lnkID).href="viewprint.asp?mode=" + mode + "&pg=" + lnkID;
	}
	
	function switchURLModeSide(lnkID,mode) {
		document.getElementById("lnkside"+lnkID).href="viewprint.asp?mode=" + mode + "&pg=" + lnkID;
	}