// clib_basicslideshow.js
// Last modification: 1/31/2011
//
// Javascript copyright 2003-2011 by Jay Krause.  All rights reserved.
//
//
// Portions of clib.js & clib_fade.js to support clib_slideShow()
//

var clib_homePrefix = '';
var clib_fLocal = false;

function clib_isFunction(a) { return (typeof a == 'function'); }
function clib_isObject(a) { return (((a && (typeof a == 'object'))) || clib_isFunction(a)); }
function clib_isString(a) { return (typeof a == 'string'); }
function clib_isNumber(a) { return (typeof a == 'number'); }
function clib_isArray(a) { return (clib_isObject(a) && (a.constructor == Array)); }

function clib_imageType(url,load,error,abort,w,h) {
	this.members = 'url,image,status,w,h';
	this.url = url;
	this.w = w;
	this.h = h;
	this.image = new Image();
	this.status = null;
	if (load != null) {
		eval("this.image.loadHandler = function () { " + load + " }" );
		this.image.onload = this.image.loadHandler;
		}
	if (error != null) {
		eval("this.image.errorHandler = function () { " + error + " }");
		this.image.onerror = this.image.errorHandler;
		}
	if (abort != null) {
		eval("this.image.errorHandler = function () { " + abort + " }");
		this.image.onabort = this.image.errorHandler;
		}
	if ((abort == null) && (error != null)) this.image.onabort = this.image.errorHandler;
	}

function clib_innerHTML(div,html) {
	clib_checkHTML(html);
	var id = clib_getElementId(div);
	if (id == null) return true;
	id.innerHTML = html;
	return false;
	}

function clib_writeln(html,div,fAutoDiv) {
	clib_checkHTML(html);
	if ((fAutoDiv == null) || (div == null)) fAutoDiv = false;
	if (fAutoDiv) {
		if (clib_isElementId(div)) clib_innerHTML(div,html);
		else document.writeln('<div id=' + div + '>' + html + '</div>');
		}
	else {
		if (div == null) document.write(html);
		else clib_innerHTML(div,html);
		}
	}

function clib_checkHTML(html) {	}

function clib_isElementId(name) {
	var o = clib_getElementId(name);
	return !(o == null);
	}

function clib_getElementId(name){
	if ((name == null) || (name == '')) return null;
	var o = null;
	if (document.getElementById) {
		o = eval('document.getElementById("' + name + '")');
		if (o != null) return o;
		}
	if (document.all) {
		o = eval('document.all.' + name);
		if (o != null) return o;
		}
	if (document.images) {
		o = eval('document.images.' + name);
		if (o != null) return o;
		}
	return null;
	}

var clib_rgSlideShow = new Array();
var clib_nrgSlideShow = 0;

function clib_slideShowType(id,nrg,interval,fade,fRandom,fReverse,callback,wLast,hLast) {
	this.members = 'id,interval,fade,fRandom,fReverse,fLoading,nShown,idTimeout,fPause,callback,wLast,hLast,imgBlank,fShowingBlank,pResize,status,nrg,irg,rgImg';
	this.id = id;
	this.interval = interval;
	this.fade = (fade == null ? 0 : fade);
	this.fRandom = fRandom;
	this.fReverse = fReverse;
	this.fLoading = false;
	this.nShown = 0;
	this.idTimeout = null;
	this.fPause = false;
	this.callback = callback;
	this.wLast = wLast;
	this.hLast = hLast;
	this.imgBlank = null;
	this.fShowingBlank = false;
	this.pResize = 1.0;
	this.status = null;
	this.nrg = nrg;
	this.irg = 0;
	this.rgImg = new Array();
	}

function clib_slideShowShuffle(is,fSkipFirst) {
	function swap(i,j) {
		var tempi = rg[i];
		var tempj = rg[j];
		rg[i] = tempj;
		rg[j] = tempi;
		}
	if (fSkipFirst == null) fSkipFirst = false;
	var rg = clib_rgSlideShow[is].rgImg;
	var i = rg.length;
	if ( i == 0 ) return;
	if (fSkipFirst) {
		swap(rg.length-1,0);
		i--;
		}
	while ( --i ) {
		var j = Math.floor( Math.random() * ( i + 1 ) );
		swap(i,j);
		}
	if (fSkipFirst) swap(rg.length-1,0);
	}

