// clib_fade.js
// Last modification: 8/5/2009
//
// Javascript copyright 2003-2009 by Jay Krause.  All rights reserved.
//
//
// Replaces clib_imageHtml() and clib_swapImage() to provide fading on non-IE browsers.
//

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;

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 += 'border-color:green; border-style:solid; border-width:1px;';
	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);';
//	imgStyle += 'border-color:red; border-style:solid; border-width:1px;';
	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) {
//		var urlBkg = (fChangedSize ? clib_homePrefix + clib_imageLoc + 'black.jpg' : d.url);
		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 + ")"; 
	}

