// ImageSlideShow: Takes List of Pictures and displays them
// in a customizable window. A unique name is required
// to differentiate different Slide Shows.
function ImageSlideShow(strName, objPictureList, objConfig) {
   this.objBrowserSupport = new BrowserSupport();
   
   if(typeof strName == "undefined" || strName == null) {
      this.strName = "";
      for(var i = 0; i < 10; i++) {
         this.strName += ""+Math.floor((Math.random()*10))+"";
      }
   } else
      this.strName = strName;
   
   if(typeof objPictureList == "undefined" || objPictureList == null)
      this.objPictureList = new ImageSlideShow.PictureList();
   else
      this.objPictureList = objPictureList;
   
   var psum = 0; 
   for(var i = 0; i < objPictureList.arrData.length; i++) {
      this.objPictureList.arrData[i]['Probability'] = (this.objPictureList.arrData[i]['Weight']/this.objPictureList.iTotalWeight) + psum;
      psum = this.objPictureList.arrData[i]['Probability'];
   }
      
   if(typeof objConfig == "undefined" || objConfig == null)
      this.objConfig = new ImageSlideShow.Config();
   else
      this.objConfig = objConfig;

   this.iCurrentOpacity = 10;
   this.objBackCanvas = null;
   this.objFrontCanvas = null;
   this.objCaption = null;
   this.iCurrentImageIndex = 0;
   this.iNextImageIndex = 0;
   this.arrPreloadedImages = null;
   this.oldInit = null;
};

ImageSlideShow.prototype.Init = function() {
   if(!this.objConfig) {
      alert("Failed Init! Not a proper Object reference!");
   }
   
   // DHTML Support Required for this script. Output Empty String
   if(!this.objBrowserSupport.blnHasDHTMLSupport)
      return;
   
   this.objCaption = this.GetElement(this.strName+"Caption");
   this.objBackCanvas  = this.GetElement(this.strName+"Canvas0");
   this.objFrontCanvas = this.GetElement(this.strName+"Canvas1");
   
   // If we don't have any of our objects, then exit. They are required.
   if((this.objConfig.blnCaption && !this.objCaption) || !this.objFrontCanvas || !this.objBackCanvas) {
      return;
   }
   //alert("Objects aquired");
   
   // Randomly generate 2 different indexes, if configured
   if(this.objConfig.blnRandom) {
      this.iCurrentImageIndex = this.RandomizeIndex();
      this.RandomizeNextIndex();
   } else {
      this.iCurrentImageIndex = 0;
      this.iNextImageIndex = 0;
   }
   
   this.Flip();
   this.LoadImage(this.objFrontCanvas,this.iCurrentImageIndex);
   this.LoadImage(this.objBackCanvas,this.iNextImageIndex);
   
   // Write the Displayed Caption
   if(this.objConfig.blnCaption && this.objPictureList.arrData[this.iCurrentImageIndex]['Caption'].length != 0) {
      this.objCaption.innerHTML="<i>("+this.objPictureList.arrData[this.iCurrentImageIndex]['Caption']+")</i>";
   }

   this.iCurrentImageIndex = this.iNextImageIndex;
   //alert("Scheduling FadeOut");
   Global_TimerManager.Start(this.objConfig.iSwitchPause, false, "FadeOut", null, this);
};

ImageSlideShow.prototype.FadeOut = function() {
   
   if (this.iCurrentOpacity > 9 && this.objConfig.blnFade){
      //alert("fading");
      this.iCurrentOpacity -= 10;
      this.SetOpacity(this.objFrontCanvas, this.iCurrentOpacity);
      Global_TimerManager.Start(50, false, "FadeOut", null, this);
   }
   else{
      //alert("done fading, fliping");
      this.Flip();
      this.LoadImage(this.objBackCanvas,this.iNextImageIndex);
      
      // Write the Displayed Caption
      if(this.objConfig.blnCaption && this.objPictureList.arrData[this.iCurrentImageIndex]['Caption'].length != 0) {
         this.objCaption.innerHTML="<i>("+this.objPictureList.arrData[this.iCurrentImageIndex]['Caption']+")</i>";
      }
            
      this.iCurrentImageIndex = this.iNextImageIndex;
      Global_TimerManager.Start(this.objConfig.iSwitchPause, false, "FadeOut", null, this);
   }
};