function clib_slideShow(id,rgUrl,nrgUrl,interval,fade,fRandom,fReverse,callback,wInit,hInit,imgBlank) {
	if (fRandom == null) fRandom = false;
	if (fReverse == null) fReverse = false;
	if (typeof callback == 'undefined') callback = null;
	var hn = clib_nrgSlideShow++;
	clib_rgSlideShow[hn] = new clib_slideShowType(id,nrgUrl,interval,fade,fRandom,fReverse,callback,wInit,hInit,imgBlank);
	var s = clib_rgSlideShow[hn];
	if ((wInit == null) && clib_isObject(rgUrl[0])) wInit = rgUrl[0].w;
	if ((hInit == null) && clib_isObject(rgUrl[0])) hInit = rgUrl[0].h;
	s.wLast = wInit;
	s.hLast = hInit;
	if (clib_isString(imgBlank)) {
		s.imgBlank = new Image();
		s.imgBlank.src = imgBlank;
		}
	for (var i = 0; i < nrgUrl; i++) {
		var url = null;
		var w = null;
		var h = null;
		if (clib_isObject(rgUrl[i])) {
			url = rgUrl[i].url;
			w = rgUrl[i].w;
			h = rgUrl[i].h;
			}
		else url = rgUrl[i];
		var x = 'clib_rgSlideShow[' + hn + '].rgImg[' + i + '].status = ';
		s.rgImg[i] = new clib_imageType(url,x + 'true', x + 'false', x + 'false',w,h);
		s.rgImg[i].image.src = url;
		}
	if (fRandom) clib_slideShowShuffle(hn,true);
	s.idTimeout = setTimeout('clib_doSlideShow(' + hn + ')',interval);
	return hn;
	}

function clib_doSlideShow(hn,irg) {
	if ((hn == null) || (hn < 0) || (hn >= clib_rgSlideShow.length)) return false;
	var s = clib_rgSlideShow[hn];
	var log = '';
	s.fLoading = true;
	if (!clib_isNumber(irg)) irg = s.irg + (s.fReverse ? -1 : 1);	
	if (s.idTimeout != null) { clearTimeout(s.idTimeout); s.idTimeout = null; }
	for (var j = 0; j < s.nrg; j++) {			// go through array looking for a loaded picture, skipping errors
		irg = irg + j;
		if (irg >= s.nrg) irg = irg - s.nrg;
		else if (irg < 0) irg = irg + s.nrg;
		log += '\nchecking irg=' + irg;
		var status = s.rgImg[irg].status;
		if (status == null) {
			if (s.rgImg[irg].image.complete) {
				status, s.rgImg[irg].status = true;
				}
			else if (!s.fRandom) {								// if not fRandom do nothing and wait for it to load
				s.idTimeout = setTimeout('clib_doSlideShow(' + hn + ')',100);
				if (s.callback != null) eval(s.callback);
				s.status = 'waiting for ' + s.rgImg[irg].url + ' [irg=' + irg + ']' + log;
				return;
				}
			}
		if (status == true) {					// image is ready
			var time = s.fade;
			var msNextPic = s.interval;
			if (s.fShowingBlank) {				// last image was blank
				irg = s.irg;
				time = time/2;
				s.wLast = s.rgImg[irg].w;
				s.hLast = s.rgImg[irg].h;
				s.fShowingBlank = false;
				}
			var url = s.rgImg[irg].url;
			var w = s.rgImg[irg].w;
			var h = s.rgImg[irg].h;
			if ((time > 0) && ((s.rgImg[irg].w != s.wLast) || (s.rgImg[irg].h != s.hLast))) {	// next image is a different size
				time = time/2;
				if (s.imgBlank != null) url = s.imgBlank.src;
				msNextPic = Math.floor(time * 1000 + 50);
				w = s.wLast;
				h = s.hLast;
				s.fShowingBlank = true;
				}
			clib_swapImage(s.id,url,time,w == null ? null : Math.floor(w * s.pResize),h == null ? null : Math.floor(h * s.pResize));
			s.wLast = w;
			s.hLast = h;		
			if (!s.fShowingBlank) s.nShown++;
			s.fLoading = false;
			s.irg = irg;
			if (s.fRandom) if (s.nShown % s.nrg == 0) clib_slideShowShuffle(hn);
			if (!s.fPause) {
				s.idTimeout = setTimeout('clib_doSlideShow(' + hn + ')',msNextPic);
				}
			if (s.callback != null) eval(s.callback);
			s.status = (s.fPause ? 'paused ' : '') + 'showing ' + s.rgImg[irg].url + ' [irg=' + irg + ']' + log;
			return;
			}
		// else if (status == false) {}			// try next pic because this had error or was not ready and fRandom is set
		}
	if (!s.fPause) {
		s.idTimeout = setTimeout('clib_doSlideShow(' + hn + ')',s.interval);
		}
	if (s.callback != null) eval(s.callback);
	s.status = 'waiting for an image' + ' [irg=' + irg + ']' + log;
	return;
	}

