/* shwFade.js (c)2008 SugarHill Works LLC - http://www.sugarhillworks.com */
/* v3-20081115_Z.1  [thumbs nav only, autoscroll thumbs, single init, fsmenu, zoomify] */

// -------- SETTINGS -----------
var ss_interval = 2500;

//----- init the slideshows
addEvent(window, 'load', function() { 
	ss_p = d.getElementById('zoom_div');
	ss_init('zoom_div', 1);//no preload
});

// randomize the order
//imageFilenames.sort( randOrd );
// -----------------------------

var d = document;
var ifrs = [];
var paramStrings = [];
var parampass;
var current = 0, current_fr = 0, current_title = 0;
var nIndex = 1;
var ssRunning;
var nPause;
var lastRun;
var fadeComplete = true;
var ssPauseTO;
//var resumeImg;
var navArwL_div;
var navArwR_div;
var navArwL = d.createElement('img');
var navArwR = d.createElement('img');
var ssPoll;
var ss_p;
var thms_div;
var thms_div_scr;
var thms_ul;
var thms_lis = [];
var thms_as = [];
var thms = [];
var title_cntr;
var titles = [];
var PP_names = [];
var PP_numbers = [];
var PP_amounts = [];
var sale_amounts = [];
var sale_msgs = [];
var rsMsg = '';
var dir;
//var resumeToggle = '';
var scr;
var thms_divW;
var thms_ul_newW;
var clrbr;
var scrolling;

var isitFF2mac = false;
//if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)){ //test for Firefox/x.x or Firefox x.x (ignoring remaining digits);
//	 var ffversion=new Number(RegExp.$1); // capture x.x portion and store as a number
//	 if (ffversion < 3 && ffversion >= 2) {
//		isitFF2 = true;
//	 }
//}
//function detectMacXFF2() {
 var userAgent = navigator.userAgent.toLowerCase();
  if (/firefox[\/\s](\d+\.\d+)/.test(userAgent)) {
    var ffversion = new Number(RegExp.$1);
    if (ffversion < 3 && userAgent.indexOf('mac') != -1) {
      isitFF2mac = true;
    }
  }
//}