ImageSlideShow.prototype.LoadImage = function(objCanvas, iIndex) {

   if(this.objBrowserSupport.blnHasImageObj && this.objBrowserSupport.blnHasW3CDOMSupport) {
      while(objCanvas.childNodes.length > 0) {
         objCanvas.removeChild(objCanvas.childNodes[0]);
      }
      
      var objLink = document.createElement('A');
      objLink.href = this.objPictureList.arrData[iIndex]['OriginalUrl'];
      objLink.target = '_blank';
      
      if(typeof this.objPictureList.arrData[iIndex]['Data'] == "undefined") {
         var objImage = document.createElement('IMG');
         objImage.src = this.objPictureList.arrData[iIndex]['SmallUrl'];
         objImage.alt = this.objPictureList.arrData[iIndex]['Caption'];
         objImage.setAttribute('width',this.objPictureList.iWidth);
         objImage.setAttribute('height',this.objPictureList.iHeight);
         objImage.setAttribute('border',0);
         this.objPictureList.arrData[iIndex]['Data'] = objImage;
      }
      
      objLink.appendChild(this.objPictureList.arrData[iIndex]['Data']);
      objCanvas.appendChild(objLink);
   } else {

      var imgIndex1 = iIndex;
      var img1 = '<img src="'+this.objPictureList.arrData[imgIndex1]['SmallUrl']+'" alt="'+this.objPictureList.arrData[imgIndex1]['Caption']+'" border="0" width="'+this.objPictureList.iWidth+'" height="'+this.objPictureList.iHeight+'">';
   
      var link1 = '<a href="'+this.objPictureList.arrData[imgIndex1]['OriginalUrl']+'" target="_blank">';
   
      link1 += img1+"</a>";
   
      objCanvas.innerHTML = "";
      objCanvas.innerHTML = link1;
   }
};

ImageSlideShow.prototype.RandomizeNextIndex = function() {
   this.iNextImageIndex = this.RandomizeIndex();
   while(this.iNextImageIndex == this.iCurrentImageIndex) { 
      this.iNextImageIndex = this.RandomizeIndex(); 
   }
};

ImageSlideShow.prototype.RandomizeIndex = function() {
   // Now get a random number (assumed to be equally distributed,
   // i.e. perfectly random). Value of number is the probability
   // that the index would be chosen, so map it to the indexes.
   rand = Math.random();
//   var win = window.open('','RandHistory',"width="+900+",height="+300+",top="+0+",left="+0+
//                         ",menubar=no,toolbar=no,location=center,scrollbars=yes"+
//                         ",resizable=yes,status=no,location=no'");
//   win.document.write('<textarea rows="10" cols="100">Rand = ' + rand + "\n");
   for(var i = 0; i < this.objPictureList.arrData.length; i++) {
//      win.document.write("this.objPictureList.arrData["+i+"]['Probability'] = "+this.objPictureList.arrData[i]['Probability']+"\n");
      if(rand <= this.objPictureList.arrData[i]['Probability']){ 
//         win.document.close();
         return i;
      }
   }

//   win.document.close();
   
   alert("Failed to generate Index!!! Rand: " + rand);
};

ImageSlideShow.prototype.Flip = function() {

      // Swap Canvases.
      var objTemp = this.objFrontCanvas;
      this.objFrontCanvas = this.objBackCanvas;
      this.objBackCanvas = objTemp;
      
      // Start at 99 Opacity. 100 Opacity causes flickering in Netscape.
      this.iCurrentOpacity = 99;
      
      // Place Front Canvas in front, back canvas behind it
      this.SetZIndex(this.objFrontCanvas, 200);
      this.SetZIndex(this.objBackCanvas, 100);
      
      // Start Both at the current Opacity.
      this.SetOpacity(this.objFrontCanvas, this.iCurrentOpacity);
      this.SetOpacity(this.objBackCanvas, this.iCurrentOpacity);
      
      // Get the next image.
      if(this.objConfig.blnRandom) {
         this.RandomizeNextIndex();
      } else {
         if(this.iNextImageIndex + 1 >= this.objPictureList.arrData.length) {
            this.iNextImageIndex = 0;
         } else {
            this.iNextImageIndex++;
         }
      }
};

// This function registers an instance of this class to init
// when the page finishes loading.
ImageSlideShow.prototype.Register = function() {
   // This sets up the this object in the scope of the function
   // registered below. Interesting....
   var SlideShowObject = this;
   
   // DHTML Support Required for this script. Output Empty String
   if(!this.objBrowserSupport.blnHasDHTMLSupport)
      return;
      
   this.AddEvent(window,'load',function() { SlideShowObject.Init(); } );
}

ImageSlideShow.prototype.GetHTML = function() {
   var Output = '';
   
   Output += '<table class="ContainerTable" id="'+this.strName+'ContainerTable" ';
   Output += (this.objConfig.strTableAlign.length>0?'align="'+this.objConfig.strTableAlign+'"':'')+'';
   Output += 'cellspacing="'+this.objConfig.iTableCellSpacing+'" ';
   Output += 'cellpadding="'+this.objConfig.iTableCellPadding+'" width="';
   
   Output += this.objPictureList.iWidth+'px">\n';
   
   Output += '   <tr>\n';
   Output += '      <td class="ContainerColumn" align="left" height="'+this.objPictureList.iHeight+'px">\n';
   
   Output += '         <div class="Container" style="width:'+this.objPictureList.iWidth+'px;height:'+this.objPictureList.iHeight+'px;">\n';
   Output += '            <div id="'+this.strName+'Canvas0" class="Canvas"></div>\n';
   Output += '            <div id="'+this.strName+'Canvas1" class="Canvas"></div>\n';
   Output += '         </div>\n';
   Output += '      </td>\n';
   Output += '   </tr>\n';
   if(this.objConfig.blnCaption) {
      Output += '   <tr>\n';
      Output += '      <td height="25" class="DescriptionColumn">\n'
      Output += '         <div class="Description" id="'+this.strName+'Caption"></div>\n';
      Output += '      </td>\n';
      Output += '   </tr>\n';
   }
   Output += '</table>';
   
   // DHTML Support Required for this script. Output Empty String
   if(!this.objBrowserSupport.blnHasDHTMLSupport)
      return "";
   
   return Output;
};

