

//cache the images
ixf2.imgsLen = ixf2.imgs.length;
ixf2.cache = [];
for(var i=0; i<ixf2.imgsLen; i++)
{
    ixf2.cache[i] = new Image;
    ixf2.cache[i].src = ixf2.imgs[i];
}


//crossfade setup function
function crossfade2()
{
    //if the timer is not already going
	//alert(ixf2.clock);
    if(ixf2.clock == null)
    {
        //copy the image object 
        ixf2.obj = document.getElementById(arguments[0]);
        
        //copy the image src argument 
        ixf2.src = ixf2.imgs[arguments[5]];
        
        //store the supported form of opacity
        if(typeof ixf2.obj.style.opacity != 'undefined')
        {
            ixf2.type = 'w3c';
        }
        else if(typeof ixf2.obj.style.MozOpacity != 'undefined')
        {
            ixf2.type = 'moz';
        }
        else if(typeof ixf2.obj.style.KhtmlOpacity != 'undefined')
        {
            ixf2.type = 'khtml';
        }
        else if(typeof ixf2.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
            ixf2.type = (ixf2.obj.filters.length > 0 && typeof ixf2.obj.filters.alpha == 'object' && typeof ixf2.obj.filters.alpha.opacity == 'number') ? 'ie' : 'none';
        }
        else
        {
            ixf2.type = 'none';
        }
        
        //change the image alt text if defined
        if(typeof arguments[4] != 'undefined' && arguments[4] != '')
        {
            ixf2.obj.alt = arguments[4];
        }
        
        //if any kind of opacity is supported
        if(ixf2.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
            ixf2.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
            ixf2.newimg.className = 'idupe2';
            
            //set src to new image src
            ixf2.newimg.src = ixf2.src

            //move it to superimpose original image
            ixf2.newimg.style.left = ixf2.getRealPosition(ixf2.obj, 'x') + 'px';
            ixf2.newimg.style.top = ixf2.getRealPosition(ixf2.obj, 'y') + 'px';
            
            //copy and convert fade duration argument 
            ixf2.length = parseInt(eval(arguments[2]* 1000), 10) ;
            
            //create fade resolution argument as 20 steps per transition
            ixf2.resolution = parseInt(eval(arguments[2]* 20), 10) ;
            
            //start the timer
            ixf2.clock = setInterval('ixf2.crossfade()', ixf2.length/ixf2.resolution);
        }
        
        //otherwise if opacity is not supported
        else
        {
            //just do the image swap
            ixf2.obj.src = ixf2.src;
        }

        //alert(arguments[6]);
        //alert(arguments[5]);
        //alert(eval(parseInt(arguments[5]) + 1));
        //alert((eval(parseInt(arguments[5]) + 1)  % ixf2.imgs.length));

        if(arguments[6]==1) {
			for (tno=0;tno<20;tno++) {
				thisNumberAround =(Math.round(ixf2.imgs.length*Math.random())+1)  % ixf2.imgs.length;
				if(thisNumberAround != arguments[5]) { break; }

			}

		}
		else {
			thisNumberAround = (eval(parseInt(arguments[5]) + 1)  % ixf2.imgs.length);

		}

        timermSeconds = arguments[3] * 1000;
        setTimeout("crossfade2('"+arguments[0]+"',"+arguments[1]+",'"+arguments[2]+"','"+arguments[3]+"','"+arguments[4]+"',"+thisNumberAround+","+arguments[6]+")",timermSeconds);
      
    }
	else {

        timermSeconds = arguments[3] * 1000;
        setTimeout("crossfade2('"+arguments[0]+"',"+arguments[1]+",'"+arguments[2]+"','"+arguments[3]+"','"+arguments[4]+"',"+arguments[5]+","+arguments[6]+")",timermSeconds);
	}
};


//crossfade timer function
ixf2.crossfade = function()
{
    //decrease the counter on a linear scale
    ixf2.count -= (1 / ixf2.resolution);
    
    //if the counter has reached the bottom
    if(ixf2.count < (1 / ixf2.resolution))
    {
        //clear the timer
        clearInterval(ixf2.clock);
        ixf2.clock = null;
        
        //reset the counter
        ixf2.count = 1;
        
        //set the original image to the src of the new image
        ixf2.obj.src = ixf2.src;
    }
    
    //set new opacity value on both elements
    //using whatever method is supported
    switch(ixf2.type)
    {
        case 'ie' :
            ixf2.obj.filters.alpha.opacity = ixf2.count * 100;
            ixf2.newimg.filters.alpha.opacity = (1 - ixf2.count) * 100;
            break;
            
        case 'khtml' :
            ixf2.obj.style.KhtmlOpacity = ixf2.count;
            ixf2.newimg.style.KhtmlOpacity = (1 - ixf2.count);
            break;
            
        case 'moz' : 
            //restrict max opacity to prevent a visual popping effect in firefox
            ixf2.obj.style.MozOpacity = (ixf2.count == 1 ? 0.9999999 : ixf2.count);
            ixf2.newimg.style.MozOpacity = (1 - ixf2.count);
            break;
            
        default : 
            //restrict max opacity to prevent a visual popping effect in firefox
            ixf2.obj.style.opacity = (ixf2.count == 1 ? 0.9999999 : ixf2.count);
            ixf2.newimg.style.opacity = (1 - ixf2.count);
    }
    
    //now that we've gone through one fade iteration 
    //we can show the image that's fading in
    ixf2.newimg.style.visibility = 'visible';
    
    //keep new image in position with original image
    //in case text size changes mid transition or something
    ixf2.newimg.style.left = ixf2.getRealPosition(ixf2.obj, 'x') + 'px';
    ixf2.newimg.style.top = ixf2.getRealPosition(ixf2.obj, 'y') + 'px';
    
    //if the counter is at the top, which is just after the timer has finished
    if(ixf2.count == 1)
    {
        //remove the duplicate image
        ixf2.newimg.parentNode.removeChild(ixf2.newimg);
    }
};



//get real position method
ixf2.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;
};
