/***********************************************
COPYRIGHT (C) 2006,2007 WILDFIREWEB INCORPORATED
All Rights Reserved
Written by Stephen Sweet
steve@wildfireweb.com
Last Modified 1/15/07
***********************************************/

var piccontainerobj;
var picobj;
var fadepictoview;
var picindex = 0;
var lastselected = 0;
var ssTimer=null;

function autoSlideShow() {
	var index = picindex;

	if (ssTimer)
		clearTimeout(ssTimer);

	if (scrollTimer) 
		clearTimeout(scrollTimer);

	ssTimer = scrollTimer = null;

	index++;
	if (index >= picsarray.length)
		index = 0;

	showpicture(index);

	ssTimer=setTimeout("autoSlideShow()",ssinterval);
}

function startSlideShow() {
	var obj = document.getElementById('sslink');
	if (obj) {
		obj.href = 'javascript:stopSlideShow();';
		obj.innerHTML = 'stop slideshow';
	}
	autoSlideShow();
}

function stopSlideShow() {
	if (ssTimer)
		clearTimeout(ssTimer);
	if (scrollTimer)
		clearTimeout(scrollTimer);
	ssTimer = scrollTimer = null;
	var obj = document.getElementById('sslink');
	if (obj) {
		obj.href = 'javascript:startSlideShow();';
		obj.innerHTML = 'start slideshow';
	}
}

var scrollto = 0;
var scrollTimer=null;
var lastscroll=0;
var thumbDiv=null;
var horizontal_thumbs;
var vertical_thumbs;

function scrollIt() {
	var dif, inc, scroll;

	// wait for fade-in to complete
	if (fadepictoview) {
		scrollTimer = setTimeout("scrollIt()",10);
		return;
	}

	if (scrollTimer)
		clearTimeout(scrollTimer);

	scrollTimer = null;

	if (horizontal_thumbs)
		scroll = thumbDiv.scrollLeft;
	else
		scroll = thumbDiv.scrollTop;

	if (scroll < scrollto) {
		dif = scrollto-scroll;
		inc = parseInt(dif/16);
		if (inc < 1) inc = 1;
		scroll = (dif > inc) ? scroll+inc : scrollto;
	}
	else if (scroll > scrollto) {
		dif = scroll-scrollto;
		inc = parseInt(dif/16);
		if (inc < 1) inc = 1;
		scroll = (dif > inc) ? scroll-inc : scrollto;
	}

	if (horizontal_thumbs)
		thumbDiv.scrollLeft = scroll;
	else
		thumbDiv.scrollTop = scroll;

	if (scroll != scrollto && scroll != lastscroll) {
		scrollTimer = setTimeout("scrollIt()",10);
	}

	lastscroll = scroll;
}

function moveTo(index) {
	var thumb_size;

	thumbDiv = document.getElementById('thumbnail_row');
	if (!thumbDiv) return;

	// determine if horizontal or vertical thumbs
	if ((thumbDiv.scrollWidth - thumbDiv.clientWidth) >= thumb_width) {
		thumb_size = thumb_width;
		horizontal_thumbs = true;
	}
	else
		horizontal_thumbs = false;

	if ((thumbDiv.scrollHeight - thumbDiv.clientHeight) >= thumb_height) {
		thumb_size = thumb_height;
		vertical_thumbs = true;
	}
	else
		vertical_thumbs = false;

	if (!horizontal_thumbs && !vertical_thumbs) return;

	var showing = div_size / thumb_size;
	var center = showing / 2;

	scrollto = parseInt(((index - center) * thumb_size) + (thumb_size / 2));

	scrollIt();
}

