// ixfit1.11 :: Image cross-fade 
// *****************************************************
// DOM scripting by brothercake -- http://www.brothercake.com/
//******************************************************
//global object
var ixfit = { 'clock' : null, 'count' : 1 }
/*******************************************************



/*****************************************************************************
 List the images that need to be cached
*****************************************************************************/

ixfit.imgs = [
	'/products/managedservices/images/it-banner-1.jpg',
	'/products/managedservices/images/it-banner-2.jpg',
	'/products/managedservices/images/it-banner-3.jpg',
	'/products/managedservices/images/it-banner-4.jpg'
	];

/*****************************************************************************
*****************************************************************************/



//cache the images
ixfit.imgsLen = ixfit.imgs.length;
ixfit.cache = [];
for(var i=0; i<ixfit.imgsLen; i++)
{
	ixfit.cache[i] = new Image;
	ixfit.cache[i].src = ixfit.imgs[i];
}


//itcrossfade setup function
function itcrossfade()
{
    // arguments:
    // ---------------
    // 0 - img obj
    // 1 - img src
    // 2 - transition duration
    // 3 - img alt text
    // 4 - area obj
    // 5 - area href url
    
	//if the timer is not already going
	if(ixfit.clock == null)
	{
		//copy the image object 
		ixfit.obj = arguments[0];
		
		//copy the image src argument
		ixfit.src = arguments[1];

		//same link - do nothing
		if (ixfit.obj != null && 
		    ixfit.obj.src == ixfit.src)
		{
		    return;
		}

		ixfit.area = arguments[4];

		if (typeof arguments[5] != 'undefined' && arguments[5] != '')
		{
		    ixfit.href = arguments[5];
		}
		else
		{
		    ixfit.href = '';
		}
				
		//store the supported form of opacity
		if(typeof ixfit.obj.style.opacity != 'undefined')
		{
			ixfit.type = 'w3c';
		}
		else if(typeof ixfit.obj.style.MozOpacity != 'undefined')
		{
			ixfit.type = 'moz';
		}
		else if(typeof ixfit.obj.style.KhtmlOpacity != 'undefined')
		{
			ixfit.type = 'khtml';
		}
		else if(typeof ixfit.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
			ixfit.type = (ixfit.obj.filters.length > 0 && typeof ixfit.obj.filters.alpha == 'object' && typeof ixfit.obj.filters.alpha.opacity == 'number') ? 'ie' : 'none';
		}
		else
		{
			ixfit.type = 'none';
		}
		
		//change the image alt text if defined
		if(typeof arguments[3] != 'undefined' && arguments[3] != '')
		{
			ixfit.obj.alt = arguments[3];
		}
		
		//if any kind of opacity is supported
		if(ixfit.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
			ixfit.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
			ixfit.newimg.className = 'idupe';
			
			//set src to new image src
			ixfit.newimg.src = ixfit.src

			//move it to superimpose original image
			ixfit.newimg.style.left = ixfit.getRealPosition(ixfit.obj, 'x') + 'px';
			ixfit.newimg.style.top = ixfit.getRealPosition(ixfit.obj, 'y') + 'px';
			
			//copy and convert fade duration argument 
			ixfit.length = parseInt(arguments[2], 10) * 1000;
			
			//create fade resolution argument as 20 steps per transition
			ixfit.resolution = parseInt(arguments[2], 10) * 20;
			
			//start the timer
			ixfit.clock = setInterval('ixfit.itcrossfade()', ixfit.length/ixfit.resolution);
		}
		
		//otherwise if opacity is not supported
		else
		{
			//just do the image swap
			ixfit.obj.src = ixfit.src;
		}
		
	}
};


//itcrossfade timer function
ixfit.itcrossfade = function()
{
    //decrease the counter on a linear scale
    ixfit.count -= (1 / ixfit.resolution);

    //if the counter has reached the bottom
    if (ixfit.count < (1 / ixfit.resolution))
    {
        //clear the timer
        clearInterval(ixfit.clock);
        ixfit.clock = null;

        //reset the counter
        ixfit.count = 1;

        //set the original image to the src of the new image
        ixfit.obj.src = ixfit.src;

        if (typeof ixfit.area != 'undefined')
        {
            if (typeof ixfit.href != 'undefined' && ixfit.href != '')
            {
                ixfit.area.href = ixfit.href;
            }
            else
            {
                ixfit.area.href = '#';
            }
        }
    }

    //set new opacity value on both elements
    //using whatever method is supported
    switch (ixfit.type)
    {
        case 'ie':
            ixfit.obj.filters.alpha.opacity = ixfit.count * 100;
            ixfit.newimg.filters.alpha.opacity = (1 - ixfit.count) * 100;
            break;

        case 'khtml':
            ixfit.obj.style.KhtmlOpacity = ixfit.count;
            ixfit.newimg.style.KhtmlOpacity = (1 - ixfit.count);
            break;

        case 'moz':
            //restrict max opacity to prevent a visual popping effect in firefox
            ixfit.obj.style.MozOpacity = (ixfit.count == 1 ? 0.9999999 : ixfit.count);
            ixfit.newimg.style.MozOpacity = (1 - ixfit.count);
            break;

        default:
            //restrict max opacity to prevent a visual popping effect in firefox
            ixfit.obj.style.opacity = (ixfit.count == 1 ? 0.9999999 : ixfit.count);
            ixfit.newimg.style.opacity = (1 - ixfit.count);
    }

    //now that we've gone through one fade iteration 
    //we can show the image that's fading in
    ixfit.newimg.style.visibility = 'visible';

    //keep new image in position with original image
    //in case text size changes mid transition or something
    ixfit.newimg.style.left = ixfit.getRealPosition(ixfit.obj, 'x') + 'px';
    ixfit.newimg.style.top = ixfit.getRealPosition(ixfit.obj, 'y') + 'px';

    //if the counter is at the top, which is just after the timer has finished
    if (ixfit.count == 1)
    {
        //remove the duplicate image
        ixfit.newimg.parentNode.removeChild(ixfit.newimg);
    }
};



//get real position method
ixfit.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;
};

