// *****************************************************
// Image Transitions Library
// 
// Copyright 2007 - Indigo Web Designs
// ******************************************************

// Master transition control object
var MTC = { 'timerID' : null, 'pctLeft' : 1 }

// ******************************************************

function invokeCallback() {
    // invoke the callback (if defined)
    if( typeof MTC.callback != 'undefined' && MTC.callback != '' ) {
        funcRef = window[MTC.callback];
        if( 'function' == typeof funcRef ) {
            funcRef();
	}
    }
}

function setOpacityType() {
    MTC.type = 'none';

    if( typeof MTC.obj.style.opacity != 'undefined' ) {
	MTC.type = 'w3c';
    }
    else if( typeof MTC.obj.style.MozOpacity != 'undefined' ) {
	MTC.type = 'moz';
    }
    else if( typeof MTC.obj.style.KhtmlOpacity != 'undefined' ) {
	MTC.type = 'khtml';
    }
    else if(   typeof MTC.obj.filters == 'object'
	    && MTC.obj.filters.length > 0
	    && typeof MTC.obj.filters.alpha == 'object'
	    && typeof MTC.obj.filters.alpha.opacity == 'number' )
    {
	MTC.type = 'ie';
    }
}

function createImageElement() {
    var imgElem = null;

    //Is dynamic element creation supported?
    crElementNS = (typeof document.createElementNS != 'undefined');
    crElement   = (typeof document.createElement   != 'undefined');

    if( crElementNS || crElement ) {
        docBody = document.getElementsByTagName('body')[0];

	imgElem = crElementNS
	    ? document.createElementNS('http://www.w3.org/1999/xhtml', 'img')
	    : document.createElement('img');
    }

    return imgElem;
}

function IWD_crosswipe() {
	//if the timer is not already going
	if( MTC.timerID == null ) {
		//copy the image object 
		MTC.obj = arguments[0];
		
		//get its dimensions
		MTC.size = { 'w' : MTC.obj.width, 'h' : MTC.obj.height };
		
		//copy the image src argument 
		MTC.src = arguments[1];
		
		//change the image alt text if defined
		if( typeof arguments[4] != 'undefined' && arguments[4] != '' ) {
			MTC.obj.alt = arguments[4];
		}

		// set the callback function name
		MTC.callback = arguments[5];

//		//Is dynamic element creation supported?
//		crElementNS = (typeof document.createElementNS != 'undefined');
//		crElement   = (typeof document.createElement   != 'undefined');
//
//
//		if( crElementNS || crElement ) {
//			docBody = document.getElementsByTagName('body')[0];
//
//			imgChild = crElementNS
//				? document.createElementNS('http://www.w3.org/1999/xhtml', 'img')
//				: document.createElement('img');

		imgChild = createImageElement();
		if( imgChild ) {
			MTC.tgtimg = docBody.appendChild(imgChild);

			//set positioning classname
			MTC.tgtimg.className = 'tgtimage';

			//set src to new image src
			MTC.tgtimg.src = MTC.src

			//move it to superimpose original image
			MTC.tgtimg.style.left = MTC.getXAnchor(MTC.obj);
			MTC.tgtimg.style.top  = MTC.getYAnchor(MTC.obj);

			//set it to be completely hidden with clip
			MTC.tgtimg.style.clip = 'rect(0, 0, 0, 0)';

			//show the image 
			MTC.tgtimg.style.visibility = 'visible';

			//copy and convert fade duration argument 
			MTC.length = parseInt(arguments[2], 10) * 1000;

			//create fade resolution argument as 20 steps per transition
			MTC.resolution = parseInt(arguments[2], 10) * 20;

			//copy slide direction argument
			MTC.dir = arguments[3];

			//start the timer
			MTC.timerID = setInterval('MTC.crosswipe()', MTC.length/MTC.resolution);
		}
		
		//otherwise if dynamic element creation is not supported
		else {
			//just do the image swap
			MTC.obj.src = MTC.src;

			invokeCallback();
		}
	}
};


