// JavaScript Document
/*
This script Copyright 2006 Michael Chamberlain.
This script may be used under the following conditions:
1.	The script may not be altered in any way
2.	This header must be included with the script

*/

	function gallery(name, title, container, contact, contactemail, thumbsize, delay, hr){
		this.gallerycontainer = container;
		this.galleryname = title;
		this.name = name;
		this.slideshowon = false;
		this.slideshowhalt = false;
		this.slideshowdelay = delay;
		this.highres = hr;
		this.collectionindex = 0;
		this.collections = new Array;
		this.collectiontext = "";
		this.gallerycontact = contact;
		this.galleryemail = contactemail;
		this.addcollection = addcollection;
		this.printgallery = printgallery;
		this.loadcollection = loadcollection;
		this.changeversion = changeversion;
		this.changesize = changesize;
		this.autosize = autosize;
		this.previous = previous;
		this.next = next;
		this.play = startslide;
		this.stopslide = stopslide;
		this.downloadimage = downloadimage;
		this.loadimage = loadimage;
		preload = true;
		lock = false;
		scrolllock = false;
		globalposition = 0;
		thumboffset = thumbsize;
	};
	
	function collection(name, path, auto){
		this.name = ""
		this.collectionname = name;
		this.collectionpath = path;
		this.sizeauto = auto;
		this.sizeindex = 0;
		this.calculatedsize = 0;
		this.displaysize = 0;
		this.versionindex = 0;
		this.imageindex = 0;
		this.version = new Array();
		this.versionpath = new Array();
		this.versiontext = "";
		this.size = new Array();
		this.sizepath = new Array();
		this.sizelength = new Array();
		this.sizetext = ""
		this.image = new Array();
		this.alt = new Array();
		this.caption = new Array();
		this.thumbnails = "";
		this.addversion = addversion;
		this.addsize = addsize;
		this.addimage = addimage;
	};
	
	function addcollection(name, path, auto){
		this.collections[this.collections.length] = new collection(name, path, auto);
		if (this.collectiontext == ""){
			this.collectiontext = "<a id='collection" + (this.collections.length-1) + "' class='active' href='javascript:" + this.name + ".stopslide(); " + this.name + ".loadcollection(" + (this.collections.length-1) + ")'>" + name + "</a>";
		}else{
			this.collectiontext = this.collectiontext + "<span class='spacer'><span>&nbsp;&nbsp;|&nbsp;&nbsp;</span></span><a id='collection" + (this.collections.length-1) + "' class='inactive' href='javascript:" + this.name + ".stopslide(); " + this.name + ".loadcollection(" + (this.collections.length-1) + ")'>" + name + "</a>";
		};
		this.collections[this.collections.length-1].name = this.name;
	};
	
	function addimage(newimage, newalt, newcaption){
		arraylength = this.image.length;
		this.image[arraylength] = newimage;
		if (newalt == ""){
			this.alt[arraylength] = newcaption;
		}else{
			this.alt[arraylength] = newalt;
		}
		this.caption[arraylength] = newcaption;
		this.thumbnails = this.thumbnails + "<div id='thumbnail" + arraylength + "' class='thumbnail'><table><tr><td><a href='javascript:" + this.name + ".stopslide(); " + this.name + ".loadimage(" + arraylength + ")'><img src=" + this.collectionpath + "thumbs/" + newimage + "></a></td></tr></table></div>";
	};

	function addversion(name, path){
		this.version[this.version.length] = name;
		this.versionpath[this.versionpath.length] = path;
		if (this.versiontext == ""){
			this.versiontext = "<a id='version" + (this.version.length-1) + "' class='active' href='javascript:" + this.name + ".changeversion(" + (this.version.length-1) + ")'>" + name + "</a>";
		}else{
			this.versiontext = this.versiontext + "<span class='spacer'><span>&nbsp;&nbsp;|&nbsp;&nbsp;</span></span><a id='version" + (this.version.length-1) + "' class='inactive' href='javascript:" + this.name + ".changeversion(" + (this.version.length-1) + ")'>" + name + "</a>";
		};
	};

	function addsize(name, path, maxlength){
		this.size[this.size.length] = name;
		this.sizepath[this.sizepath.length] = path;
		this.sizelength[this.sizelength.length] = maxlength;
		if (this.sizetext == ""){
			this.sizetext = "<a id='size" + (this.size.length-1) + "' class='active' href='javascript:" + this.name + ".changesize(" + (this.size.length-1) + ")'>" + name + "</a>";
		}else{
			this.sizetext = this.sizetext + "<span class='spacer'><span>&nbsp;&nbsp;|&nbsp;&nbsp;</span></span><a id='size" + (this.size.length-1) + "' class='inactive' href='javascript:" + this.name + ".changesize(" + (this.size.length-1) + ")'>" + name + "</a>";
		};
	};
	
	function printgallery(){
		if (this.collections.length < 1){
			document.getElementById(this.gallerycontainer).innerHTML = "Error no collections defined";	
		}else{
			if(this.collections.length == 1){
				this.collectiontext = "";			
			};
			if(this.highres === true){
				highrestext = "<div id='download'><a href='javascript:" + this.name + ".downloadimage();'><span>Download</span></a></div>";
			}else{
				highrestext = "";
			};
			if((this.gallerycontact != "") && (this.galleryemail != "")){
				contacttext = "<div id='contact'>Email <a href='mailto:" + this.galleryemail + "?subject=" + this.galleryname + "'>" + this.gallerycontact + "</a></div>";
			}else{
				contacttext = "";
			};
			document.getElementById(this.gallerycontainer).innerHTML = "<div id='header'><div id='title'>" + this.galleryname + "</div><div id='collectiontitle'></div>" + contacttext + "</div><div id='control'><div id='collections'>" + this.collectiontext + "</div><div id='versions'></div><div id='sizes'></div></div><div id='thumbnailwrapper'><div id='thumbnails'><div id='thumbnailholder'></div></div><div id='slider'><div id='uparrow'></div><div id='handle'></div><div id='downarrow'></div></div><div class='border_t'></div><div class='border_l'></div><div class='border_r'></div><div class='border_b'></div><div class='border_tr'></div><div class='border_tl'></div><div class='border_br'></div><div class='border_bl'></div></div><div id='imagewrapper'><div id='imagewindow'><div id='displayimage' style='display:none';></div></div><div id='remote'><div id='previous'><a href='javascript:" + this.name + ".stopslide(); " + this.name + ".previous();'><span>Previous</span></a></div>" + highrestext + "<div id='slideshow'><a class='play' href='javascript:" + this.name + ".play();'><span>Play</span></a></div><div id='next'><a href='javascript:" + this.name + ".stopslide(); " + this.name + ".next();'><span>Next</span></a></div><div id='caption'></div></div><div class='border_t'></div><div class='border_l'></div><div class='border_r'></div><div class='border_b'></div><div class='border_tr'></div><div class='border_tl'></div><div class='border_br'></div><div class='border_bl'></div></div>";
			if(this.collections.length == 1){
				document.getElementById("collections").style.position = "absolute";
			};
		};
		myslider = new Control.Slider('handle','slider',{axis:'vertical',
			onSlide:function(v){movethumbnails(Math.floor(v*imagecount))},
			onChange:function(v){movethumbnails(Math.floor(v*imagecount))}});
	};
	
	function loadimage(index){
		if (lock === false){
			if ((document.getElementById("displayimage").innerHTML == "") || (this.collections[this.collectionindex].imageindex != index)){
				lock = true;
				this.collections[this.collectionindex].imageindex = index;
				path = "";
				path = path + this.collections[this.collectionindex].collectionpath;
				path = path + this.collections[this.collectionindex].versionpath[this.collections[this.collectionindex].versionindex];
				if (this.collections[this.collectionindex].sizeindex == this.collections[this.collectionindex].size.length){
					path = path + this.collections[this.collectionindex].sizepath[this.collections[this.collectionindex].displaysize];
				}else{
					path = path + this.collections[this.collectionindex].sizepath[this.collections[this.collectionindex].sizeindex];
				};
				path = path + this.collections[this.collectionindex].image[this.collections[this.collectionindex].imageindex];
				newinnerhtml = "<table><tr><td><img src='" + path + "' alt='" + this.collections[this.collectionindex].alt[this.collections[this.collectionindex].imageindex] + "'></td></tr></table>";
				newcaption = this.collections[this.collectionindex].caption[this.collections[this.collectionindex].imageindex];
				setTimeout('document.getElementById("displayimage").innerHTML = newinnerhtml;', 1000);
				setTimeout('document.getElementById("caption").innerHTML = newcaption;', 1000);
				Effect.Fade('displayimage');
				setTimeout("Effect.Appear('displayimage')", 1200);
				setTimeout("lock = false", 2000);
			}else{
				this.collections[this.collectionindex].imageindex = index;
				path = "";
				path = path + this.collections[this.collectionindex].collectionpath;
				path = path + this.collections[this.collectionindex].versionpath[this.collections[this.collectionindex].versionindex];
				if (this.collections[this.collectionindex].sizeindex == this.collections[this.collectionindex].size.length){
					path = path + this.collections[this.collectionindex].sizepath[this.collections[this.collectionindex].displaysize];
				}else{
					path = path + this.collections[this.collectionindex].sizepath[this.collections[this.collectionindex].sizeindex];
				};
				path = path + this.collections[this.collectionindex].image[this.collections[this.collectionindex].imageindex];
				newinnerhtml = "<table><tr><td><img src='" + path + "' alt='" + this.collections[this.collectionindex].alt[this.collections[this.collectionindex].imageindex] + "'></td></tr></table>";
				document.getElementById("displayimage").innerHTML = newinnerhtml;
				document.getElementById("caption").innerHTML = this.collections[this.collectionindex].caption[this.collections[this.collectionindex].imageindex];
			};
		}else{
			this.collections[this.collectionindex].imageindex = index;
			path = "";
			path = path + this.collections[this.collectionindex].collectionpath;
			path = path + this.collections[this.collectionindex].versionpath[this.collections[this.collectionindex].versionindex];
			if (this.collections[this.collectionindex].sizeindex == this.collections[this.collectionindex].size.length){
				path = path + this.collections[this.collectionindex].sizepath[this.collections[this.collectionindex].displaysize];
			}else{
				path = path + this.collections[this.collectionindex].sizepath[this.collections[this.collectionindex].sizeindex];
			};
			path = path + this.collections[this.collectionindex].image[this.collections[this.collectionindex].imageindex];
			newinnerhtml = "<table><tr><td><img src='" + path + "' alt='" + this.collections[this.collectionindex].alt[this.collections[this.collectionindex].imageindex] + "'></td></tr></table>";
			document.getElementById("displayimage").innerHTML = newinnerhtml;
		};
	};
	
	function changeversion (version){
		if (this.collections[this.collectionindex].version.length > 1){
			for (v=0; v<this.collections[this.collectionindex].version.length; v++){
				document.getElementById("version" + v).className = "inactive";
			};
			document.getElementById("version" + version).className = "active";
		};
		if (this.collections[this.collectionindex].versionindex != version){
			this.collections[this.collectionindex].versionindex = version;
			this.loadimage(this.collections[this.collectionindex].imageindex);
		};
	};

	function changesize (size){
		if (this.collections[this.collectionindex].size.length > 1){
			for (v=0; v<=this.collections[this.collectionindex].size.length; v++){
				document.getElementById("size" + v).className = "inactive";
			};
			document.getElementById("size" + size).className = "active";
		};
		if (this.collections[this.collectionindex].sizeindex != size){
			this.collections[this.collectionindex].sizeindex = size;
			this.loadimage(this.collections[this.collectionindex].imageindex);
		};
	};
	
	function previous(){
		if (this.collections[this.collectionindex].imageindex == 0){
			this.loadimage(this.collections[this.collectionindex].image.length-1);			
		}else{
			this.loadimage(this.collections[this.collectionindex].imageindex-1);
		};
	}

	function next(){
		if (this.slideshowhault === true){
			this.slideshowhault = false;
		}else{
			if (this.collections[this.collectionindex].imageindex == this.collections[this.collectionindex].image.length-1){
				this.loadimage(0);			
			}else{
				this.loadimage(this.collections[this.collectionindex].imageindex+1);
			};
		};
		if (this.slideshowon === true) {
			setTimeout((this.name + '.next();'), this.slideshowdelay);
		};
	}

	function startslide(){
			document.getElementById("slideshow").innerHTML = "<a class='stop' href='javascript:" + this.name + ".stopslide();'><span>Stop</span></a>"
			this.slideshowon = true;
			this.next();
	}

	function stopslide(){
		if (this.slideshowon === true){
			document.getElementById("slideshow").innerHTML = "<a class='play' href='javascript:" + this.name + ".play();'><span>Play</span></a>"
			this.slideshowon = false;
			this.slideshowhault = true;
		}
	}

	function downloadimage(){
		path = "";
		path = path + this.collections[this.collectionindex].collectionpath;
		path = path + this.collections[this.collectionindex].versionpath[this.collections[this.collectionindex].versionindex];
		path = path + "highres/";
		path = path + this.collections[this.collectionindex].image[this.collections[this.collectionindex].imageindex];
		window.open (path,"newwindow"); 
	}

	function autosize(offset){
		wrapperheight = document.getElementById("imagewrapper").offsetHeight - offset;
		wrapperwidth = document.getElementById("imagewrapper").offsetWidth - offset;
		if (wrapperheight < wrapperwidth){
			longedge = wrapperheight;
		}else{
			longedge = wrapperwidth;
		};
		newsize = 0;
		newsizevalue = 0;
		smallest = 0;
		smallestvalue = 50000;
		for (v=0; v<this.collections[this.collectionindex].size.length; v++){
			if (this.collections[this.collectionindex].sizelength[v] < longedge){
				if (this.collections[this.collectionindex].sizelength[v] > newsizevalue){
					newsizevalue = this.collections[this.collectionindex].sizelength[v];
					newsize = v;
				};
			};
			if (this.collections[this.collectionindex].sizelength[v] < smallestvalue){
				smallestvalue = this.collections[this.collectionindex].sizelength[v];
				smallest = v;
			};
		};
		if (newsizevalue == 0){
			newsize = smallest;
		};
		this.collections[this.collectionindex].calculatedsize = newsize;
		if (this.collections[this.collectionindex].calculatedsize != this.collections[this.collectionindex].displaysize){
			this.collections[this.collectionindex].displaysize = this.collections[this.collectionindex].calculatedsize;
			this.loadimage(this.collections[this.collectionindex].imageindex);
		};
	};
	
	function movethumbnails(position){
		if (globalposition != position && scrolllock === false){
			globalposition = position;
			document.getElementById("thumbnailholder").style.marginTop = "-" + (globalposition * thumboffset) + "px";
		};
	};
	
	function loadcollection(collection){
		document.getElementById("collectiontitle").innerHTML = this.collections[collection].collectionname;
		document.getElementById("thumbnailholder").innerHTML = this.collections[collection].thumbnails;
		if (this.collections.length >1){
			document.getElementById("collection" + this.collectionindex).className = "inactive";
			document.getElementById("collection" + collection).className = "active";
		}
		this.collectionindex = collection;
		if (this.collections[collection].version.length <= 1){
			document.getElementById("versions").innerHTML = "";
			document.getElementById("versions").style.position = "absolute";
		}else{
			document.getElementById("versions").style.position = "relative";
			document.getElementById("versions").innerHTML = this.collections[collection].versiontext;
		};
		this.changeversion(this.collections[collection].versionindex);
		if (this.collections[collection].sizeauto === true){
			this.collections[collection].sizetext = this.collections[collection].sizetext + "<span class='spacer'><span>&nbsp;&nbsp;|&nbsp;&nbsp;</span></span><a id='size" + (this.collections[collection].size.length) + "' class='active' href='javascript:" + this.name + ".changesize(" + this.collections[collection].size.length + ")'>Auto</a>";
			this.collections[collection].sizeauto = false;
			this.collections[collection].sizeindex = this.collections[collection].size.length;
		};
		if (this.collections[collection].size.length <= 1){
			document.getElementById("sizes").innerHTML = "";
			document.getElementById("sizes").style.position = "absolute";
		}else{
			document.getElementById("sizes").style.position = "relative";
			document.getElementById("sizes").innerHTML = this.collections[collection].sizetext;
		};
		imagecount = (this.collections[collection].image.length - (document.getElementById("thumbnails").offsetHeight/(thumboffset-1)))+1;
		myslider.dispose;
		myslider = new Control.Slider('handle','slider',{axis:'vertical',
			onSlide:function(v){movethumbnails(Math.floor(v*imagecount))},
			onChange:function(v){movethumbnails(Math.floor(v*imagecount))}});
		if (imagecount > this.collections[collection].imageindex) {
			myslider.setValue(this.collections[collection].imageindex/imagecount);
		}else{
			myslider.setValue(1);
		};
		this.changesize(this.collections[collection].sizeindex);
		windowwidth = windowwidth-1;
		this.loadimage(this.collections[this.collectionindex].imageindex);
	};
	
	//This will have to be modified in relation to your CSS design.  Is not necessary for fixed layouts.
	windowwidth = 0;
	windowheight = 0;
	function resize(galleryname, gallerynametext){
		if ((document.getElementById("GalleryWrap").offsetWidth != windowwidth) || (document.getElementById("GalleryWrap").offsetHeight != windowheight)){
			galleryheight = document.getElementById("GalleryWrap").offsetHeight;
			gallerywidth = document.getElementById("GalleryWrap").offsetWidth;
			headerheight = (document.getElementById("header").offsetHeight + document.getElementById("control").offsetHeight);
			thumbwidth = document.getElementById("thumbnailwrapper").offsetWidth;
			thumbheight = Math.abs(galleryheight-headerheight-20);
			document.getElementById("thumbnailwrapper").style.height = thumbheight;
			document.getElementById("imagewrapper").style.height = thumbheight;
			document.getElementById("imagewrapper").style.width = Math.abs(gallerywidth-thumbwidth-30);
			document.getElementById("displayimage").style.height = document.getElementById("imagewrapper").offsetHeight - 80;
			if (((thumbheight - 40)/(thumboffset-1))<1){
				thumbboxheight = (thumboffset-1);		
			}else{
				thumbboxheight = Math.floor((thumbheight - 40)/(thumboffset-1))*(thumboffset-1);		
			}
			thumbmargin = Math.floor((thumbheight-thumbboxheight-40)/2);
			if (thumbmargin < 0){
				thumbmargin = 0;	
			}
			document.getElementById("thumbnails").style.height = thumbboxheight;
			document.getElementById("thumbnails").style.marginTop = thumbmargin;
			document.getElementById("thumbnails").style.marginBottom = thumbmargin;
			document.getElementById("slider").style.height = thumbboxheight - 20;
			document.getElementById("slider").style.marginTop = thumbmargin;
			document.getElementById("slider").style.marginBottom = thumbmargin;
			imagecount = (galleryname.collections[galleryname.collectionindex].image.length - (document.getElementById("thumbnails").offsetHeight/(thumboffset-1)))+1;
			myslider.dispose;
			myslider = new Control.Slider('handle','slider',{axis:'vertical',
				onSlide:function(v){movethumbnails(Math.floor(v*imagecount))},
				onChange:function(v){movethumbnails(Math.floor(v*imagecount))}});
			if (imagecount > galleryname.collections[galleryname.collectionindex].imageindex) {
				myslider.setValue(galleryname.collections[galleryname.collectionindex].imageindex/imagecount);
			}else{
				myslider.setValue(1);
			};
			windowwidth = gallerywidth;
			windowheight = galleryheight;
			galleryname.autosize(80);
		};
		setTimeout('resize(etca, "etca")', 250);
	};