/****************************************************
AJAX Gallery System 
Copyright© 2008 by Justin Kercher
This system may not be distributed or used without
explicit consent from the author. This is not 
an Open Source gallery, this is a private system.

Version 1.0
This version of the script has been licensed for
use to Apollo Internet Media for implementation
on the Dreamcatcher website. It may not be used
for further projects.
*****************************************************/


	/* define class for image library browser */

  
	//Connstructor for the gallery image 'Class'.
	//Instantiate with photo = new Photo(...);
	function Photo(title,imagePath) {
		this.title = title;
		this.imagePath = imagePath;
	}

	//create array of photos for javascript enabled gallery browsing
	var gallery = [];
	//gallery variables
	var curPic = 0;
	var playing = true;

	//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;
				
			//displayPic(curPic);
			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;
			
			//displayPic();
			fadeOut();
		}
	}
	
	//function to play slideshow
	function playSlideShow() {		
		if(playing) {
			moveNext();
		} 
		else 
			clearTimeout;
	}
	
	function stopSlideShow() {
		clearTimeout();
	}
	
	function toggleSlideShow() {
		if(!playing) {
			document.getElementById("btnPlay").src = "images/stop.jpg";
			document.getElementById("btnPlay").alt = "Pause Slideshow";
			playing = true;
			playSlideShow();
		}
		else {
			document.getElementById("btnPlay").src = "images/play.jpg";
			document.getElementById("btnPlay").alt = "Play Slideshow";
			playing = false;
		}
	}
	
	//function to display picture
	function displayPic() {
		//set document title and others
		//document.title = gallery[curPic].title + " - Photography Prints by Justin Kercher";
		//document.getElementById("picTitle").innerHTML = gallery[curPic].title;

		//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 {
			//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;
		  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];
		}	
	}
	