function ss_init(ss_p_id, arg) {
	var i; 
	if (!d.getElementById || !d.createElement) {
		return;
	}
	ss_p = d.getElementById(ss_p_id);
	thms_div = d.getElementById('thms_div');
	thms_divW = thms_div.offsetWidth;
	thms_ul = d.createElement('ul');
	thms_ul.id = 'thms_ul';
	clrbr = d.createElement('br');
	clrbr.className = "clearBoth";
	title_cntr = d.getElementById('title_cntr');
	
	if (arg === 1) { //this is the auto restart run
//		ss_p.innerHTML = '';
//		if (thms_div_scr) {
//			thms_div_scr.innerHTML = '';
//		} else {
//			thms_div.innerHTML = '';
//		}
		for (i = 0; i < ssn; i++) {
			paramStrings[i] = 'zoomifyImagePath=' + slideshow.sPicsFolder + slideshow[i].zoom_f + '/&zoomifyNavWindow=0';
			PP_names[i] = slideshow[i].PP_item_name;
			PP_numbers[i] = slideshow[i].PP_item_number;
			PP_amounts[i] = slideshow[i].PP_amount;
			if (slideshow[i].onSale === true) { 
				sale_amounts[i] = slideshow[i].sale_amount;
				sale_msgs[i] = slideshow[i].sale_message;
			}
			//populate the thumbnails table for this portfolio
			thms_lis[i] = d.createElement('li');
			thms_lis[i].style.width = Math.round(thms_divW / thmsPerPg) + 'px';
			thms_as[i] = d.createElement('a');
			thms[i] = d.createElement('img');
			thms[i].src = thmsFolder + slideshow[i].thumb;
			thms[i].id = i;
			thms[i].alt = '';//slideshow[i].info;
			thms[i].onmouseover = function() {
				this.xOpacity = 0.99;
				this.style.filter = "alpha(opacity=99)";
				this.style.MozOpacity = "0.99";
				this.style.opacity = "0.99";

			};
			if (i !== 0) {
				thms[i].onmouseout = function() {
					this.xOpacity = 0.70;
					this.style.filter = "alpha(opacity=70)";
					this.style.MozOpacity = "0.70";
					this.style.opacity = "0.70";
				};
			} else {
				thms[i].onmouseout = function() {
					this.xOpacity = 0.99;
					this.style.filter = "alpha(opacity=99)";
					this.style.MozOpacity = "0.99";
					this.style.opacity = "0.99";
				};
			}
			thms[i].onclick = function() { 
				this.xOpacity = 0.70;
				this.style.filter = "alpha(opacity=70)";
				this.style.MozOpacity = "0.70";
				this.style.opacity = "0.70";
				window.clearTimeout(ssRunning);
				if (fadeComplete === false) {
					//do nothing 
				} else {
					nIndex = parseInt(this.id, 10);
					nPause = 'paused';
					getImgsToFade(ss_p_id,current,current_fr,nPause,nIndex);
					return false;
				}
			};
			thms_as[i].appendChild(thms[i]);
			thms_lis[i].appendChild(thms_as[i]);
			thms_ul.appendChild(thms_lis[i]);
			if(thms.length < 9) {
				d.getElementById('thmArwL_div').style.visibility = 'hidden';
				d.getElementById('thmArwR_div').style.visibility = 'hidden';
			} else {
				d.getElementById('thmArwL_div').style.visibility = 'visible';
				d.getElementById('thmArwR_div').style.visibility = 'visible';
			}
			
		}
	}
	
	
	if (thms_div_scr) {
		thms_div_scr.appendChild(thms_ul);
		thms_div_scr.appendChild(clrbr);
	} else {
		thms_div.appendChild(thms_ul);
		thms_div.appendChild(clrbr);
	}
	
	// resize the thumbs table for pretty layout
	thms_ul_newW = Math.round(thms_lis[0].offsetWidth * thms.length);// + tpad);//10 for padding	
	thms_ul.style.width = thms_ul_newW + 'px';

	//var csb = new CSBfleXcroll('thms_div');
	CSBfleXcroll('thms_div');
	thms_div_scr = d.getElementById('thms_div_contentwrapper');
	// setup image styles
	for (i = 0; i < ssn; i++) {
		var thm = thms[i];
		if (i === 0) {
//			img.xOpacity = 0.99;
//			img.style.filter = "alpha(opacity=99)";
//			img.style.MozOpacity = "0.99";
//			img.style.opacity = "0.99";
			thm.xOpacity = 0.99;
			thm.style.filter = "alpha(opacity=99)";
			thm.style.MozOpacity = "0.99";
			thm.style.opacity = "0.99";
		}
		else {
//			img.xOpacity = 0;
//			img.style.filter = "alpha(opacity=0)";
//			img.style.MozOpacity = "0";
//			img.style.opacity = "0";
			thm.xOpacity = 0.70;
			thm.style.filter = "alpha(opacity=70)";
			thm.style.MozOpacity = "0.70";
			thm.style.opacity = "0.70";
		}
	}
	
	ifrs[0] = d.createElement('iframe');
	ifrs[0].className = 'zframe';
	ifrs[0].frameBorder = 0;
	ifrs[0].scrolling = 'no';//ifrs[i].setAttribute('scrolling', 'no');
	if (isitFF2mac === false) {
		ifrs[0].xOpacity = 0.99;
		ifrs[0].style.filter = "alpha(opacity=99)";
		ifrs[0].style.MozOpacity = "0.99";
		ifrs[0].style.opacity = "0.99";
	}
	ifrs[0].style.zIndex = '980';
	if (isitIE6 === true) {
		window.setTimeout("ifrs[0].src = '../../shw_zoom/ifr.html';", 2000);
	} else {
		ifrs[0].src = '../../shw_zoom/ifr.html'
	}
	ifrs[1] = d.createElement('iframe');
	ifrs[1].className = 'zframe';
	ifrs[1].frameBorder = 0;
	ifrs[1].scrolling = 'no';//ifrs[i].setAttribute('scrolling', 'no');
	if (isitFF2mac === false) {
		ifrs[1].xOpacity = 0;
		ifrs[1].style.filter = "alpha(opacity=0)";
		ifrs[1].style.MozOpacity = "0";
		ifrs[1].style.opacity = "0";
	}
	ifrs[1].style.zIndex = '960';
	titles[0] = d.createElement('div');
	titles[0].xOpacity = 0.99;
//	titles[0].style.filter = "alpha(opacity=99)";
//	titles[0].style.MozOpacity = ".99";
//	titles[0].style.opacity = ".99";
	titles[1] = d.createElement('div');
	titles[1].xOpacity = 0;
//	titles[1].style.filter = "alpha(opacity=0)";
//	titles[1].style.MozOpacity = "0";
//	titles[1].style.opacity = "0";
	// append the visible obj[0] last so it's stacked on top in the html
	parampass = paramStrings[0];
	ss_p.appendChild(ifrs[1]);
	ss_p.appendChild(ifrs[0]);
	titles[0].innerHTML = slideshow[0].title;
	title_cntr.appendChild(titles[1]);
	titles[1].className = 'title_txt';
	title_cntr.appendChild(titles[0]);
	titles[0].className = 'title_txt';
	if(d.paypal_fm) {
		d.paypal_fm.item_name.value = PP_names[0] + ' - ' + slideshow[0].title;
		d.paypal_fm.item_number.value = PP_numbers[0];
		if(slideshow[0].onSale === false) {
			d.paypal_fm.amount.value = parseFloat(PP_amounts[0]);
		} else {
			d.paypal_fm.amount.value = parseFloat(sale_amounts[0]);
		}
	}
	if(d.getElementById('card_info')) {
		var card_info = d.getElementById('card_info');
		var card_info_p = d.createElement('p');
		card_info.innerHTML = '';
		if(slideshow[0].onSale === false) {
			card_info_p.innerHTML = PP_names[0] + '<br />' + PP_numbers[0] + '<br />' + '$' + PP_amounts[0];
		} else {
			card_info_p.innerHTML = PP_names[0] + '<br />' + PP_numbers[0] + '<br /><span class="reg_amt_onsale">' + '$' + PP_amounts[0] + '</span><span class="sale_amt">&nbsp;$' + sale_amounts[0] + '</span><br /><span class="sale_msg">' + sale_msgs[0] + '</span>';			
		}
		card_info.appendChild(card_info_p);
	}
	//start slideshow
	if (arg === 0) {
	} else if ((arg === 1) || (arg === 2)) {
		safeStart(1);
	}
	return ssRunning, ss_p_id;
}


