/**
  Title: Fader (quick and dirty)
 Author: OPT
Version: 1.0
  Notes: Obvious room for improvement when time allows, tested with IE, Firefox, Safari.
**/

slideShows = new Object();

function createSlideShow(divElement, imageArray, speed)
{
  var img1 = document.createElement('img');
  var img2 = document.createElement('img');
  img1.src = imageArray[0];
  img2.src = imageArray[1];
  
  var slideShow        = new Object();
  slideShow.imageArray = imageArray;
  slideShow.div        = divElement;
  slideShow.speed      = speed;
  slideShow.countdown  = speed;
  slideShow.img1       = img1;
  slideShow.img2       = img2;
  slideShow.pointer    = 0;
  slideShow.fadeOut    = 1;
  slideShow.fadeSpeed  = 0.02;
  
  slideShow.operate = function()
  {
    if(slideShow.countdown > 0) slideShow.countdown = slideShow.countdown - 50;
    if(slideShow.countdown == 0)
    {
      /*are we fading out or in?*/
      if(!this.fadeOut)
      {
        /*fades in the 1st element revealing the 2nd element*/
        if (slideShow.img1.style.opacity >= 1)
        {
          /*increment internal pointer*/
          slideShow.pointer++;
          if(slideShow.pointer+1 > slideShow.imageArray.length ) slideShow.pointer = 0;
          
          /*src next image in array*/
          if( slideShow.pointer+2 <= slideShow.imageArray.length )
            slideShow.img2.src = slideShow.imageArray[slideShow.pointer+1];
          else
            slideShow.img2.src = slideShow.imageArray[0];

          /*reset transition countdown*/
          slideShow.countdown = slideShow.speed;
          
          /*change fade direction*/
          slideShow.fadeOut = 1;
          
          //debug
          //clearInterval(this.interval);
        }
        else if (this.img1.style.opacity < 1)
		{
			this.img1.style.opacity = parseFloat(this.img1.style.opacity) + slideShow.fadeSpeed;
			this.img1.style.filter  = 'alpha(opacity=' + parseInt( parseFloat(this.img1.style.opacity)*100 ) + ')';
		}
        //clearInterval(this.interval);
        return true;           
      }

      if(this.fadeOut)
      {
        /*fades the 1st element revealing the 2nd element*/
        if (slideShow.img1.style.opacity > 0) 
		{
			this.img1.style.opacity = parseFloat(this.img1.style.opacity) - slideShow.fadeSpeed;
			this.img1.style.filter  = 'alpha(opacity=' + parseInt( parseFloat(this.img1.style.opacity)*100 ) + ')';
		}
        if (slideShow.img1.style.opacity <= 0)
        {
          /*increment internal pointer*/
          slideShow.pointer++;
          if(slideShow.pointer+1 > slideShow.imageArray.length ) slideShow.pointer = 0;
          
          /*src next image in array*/
          if( slideShow.pointer+2 <= slideShow.imageArray.length )
            slideShow.img1.src = slideShow.imageArray[slideShow.pointer+1];
          else
            slideShow.img1.src = slideShow.imageArray[0];

          /*reset transition countdown*/
          slideShow.countdown = slideShow.speed;
          
          /*change fade direction*/
          slideShow.fadeOut = 0;
          
          //debug
          //clearInterval(this.interval);
          return true;
        }                
      }
    }
  }
   img1.style.position = "absolute";
   img1.style.opacity = 1;
   img2.style.position = "absolute";
   img2.style.opacity = 1;
             
   divElement.appendChild(img2);
   divElement.appendChild(img1);

   
   var uid = "x" + Math.floor(Math.random()*500000);
   slideShows[uid] = slideShow;
   slideShow.interval = setInterval("slideShows['"+uid+"'].operate()", 50);
   
 }