/* shwFade.js (c)2008 SugarHill Works LLC - http://www.sugarhillworks.com */
/* v3-20081120_AR_S [auto-run, no user controls, no thumbs, single init] */


var ss_interval = 4000;
// -------- SETTINGS -----------

//----- init the slideshows
addEvent(window, 'load', function() { 
//	window.setTimeout(ss_init('"+ssns[0]+"')", (2000));
	ss_init('ss_p');
});
// randomize the order
//imageFilenames.sort( randOrd );

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

var imgs = [];
var ssRunning;
var fadeComplete = true;
var ss_p;
//var info_divs, info_h5s, info_ps, hasInfo;
//var ssn; -->in settings
var ssCount = 0;

function ss_init(ss_p_id) {
	if (!d.getElementById || !d.createElement) {
		return;
	}
	ss_p = d.getElementById(ss_p_id);
//	if (slideshow[0].info) { hasInfo = true; } else { hasInfo = false; }

	for (var i = 0; i < ssn; i++) {
		imgs[i] = d.createElement('img');
//		imgs[i].id = 'ss_img_' + i;
		imgs[i].src = slideshow.sPicsFolder + slideshow[i].image;
	}
	
//	if (hasInfo === true) {
//		for (var i = 0; i < ssn; i++) {
//			if(d.getElementById(ss_p_id + '_default_info')) { ss_p.removeChild(d.getElementById(ss_p_id + '_default_info')); }
//			info_divs[i] = d.createElement('div');
//			info_divs[i].className = ss_p_id + '_info';
//			info_h5s[i] = d.createElement('h5');
//			if (slideshow[i].caption !== '') {
//				info_h5s[i].innerHTML = slideshow[i].caption;
//			} else { info_h5s[i].innerHTML = '&nbsp;' }
//			info_ps[i] = d.createElement('p');
//			info_ps[i].innerHTML = slideshow[i].info;
//			info_divs[i].appendChild(info_h5s[i]);
//			info_divs[i].appendChild(info_ps[i]);
////			info_p.id = 'ss_info_p_' + i;
//		}
//	} 

	// setup image styles
	for (var i = 0; i < imgs.length; i++) {
		var img = imgs[i];
//		var info_div = info_divs[i];
		if (i === 0) {
			img.xOpacity = 0.99;
			img.style.filter = "alpha(opacity=99)";
			img.style.MozOpacity = "0.99";
			img.style.opacity = "0.99";
			posImgs(i, 'init')
			ss_p.appendChild(img);
//			if (hasInfo) {
//				info_div.xOpacity = 0.99;
//				info_div.style.filter = "alpha(opacity=99)";
//				info_div.style.MozOpacity = "0.99";
//				info_div.style.opacity = "0.99";
//				ss_p.appendChild(info_div);
//			}
		}
		else {
			img.xOpacity = 0;
			img.style.filter = "alpha(opacity=0)";
			img.style.MozOpacity = "0";
			img.style.opacity = "0";
			posImgs(i, 'init')
//			if (hasInfo) {
//				info_div.xOpacity = 0;
//				info_div.style.filter = "alpha(opacity=0)";
//				info_div.style.MozOpacity = "0";
//				info_div.style.opacity = "0";
//			}
		}
	}
	
	//start slideshow
	safeStart(1);
	return ssRunning;
}
function posImgs(i,from) {//from can be 'init', 'gitf', 'resz'
	var img = imgs[i];
	var imgsW = img.width, imgsH = img.height, ss_pW = ss_p.offsetWidth, ss_pH = ss_p.offsetHeight, rszRate = 1, wt, lt;//, thm = thms[i];
	ss_p.style.visibility = "hidden";

	function fixIE6() {
		if (from == 'init' && i === 0) {
			ss_p.appendChild(imgs[0]);
		}
		if (from == 'gitf') {
			img.xOpacity = 0;
			img.style.filter = "alpha(opacity=0)";
			img.style.MozOpacity = "0";
			img.style.opacity = "0";
		}
	}
	if (typeof document.body.style.maxHeight == "undefined") {
		fixIE6();
	}
	
	if ((img.width < 50) || (img.height < 50)) {
		window.setTimeout("posImgs("+i+",'"+from+"');", 50);
	} else {
//		if (from == 'init' || from == 'resz') {
//			//resize the ss p
//			if (ss_p.offsetHeight < 430) {
//				ss_p.style.height = '430px';
//			}
//			resized = false;
//		}
//		if (from == 'resz') {
//			if (scrolling === true) { };
//			var thms_div_oW = thms_div.offsetWidth, thms_lis_newW = Math.round((thms_div.offsetWidth/9)), tri;
//			thms_ul_newW = thms_lis_newW * thms.length;
//			thms_ul.style.width = thms_ul_newW + 'px';
//			for(tri = 0; tri < thms.length; tri++) {
//				thms_lis[tri].style.width = thms_lis_newW +'px';
//			}
//		}
//		ss_p.style.width = (ceibw.offsetWidth*.88-130).toFixed(0) + 'px';
//		rszRate = (ss_pH / imgsH).toFixed(2);
//		if ((imgsW * rszRate) > (ss_pW)) {
//			rszRate = (ss_pW / imgsW).toFixed(2);
//		}
		if (from == 'init' && i === 0) {
			ss_p.appendChild(imgs[0]);
		}
//		wt = Math.round(imgsW * rszRate);
//		img.style.width = wt + 'px';
//		img.style.height = Math.round(imgsH * rszRate) + 'px';
		img.style.top = Math.round((ss_p.offsetHeight - img.height) / (2)) + 'px';
		lt = Math.round((ss_p.offsetWidth - img.width) / (2));
		img.style.left = lt + 'px';
	}
	ss_p.style.visibility = "visible";
}