function safeStart(n) {
		current = 0;
		current_fr = 0;
		nIndex = 1;
		if (fadeComplete === true) {
			if (ssRunning) { clearTimeout(ssRunning); }
			if (isitIE6 === true) {
				window.setTimeout("checkThmsLoaded()", 10000);
			}
			return current, current_fr, nIndex;
		}
		window.setTimeout("safeStart("+n+")", 25);
}


function checkThmsLoaded() {
	for (var i = 0; i < ssn; i++) {
		if (thms[i].complete == false) {
			window.setTimeout("thms["+i+"].src = thmsFolder + slideshow["+i+"].thumb;", ((i * 25) + 1000));
		}
	}

}

function ssPause(ss_p_id,dir_arg) { 
	if (fadeComplete === true) {
		window.clearTimeout(ssPauseTO);
		window.clearTimeout(ssRunning);
		nPause = 'paused';
		dir = dir_arg;
		getImgsToFade(ss_p_id, current, current_fr, nPause, nIndex);
		return nPause, ssRunning, dir;
	}
	else { // try again later
		ssPauseTO = window.setTimeout("ssPause('"+ss_p_id+"',"+dir_arg+")", 25);
		return ssPauseTO;
	}
}





//auto scroll the thumbs into view if necessary...
function scrollThms(fadingIn) {
	if (!thms_div_scr) { return; }
	if (scrolling === true) { return; }
	var thmScrSpeed = 4, intThmsPerPg, thmLmin, thmLmax, thmPos, thmScrAmt, scrL = false, thmAutoScr, i;
	intThmsPerPg = thmsPerPg;
	if (!intThmsPerPg) { intThmsPerPg = thmsPerPg; }
	if (window.attachEvent && !window.opera) {
		thmLmin = thms[1].offsetParent.offsetLeft;
		thmLmax = thms[intThmsPerPg - 2].offsetParent.offsetLeft;
		thmPos = thms[fadingIn].offsetParent.offsetLeft + parseInt(thms_div_scr.style.left, 10);
	} else {
		thmLmin = thms[1].offsetLeft;
		thmLmax = thms[intThmsPerPg - 2].offsetLeft;
		thmPos = thms[fadingIn].offsetLeft + thms_div_scr.offsetLeft;
	}
	if (thmPos < thmLmin) {
		thmScrAmt = thmLmin - thmPos;
		scrL = true
	} else if (thmPos > thmLmax) {
		thmScrAmt = thmPos - thmLmax;
	} else { 
		thmScrAmt = 0;
		scrolling = false;
		return;
	}
	if (thmScrAmt > 500) {
		thmScrSpeed *= 5;
	}
	if (thmScrAmt === 0) { return; }
	
	for (i = 0; i < Math.ceil(thmScrAmt/thmScrSpeed); i++) {
		scrolling = true;
		if (scrL) {
			thmAutoScr = window.setTimeout("fleXcrollTo('thms_div','-"+thmScrSpeed+"px',false,true);", i*20);
		} else {
			thmAutoScr = window.setTimeout("fleXcrollTo('thms_div','"+thmScrSpeed+"px',false,true);", i*20);
		}
	}
	window.clearTimeout(thmAutoScr);
	scrolling = false;
}