function clib_slideShowControl(hn,fPause,interval,fade,fRandom,fReverse,pResize) {
	if ((hn == null) || (hn < 0) || (hn >= clib_rgSlideShow.length)) return false;
	var s = clib_rgSlideShow[hn];
	if (interval != null) s.interval = interval;
	if (fade != null) s.fade = fade;
	if (fRandom != null) s.fRandom = fRandom;
	if (fReverse != null) s.fReverse = fReverse;
	if (pResize != null) s.pResize = pResize;
	if ((fPause != null) && (fPause != s.fPause)) {
		s.fPause = fPause;
		if (fPause) if (s.idTimeout!= null) { clearTimeout(s.idTimeout); s.idTimeout = null; return;}
		clib_doSlideShow(hn);
		}
	}

function clib_debugFunction(t,xtraMsg,maxDepth) {}

// clibdb_register('clib_rgBlendDiv');
var clib_rgBlendDiv = new Array();
var clib_nrgBlendDiv = 0;
var clibx_blendDivSuf = '_clibBlendDiv';
var clibx_fUseIeFade = true;
var clibx_minMsTic = 25;
var clibx_wOffset = 0;
var clibx_xOffset = 0;

function clib_imageHtml(idImg,url,w,h,border,alt,imgClass,imgStyle,imgExtras,divClass,divStyle,aUrl,aTarget,aClass,aStyle) {
	var html = '';
	clib_rgBlendDiv[clib_nrgBlendDiv++] = new clibx_blendImgType(idImg,url);
	var d = clib_rgBlendDiv[clib_nrgBlendDiv-1];
	html += '<div id="' + d.idDiv + '"' + (divClass == null ? '' : ' class="' + divClass + '"');
	html += ' style="background-image:url(' + clib_homePrefix + 'blank.gif' + '); background-repeat:no-repeat; width:' + w + 'px; height:' +h + 'px;';
	html += (divStyle == null ? '' : divStyle) + '">';
	if (aUrl != null) {
		html += '<a href="' + aUrl + '"';
		if (aTarget != null) html += ' target=' + aTarget;
		if (aClass != null) html += ' class=' + aClass;
		if (aStyle != null) html += ' style="' + aStyle + '"';
		html += '>';
		}
	html += '<img id="' + idImg + '" src="' + url + '"';
	if (imgStyle == null) imgStyle = '';
	imgStyle += 'filter:alpha(opacity=100); -moz-opacity:100; opacity:100; filter:blendTrans(duration=1.5);';
	if (w != null) html += ' width="' + w + '"';
	if (h != null) html += ' height="' + h + '"';
	if (border != null) html += ' border="' + border + '"';
	if (imgClass != null) html += ' class="' + imgClass + '"';
	if (imgStyle != null) html += ' style="' + imgStyle + '"';	
	if (alt != null) html += ' alt="' + alt + '"';
	if (imgExtras != null) html += ' ' + imgExtras;
	html += '>';
	if (aUrl != null) html += '</a>';
	html += '</div>';
	return html;
	}


function clibx_blendImgType(idImg,url) {
	this.members = 'idImg,idDiv,url,urlLast,fFading,idTimeout,timeoutTic,nSec,opacity,opacityTic';
	this.idImg = idImg;
	this.idDiv = idImg + clibx_blendDivSuf;
	this.url = url;
	this.urlLast = null;
	this.fFading = false;
	this.idTimeout = null;
	this.timeoutTic = null;
	this.nSec = null;
	this.opacity = null;
	this.opacityTic = null;
	}