//crosswipe timer function
MTC.crosswipe = function() {
	delta = (1 / MTC.resolution);
	
	MTC.pctLeft -= delta;
	
	//if the counter has reached the bottom
	if( MTC.pctLeft < delta ) {
		//clear the timer
		clearInterval(MTC.timerID);
		MTC.timerID = null;
		
		//reset the counter
		MTC.pctLeft = 1;
		
		//set the original image to the src of the new image
		MTC.obj.src = MTC.src;
	}
	
	// Clip the new image based on the pctLeft and wipe direction
	hOffset = (MTC.size.h * MTC.pctLeft);		// Height offset
	wOffset = (MTC.size.w * MTC.pctLeft);		// Width  offset

	topWipe = (/tb|tlbr|trbl/.test(MTC.dir));	// Wipe starts at top
	btmWipe = (/bt|bltr|brtl/.test(MTC.dir));	// Wipe starts at bottom
	lftWipe = (/lr|tlbr|bltr/.test(MTC.dir));	// Wipe starts at left
	rgtWipe = (/rl|trbl|brtl/.test(MTC.dir));	// Wipe starts at right
	cthWipe = (/che|cc/.test(MTC.dir));		// Wipe starts in center (horz)
	ctvWipe = (/cve|cc/.test(MTC.dir));		// Wipe starts in center (vert)
	othWipe = (/tb|bt|che/.test(MTC.dir));		// Other wipe


	clipT = ( btmWipe ? hOffset : cthWipe ? (hOffset / 2) : 0 );
	clipL = ( lftWipe ? (MTC.size.w - wOffset) : ctvWipe ? (MTC.size.w - (wOffset / 2)) : MTC.size.w );
	clipB = ( topWipe ? (MTC.size.h - hOffset) : cthWipe ? (MTC.size.h - (hOffset / 2)) : MTC.size.h );
	clipR = ( lftWipe ? 0 : othWipe ? 0 : ctvWipe ? (wOffset / 2) : wOffset );

	MTC.tgtimg.style.clip = 'rect(' + clipT + 'px,' + clipL + 'px,' + clipB + 'px,' + clipR + 'px)';
			
	//keep new image in position with original image
	//in case text size changes mid transition or something
	MTC.tgtimg.style.left = MTC.getXAnchor(MTC.obj);
	MTC.tgtimg.style.top  = MTC.getYAnchor(MTC.obj);
	
	//if the counter is at the top, which is just after the timer has finished
	if( MTC.pctLeft == 1 ) {
		//remove the duplicate image
		MTC.tgtimg.parentNode.removeChild(MTC.tgtimg);

		invokeCallback();
	}
};

//overlay setup function
function IWD_overlay() {

	//if the timer is not already going
	if( MTC.timerID == null ) {

		//copy the image object 
		MTC.obj = arguments[0];
		
		//copy the image src argument 
		MTC.src = arguments[1];
		
		//change the image alt text if defined
		if( typeof arguments[3] != 'undefined' && arguments[3] != '' ) {
			MTC.obj.alt = arguments[3];
		}

		// set the callback function name
		MTC.callback = arguments[4];

		setOpacityType();

//		//Is dynamic element creation supported?
//		crElementNS = (typeof document.createElementNS != 'undefined');
//		crElement   = (typeof document.createElement   != 'undefined');
//		
//		//if any kind of opacity is supported
//		if( MTC.type != 'none' && (crElementNS || crElement) ) {
//
//			docBody = document.getElementsByTagName('body')[0];
//
//			imgChild = crElementNS
//				? document.createElementNS('http://www.w3.org/1999/xhtml', 'img')
//				: document.createElement('img');

		imgChild = createImageElement();

		if( MTC.type != 'none' && imgChild ) {

			MTC.tgtimg = docBody.appendChild(imgChild);

			//set positioning classname
			MTC.tgtimg.className = 'tgtimage MTCimg';
			
			//set src to new image src
			MTC.tgtimg.src = MTC.src

			//move it to superimpose original image
			MTC.tgtimg.style.left = MTC.getXAnchor(MTC.obj);
			MTC.tgtimg.style.top  = MTC.getYAnchor(MTC.obj);
			
			// copy and convert fade duration argument 
			MTC.length = parseInt(arguments[2], 10) * 1000;
			
			// create fade resolution argument as 20 steps per transition
			MTC.resolution = parseInt(arguments[2], 10) * 20;
			
			//start the timer
			MTC.timerID = setInterval('MTC.overlay()', MTC.length/MTC.resolution);
		}
		
		//otherwise if opacity or dynamic creation is not supported
		else {
			//just do the image swap
			MTC.obj.src = MTC.src;

			invokeCallback();
		}

	}
};

