/*
 ******************************************************************************
 * scmSlideshow.js           1.0               18/July/2006
 *
 * "COPYRIGHT.  HSBC HOLDINGS PLC 2004.  ALL RIGHTS RESERVED.
 *
 * This software is only to be used for the purpose for which it has been
 * provided.  No part of it is to be reproduced, disassembled, transmitted,
 * stored in a retrieval system nor translated in any human or computer
 * language in any way or for any other purposes whatsoever without the
 * prior written consent of HSBC Holdings plc."
 *
 ******************************************************************************
 */

var domCompliant=(document.getElementById); 
var ie4Browser=document.all; 

function adImage(imageURL, targetURL, imageAlt){
  this.imageURL = imageURL;
  this.targetURL = targetURL;
  this.imageAlt = imageAlt;
}
 
function slideShow(adImages, imageWidth, imageHeight, timeInterval, isMouseOverPause, parentCanvasPrefix, childCanvasPrefix){

  this.adImages=adImages;
  this.cachedImages=new Array(); 
  for (p=0;p<adImages.length;p++){
       this.cachedImages[p]=new Image();
       this.cachedImages[p].src=adImages[p].imageURL;
  }
  this.imageborder=0;
  var imageWidth=imageWidth;
  var imageHeight=imageHeight;
  this.timeInterval=timeInterval * 1000;  
  this.isMouseOverPausecheck=isMouseOverPause;
  this.mouseOverCheck=0;
  this.opacityDegree=10; 
  this.currentImageIndex=0;
  this.nextImageIndex=1;
  this.slideShowCanvasPrefix = childCanvasPrefix;
  this.currentCanvasId= this.slideShowCanvasPrefix+"_0";
  this.parentCanvasId = parentCanvasPrefix;
  this.timeIntervalHander; 
  this.slideShowBackgroundColor="white";

  if(this.adImages.length == 1){
     this.drawSingleImage();
     return 0;
  }
  
  if (ie4Browser&&domCompliant||domCompliant){ 
      document.write('<div id="'+this.parentCanvasId+'" style="position:relative;width:'+imageWidth+'px;height:'+imageHeight+'px;overflow:hidden;"><div id="'+this.slideShowCanvasPrefix+'_0" style="position:absolute;width:'+imageWidth+'px;height:'+imageHeight+'px;top:0;left:0;filter:progid:DXImageTransform.Microsoft.Alpha(opacity=10);-moz-opacity:10;-khtml-opacity:10;background-color:'+this.slideShowBackgroundColor+'"></div><div id="'+this.slideShowCanvasPrefix+'_1" style="position:absolute;width:'+imageWidth+'px;height:'+imageHeight+'px;top:0;left:0;filter:progid:DXImageTransform.Microsoft.alpha(opacity=10);-moz-opacity:10;background-color:'+this.slideShowBackgroundColor+'"></div></div>');
  }
  else{
      document.write('<div><img name="'+this.parentCanvasId+'" src="'+this.cachedImages[0].src+'"></img></div>');
  }
  
  if (ie4Browser&&domCompliant||domCompliant){ 
      if(this.adImages.length > 1){
         this.startSlideShow();
      }     
  }
  else{
      this.currentImageIndex++;
      var cachedSlideShowObject=this;
      setInterval(function(){cachedSlideShowObject.rotateImage()}, this.timeInterval);
  }
  
}

slideShow.prototype.drawSingleImage=function(){
  var slideHTML="";
  if (this.adImages[0].targetURL!=""){ 
      slideHTML='<a href="'+this.adImages[0].targetURL+'">';
  }
  slideHTML+='<img src="'+this.cachedImages[0].src+'" border="'+this.imageborder+'px"'+' alt="'+this.adImages[0].imageAlt+'"/>';
  slideHTML+='</img>'
  if (this.adImages[0].targetURL!=""){ 
      slideHTML+='</a>';
  }
  document.write('<div id="temp">'+slideHTML+'</div>');   
}

slideShow.prototype.startSlideShow=function(){
  var currentCanvas=ie4Browser? ie4Browser[this.currentCanvasId] : document.getElementById(this.currentCanvasId);
  this.drawImage(currentCanvas, this.currentImageIndex);
  if (this.isMouseOverPausecheck==1){ 
      var slideShowInstance=this;
      var slideShowElement=ie4Browser? ie4Browser[this.parentCanvasId] : document.getElementById(this.parentCanvasId);
      slideShowElement.onmouseover=function(){slideShowInstance.mouseOverCheck=1};
      slideShowElement.onmouseout=function(){slideShowInstance.mouseOverCheck=0};
  }
  this.rotateImage();
}
 