function safeStart(n) {
	if (n >= imgs.length) { //this code block only happens when all the images have loaded.
		if (fadeComplete === true) {
			if (ssRunning) { clearTimeout(ssRunning); }
			getImgsToFade(0);
			return;
		} else {
			window.setTimeout("safeStart("+n+")", 25);
			return n;
		}
	}
	if (imgs[n].complete === false) {
		window.setTimeout("safeStart("+n+")", 250);
	} else {
		n++;
		safeStart(n);
	}
}

function getImgsToFade(current) {
	var fadingOut = current, fadingIn;//,fadingIn_info;
	fadingIn = imgs[current+1]?current+1:0;
//	fadingIn_info = info_divs[current+1]?current+1:0;
	ss_p.appendChild(imgs[fadingIn]);
//	if(hasInfo === true) {
//		ss_p.appendChild(info_divs[fadingIn]);
//	}
	ssRunning = window.setTimeout("shw_fade("+fadingOut+","+fadingIn+");", ss_interval);
	// to 'toggle the slideshow panels, reset the interval...
	return ssRunning;
}
	



function shw_fade(fOut,fIn) { 
	var fading, fadingOut = imgs[fOut], fadingIn = imgs[fIn];//, fadingOutInfo = info_divs[fOut], fadingInInfo = info_divs[fIn];
	if (fOut == fIn) {
		return;
	}
	if (!fadingIn) {
		return;
	}
	cOpacity = fadingOut.xOpacity;
	nOpacity = fadingIn.xOpacity;
	cOpacity -= 0.04; 
	cOpacity.toFixed(2);
	nOpacity += 0.04; 
	nOpacity.toFixed(2);
	fadingOut.xOpacity = cOpacity;
	fadingIn.xOpacity = nOpacity;
	set_Opacity(fadingOut);
	set_Opacity(fadingIn);
//	if(hasInfo === true) {
//		fadingOutInfo.xOpacity = cOpacity;
//		fadingInInfo.xOpacity = nOpacity;
//		set_Opacity(fadingOutInfo);
//		set_Opacity(fadingInInfo);
//	}
	if ((cOpacity <= 0) || (nOpacity >= 0.99)) {
		fadingOut.xOpacity = -1;
		fadingIn.xOpacity = 0.99;
		set_Opacity(fadingOut);
		set_Opacity(fadingIn);
		ss_p.removeChild(fadingOut);
//		if(hasInfo === true) {
//			set_Opacity(fadingOutInfo);
//			set_Opacity(fadingInInfo);
//			ss_p.removeChild(fadingOutInfo);
//		}
		//------- reset --------
		fOut = fIn;
		fadeComplete = true;
		if ((ssCount == 1) && (d.getElementById(ss_p.id + '_default_img'))) { ss_p.removeChild(d.getElementById(ss_p.id + '_default_img')); }
		ssCount += 1;
		getImgsToFade(fOut);
		current = fOut;
	} else {
		fadeComplete = false;
		fading = window.setTimeout("shw_fade("+fOut+","+fIn+")", 25);
	}
	
	function set_Opacity(obj) {
		if (obj.xOpacity > 0.99) {
			obj.xOpacity = 0.99;
		}
		if (obj.xOpacity < 0) {
			obj.xOpacity = 0;
		}
		obj.style.opacity = obj.xOpacity;
		obj.style.MozOpacity = obj.xOpacity;
		obj.style.filter = "alpha(opacity=" + (obj.xOpacity*100) + ")";
	}
}


