/***********************************************
* Ultimate Fade-In Slideshow (v1.51): © Dynamic Drive (http://www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit http://www.dynamicdrive.com/ for this script and 100s more.
***********************************************/

var fadearray = new Array(); //cache slideshow instances
var fadeclear = new Array(); //cache corresponding clearinterval pointers

var iebrowser = document.all;
var dom = ( document.getElementById ); //modern dom browsers

function slideshow()
{
  this.pausecheck = APP_vars.pause;
  this.mouseovercheck = 0;
  this.repeat = APP_vars.repeat ? APP_vars.repeat : 0;
  this.repeated = 0;
  this.delay = APP_vars.delay ? APP_vars.delay : 3000;
  APP_vars.fade_rate = APP_vars.fade_rate ? APP_vars.fade_rate : 10;
  this.fade_rate = APP_vars.fade_rate;
  this.degree = this.fade_rate; //initial opacity degree (%)
  this.curimageindex = 0;
  this.nextimageindex = 1;
  fadearray[fadearray.length] = this;
  this.slideshowid = fadearray.length-1;
  this.canvasbase = "canvas" + this.slideshowid;
  this.curcanvas = this.canvasbase + "_0";
  this.images = new Array(); //preload images
  this.slides = new Array();
// Deprecate:
  if ( APP_vars["images"] )
  {
    for ( p=0; p < APP_vars["images"].length; p++ )
    {
      this.images[p] = new Image();
      this.images[p].src = APP_vars["images"][p];
      this.slides[p] = APP_vars["images"][p];
    }
  }
  if ( APP_vars["slides"] )
  {
    for ( s = 0; s < APP_vars["slides"].length; s++ )
    {
      if ( APP_vars["slides"][s].match ( /^.+\.(?:gif|jpe?g|png)$/ ) )
      {
        this.images[this.images.length] = new Image();
        this.images[this.images.length-1].src = APP_vars["slides"][s];
      }
      this.slides[s] = APP_vars["slides"][s];
    }
  }
  this.fadewidth = APP_vars.width;
  this.fadeheight = APP_vars.height;
 
  if ( iebrowser && dom||dom ) //if IE5+ or modern browsers (ie: Firefox)
    document.write ( '<div id="master' + this.slideshowid + '" style="position:relative;width:' + this.fadewidth + 'px;height:' + this.fadeheight + 'px;overflow:hidden;"><div id="' + this.canvasbase + '_0" style="position:absolute;width:' + this.fadewidth + 'px;height:' + this.fadeheight + 'px;top:0;left:0;filter:progid:DXImageTransform.Microsoft.alpha(opacity=' + this.fade_rate + ');opacity:0.' + this.fade_rate + ';-moz-opacity:0.' + this.fade_rate + ';-khtml-opacity:0.' + this.fade_rate + ';"></div><div id="' + this.canvasbase + '_1" style="position:absolute;width:' + this.fadewidth + 'px;height:' + this.fadeheight + 'px;top:0;left:0;filter:progid:DXImageTransform.Microsoft.alpha(opacity=' + this.fade_rate + ');opacity:0.' + this.fade_rate + ';-moz-opacity:0.' + this.fade_rate + ';-khtml-opacity:0.' + this.fade_rate + ';"></div></div>' );
  else
// How to deal with HTML slides?
    document.write ( '<div><img name="defaultslide' + this.slideshowid + '" src="' + this.slides[0].src + '" border=0></div>' );
 
  if ( iebrowser && dom||dom ) //if IE5+ or modern browsers such as Firefox
    this.startit();
  else
  {
    this.curimageindex++;
    setInterval ( "fadearray[" + this.slideshowid + "].rotateimage()", this.delay );
  }
}