function getImgsToFade(ss_p_id, current, current_fr, nPause, nIndex) {
	var fadingOut = current, fadingOut_fr = current_fr, fadingIn, fadingIn_fr;
	fadingIn_fr = ((current_fr === 0) ? 1 : 0);
	if (nIndex > -1) {  // direct call, we know what ifrs to fade...
		fadingIn = nIndex;
		parampass = 'zoomifyImagePath=' + slideshow.sPicsFolder + slideshow[fadingIn].zoom_f + '/&zoomifyNavWindow=0';
		ifrs[fadingIn_fr].src = '../../shw_zoom/ifr.html';
		titles[fadingIn_fr].innerHTML = slideshow[fadingIn].title;
		if(d.paypal_fm) {
			d.paypal_fm.item_name.value = PP_names[fadingIn] + ' - ' + slideshow[fadingIn].title;
			d.paypal_fm.item_number.value = PP_numbers[fadingIn];
			if(slideshow[fadingIn].onSale === false) {
				d.paypal_fm.amount.value = parseFloat(PP_amounts[fadingIn]);
			} else {
				d.paypal_fm.amount.value = parseFloat(sale_amounts[fadingIn]);
			}			
		}
		if(d.getElementById('card_info')) {
			var card_info = d.getElementById('card_info');
			var card_info_p = d.createElement('p');
			card_info.innerHTML = '';
			if(slideshow[fadingIn].onSale === false) {
				card_info_p.innerHTML = PP_names[fadingIn] + '<br />' + PP_numbers[fadingIn] + '<br />' + '$' + PP_amounts[fadingIn];
			} else {
				card_info_p.innerHTML = PP_names[fadingIn] + '<br />' + PP_numbers[fadingIn] + '<br /><span class="reg_amt_onsale">' + '$' + PP_amounts[fadingIn] + '</span><span class="sale_amt">&nbsp;$' + sale_amounts[fadingIn] + '</span><br /><span class="sale_msg">' + sale_msgs[fadingIn] + '</span>';			
			}
			card_info.appendChild(card_info_p);
		}
		if (ssRunning) {
			window.clearTimeout(ssRunning);
		}
		scrollThms(fadingIn);
		ifrs[fadingIn_fr].style.visibility = 'visible';
		ssRunning = window.setTimeout("shw_fade("+fadingOut+","+fadingIn+","+fadingIn_fr+","+fadingOut_fr+",'paused');", 750);
	}
	nIndex = fadingIn;
	
	return current, current_fr, nIndex, nPause, ssRunning;
}
	



