// ISF1.11 :: Image swap-fade 
// *****************************************************
// DOM scripting by brothercake -- http://www.brothercake.com/
//******************************************************
//global object
var isf = { 'clock' : null, 'fade' : true, 'count' : 1 }
var isf2 = { 'clock' : null, 'fade' : true, 'count' : 1 }
var isf3 = { 'clock' : null, 'fade' : true, 'count' : 1 }
var isf4 = { 'clock' : null, 'fade' : true, 'count' : 1 }
/*******************************************************

/*****************************************************************************
 List the images that need to be cached
*****************************************************************************/

isf.imgs = [
   'SS2010/1.gif',
   'SS2010/5.gif',
   'SS2010/9.gif',
   'SS2010/13.gif',
   'SS2010/17.gif',
   'SS2010/21.gif',
   ];

//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];
}

isf2.imgs = [
   'SS2010/2.gif',
   'SS2010/6.gif',
   'SS2010/10.gif',
   'SS2010/14.gif',
   'SS2010/18.gif',
   'SS2010/22.gif',
   ];

//cache the images
isf2.imgsLen = isf2.imgs.length;
isf2.cache = [];
for(var i=0; i<isf2.imgsLen; i++)
{
   isf2.cache[i] = new Image;
   isf2.cache[i].src = isf2.imgs[i];
}

isf3.imgs = [
   'SS2010/3.gif',
   'SS2010/7.gif',
   'SS2010/11.gif',
   'SS2010/15.gif',
   'SS2010/19.gif',
   'SS2010/23.gif',
   ];

//cache the images
isf3.imgsLen = isf3.imgs.length;
isf3.cache = [];
for(var i=0; i<isf3.imgsLen; i++)
{
   isf3.cache[i] = new Image;
   isf3.cache[i].src = isf3.imgs[i];
}

isf4.imgs = [
   'SS2010/4.gif',
   'SS2010/8.gif',
   'SS2010/12.gif',
   'SS2010/16.gif',
   'SS2010/20.gif',
   'SS2010/24.gif',
   ];

//cache the images
isf4.imgsLen = isf4.imgs.length;
isf4.cache = [];
for(var i=0; i<isf4.imgsLen; i++)
{
   isf4.cache[i] = new Image;
   isf4.cache[i].src = isf4.imgs[i];
}

/*****************************************************************************
*****************************************************************************/

/* script to cycle through an image buffer; pehu */
var NR_OF_PHOTOS=6
var myIndex=0

/*****************************************************************************
*****************************************************************************/