slideShow.prototype.drawImage=function(canvasElement, imageIndex){
  var slideHTML="";
  if (this.adImages[imageIndex].targetURL!=""){ 
      slideHTML='<a href="'+this.adImages[imageIndex].targetURL+'">';
  }
  slideHTML+='<img src="'+this.cachedImages[imageIndex].src+'" border="'+this.imageborder+'px"'+' alt="'+ this.adImages[imageIndex].imageAlt+'"/>';
  slideHTML+='</img>'
  if (this.adImages[imageIndex].targetURL!=""){ 
      slideHTML+='</a>';
  }
  canvasElement.innerHTML=slideHTML;
}

slideShow.prototype.rotateImage=function(){

  var cachedSlideShowObject=this;
  if (this.mouseOverCheck==1){
      setTimeout(function(){cachedSlideShowObject.rotateImage()}, 100);
  }
  else if (ie4Browser&&domCompliant||domCompliant){
           this.resetCanvas();
           var currentCanvas=this.currentCanvasElement=ie4Browser? ie4Browser[this.currentCanvasId] : document.getElementById(this.currentCanvasId);
           currentCanvas.style.zIndex++;
           this.currentCanvasId=(this.currentCanvasId==this.slideShowCanvasPrefix+"_0")? this.slideShowCanvasPrefix+"_1" : this.slideShowCanvasPrefix+"_0";
           this.timeIntervalHander=setInterval(function(){cachedSlideShowObject.fadeInImage()},38);
        }else{
             var ns4ImageElement=document.images[this.parentCanvasId];
             ns4ImageElement.src=this.cachedImages[this.currentImageIndex].src;
        }
  this.currentImageIndex=(this.currentImageIndex<this.cachedImages.length-1)? this.currentImageIndex+1 : 0;
}

slideShow.prototype.resetCanvas=function(){
  this.opacityDegree=10;
  var currentCanvas=ie4Browser? ie4Browser[this.currentCanvasId] : document.getElementById(this.currentCanvasId);
  if (currentCanvas.filters&&currentCanvas.filters[0]){
      if (typeof currentCanvas.filters[0].opacity=="number"){ 
          currentCanvas.filters(0).opacity=this.opacityDegree;
      }
      else{ 
          currentCanvas.style.filter="alpha(opacity="+this.opacityDegree+")";
      }
  }
  else if (currentCanvas.style.MozOpacity){
           currentCanvas.style.MozOpacity=this.opacityDegree/101;
       }
  else if (currentCanvas.style.KhtmlOpacity){
           currentCanvas.style.KhtmlOpacity=this.opacityDegree/100;
       }
}

slideShow.prototype.fadeInImage=function(){
  if (this.opacityDegree<100){
    this.opacityDegree+=10;
      if (this.currentCanvasElement.filters&&this.currentCanvasElement.filters.item("DXImageTransform.Microsoft.Alpha")){
          if (typeof this.currentCanvasElement.filters.item("DXImageTransform.Microsoft.Alpha").opacity=="number") //if IE6+
              this.currentCanvasElement.filters.item("DXImageTransform.Microsoft.Alpha").opacity=this.opacityDegree;
          else 
              this.currentCanvasElement.style.filter="alpha(opacity="+this.opacityDegree+")";
      }
      else if (this.currentCanvasElement.style.MozOpacity){
             this.currentCanvasElement.style.MozOpacity=this.opacityDegree/101;
           }
      else if (this.currentCanvasElement.style.KhtmlOpacity){
             this.currentCanvasElement.style.KhtmlOpacity=this.opacityDegree/100;
           }
  }
  else{
    clearInterval(this.timeIntervalHander); 
    this.nextcanvas=(this.currentCanvasId==this.slideShowCanvasPrefix+"_0")? this.slideShowCanvasPrefix+"_0" : this.slideShowCanvasPrefix+"_1";
    this.currentCanvasElement=ie4Browser? ie4Browser[this.nextcanvas] : document.getElementById(this.nextcanvas);
    this.drawImage(this.currentCanvasElement, this.nextImageIndex);
    this.nextImageIndex=(this.nextImageIndex<this.cachedImages.length-1)? this.nextImageIndex+1 : 0;
    var cachedSlideShowObject=this;
    setTimeout(function(){cachedSlideShowObject.rotateImage()}, this.timeInterval);
  }
  
 }