function showpicture(index){

	var obj, numpics, thispic, backpic, nextpic, url;

	picindex = index;

	numpics = picsarray.length;
	thispic = index+1;
	nextpic = index+1;
	backpic = index-1;

	if (nextpic >= numpics)
		nextpic = 0;

	if (backpic < 0)
		backpic = numpics-1;

	obj=document.getElementById("img_"+lastselected);
	if (obj)
		obj.className='thumbnail';

	obj=document.getElementById("img_"+index);
	if (obj)
		obj.className='thumbnail_selected';

	lastselected = index;

	// set 1 of N  count
	obj=document.getElementById("count");
	if (obj)
		obj.innerHTML = thispic+" of "+numpics;

	// the back link
	obj=document.getElementById("navback");
	if (obj) {
		obj.href = obj.href.replace(/currentPic=\d*/, 'currentPic='+backpic);
		obj.onclick = function() { showpicture(backpic); return false; }
	}
	
	// the next link
	obj=document.getElementById("navnext");
	if (obj) {
		obj.href = obj.href.replace(/currentPic=\d*/, 'currentPic='+nextpic);
		obj.onclick = function() { showpicture(nextpic); return false; }
	}

	// the main image when clicked goes to next
	obj=document.getElementById("navcur");
	if (obj) {
		obj.href = obj.href.replace(/currentPic=\d*/, 'currentPic='+nextpic);
		obj.onclick = function() { showpicture(nextpic); return false; }
	}

	piccontainerobj=document.getElementById("picturearea");
	picobj=document.getElementById('picturefade');
	resetfade(10);
	
	picobj.src=picsarray[index];

	obj = document.getElementById("description");
	if (obj)
		obj.innerHTML=description[index];
	
	obj = document.getElementById("title");
	if (obj)
		obj.innerHTML=title[index];

	obj = document.getElementById("amount");
	if (obj) {
		if (price[index]) {
			obj.innerHTML='Amount $'+price[index];
			obj = document.getElementById("cart_form");
			if (obj) obj.style.display='block';
		}
		else {
			obj.innerHTML='';
			obj = document.getElementById("cart_form");
			if (obj) obj.style.display='none';
		}
	}

	// shopping cart
	obj = document.getElementById("Item");
	if (obj) obj.value=title[index]; 
	obj = document.getElementById("PartNo");
	if (obj) obj.value=partno[index];
	obj = document.getElementById("Weight");
	if (obj) obj.value=weight[index];
	obj = document.getElementById("Price");
	if (obj) obj.value=price[index];

	// resize the iframe because picture may change height
	if (parent.adjustIFrameSize) 
		parent.adjustIFrameSize('galleryframe', -1);
	
	fadepictoview=setTimeout("gradualfade(0)",40);
	scrollTimer=setTimeout("moveTo(picindex)",50);

	if (ssTimer) {
		clearTimeout(ssTimer);
		ssTimer=setTimeout("autoSlideShow()",ssinterval);
	}

}

function resetfade(degree){
	if (window.fadepictoview)
		clearTimeout(fadepictoview);

	if (typeof piccontainerobj.style.MozOpacity=="string")
		piccontainerobj.style.MozOpacity=degree/100;
	else if (picobj.filters) {
		picobj.style.filter = "alpha(opacity=" + degree + ")";
		picobj.filters.alpha.opacity=degree;
	}
}

function gradualfade(){

	fadepictoview=null;

	if (typeof piccontainerobj.style.MozOpacity=="string" && piccontainerobj.style.MozOpacity<0.99) 
		piccontainerobj.style.MozOpacity=Math.min(parseFloat(piccontainerobj.style.MozOpacity)+0.1, 0.99);
	else if (picobj.filters && picobj.filters.alpha.opacity<100)
		picobj.filters.alpha.opacity+=10;
	else  // finished
		return; 

	fadepictoview=setTimeout("gradualfade(0)",40);
}

function arrow_keys(evt) { 
	var key;

	if (window.event) {
		key = window.event.keyCode;
	}
	else if (window.top.event) {
		key = window.top.event.keyCode;
	}
	else {
		key = evt.keyCode;
	}

	if (key == 37 || key == 39) { // left or right arrow keys
		numpics = picsarray.length;
		if (key == 37) {
			nextpic = picindex-1;

			if (nextpic < 0)
				nextpic = numpics-1;
		}
		else if (key == 39) {
			nextpic = picindex+1;

			if (nextpic >= numpics)
				nextpic = 0;
		}
		showpicture(nextpic);

		if (window.event) {
			window.event.cancelBubble = true;
		}
		else if (window.top.event) {
			window.top.event.cancelBubble = true;
		}
		else {
			evt.preventDefault(); 
			evt.stopPropagation();
		}
	}
}

if (window.addEventListener)  {
	window.addEventListener("keypress", arrow_keys, true); 
	window.top.addEventListener("keypress", arrow_keys, true); 
}
else {
	document.onkeyup = arrow_keys; 
	window.top.document.onkeyup = arrow_keys;
}

