var Gallery = 
{
	
	slideTime : 5000
	,
	transitionTime  : 2000
	,
	imgWidth  : 0
	,
	imgHeight : 0
	,
	         // src, href, SKU, width, height, copyright
	slides : [ [ "",   "",  "",     0,      0,		""] ]
	,
	photographer : ""
	,
	currentSlide : 0
	,
	screenID : "Gallery"
	,
	format : "generic"
	,
	nextText : "Next"
	,
	faderHandle : setTimeout("void(0);", 1)
	,
	galleryHandle : setTimeout("void(0);", 1)
	,
	opacity : 100
	,
	aryNumbers : [ "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten" ]
	,
	start : function ()
	{
		this.preload();
	}
	,
	start2 : function ()
	{
		this.drawControls();
		this.run();
	}
	,
	run : function ()
	{
		clearTimeout(this.faderHandle);
		clearTimeout(this.galleryHandle);
		captionObj = document.getElementById(this.screenID + "_caption").innerHTML = "";
		this.galleryHandle = setTimeout(this.screenID + '.running();', this.slideTime + this.transitionTime);
	}
	,
	running : function()
	{
		this.fadeNext();
		this.galleryHandle = setTimeout(this.screenID + '.running();', this.slideTime + this.transitionTime);
	}
	,
	preload : function ()
	{
		var imgPreloads = new Array();
		var i;
		for (i = 0; i < this.slides.length; i++) {
			imgPreloads[i] = new Image();
			imgPreloads[i].src = this.slides[i][0];//this.slides[i][0];
			//this.slides[i] = [ this.slides[i][0], this.slides[i][1], this.slides[i][2], imgPreloads[i].width, imgPreloads[i].height ];
		}
		if (i > 0) imgPreloads[(i - 1)].onLoad = this.start2();
		
	}
	,
	drawControls : function ()
	{
		if ("tiles" == this.format) {
			var bgColor = "";
			document.getElementById(this.screenID + "_controls").innerHTML = temp;

			//this.loadSlide();
			
			setTimeout(this.screenID + '.cycleControls(0);', 500);
		} else {
			var temp = '';
			document.getElementById(this.screenID + "_controls").innerHTML = temp;
		}
		this.gotoSlide(0);
	}
	,
	cycleControls : function (intSlideNo)
	{
		if (intSlideNo < this.slides.length) {
			document.getElementById(this.screenID + '_slidePicker_' + intSlideNo).style.backgroundColor = "#000";
			if (intSlideNo > 0) document.getElementById(this.screenID + '_slidePicker_' + (intSlideNo - 1)).style.backgroundColor = "#999";
			
			intSlideNo++;
			setTimeout(this.screenID + '.cycleControls(' + intSlideNo + ');', 80);
		} else if (intSlideNo == this.slides.length) {
			document.getElementById(this.screenID + '_slidePicker_0').style.backgroundColor = "#333";
			document.getElementById(this.screenID + '_slidePicker_' + (intSlideNo - 1)).style.backgroundColor = "#999";
			this.loadSlide();
		}
	}
	,
	getNext : function ()
	{
		this.currentSlide++;
		if (this.currentSlide >= this.slides.length) this.currentSlide = 0;
	}
	,
	getPrev : function ()
	{
		this.currentSlide--;
		if (this.currentSlide < 0) this.currentSlide = this.slides.length;
	}
	,
	goNext : function()
	{
		clearTimeout(this.galleryHandle);
		clearTimeout(this.faderHandle);
		
		/*this.getNext();
		this.loadSlide();
		var imgObj = document.getElementById(this.screenID + "_img");
		this.setOpacity(imgObj, 100);*/
		
		this.fadeNext();
	}
	,
	goPrev : function()
	{
		clearTimeout(this.galleryHandle);
		clearTimeout(this.faderHandle);
		
		this.getPrev();
		this.loadSlide();
		var imgObj = document.getElementById(this.screenID + "_img");
		this.setOpacity(imgObj, 100);
	}
	,
	gotoSlide : function (intSlideNo)
	{
		clearTimeout(this.galleryHandle);
		clearTimeout(this.faderHandle);
		
		this.currentSlide = intSlideNo;
		if (this.currentSlide >= this.slides.length) this.currentSlide = 0;
		if (this.currentSlide < 0) this.currentSlide = this.slides.length;
		this.loadSlide();
		var imgObj = document.getElementById(this.screenID + "_img");
		this.setOpacity(imgObj, 100);
	}
	,
	loadSlide : function()
	{
		var imgObj = document.getElementById(this.screenID + "_img");
		var linkObj = document.getElementById(this.screenID + "_link");
		var captionObj = document.getElementById(this.screenID + "_caption");
		var copyObj = document.getElementById(this.screenID + "_copyright");
		imgObj.src = '';
		imgObj.src = this.slides[this.currentSlide][0];
		if (this.slides[this.currentSlide][3] > 0 && this.slides[this.currentSlide][4] > 0) {
			imgObj.width = this.slides[this.currentSlide][3];
			imgObj.height = this.slides[this.currentSlide][4];
		}
		
		linkObj.href = this.slides[this.currentSlide][1];
		if ("tiles" == this.format) {
			captionObj.innerHTML = '<a href="#' + (this.currentSlide + 1) + '" class="SKUlink" onclick="location.href = \'/store/disp?cmd=nav&ef_orig_search_spec=&ef_orig_mode=&ef_srch_new_within=new&ef_vendors=all&ef_images_per_page=20&ef_sort_by=default&ef_shgrp=all&ef_search_spec=\' + Gallery.slides[Gallery.currentSlide][2] + \'&ef_rf=0&ef_rp=1&ef_image_cd=image\'; return false;">' + this.slides[this.currentSlide][2] + '</a> <em>(' + this.aryNumbers[this.currentSlide + 1] + ' of ' + this.aryNumbers[this.slides.length] + ')</em>';
			
			
			var slidePickerObj = document.getElementById(this.screenID + "_slidePicker_" + this.currentSlide);
			slidePickerObj.style.backgroundColor = "#000";
			
			for (var i = 0; i < this.slides.length; i++) {
				if (i != this.currentSlide) document.getElementById(this.screenID + "_slidePicker_" + i).style.backgroundColor = "#999";
			}
		} else {
			captionObj.innerHTML = "";
		}
	}
	,
	fadeNext : function()
	{
		clearTimeout(this.faderHandle);
		this.faderHandle = setTimeout(this.screenID + '.fadeNext_out();', 50);
	}
	,
		fadeNext_out : function()
		{
			var imgObj = document.getElementById(this.screenID + "_img");
			if (this.opacity > 0) {
				this.setOpacity(imgObj, this.opacity - 5);
				this.faderHandle = setTimeout(this.screenID + '.fadeNext_out();', (this.transitionTime/50) );
			} else {
				this.setOpacity(imgObj, 0);
				this.fadeNext_switch();
			}
		}
	,
		fadeNext_switch : function()
		{
			this.getNext();
			this.loadSlide();
			
			this.faderHandle = setTimeout(this.screenID + '.fadeNext_in();', (this.transitionTime/50) );
		}
	,
		fadeNext_in : function()
		{
			var imgObj = document.getElementById(this.screenID + "_img");
			if (this.opacity < 99) {
				this.setOpacity(imgObj, this.opacity + 5);
				this.faderHandle = setTimeout(this.screenID + '.fadeNext_in();', (this.transitionTime/50) );
			} else {
				this.setOpacity(imgObj, 100);
			}
		}
	,
	setOpacity : function(myObj, myOpacity)
	{ // set opacity of the element
		// Fix for math error in some browsers
		myOpacity = (myOpacity >= 100)? 99.999: myOpacity;
		myOpacity = (myOpacity < 0)? 0: myOpacity;
		// IE/Windows
		myObj.style.filter = "alpha(opacity: " + myOpacity + ")";
		// Safari < 1.2, Konqueror
		myObj.style.KHTMLOpacity = myOpacity/100;	
		// Older Mozilla and Firefox
		myObj.style.MozOpacity = myOpacity/100;
		// Safari 1.2, newer Firefox and Mozilla, CSS3
		myObj.style.opacity = myOpacity/100;
		
		
		
		this.opacity = myOpacity;
	}	
}

