// IXF1.11 :: Image cross-fade 
// *****************************************************
// DOM scripting by brothercake -- http://www.brothercake.com/
//******************************************************

//global object
var imgFx = { 'clock' : null, 'count' : 1 }

//crossfade setup function
function crossfade() {
	//if the timer is not already going
	if(imgFx.clock == null) {
		//copy the image object 
		imgFx.obj = arguments[0];
		//copy the image src argument 
		imgFx.src = arguments[1];
		//store the supported form of opacity
		if(typeof imgFx.obj.style.opacity != 'undefined') {
			imgFx.type = 'w3c';
		} else if(typeof imgFx.obj.style.MozOpacity != 'undefined') {
			imgFx.type = 'moz';
		} else if(typeof imgFx.obj.style.KhtmlOpacity != 'undefined') {
			imgFx.type = 'khtml';
		} else if(typeof imgFx.obj.filters == 'object') {
			//weed out win/ie5.0 by testing the length of the filters collection (where filters is an object with no data)
			//then weed out mac/ie5 by testing first the existence of the alpha object (to prevent errors in win/ie5.0)
			//then the returned value type, which should be a number, but in mac/ie5 is an empty string
			imgFx.type = (imgFx.obj.filters.length > 0 && typeof imgFx.obj.filters.alpha == 'object' && typeof imgFx.obj.filters.alpha.opacity == 'number') ? 'ie' : 'none';
		} else {
			imgFx.type = 'none';
		}
		//change the image alt text if defined
		if(typeof arguments[3] != 'undefined' && arguments[3] != '') {
			imgFx.obj.alt = arguments[3];
		}
		//if any kind of opacity is supported
		if(imgFx.type != 'none') {
			//create a new image object and append it to body
			//detecting support for namespaced element creation, in case we're in the XML DOM
			imgFx.newimg = document.getElementsByTagName('body')[0].appendChild((typeof document.createElementNS != 'undefined') ? document.createElementNS('http://www.w3.org/1999/xhtml', 'img') : document.createElement('img'));
			//set positioning classname
			imgFx.newimg.className = 'idupe';
			//set src to new image src
			imgFx.newimg.src = imgFx.src
			//move it to superimpose original image
			imgFx.newimg.style.left = imgFx.getRealPosition(imgFx.obj, 'x') + 'px';
			imgFx.newimg.style.top = imgFx.getRealPosition(imgFx.obj, 'y') + 'px';
			//copy and convert fade duration argument 
			imgFx.length = parseInt(arguments[2], 10) * 1000;
			//create fade resolution argument as 20 steps per transition
			imgFx.resolution = parseInt(arguments[2], 10) * 20;
			//start the timer
			imgFx.clock = setInterval('imgFx.crossfade()', imgFx.length/imgFx.resolution);
		} else {
			//otherwise if opacity is not supported
			//just do the image swap
			imgFx.obj.src = imgFx.src;
		}
	}
};

//crossfade timer function
imgFx.crossfade = function() {
	//decrease the counter on a linear scale
	imgFx.count -= (1 / imgFx.resolution);
	//if the counter has reached the bottom
	if(imgFx.count < (1 / imgFx.resolution)) {
		//clear the timer
		clearInterval(imgFx.clock);
		imgFx.clock = null;
		//reset the counter
		imgFx.count = 1;
		//set the original image to the src of the new image
		imgFx.obj.src = imgFx.src;
	}
	//set new opacity value on both elements
	//using whatever method is supported
	switch(imgFx.type) {
		case 'ie' :
			imgFx.obj.filters.alpha.opacity = imgFx.count * 100;
			imgFx.newimg.filters.alpha.opacity = (1 - imgFx.count) * 100;
			break;
		case 'khtml' :
			imgFx.obj.style.KhtmlOpacity = imgFx.count;
			imgFx.newimg.style.KhtmlOpacity = (1 - imgFx.count);
			break;
		case 'moz' : 
			//restrict max opacity to prevent a visual popping effect in firefox
			imgFx.obj.style.MozOpacity = (imgFx.count == 1 ? 0.9999999 : imgFx.count);
			imgFx.newimg.style.MozOpacity = (1 - imgFx.count);
			break;
		default : 
			//restrict max opacity to prevent a visual popping effect in firefox
			imgFx.obj.style.opacity = (imgFx.count == 1 ? 0.9999999 : imgFx.count);
			imgFx.newimg.style.opacity = (1 - imgFx.count);
	}
	//now that we've gone through one fade iteration 
	//we can show the image that's fading in
	imgFx.newimg.style.visibility = 'visible';
	//keep new image in position with original image
	//in case text size changes mid transition or something
	imgFx.newimg.style.left = imgFx.getRealPosition(imgFx.obj, 'x') + 'px';
	imgFx.newimg.style.top = imgFx.getRealPosition(imgFx.obj, 'y') + 'px';
	//if the counter is at the top, which is just after the timer has finished
	if(imgFx.count == 1) {
		//remove the duplicate image
		imgFx.newimg.parentNode.removeChild(imgFx.newimg);
	}
};

//get real position method
imgFx.getRealPosition = function() {
	this.pos = (arguments[1] == 'x') ? arguments[0].offsetLeft : arguments[0].offsetTop;
	this.tmp = arguments[0].offsetParent;
	while(this.tmp != null) {
		this.pos += (arguments[1] == 'x') ? this.tmp.offsetLeft : this.tmp.offsetTop;
		this.tmp = this.tmp.offsetParent;
	}
	return this.pos;
};

// global variable to hold the current index of the image displayed
var ssIdx = 1;
var ssImages = new Array();
var ssAltTxt = new Array();
function crossfadeSlideShow(elemID, fadeTime, dspyTime) {
	crossfade(document.getElementById(elemID), ssImages[ssIdx], fadeTime, '');
	if (ssIdx >= ssImages.length) {
		ssIdx = 0;
	} else {
		ssIdx = ssIdx + 1;
	}
	setTimeout("crossfadesSlideShow('"+elemID+"', "+fadeTime+", "+dspyTime+")", dspyTime);
}

// global variable to hold the current index of the image displayed
var ssIdx = 1;
var ssImages = new Array();

function crossfadeStartShow() {
	crossfade(document.getElementById('txImg'), ssImages[ssIdx], 2, '');
	if (ssIdx >= ssImages.length - 1) {
		ssIdx = 0;
	} else {
		ssIdx = ssIdx + 1;
	}
	setTimeout("crossfadeStartShow()", 6000);
}

function crossfadeSlideShow() {
	setTimeout("crossfadeStartShow()", 4000);
}