//swapfade setup function
function swapfade()
{
   //if the timer is not already going
   if(isf.clock == null && isf2.clock == null && isf3.clock == null && isf4.clock == null)
   {
      //copy the image object 
      isf.obj = arguments[0];
      isf2.obj = arguments[1];
      isf3.obj = arguments[2];
      isf4.obj = arguments[3];
      
      //copy the image src argument 

      if(arguments[4] == 1)
      {
         if(myIndex == NR_OF_PHOTOS-1)
         {
            myIndex = 0;
         }
         else
         {
            myIndex++;
         }
      }
      else if (arguments[4] == 0)
      {
         if(myIndex == 0)
         {
            myIndex = NR_OF_PHOTOS-1;
         }
         else
         {
            myIndex--;
         }
      }
      else
      {
         // arguments[4] is -1, called in the body.onLoad() -> first time, so set index to 0
         myIndex == 0;
      }

      isf.src = isf.imgs[myIndex];
      isf2.src = isf2.imgs[myIndex];
      isf3.src = isf3.imgs[myIndex];
      isf4.src = isf4.imgs[myIndex];
      
      //store the supported form of opacity
      if(typeof isf.obj.style.opacity != 'undefined')
      {
         isf.type = 'w3c';
         isf2.type = 'w3c';
         isf3.type = 'w3c';
         isf4.type = 'w3c';
      }
      else if(typeof isf.obj.style.MozOpacity != 'undefined')
      {
         isf.type = 'moz';
         isf2.type = 'moz';
         isf3.type = 'moz';
         isf4.type = 'moz';
      }
      else if(typeof isf.obj.style.KhtmlOpacity != 'undefined')
      {
         isf.type = 'khtml';
         isf2.type = 'khtml';
         isf3.type = 'khtml';
         isf4.type = '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.type = (isf.obj.filters.length > 0 && typeof isf.obj.filters.alpha == 'object' && typeof isf.obj.filters.alpha.opacity == 'number') ? 'ie' : 'none';
         isf2.type = (isf2.obj.filters.length > 0 && typeof isf2.obj.filters.alpha == 'object' && typeof isf2.obj.filters.alpha.opacity == 'number') ? 'ie' : 'none';
         isf3.type = (isf3.obj.filters.length > 0 && typeof isf3.obj.filters.alpha == 'object' && typeof isf3.obj.filters.alpha.opacity == 'number') ? 'ie' : 'none';
         isf4.type = (isf4.obj.filters.length > 0 && typeof isf4.obj.filters.alpha == 'object' && typeof isf4.obj.filters.alpha.opacity == 'number') ? 'ie' : 'none';
      }
      else
      {
         isf.type = 'none';
         isf2.type = 'none';
         isf3.type = 'none';
         isf4.type = 'none';
      }
      
      //change the image alt text if defined
      if(typeof arguments[4] != 'undefined' && arguments[4] != '')
      {
         isf.obj.alt = arguments[6];
         isf2.obj.alt = arguments[6];
         isf3.obj.alt = arguments[6];
         isf4.obj.alt = arguments[6];
      }
      
      //if any kind of opacity is supported
      if(isf.type != 'none')
      {
         //copy and convert fade duration argument 
         //the duration specifies the whole transition
         //but the swapfade is two distinct transitions
         isf.length = parseInt(arguments[5], 10) * 500;
         isf2.length = parseInt(arguments[5], 10) * 500;
         isf3.length = parseInt(arguments[5], 10) * 500;
         isf4.length = parseInt(arguments[5], 10) * 500;
         
         //create fade resolution argument as 20 steps per transition
         //again, split for the two distrinct transitions
         isf.resolution = parseInt(arguments[5], 10) * 10;
         isf2.resolution = parseInt(arguments[5], 10) * 10;
         isf3.resolution = parseInt(arguments[5], 10) * 10;
         isf4.resolution = parseInt(arguments[5], 10) * 10;
         
         //start the timer
         isf.clock = setInterval('isf.swapfade()', isf.length/isf.resolution);
         isf2.clock = setInterval('isf2.swapfade()', isf2.length/isf2.resolution);
         isf3.clock = setInterval('isf3.swapfade()', isf3.length/isf3.resolution);
         isf4.clock = setInterval('isf4.swapfade()', isf4.length/isf4.resolution);
      }
      
      //otherwise if opacity is not supported
      else
      {
         //just do the image swap
         isf.obj.src = isf.src;
         isf2.obj.src = isf2.src;
         isf3.obj.src = isf3.src;
         isf4.obj.src = isf4.src;
      }
   }
};

//swapfade timer function
isf.swapfade = function()
{
   //increase or reduce the counter on an exponential scale
   isf.count = (isf.fade) ? isf.count * 0.5 : (isf.count * (1/0.3)); 
   
   //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.type)
   {
      case 'ie' :
         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);
   }
};

//swapfade timer function
isf2.swapfade = function()
{
   //increase or reduce the counter on an exponential scale
   isf2.count = (isf2.fade) ? isf2.count * 0.5 : (isf2.count * (1/0.5)); 
   
   //if the counter has reached the bottom
   if(isf2.count < (1 / isf2.resolution))
   {
      //clear the timer
      clearInterval(isf2.clock);
      isf2.clock = null;

      //do the image swap
      isf2.obj.src = isf2.src;

      //reverse the fade direction flag
      isf2.fade = false;
      
      //restart the timer
      isf2.clock = setInterval('isf2.swapfade()', isf2.length/isf2.resolution);

   }
   
   //if the counter has reached the top
   if(isf2.count > (1 - (1 / isf2.resolution)))
   {
      //clear the timer
      clearInterval(isf2.clock);
      isf2.clock = null;

      //reset the fade direction flag
      isf2.fade = true;
      
      //reset the counter
      isf2.count = 1;
   }

   //set new opacity value on element
   //using whatever method is supported
   switch(isf2.type)
   {
      case 'ie' :
         isf2.obj.filters.alpha.opacity = isf2.count * 100;
         break;
         
      case 'khtml' :
         isf2.obj.style.KhtmlOpacity = isf2.count;
         break;
         
      case 'moz' : 
         //restrict max opacity to prevent a visual popping effect in firefox
         isf2.obj.style.MozOpacity = (isf2.count == 1 ? 0.9999999 : isf2.count);
         break;
         
      default : 
         //restrict max opacity to prevent a visual popping effect in firefox
         isf2.obj.style.opacity = (isf2.count == 1 ? 0.9999999 : isf2.count);
   }
};