function shw_fade(fOut,fIn,frfIn,frfOut,nPause) { 
	//frfOut =  (frfIn === 0) ? 1 : 0; 
	var fading, fadingOut = ifrs[frfOut], fadingIn = ifrs[frfIn], fadingOutThm = thms[fOut], fadingInThm = thms[fIn], fadingOutTitle = titles[frfOut], fadingInTitle = titles[frfIn];
	if (fOut == fIn) {
		return;
	}
	if (!fadingIn) {
		return;
	}
	if (isitFF2mac === false) {
		cOpacity = fadingOut.xOpacity;
		nOpacity = fadingIn.xOpacity;
		cOpacity -= 0.09; 
		nOpacity += 0.09; 
		fadingOut.xOpacity = cOpacity;
		fadingIn.xOpacity = nOpacity;
		set_Opacity(fadingOut);
		set_Opacity(fadingIn);
	}
	ctOpacity = fadingOutThm.xOpacity;
	ntOpacity = fadingInThm.xOpacity;
	ctOpacity -= 0.05;
	ntOpacity += 0.05;
	fadingOutThm.xOpacity = ctOpacity;
	fadingInThm.xOpacity = ntOpacity;
	set_tOpacity(fadingOutThm);
	set_tOpacity(fadingInThm);
	ciOpacity = fadingOutTitle.xOpacity;
	niOpacity = fadingInTitle.xOpacity;
	ciOpacity -= 0.05;
	niOpacity += 0.05;
	fadingOutTitle.xOpacity = ciOpacity;
	fadingInTitle.xOpacity = niOpacity;
	set_Opacity(fadingOutTitle);
	set_Opacity(fadingInTitle);
	if ((ciOpacity <= 0) || (niOpacity >= 0.99)) {
		//re-stack so you can get to zoomify
		fadingOut.style.visibility = 'hidden';
		fadingOut.style.zIndex = '9600';
		fadingIn.style.zIndex = '9800';
		ss_p.insertBefore(fadingOut, fadingIn);
		title_cntr.insertBefore(fadingOutTitle, fadingInTitle);
		fadingInTitle.className = 'title_txt';
		if (window.opera) {
			fadingInTitle.style.filter = "";
			fadingInTitle.style.MozOpacity = "";
			fadingInTitle.style.opacity = "";
		}
		fadingInThm.onmouseout = function() {
			this.xOpacity = 0.99;
			this.style.filter = "alpha(opacity=99)";
			this.style.MozOpacity = "0.99";
			this.style.opacity = "0.99";

		};
		fadingOutThm.onmouseout = function() {
			this.xOpacity = 0.70;
			this.style.filter = "alpha(opacity=70)";
			this.style.MozOpacity = "0.70";
			this.style.opacity = "0.70";
		};
		//------- reset --------
		fOut = fIn;
		frfOut = frfIn;
		lastRun = new Date();
		fadeComplete = true;
//		if (nPause == 'run') {
//			getImgsToFade(ss_p_id,fOut,frfOut,nPause);
////			resumeImg = fOut;
//		}
		current = fOut;
		current_fr = frfOut;
		current_title = frfOut;
	} else {
		fadeComplete = false;
		if (nPause == 'paused') { 
			fading = window.setTimeout("shw_fade("+fOut+","+fIn+","+frfIn+","+frfOut+",'paused')", 25);
		} else {
			fading = window.setTimeout("shw_fade("+fOut+","+fIn+","+frfIn+","+frfOut+",'run')", 25);
		}
	}


	function set_Opacity(obj) {
		if (obj == fadingIn && isitFF2mac === true) {
			obj.xOpacity = 1;
		}
		if (obj.xOpacity > 0.99) {
			obj.xOpacity = 0.99;
		}
		if (obj.xOpacity < 0) {
			obj.xOpacity = 0;
		}
		obj.style.opacity = (obj.xOpacity).toFixed(2);
		obj.style.MozOpacity = (obj.xOpacity).toFixed(2);
		obj.style.filter = "alpha(opacity=" + (obj.xOpacity*100) + ")";

	}
	function set_tOpacity(obj) {
		if (obj.xOpacity > 0.99) {
			obj.xOpacity = 0.99;
		}
		if (obj.xOpacity < 0.70) {
			obj.xOpacity = 0.70;
		}
		obj.style.opacity = (obj.xOpacity).toFixed(2);
		obj.style.MozOpacity = (obj.xOpacity).toFixed(2);
		obj.style.filter = "alpha(opacity=" + (obj.xOpacity*100) + ")";
	}
}





//------------------- scrolling function for thumbs div ---------------------
function fleXcrollTo(id,x,y,relative) {
	var scrollDiv = document.getElementById(id);
	if (scrollDiv === null) {
		return;
	}
	if (scrollDiv.contentScroll) { 
		scrollDiv.contentScroll(x,y,relative);
	}
}
// for the thumbs arrows onclick
function contScroll(n) {
	if (scrolling === true) { return; }
	if (ssRunning) { window.clearTimeout(ssRunning); }
	if (scr) { window.clearInterval(scr); }
	scr = window.setInterval("fleXcrollTo('thms_div','" + n*4 + "px',false,true)",20);
}
function stopScroll() {
	window.clearInterval(scr);
	scrolling = false;
}

//-----------------------------------------------------------------------------