//overlay timer function
MTC.overlay = function() {

	delta = (1 / MTC.resolution);

	MTC.pctLeft -= delta;
	
	//if the counter has reached the bottom
	if( MTC.pctLeft < delta ) {
		//clear the timer
		clearInterval(MTC.timerID);
		MTC.timerID = null;
		
		//reset the counter
		MTC.pctLeft = 1;
		
		//set the original image to the src of the new image
		MTC.obj.src = MTC.src;
	}
	
	//set new opacity value on both elements
	//using whatever method is supported
	//restrict max opacity to prevent a visual popping effect in firefox
	//srcOpacity = (MTC.pctLeft == 1 ? 0.9999999 : MTC.pctLeft);
	tgtOpacity = (1 - MTC.pctLeft);
	switch( MTC.type ) {
		case 'ie' :
			MTC.tgtimg.filters.alpha.opacity = tgtOpacity * 100;
			break;
			
		case 'khtml' :
			MTC.tgtimg.style.KhtmlOpacity = tgtOpacity;
			break;
			
		case 'moz' : 
			//restrict max opacity to prevent a visual popping effect in firefox
			MTC.tgtimg.style.MozOpacity = tgtOpacity;
			break;
			
		default : 
			//restrict max opacity to prevent a visual popping effect in firefox
			MTC.tgtimg.style.opacity = tgtOpacity;
	}
	
	//now that we've gone through one fade iteration 
	//we can show the image that's fading in
	MTC.tgtimg.style.visibility = 'visible';
	
	//keep new image in position with original image
	//in case text size changes mid transition or something
	MTC.tgtimg.style.left = MTC.getXAnchor(MTC.obj);
	MTC.tgtimg.style.top  = MTC.getYAnchor(MTC.obj);
	
	//if the counter is at the top, which is just after the timer has finished
	if( MTC.pctLeft == 1 ) {
		//remove the duplicate image
		MTC.tgtimg.parentNode.removeChild(MTC.tgtimg);

		invokeCallback();
	}
};


//crossfade setup function
function IWD_crossfade() {

	//if the timer is not already going
	if( MTC.timerID == null ) {

		//copy the image object 
		MTC.obj = arguments[0];
		
		//copy the image src argument 
		MTC.src = arguments[1];
		
		//change the image alt text if defined
		if( typeof arguments[3] != 'undefined' && arguments[3] != '' ) {
			MTC.obj.alt = arguments[3];
		}

		// set the callback function name
		MTC.callback = arguments[4];

		setOpacityType();

//		//Is dynamic element creation supported?
//		crElementNS = (typeof document.createElementNS != 'undefined');
//		crElement   = (typeof document.createElement   != 'undefined');
//		
//		//if any kind of opacity is supported
//		if( MTC.type != 'none' && (crElementNS || crElement) ) {
//
//			docBody = document.getElementsByTagName('body')[0];
//
//			imgChild = crElementNS
//				? document.createElementNS('http://www.w3.org/1999/xhtml', 'img')
//				: document.createElement('img');

		imgChild = createImageElement();
		if( MTC.type != 'none' && imgChild ) {
			MTC.tgtimg = docBody.appendChild(imgChild);

			//set positioning classname
			MTC.tgtimg.className = 'tgtimage MTCimg';
			
			//set src to new image src
			MTC.tgtimg.src = MTC.src

			//move it to superimpose original image
			MTC.tgtimg.style.left = MTC.getXAnchor(MTC.obj);
			MTC.tgtimg.style.top  = MTC.getYAnchor(MTC.obj);
			
			// copy and convert fade duration argument 
			MTC.length = parseInt(arguments[2], 10) * 1000;
			
			// create fade resolution argument as 20 steps per transition
			MTC.resolution = parseInt(arguments[2], 10) * 20;
			
			//start the timer
			MTC.timerID = setInterval('MTC.crossfade()', MTC.length/MTC.resolution);
		}
		
		//otherwise if opacity or dynamic creation is not supported
		else {
			//just do the image swap
			MTC.obj.src = MTC.src;

			invokeCallback();
		}

	}
};


//crossfade timer function
MTC.crossfade = function() {

	delta = (1 / MTC.resolution);

	MTC.pctLeft -= delta;
	
	//if the counter has reached the bottom
	if( MTC.pctLeft < delta ) {
		//clear the timer
		clearInterval(MTC.timerID);
		MTC.timerID = null;
		
		//reset the counter
		MTC.pctLeft = 1;
		
		//set the original image to the src of the new image
		MTC.obj.src = MTC.src;
	}
	
	//set new opacity value on both elements
	//using whatever method is supported
	//restrict max opacity to prevent a visual popping effect in firefox
	srcOpacity = (MTC.pctLeft == 1 ? 0.9999999 : MTC.pctLeft);
	tgtOpacity = (1 - MTC.pctLeft);
	switch( MTC.type ) {
		case 'ie' :
			MTC.obj.filters.alpha.opacity = MTC.pctLeft * 100;
			MTC.tgtimg.filters.alpha.opacity = tgtOpacity * 100;
			break;
			
		case 'khtml' :
			MTC.obj.style.KhtmlOpacity = MTC.pctLeft;
			MTC.tgtimg.style.KhtmlOpacity = tgtOpacity;
			break;
			
		case 'moz' : 
			//restrict max opacity to prevent a visual popping effect in firefox
			MTC.obj.style.MozOpacity = srcOpacity;
			MTC.tgtimg.style.MozOpacity = tgtOpacity;
			break;
			
		default : 
			//restrict max opacity to prevent a visual popping effect in firefox
			MTC.obj.style.opacity = srcOpacity;
			MTC.tgtimg.style.opacity = tgtOpacity;
	}
	
	//now that we've gone through one fade iteration 
	//we can show the image that's fading in
	MTC.tgtimg.style.visibility = 'visible';
	
	//keep new image in position with original image
	//in case text size changes mid transition or something
	MTC.tgtimg.style.left = MTC.getXAnchor(MTC.obj);
	MTC.tgtimg.style.top  = MTC.getYAnchor(MTC.obj);
	
	//if the counter is at the top, which is just after the timer has finished
	if( MTC.pctLeft == 1 ) {
		//remove the duplicate image
		MTC.tgtimg.parentNode.removeChild(MTC.tgtimg);

		invokeCallback();
	}
};

