// ISF1.11 :: Image swap-fade 
// *****************************************************
// DOM scripting by brothercake -- http://www.brothercake.com/
//******************************************************
//global object
var isf = { 'clock' : null, 'fade' : true, 'count' : 1 }
/*******************************************************



/*****************************************************************************
 List the images that need to be cached
*****************************************************************************/

isf.imgs = [];

/*****************************************************************************
*****************************************************************************/



//cache the images
isf.imgsLen = isf.imgs.length;
isf.cache = [];
for(var i=0; i<isf.imgsLen; i++)
{
	isf.cache[i] = new Image;
	isf.cache[i].src = isf.imgs[i];
}


//swapfade setup function
function swapfade()
{
//copy the image object 
isf.obj = arguments[0];

//copy the image src argument 
isf.src = arguments[1];
		
//if the timer is not already going
	if(isf.clock == null)
	{
			
		//isf.obj.filters.alpha.opacity = '100';
		//alert('oh my ' + isf.src);
		
		//store the supported form of opacity
		if(typeof isf.obj.style.opacity != 'undefined')
		{
			isf.myType = 'w3c';
		}
		else if(typeof isf.obj.style.MozOpacity != 'undefined')
		{
			isf.myType = 'moz';
		}
		else if(typeof isf.obj.style.KhtmlOpacity != 'undefined')
		{
			isf.myType = 'khtml';
		}
		else if(typeof isf.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
			isf.myType = (isf.obj.filters.length > 0 && typeof isf.obj.filters.alpha == 'object' && typeof isf.obj.filters.alpha.opacity == 'number') ? 'ie' : 'none';
			
			
		}
		else
		{
			isf.myType = 'none';
		}
		
		//change the image alt text if defined
		if(typeof arguments[3] != 'undefined' && arguments[3] != '')
		{
			isf.obj.alt = arguments[3];
		}
		
		//if any kind of opacity is supported
		if(isf.myType != 'none')
		{
			//copy and convert fade duration argument 
			//the duration specifies the whole transition
			//but the swapfade is two distinct transitions
			isf.length = parseInt(arguments[2], 10) * 500;
			
			//create fade resolution argument as 20 steps per transition
			//again, split for the two distrinct transitions
			isf.resolution = parseInt(arguments[2], 10) * 10;
			
			//start the timer
			isf.clock = setInterval('isf.swapfade()', isf.length/isf.resolution);
		}
		
		//otherwise if opacity is not supported
		else
		{
			//just do the image swap
			isf.obj.src = isf.src;
			
		}
		
	}
	else	{
		isf.clock = null;
		swapfade(arguments[0],arguments[1],arguments[2],arguments[3]);
		//isf.obj.src = isf.src;
	}
	
};


//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 bottom
	if(isf.count < (1 / isf.resolution))
	{
		//clear the timer
		clearInterval(isf.clock);
		isf.clock = null;

		//do the image swap
		isf.obj.src = isf.src;

		//reverse the fade direction flag
		isf.fade = false;
		
		//restart the timer
		isf.clock = setInterval('isf.swapfade()', isf.length/isf.resolution);

	}
	
	//if the counter has reached the top
	if(isf.count > (1 - (1 / isf.resolution)))
	{
		//clear the timer
		clearInterval(isf.clock);
		isf.clock = null;

		//reset the fade direction flag
		isf.fade = true;
		
		//reset the counter
		isf.count = 1;
	}

	//set new opacity value on element
	//using whatever method is supported
	switch(isf.myType)
	{
		case 'ie' :
			//alert('test');
			isf.obj.filters.alpha.opacity = isf.count * 100;
			break;
			
		case 'khtml' :
			isf.obj.style.KhtmlOpacity = isf.count;
			break;
			
		case 'moz' : 
			//restrict max opacity to prevent a visual popping effect in firefox
			isf.obj.style.MozOpacity = (isf.count == 1 ? 0.9999999 : isf.count);
			break;
			
		default : 
			//restrict max opacity to prevent a visual popping effect in firefox
			isf.obj.style.opacity = (isf.count == 1 ? 0.9999999 : isf.count);
	}
};

//restart timer, when user clicks 'next'
function resetTimer()	{
	//isf.clock == null;
	clearInterval(isf.clock);
	//restart the timer
	//isf.clock = setInterval('isf.swapfade()', isf.length/isf.resolution);
}

function MakeArray(n){
    this.length=n;
    for(var i=1; i<=n; i++) this[i]=i-1;
    return this
  }

  hex=new MakeArray(16);
  hex[11]="A"; hex[12]="B"; hex[13]="C"; hex[14]="D";
  hex[15]="E"; hex[16]="F";

  function ToHex(x){  // Changes a int to hex (in the range 0 to 255)
    var high=x/16;
    var s=high+"";        //1
    s=s.substring(0,2);   //2 the combination of these = trunc funct.
    high=parseInt(s,10);  //3
    var left=hex[high+1]; // left part of the hex-value
    var low=x-high*16;    // calculate the rest of the values
    s=low+"";             //1
    s=s.substring(0,2);   //2 the combination of these = trunc funct.
    low=parseInt(s,10);   //3
    var right=hex[low+1]; // right part of the hex-value
    var string=left+""+right; // add the high and low together
    return string;
  }

  function fadein(text){
    text=text.substring(3,text.length-4); 
                          // gets rid of the HTML-comment-tags
    color_d1=255;         // any value in 'begin' 0 to 255
    mul=color_d1/text.length;
    for(i=0;i<text.length;i++){
      color_d1=mul*i;
      // some other things you can try>>
      // "=255-mul*i" to fade out, "=mul*i" to fade in, 
      // or try "255*Math.sin(i/(text.length/3))"
      color_h1=ToHex(color_d1);
      color_d2=mul*i;
      color_h2=ToHex(color_d2);
      document.write("<FONT COLOR='#FF"+color_h1+color_h2+"'>"
        +text.substring(i,i+1)+'</FONT>');
    }
  }