//swapfade timer function
isf3.swapfade = function()
{
   //increase or reduce the counter on an exponential scale
   isf3.count = (isf3.fade) ? isf3.count * 0.5 : (isf3.count * (1/0.5)); 
   
   //if the counter has reached the bottom
   if(isf3.count < (1 / isf3.resolution))
   {
      //clear the timer
      clearInterval(isf3.clock);
      isf3.clock = null;

      //do the image swap
      isf3.obj.src = isf3.src;

      //reverse the fade direction flag
      isf3.fade = false;
      
      //restart the timer
      isf3.clock = setInterval('isf3.swapfade()', isf3.length/isf3.resolution);

   }
   
   //if the counter has reached the top
   if(isf3.count > (1 - (1 / isf3.resolution)))
   {
      //clear the timer
      clearInterval(isf3.clock);
      isf3.clock = null;

      //reset the fade direction flag
      isf3.fade = true;
      
      //reset the counter
      isf3.count = 1;
   }

   //set new opacity value on element
   //using whatever method is supported
   switch(isf3.type)
   {
      case 'ie' :
         isf3.obj.filters.alpha.opacity = isf3.count * 100;
         break;
         
      case 'khtml' :
         isf3.obj.style.KhtmlOpacity = isf3.count;
         break;
         
      case 'moz' : 
         //restrict max opacity to prevent a visual popping effect in firefox
         isf3.obj.style.MozOpacity = (isf3.count == 1 ? 0.9999999 : isf3.count);
         break;
         
      default : 
         //restrict max opacity to prevent a visual popping effect in firefox
         isf3.obj.style.opacity = (isf3.count == 1 ? 0.9999999 : isf3.count);
   }
};

//swapfade timer function
isf4.swapfade = function()
{
   //increase or reduce the counter on an exponential scale
   isf4.count = (isf4.fade) ? isf4.count * 0.5 : (isf4.count * (1/0.5)); 
   
   //if the counter has reached the bottom
   if(isf4.count < (1 / isf4.resolution))
   {
      //clear the timer
      clearInterval(isf4.clock);
      isf4.clock = null;

      //do the image swap
      isf4.obj.src = isf4.src;

      //reverse the fade direction flag
      isf4.fade = false;
      
      //restart the timer
      isf4.clock = setInterval('isf4.swapfade()', isf4.length/isf4.resolution);

   }
   
   //if the counter has reached the top
   if(isf4.count > (1 - (1 / isf4.resolution)))
   {
      //clear the timer
      clearInterval(isf4.clock);
      isf4.clock = null;

      //reset the fade direction flag
      isf4.fade = true;
      
      //reset the counter
      isf4.count = 1;
   }

   //set new opacity value on element
   //using whatever method is supported
   switch(isf4.type)
   {
      case 'ie' :
         isf4.obj.filters.alpha.opacity = isf4.count * 100;
         break;
         
      case 'khtml' :
         isf4.obj.style.KhtmlOpacity = isf4.count;
         break;
         
      case 'moz' : 
         //restrict max opacity to prevent a visual popping effect in firefox
         isf4.obj.style.MozOpacity = (isf4.count == 1 ? 0.9999999 : isf4.count);
         break;
         
      default : 
         //restrict max opacity to prevent a visual popping effect in firefox
         isf4.obj.style.opacity = (isf4.count == 1 ? 0.9999999 : isf4.count);
   }
};