function clib_swapImage(idImg,url,nSec,w,h) {
	function clibx_fBlendDiv() {
		for (var i = 0; i < clib_nrgBlendDiv; i++) if (clib_rgBlendDiv[i].idImg == idImg) {
			hn = i;
			d = clib_rgBlendDiv[hn];
			return true;
			}
		return false;
		}
	var fIeFade = false;
	var fMzFade = false;
	var d = null;
	var hn = null;
	var id = clib_getElementId(idImg);
	var fBlendDiv = clibx_fBlendDiv();
	var idDiv = (fBlendDiv ? clib_getElementId(d.idDiv) : null);
	if ((id == null) || (url == null)) { clib_debugFunction(clib_fLocal,'null id/url'); return true; }
	var fChangedSize = false;
	if (w != null) if (w != id.width) { id.width = w; if (fBlendDiv) idDiv.style.width = w + 'px'; fChangedSize = true; }
	if (h != null) if (h != id.height) { id.height = h; if (fBlendDiv) idDiv.style.height = h + 'px'; fChangedSize = true; }
	if (fBlendDiv && (d.fFading || (d.idTimeout != null))) {
		clearTimeout(d.idTimeout);
		d.idTimeout = null;
		d.fFading = false;
		if ((nSec == null) || (nSec == 0)) clib_imageOpacity(idImg,100);
		}
	if ((nSec != null) && (nSec > 0)) {
		if (id.filters) if (id.filters.blendTrans) fIeFade = clibx_fUseIeFade;
		fMzFade = (!fIeFade && fBlendDiv);
		}
	if (fIeFade) {
		var fFadeOK = (id.filters.blendTrans.status == 0);
		if (fFadeOK) id.filters[0].apply();
		id.src = url;
		if (fFadeOK) { id.filters.blendTrans.duration = nSec; id.filters[0].play(); }
		}
	else if (fMzFade) {
		idDiv.style.backgroundImage = 'url(' + d.url + ')';		// 1. set div.background to old image
		clib_imageOpacity(idImg,0);								// 2. set img.opacity to 0
		d.opacity = 0;
		id.src = url;											// 3. set img.src to new image
		d.timeoutTic = Math.max(clibx_minMsTic,nSec * 10);		// 4. set timers to fade image over nSec time
		var nTics = (nSec * 1000) / d.timeoutTic;
		d.opacityTic = Math.max(1,Math.floor(100/nTics));
		d.fFading = true;
		d.nSec = nSec;
		d.idTimeout = setTimeout('clibx_fadeImage(' + hn + ')',d.timeoutTic);
		}
	else id.src = url;
	if (d != null) {
		d.urlLast = d.url;
		d.url = url;
		}
	return false;
	}

function clibx_fadeImage(hn) {
	if ((typeof hn != 'number') || (hn >= clib_nrgBlendDiv) || (hn < 0)) { clib_debugFunction(clib_fLocal); return; }
	var d = clib_rgBlendDiv[hn];
	if (!d.fFading) { clib_debugFunction(clib_fLocal, 'not fading: ' + hn); return; }
	d.idTimeout = null;
	d.opacity += d.opacityTic;
	if (d.opacity >= 99) d.opacity = 100;
	clib_imageOpacity(d.idImg,d.opacity);
	if (d.opacity < 100) d.idTimeout = setTimeout('clibx_fadeImage(' + hn + ')',d.timeoutTic);
	else {
		d.fFading = false;
		var idDiv = clib_getElementId(d.idDiv);
		idDiv.style.backgroundImage = 'url(' + d.url + ')';
		}
	}

function clib_imageOpacity(idImg,opacity) {
	var id = clib_getElementId(idImg);
	if ((id == null) || (typeof opacity != 'number')) return true;
	var object = id.style;
	object.opacity = (opacity / 100); 
    object.MozOpacity = (opacity / 100); 
    object.KhtmlOpacity = (opacity / 100); 
    object.filter = "alpha(opacity=" + opacity + ")"; 
	}

