// Caunton Engineering customised version of ISF1.11 :: Image swap-fade with added support for overlayed images
// *****************************************************
// DOM scripting by brothercake -- http://www.brothercake.com/
//******************************************************
//global object
var isf = { 'clock' : null, 'fade' : true, 'count' : 1 }

//swapfade setup function
function swapfade()
{
	//if the timer is not already going
	if(isf.clock == null)
	{
		//copy the image object
		isf.fadeobj = arguments[0];
		isf.staticobj = arguments[1];
		isf.prefetchpic = arguments[2];
		
		//store the supported form of opacity
		if(typeof isf.fadeobj.style.opacity != 'undefined')
		{
			isf.type = 'w3c';
			isf.fade = !(((isf.fadeobj.style.opacity.toString() != "") ? isf.fadeobj.style.opacity : 1) < 0.5);
		}
		else if(typeof isf.fadeobj.style.MozOpacity != 'undefined')
		{
			isf.type = 'moz';
			isf.fade = !(((isf.fadeobj.style.MozOpacity.toString() != "") ? isf.fadeobj.style.MozOpacity : 1) < 0.5);
		}
		else if(typeof isf.fadeobj.style.KhtmlOpacity != 'undefined')
		{
			isf.type = 'khtml';
			isf.fade = !(((isf.fadeobj.style.KhtmlOpacity.toString() != "") ? isf.fadeobj.style.KhtmlOpacity : 1) < 0.5);
		}
		else if(typeof isf.fadeobj.filters == 'object')
		{
			isf.type = (isf.fadeobj.filters.length > 0 && typeof isf.fadeobj.filters.alpha == 'object') ? 'ie' : 'none';
			if (isf.type == 'ie') {
				isf.fade = !(((isf.fadeobj.filters.alpha.opacity.toString() != "") ? isf.fadeobj.filters.alpha.opacity : 100) < 50);
			} else {
				isf.fade = false;
			}
		}
		else
		{
			isf.type = 'none';
			isf.fade = false;
		}
		
		//if any kind of opacity is supported
		if(isf.type != 'none')
		{
		    if (typeof arguments[4] != 'undefined' && arguments[4] != '') {
		        isf.fadeobj.alt = arguments[4];
            }

		    if (typeof arguments[5] != 'undefined' && arguments[5] != '') {
		        isf.fadeobj.parentNode.href = "project.aspx?id=" + arguments[5];
		    }
		
			//copy and convert fade duration argument 
			isf.length = parseInt(arguments[3], 10) * 500;
			
			//create fade resolution argument as 20 steps per transition
			isf.resolution = parseInt(arguments[3], 10) * 10;

			if (isf.fade) {
				isf.count = 1;
			} else {
				isf.count = 1;
				while (isf.count >= (1 / isf.resolution)) isf.count *= 0.9;
			}
			
			//start the timer
			isf.clock = setInterval('isf.swapfade()', isf.length/isf.resolution);
		}
		
		//otherwise if opacity is not supported
		else
		{
			//swap the source from the hidden pic to the visible pic and prefetch the next pic in to the hidden pic
		    isf.fadeobj.src = isf.staticobj.src;
		    isf.staticobj.src = isf.prefetchpic;
		}
		
	}
};


//swapfade timer function
isf.swapfade = function () {
    //increase or reduce the counter on an exponential scale
    isf.count = (isf.fade) ? isf.count * 0.9 : (isf.count * (1 / 0.9));

    //if the counter has reached the limit
    if ((isf.count < (1 / isf.resolution) && isf.fade) || (isf.count > (1 - (1 / isf.resolution)) && !isf.fade)) {
        //clear the timer
        clearInterval(isf.clock);
        isf.clock = null;
        //preload the next image
        if (isf.fade)
            isf.fadeobj.src = isf.prefetchpic;
        else
            isf.staticobj.src = isf.prefetchpic;
    }

    //set new opacity value on element
    //using whatever method is supported
    switch (isf.type) {
        case 'ie':
            if (isf.clock == null) {
                isf.fadeobj.filters.alpha.opacity = (isf.fade ? 0 : 100);
            } else {
                isf.fadeobj.filters.alpha.opacity = isf.count * 100;
            }
            break;

        case 'khtml':
            if (isf.clock == null) {
                isf.fadeobj.style.KhtmlOpacity = (isf.fade ? 0 : 1);
            } else {
                isf.fadeobj.style.KhtmlOpacity = isf.count;
            }
            break;

        case 'moz':
            //restrict max opacity to prevent a visual popping effect in firefox
            if (isf.clock == null) {
                isf.fadeobj.style.MozOpacity = (isf.fade ? 0 : 1);
            } else {
                isf.fadeobj.style.MozOpacity = isf.count;
            }
            break;

        default:
            //restrict max opacity to prevent a visual popping effect in firefox
            if (isf.clock == null) {
                isf.fadeobj.style.opacity = (isf.fade ? 0 : 1);
            } else {
                isf.fadeobj.style.opacity = isf.count;
            }
    }
};