//swapfade setup function isf
function IWD_swapfade() {
	//if the timer is not already going
	if( MTC.clock == null ) {
		//copy the image object 
		MTC.obj = arguments[0];
		
		//copy the image src argument 
		MTC.src = arguments[1];
		
		//change the image alt text if defined
		if( typeof arguments[3] != 'undefined' && arguments[3] != '' ) {
			MTC.obj.alt = arguments[3];
		}

		// set the callback function name
		MTC.callback = arguments[4];

		MTC.fade = true;

		setOpacityType();
		
		//if any kind of opacity is supported
		if( MTC.type != 'none' ) {
			//copy and convert fade duration argument 
			//the duration specifies the whole transition
			//but the swapfade is two distinct transitions
			MTC.length = parseInt(arguments[2], 10) * 500;
			
			//create fade resolution argument as 20 steps per transition
			//again, split for the two distrinct transitions
			MTC.resolution = parseInt(arguments[2], 10) * 10;
			
			//start the timer
			MTC.timerID = setInterval('MTC.swapfade()', MTC.length/MTC.resolution);
		}
		
		//otherwise if opacity is not supported
		else {
			//just do the image swap
			MTC.obj.src = MTC.src;

			invokeCallback();
		}
		
	}
};


//swapfade timer function
MTC.swapfade = function() {
	//increase or reduce the counter on an exponential scale
	MTC.pctLeft = (MTC.fade) ? MTC.pctLeft * 0.9 : (MTC.pctLeft * (1/0.9)); 

	delta = (1 / MTC.resolution);
	
	//if the counter has reached the bottom
	if( MTC.pctLeft < delta ) {
		//clear the timer
		clearInterval(MTC.timerID);
		MTC.timerID = null;

		//do the image swap
		MTC.obj.src = MTC.src;

		//reverse the fade direction flag
		MTC.fade = false;
		
		//restart the timer
		MTC.timerID = setInterval('MTC.swapfade()', MTC.length/MTC.resolution);

	}
	
	//if the counter has reached the top
	if( MTC.pctLeft > (1 - delta) ) {
		//clear the timer
		clearInterval(MTC.timerID);
		MTC.timerID = null;

		//reset the fade direction flag
		MTC.fade = true;
		
		//reset the counter
		MTC.pctLeft = 1;
	}

	//set new opacity value on element
	//using whatever method is supported
	switch( MTC.type ) {
		case 'ie' :
			MTC.obj.filters.alpha.opacity = MTC.pctLeft * 100;
			break;
			
		case 'khtml' :
			MTC.obj.style.KhtmlOpacity = MTC.pctLeft;
			break;
			
		case 'moz' : 
			//restrict max opacity to prevent a visual popping effect in firefox
			MTC.obj.style.MozOpacity = (MTC.pctLeft == 1 ? 0.9999999 : MTC.pctLeft);
			break;
			
		default : 
			//restrict max opacity to prevent a visual popping effect in firefox
			MTC.obj.style.opacity = (MTC.pctLeft == 1 ? 0.9999999 : MTC.pctLeft);
	}

	//if the counter is at the top, which is just after the timer has finished
	if( MTC.pctLeft == 1 ) {
	    invokeCallback();
	}
};


MTC.getXAnchor = function() {
    var tobj = arguments[0];

    this.pos = tobj.offsetLeft;
    this.tmp = tobj.offsetParent;

    while( this.tmp != null ) {
        this.pos += this.tmp.offsetLeft;
        this.tmp = this.tmp.offsetParent;
    }


    return this.pos + 'px';
}

MTC.getYAnchor = function() {
    var tobj = arguments[0];

    this.pos = tobj.offsetTop;
    this.tmp = tobj.offsetParent;

    while( this.tmp != null ) {
        this.pos += this.tmp.offsetTop;
        this.tmp = this.tmp.offsetParent;
    }

    return this.pos + 'px';
}

