/* shwFade.js (c)2008 SugarHill Works LLC - http://www.sugarhillworks.com */
/* v3-20081115_NT.1  [no thumbs, nav arrows, no nav nums, title, single init, fsmenu, no auto-run] */

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

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

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

var d = document;
var imgs = [];
//var paramStrings = [];
//var parampass;
var current_fr = 0;
var current = 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 = [];
//var thms_div_scr;
//var thms_ul;
//var thms_lis = [];
//var thms_as = [];
var title_cntr;
var titles = [];
//var PP_names = [];
//var PP_numbers = [];
//var PP_amounts = []
//var rsMsg = '';
var dir;
//var resumeToggle = '';
//var scr;
//var thms_divW;
//var thms_ul_newW;
//var clrbr;
//var scrolling;
var oSize_bg, oSize_cntr;

//function switch_slideshow(arg) {
//	slideshow = [];
//	imgs = [];
//	current = 0;
//	current_fr = 0;
//	current_title = 0;
//	nIndex = 1;
//	titles[0].innerHTML = '';
//	titles[1].innerHTML = '';
//	var ss_menu_items = d.getElementById('wk_index').getElementsByTagName('a');
//	for (i = 0; i < ss_menu_items.length; i++) {
//		if (ss_menu_items[i].id.match(arg)) {
//			ss_menu_items[i].className = 'active';
//		} else {
//			ss_menu_items[i].className = 'inactive';
//		}
//	}
//	load_slideshow(arg);
//	ss_init('ss_p', 1);
//}
function ss_init(ss_p_id, arg) {
	var i; 
	if (!d.getElementById || !d.createElement) {
		return;
	}
	d.getElementById('glass_cntr').onclick = function () { ssPause(1); };
	ss_p = d.getElementById(ss_p_id);
	thms_div = d.getElementById('thms_div');
	thms_div.innerHTML = '';
	for (var i = 0; i < ssn; i++) {
		var thm_div = d.createElement('div');
		var thm_a = d.createElement('a');
		thm_a.id = i + '_thm';
		thm_a.onclick = function () {
			getImgsToFade(current, current_fr, nPause, parseInt(this.id));
		}
		var thm = new Image();
		thms_folder = slideshow.sPicsFolder.replace('images', 'thumbs');
		thm.src = thms_folder + slideshow[i].image;
		thm_a.appendChild(thm);
		thm_div.appendChild(thm_a);
		thms_div.appendChild(thm_div);
	}
	
	title_cntr = d.getElementById('title_cntr');
	full_scr_cntr = d.getElementById('full_scr_cntr');
//	oSize_bg = d.getElementById('oSize_bg');
//	oSize_cntr = d.getElementById('oSize_cntr');
	if (arg === 1) { //this is the auto restart run
		ss_p.innerHTML = '';
	}
	// setup image styles
	for (var i = 0; i < ssn; i++) {
		imgs[i] = new Image();
//		imgs[ss_p_id][i].id = 'ss_img_' + i;
		imgs[i].src = slideshow.sPicsFolder + slideshow[i].image;
//		imgs[i].onclick = function () { ssPause(1); };
//		alert(imgs[i].height);
	}

	for (i = 0; i < ssn; i++) {
		var img = imgs[i];
		if (i === 0) {
			img.xOpacity = 0.99;
			img.style.filter = "alpha(opacity=99)";
			img.style.MozOpacity = "0.99";
			img.style.opacity = "0.99";
		}
		else {
			img.xOpacity = 0;
			img.style.filter = "alpha(opacity=0)";
			img.style.MozOpacity = "0";
			img.style.opacity = "0";
		}
//		alert(img.height);
	}
	
	titles[0] = d.createElement('div');
	titles[0].className = 'title_txt';
	titles[0].xOpacity = 0.99;
	titles[0].style.filter = "alpha(opacity=0)";
	titles[0].style.MozOpacity = ".99";
	titles[0].style.opacity = ".99";
	titles[1] = d.createElement('div');
	titles[1].className = 'title_txt';
	titles[1].xOpacity = 0;
	titles[1].style.filter = "alpha(opacity=0)";
	titles[1].style.MozOpacity = "0";
	titles[1].style.opacity = "0";
//alert(slideshow[0].title);
	// append the visible obj[0] last so it's stacked on top in the html
//	parampass = paramStrings[0];
//	ss_p.appendChild(imgs[1]);
	imgs[0].style.visibility = 'hidden';
	ss_p.appendChild(imgs[0]);
	titles[0].innerHTML = slideshow[0].title;
	title_cntr.appendChild(titles[1]);
	title_cntr.appendChild(titles[0]);
	//start slideshow
	if (arg === 0) {
	} else if ((arg === 1) || (arg === 2)) {
		safeStart(1);
	}
	return ssRunning;
}