function fadepic ( obj )
{
  if ( obj.degree < 100 )
  {
    obj.degree += obj.fade_rate;
    if      ( obj.tempobj.filters && obj.tempobj.filters[0] )
    {
      if ( typeof ( obj.tempobj.filters[0].opacity ) == "number" ) //if IE6+
        obj.tempobj.filters[0].opacity = obj.degree;
      else //else if IE5.5-
        obj.tempobj.style.filter="alpha(opacity=" + obj.degree + ")";
    }
    else if ( obj.tempobj.style.MozOpacity )
      obj.tempobj.style.MozOpacity = obj.degree/101;
    else if ( obj.tempobj.style.KhtmlOpacity )
      obj.tempobj.style.KhtmlOpacity = obj.degree/100;
    else if ( obj.tempobj.style.opacity && ! obj.tempobj.filters )
      obj.tempobj.style.opacity = obj.degree/101;
  }
  else
  {
    clearInterval ( fadeclear[obj.slideshowid] );
    obj.nextcanvas = ( obj.curcanvas == obj.canvasbase + "_0" )
                     ? obj.canvasbase + "_0" : obj.canvasbase + "_1";
    obj.tempobj = iebrowser ? iebrowser[obj.nextcanvas]
                            : document.getElementById ( obj.nextcanvas );
    obj.populateslide ( obj.tempobj, obj.nextimageindex );
    obj.nextimageindex = ( obj.nextimageindex < obj.slides.length-1 )
                         ? obj.nextimageindex + 1 : 0;
    setTimeout ( "fadearray[" + obj.slideshowid + "].rotateimage()",
                 obj.delay );
  }
}
 
slideshow.prototype.populateslide = function ( slide, s )
  {
    if      ( this.slides[s].match ( /^.+\.(?:gif|jpe?g|png)$/ ) )
      slide.innerHTML = '<img src="' + this.slides[s] + '" border=0 />';
    else if ( this.slides[s].match ( /^.+\.r?(?:asp|html?)$/ ) )
      slide.innerHTML = '<iframe width=100% height=100% frameborder=0 src="' + this.slides[s] + '" border=0></iframe>';
    else
      slide.innerHTML = this.slides[s];
  }
 
slideshow.prototype.rotateimage = function()
  {
    if ( this.curimageindex == 0 )
    {
      if ( this.repeat == 0 || this.repeated < this.repeat )
        this.repeated++;
      else
        return;
    }

    if ( this.pausecheck == 1 ) //if pause onMouseover enabled, cache object
      var cacheobj = this;
    if      ( this.mouseovercheck == 1 )
      setTimeout ( function() { cacheobj.rotateimage() }, 100 );
    else if ( iebrowser && dom||dom )
    {
      this.resetit();
      var crossobj = this.tempobj = iebrowser ? iebrowser[this.curcanvas]
                                  : document.getElementById ( this.curcanvas );
      crossobj.style.zIndex++;
      fadeclear[this.slideshowid] = setInterval ( "fadepic(fadearray[" + this.slideshowid + "])", 50 );
      this.curcanvas = ( this.curcanvas == this.canvasbase + "_0" )
                       ? this.canvasbase + "_1" : this.canvasbase + "_0";
    }
    else
    {
// How to deal with HTML slides?
      var ns4imgobj = document.images[ "defaultslide" + this.slideshowid ];
      ns4imgobj.src = this.slides[this.curimageindex].src;
    }

    this.curimageindex = ( this.curimageindex < this.slides.length-1 )
                         ? this.curimageindex+1 : 0;
  }

slideshow.prototype.resetit = function()
  {
    this.degree = this.fade_rate;
    var crossobj = iebrowser ? iebrowser[this.curcanvas]
                             : document.getElementById ( this.curcanvas );
    if      ( crossobj.filters && crossobj.filters[0] )
    {
      if ( typeof ( crossobj.filters[0].opacity ) == "number" ) //if IE6+
        crossobj.filters(0).opacity = this.degree;
      else //else if IE5.5-
        crossobj.style.filter = "alpha(opacity=" + this.degree + ")";
    }
    else if ( crossobj.style.MozOpacity )
      crossobj.style.MozOpacity = this.degree/101;
    else if ( crossobj.style.KhtmlOpacity )
      crossobj.style.KhtmlOpacity = this.degree/100;
    else if ( crossobj.style.opacity && ! crossobj.filters )
      crossobj.style.opacity = this.degree/101;
  }

slideshow.prototype.startit = function()
  {
    var crossobj = iebrowser ? iebrowser[this.curcanvas]
                             : document.getElementById ( this.curcanvas );
    this.populateslide ( crossobj, this.curimageindex );
    if ( this.pausecheck == 1 ) //IF SLIDESHOW SHOULD PAUSE ONMOUSEOVER
    {
      var cacheobj = this;
      var crossobjcontainer = iebrowser ? iebrowser["master" + this.slideshowid] : document.getElementById ( "master" + this.slideshowid );
      crossobjcontainer.onmouseover = function() { cacheobj.mouseovercheck = 1; }
      crossobjcontainer.onmouseout  = function() { cacheobj.mouseovercheck = 0; }
    }

    this.rotateimage();
  }

new slideshow();