ImageSlideShow.prototype.SetOpacity = function(obj, iOpacity ) {
   
   if(obj.filters && obj.filters.alpha) {
      obj.filters.alpha.opacity = iOpacity;
   } else if(obj.style && typeof obj.style.MozOpacity != "undefined") {
      obj.style.MozOpacity = iOpacity/100;
   }
};

ImageSlideShow.prototype.SetZIndex = function(obj, iZIndex)
{
   if(obj.style && typeof obj.style.zIndex != "undefined") {
       obj.style.zIndex = iZIndex;
   }
};

// Configuration object, holds the configurable options
ImageSlideShow.Config = function() {
   this.iSwitchPause = 3000;
   this.blnFade = true;
   this.blnRandom = true;
   this.blnCaption = true;
   this.strTableAlign = "";
   this.blnCaption = true;
   this.iTableCellSpacing = 0;
   this.iTableCellPadding = 0;
};

// PictureList object, is a list of pictures to chose from
ImageSlideShow.PictureList = function(iWidth, iHeight, blnPreloadImages) {
   this.objBrowserSupport = new BrowserSupport();
   if(typeof iWidth == "undefined")
      this.iWidth = 100;
   else
      this.iWidth = iWidth;

   if(typeof iHeight == "undefined")
      this.iHeight = 100;
   else
      this.iHeight = iHeight;
      
   if(typeof blnPreloadImages == "undefined")
      this.blnPreloadImages = false;
   else
      this.blnPreloadImages = blnPreloadImages;  
  
  this.arrData = new Array();
  this.iTotalWeight = 0;
};

// Adds a pictures small url, original url, and caption
ImageSlideShow.PictureList.prototype.AddPicture = function(strOriginalUrl, strSmallUrl, strCaption, iWeight) {
   var iIndex = this.arrData.push( new Array());
   this.arrData[iIndex-1]['SmallUrl'] = strSmallUrl;
   this.arrData[iIndex-1]['OriginalUrl'] = strOriginalUrl;
   this.arrData[iIndex-1]['Caption'] = strCaption;
   
   if(typeof iWeight == "undefined") {
      this.arrData[iIndex-1]['Weight'] = 1;
      this.iTotalWeight += 1;
   } else {   
      this.arrData[iIndex-1]['Weight'] = iWeight;
      this.iTotalWeight += iWeight;
   }
   
   if(this.blnPreloadImages && 
      this.objBrowserSupport.blnHasImageObj && 
      this.objBrowserSupport.blnHasW3CDOMSupport) {
      var objImage = document.createElement('IMG');
      objImage.src = this.arrData[iIndex-1]['SmallUrl'];
      objImage.alt = this.arrData[iIndex-1]['Caption'];
      objImage.setAttribute('width',this.iWidth);
      objImage.setAttribute('height',this.iHeight);
      objImage.setAttribute('border',0);
      this.arrData[iIndex-1]['Data'] = objImage;
   }
};

ImageSlideShow.prototype.GetElement = function(strName) {
   if (document.getElementById) {
      return document.getElementById(strName);
   } else if (document.all) {
      return document.all[strName];
   } else if (document.layers) {
      return document.layers[strName];
   } else {
      return null;
   }
};

ImageSlideShow.prototype.AddEvent = function(obj, strEventName, objFunction) {
	if (obj.attachEvent) {
		obj.attachEvent("on" + strEventName, objFunction);
	} else if(obj.addEventListener) {
		obj.addEventListener(strEventName, objFunction, true);
	} else {
      eval(""+
      "if(typeof obj.__queue_on"+strEventName+" == 'undefined') {\n"+
      "   obj.__queue_on"+strEventName+" = new Array();\n"+
      "}\n"+
      "if(typeof obj.on"+strEventName+" == 'function' && \n"+
      "   typeof obj.on"+strEventName+".__chainer == 'undefined') {\n"+
      "   obj.__queue_on"+strEventName+".push(obj.on"+strEventName+");\n"+
      "}\n"+
      "obj.__queue_on"+strEventName+".push(objFunction);\n"+
      "obj.on"+strEventName+" = function () {\n"+
      "   var queue = this.__queue_on"+strEventName+";\n"+
      "   if(typeof queue != 'undefined') {\n"+
      "      for( var i = 0; i < queue.length; i++ ) {\n"+
      "         queue[i]();\n"+
      "      }\n"+
      "   }\n"+
      "};\n"+
      "obj.on"+strEventName+".__chainer = true;");
   }
};