function safeStart(n) {
	if (imgs[0].height < 50) {
		window.setTimeout("safeStart("+n+")", 50);
		return;
	} else {
		current = 0;
		current_fr = 0;
		nIndex = 1;
		imgs[0].style.top = 0 + 'px'; // parseInt((ss_p.offsetHeight - imgs[0].height) / 2) + 'px';
		imgs[0].style.left = 0 + 'px'; // parseInt((ss_p.offsetWidth - imgs[0].width) / 2) + 'px';
		imgs[0].style.visibility = 'visible';
		if (fadeComplete === true) {
			if (ssRunning) { clearTimeout(ssRunning); }
			return current, current_fr, nIndex;
		}
	}	
}



function ssPause(dir_arg) { 
	if (fadeComplete === true) {
		window.clearTimeout(ssPauseTO);
		window.clearTimeout(ssRunning);
		nPause = 'paused';
		dir = dir_arg;
		getImgsToFade(current, current_fr, nPause, -1);
		return nPause, ssRunning, dir;
	}
	else { // try again later
		ssPauseTO = window.setTimeout("ssPause("+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(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 imgs to fade...
		fadingIn = nIndex;
//		parampass = 'zoomifyImagePath=' + slideshow.sPicsFolder + slideshow[fadingIn].zoom_f + '/&zoomifyNavWindow=0';
		imgs[fadingIn].style.top = 0 + 'px'; // parseInt((ss_p.offsetHeight - imgs[fadingIn].height) / 2) + 'px';
		ss_p.appendChild(imgs[fadingIn]);
		titles[fadingIn_fr].innerHTML = slideshow[fadingIn].title;
		if (ssRunning) {
			window.clearTimeout(ssRunning);
		}
		ssRunning = window.setTimeout("shw_fade("+fadingOut+","+fadingIn+","+fadingIn_fr+","+fadingOut_fr+",'paused');", 750);
	}// else {
		if (nPause == 'paused') { // make clicked thumb's image the next image to fade in
			if (ssRunning) {
				window.clearTimeout(ssRunning);
			}
			if (current+dir < 0) {
				fadingIn = ssn - 1;
				imgs[fadingIn].style.top = 0 + 'px'; // parseInt((ss_p.offsetHeight - imgs[fadingIn].height) / 2) + 'px';
				imgs[fadingIn].style.left = 0 + 'px'; // parseInt((ss_p.offsetWidth - imgs[fadingIn].width) / 2) + 'px';
				ss_p.appendChild(imgs[fadingIn]);
				titles[fadingIn_fr].innerHTML = slideshow[fadingIn].title;
			} else {
				fadingIn = slideshow[current + dir]?current+dir:0;
				imgs[fadingIn].style.top = 0 + 'px'; // parseInt((ss_p.offsetHeight - imgs[fadingIn].height) / 2) + 'px';
				imgs[fadingIn].style.left = 0 + 'px'; // parseInt((ss_p.offsetWidth - imgs[fadingIn].width) / 2) + 'px';
				ss_p.appendChild(imgs[fadingIn]);
				titles[fadingIn_fr].innerHTML = slideshow[fadingIn].title;
			}
			ssRunning = window.setTimeout("shw_fade("+fadingOut+","+fadingIn+","+fadingIn_fr+","+fadingOut_fr+",'paused');", 750);
		}// else if (nPause == 'resume') {
//			fadingIn = slideshow[current+1]?current+1:0;
//			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;
//			nPause = resumeToggle;
//			if (resumeToggle == 'paused') {
//				if (ssRunning) {
//					window.clearTimeout(ssRunning);
//				}
//			} else {
//				scrollThms(fadingIn);
//				ssRunning = window.setTimeout("shw_fade("+fadingOut+","+fadingIn+","+fadingIn_fr+","+fadingOut_fr+",'"+resumeToggle+"');", 2000);
//			}
//		} else if (nPause == 'run') { 
//			fadingIn = slideshow[current+1]?current+1:0;
//			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;
//			window.setTimeout("scrollThms("+fadingIn+");", 2225);
//			ssRunning = window.setTimeout("shw_fade("+fadingOut+","+fadingIn+","+fadingIn_fr+","+fadingOut_fr+",'run');", 3000);
//		}
//	}
	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 = imgs[fOut], fadingIn = imgs[fIn], fadingOutTitle = titles[frfOut], fadingInTitle = titles[frfIn];
	if (fOut == fIn) {
		if (thms_div.style.display !== 'none' ) {
			thms_div.style.display = 'none';
		}
		return;
	}
	if (!fadingIn) {
		return;
	}
//	if (isitFF2 === false) {
		cOpacity = fadingOut.xOpacity;
		nOpacity = fadingIn.xOpacity;
		cOpacity -= 1;//0.09; 
		nOpacity += 1;//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 -= 1;//0.09;
	niOpacity += 1;//0.09;
	fadingOutTitle.xOpacity = ciOpacity;
	fadingInTitle.xOpacity = niOpacity;
	set_Opacity(fadingOutTitle);
	set_Opacity(fadingInTitle);
	if (((cOpacity <= 0) || (nOpacity >= 0.99)) && ((ciOpacity <= 0) || (niOpacity >= 0.99))) {
		if (thms_div.style.display !== 'none' ) {
			thms_div.style.display = 'none';
		}
		//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);
		ss_p.removeChild(fadingOut);
		fadingOutTitle.innerHMTL = '';
//			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(fOut, 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 && isitFF2 === 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;
//		obj.style.MozOpacity = obj.xOpacity;
//		obj.style.filter = "alpha(opacity=" + (obj.xOpacity*100) + ")";
//	}
}





//---------------- oversize images flyout layout ------------------------------
//var oSizeImg;
//var ztUh = 800;
//var ztDw = 0;
//var ztDh = 0;
//var ztw = ztDw, zth = ztDh;
//var ztUw = 1000;
//var ztwincr = 50;
//var zthincr = 45;
//var tipIsUp = false;
//var tipIsDown = false;
//var slidingUp = false;
//var slidingDown = false;
//var full_scr_cntr;
//var fs_content_wrapper;
//function show_oSize() {
//	if(!full_scr_cntr) { return };
//	if((slidingUp == true) || (slidingDown == true)) { 
//		setTimeout('show_oSize()', 100);
//		return;
//	}
//	if((tipIsUp == true)) {//hide
//		full_scr_cntr.style.visibility = 'hidden';
//		oSize_cntr.innerHTML = '';
//		contract_oSize();
//	} else {
////		ztUh = full_scr_cntr.offsetHeight;
//		oSizeImg = new Image();
//		var img_p = d.createElement('p');
//		img_p.style.width = '100px';
//		oSizeImg.src = slideshow.oSizeFolder + slideshow[current].image;
//		oSizeImg.onclick = function() { show_oSize(); }
//		img_p.appendChild(oSizeImg);
//		oSize_cntr.appendChild(img_p);
//		oSizeImg.style.visibility = 'hidden';
//		oSize_cntr.style.visiblity = 'hidden';
//		oSize_bg.style.visibility = 'visible';
//		expand_oSize();
//	}
//}
//function expand_oSize() {
//	if(tipIsUp == true) return;
//	ztw += ztwincr; if(ztw>ztUw)ztw=ztUw;
//	zth += zthincr; if(zth>ztUh)zth=ztUh;
//	if((ztw==ztUw)&&(zth==ztUh)) {
//		oSize_bg.style.width = ztw + 'px';
//		oSize_bg.style.height = zth + 'px';
//		oSize_cntr.style.width = (oSize_bg.offsetWidth - (oSize_bg.offsetWidth - oSizeImg.width)) + 'px';
//		oSize_cntr.style.height = zth + 'px';
//		oSize_cntr.style.top = (-1 * oSize_cntr.offsetTop) +  ((oSize_cntr.offsetHeight - oSizeImg.height) / 2) + "px";
//		//oSize_cntr.style.left = parseInt(((oSize_bg.offsetWidth - oSizeImg.width) / 2) / 2) + 'px';
//		oSize_cntr.style.visibility = "visible";
//		oSizeImg.style.visibility = "visible";
//		full_scr_cntr.style.visibility = 'visible';
//		slidingUp = false;
//		tipIsUp = true;
//		tipIsDown = false;
//		return;
//	} else {
//		slidingUp = true;
//		tipIsUp = false;
//		oSize_bg.style.width = ztw + 'px';
//		oSize_bg.style.height = zth + 'px';
//		oSize_cntr.style.width = ztw + 'px';
//		oSize_cntr.style.height = zth + 'px';
//		setTimeout('expand_oSize()', 20);
//		return;
//	}
//}
//function contract_oSize() {
//	if(tipIsDown == true) return;
////	fleXcrollTo('oSize_cntr',false,'0px',false);//reset scroll pos for next img
//	ztw -= ztwincr + 30; if(ztw<ztDw)ztw=ztDw;
//	zth -= zthincr + 30; if(zth<ztDh)zth=ztDh;
//	if((ztw==ztDw)&&(zth==ztDh)) { 
//		oSize_bg.style.width = ztw + 'px';
//		oSize_bg.style.height = zth + 'px';
//		oSize_cntr.style.width = ztw + 'px';
//		oSize_cntr.style.height = zth + 'px';
//		oSize_cntr.style.top = "-" + ztDh + "px";
//		full_scr_cntr.style.visibility = 'hidden';
//		slidingDown = false;
//		tipIsDown = true;
//		tipIsUp = false;
//		return;
//	} else {
//		slidingDown = true;
//		tipIsDown = false;
//		oSize_bg.style.width = ztw + 'px';
//		oSize_bg.style.height = zth + 'px';
//		oSize_cntr.style.width = ztw + 'px';
//		oSize_cntr.style.height = zth + 'px';
//		setTimeout('contract_oSize()', 30);
//		return;
//	}
//}













