/***************************************************************************
* Program Name       : Javascript program
* Copyright Notice   : COPYRIGHT (C) 2006 BY HSBC
* Creation Date      : 2008-12-16
* Programmer         : e-Crusade
* Abstract           : This file contains common functions required
*                      for home&Away project
***************************************************************************/
/***************************************************************************************************************
* File Name		: scmSlideshowIn.js
* Usage			: 1. Define function to initial swapping banner
*			: 2. Define function to swap banner automatically
*			: 3. Define function to swap banner by button
*			:
* Author		: e-Crusade
* Creation Date		: 2008-12-16
* Amendment History	:
* Date		By		Description
* ---------	------------	--------------------------------------------------------------------------------
***************************************************************************************************************/

var domCompliant=(document.getElementById); 
var ie4Browser=document.all; 
var pauseFlag=0;
var loopCounter=0;
function pws_ha_ini()
{
	var timeInterval = parseInt(5);
	var pause = parseInt(1); 
	this.adImages =document.getElementById('haBanner').getElementsByTagName('div');
	var index = adImages.length;
	
	if(index >=1 ) {
		this.objSlideShow = new pws_ha_slideShow(adImages, timeInterval, pause); 
	}
}
  
function pws_ha_draw_button(id,imgPath)
{
	var divObj = document.getElementById(id);	
	var printContent ="";
	for (var j=0; j < adImages.length; j++)
	{
		var jIdx = j+1;
		printContent +=	"<a onClick=\"pws_ha_goToImage("+j+");\"><img src=\""+imgPath+"rotate-off-"+jIdx+".gif\" name=\"buttonImage"+jIdx+"\" id=\"buttonImage"+(j+1)+"\" width=\"14\" height=\"16\" style=\"margin: 0px 2px 0px 2px;\" /></a>";
	}
	divObj.innerHTML = printContent;
}

function pws_ha_adImage(imageURL, targetURL, imageAlt){
  this.imageURL = imageURL;
  this.targetURL = targetURL;
  this.imageAlt = imageAlt;
}

function pws_ha_slideShow(adImages,  timeInterval, isMouseOverPause){

  this.adImages=adImages;
  this.cachedImages=new Array(); 
  for (var p=0;p<adImages.length;p++){
       this.cachedImages[p]=new Image();
	   //alert(adImages[p].getElementsByTagName('img')[0].src);
       this.cachedImages[p].src=adImages[p].getElementsByTagName('img')[0].src;
  }
  this.imageborder=0;
  this.timeInterval=timeInterval * 1000;  
  this.mouseOverCheck=0;
  this.opacityDegree=10; 
  this.cuurentInd =0;
  this.nextImageIndex= + 1;
  this.timeIntervalHander; 
  this.slideShowBackgroundColor="white";

  if(this.adImages.length == 1){
     this.pws_ha_drawSingleImage();
     return 0;
  }


  
  if (ie4Browser&&domCompliant||domCompliant){ 
      if(this.adImages.length > 1){
         this.pws_ha_startSlideShow();
      }     
  }
  else{

      var cachedSlideShowObject=this;
      setInterval(function(){cachedSlideShowObject.pws_ha_rotateImage()}, this.timeInterval);
  }
  return this;
  
}


function pws_ha_goToImage(ind)
{

	if (pauseFlag == 0) {
		var buttonPath;

		for (var i=0; i < this.adImages.length; i++) {
		
			if (i==ind) {
				buttonPath = new String(document.getElementById("buttonImage"+parseInt(i+1)).src);
				
				buttonPath = buttonPath.replace('-off-','-on-');
				document.getElementById("buttonImage"+parseInt(i+1)).src = buttonPath;
			}
			else 
			{
				buttonPath = new String(document.getElementById("buttonImage"+parseInt(i+1)).src);

				buttonPath = buttonPath.replace('-on-','-off-');

				document.getElementById("buttonImage"+parseInt(i+1)).src = buttonPath;

			}
		}		
		this.objSlideShow.pws_ha_internalGoTo(ind);
		
	}	
}


pws_ha_slideShow.prototype.pws_ha_internalGoTo=function(ind){
    clearInterval(this.timeHander);     // pws_ha_rotateImage
    clearInterval(this.timeIntervalHander); // pws_ha_fadeInImage
 
    this.nextImageIndex = ind;
    this.nextImageIndex=(this.nextImageIndex<this.cachedImages.length-1)? this.nextImageIndex+1 : 0;
    this.pws_ha_rotateImage();
}
/* SGH customization end */
pws_ha_slideShow.prototype.pws_ha_drawSingleImage=function(){
 
}

pws_ha_slideShow.prototype.pws_ha_startSlideShow=function(){

  this.pws_ha_rotateImage();
}
 
pws_ha_slideShow.prototype.pws_ha_drawImage=function( imageIndex){
	if (imageIndex ==0)
		imageIndex = adImages.length;
	imageIndex--;

	for (var j=0; j < adImages.length; j++)
		adImages[j].style.display = "none";
	adImages[imageIndex].style.display = "inline";
	cuurentInd = imageIndex;



}

pws_ha_slideShow.prototype.pws_ha_rotateImage=function(){

	var cachedSlideShowObject=this;
	this.pws_ha_resetCanvas();
	this.timeIntervalHander=setInterval(function(){cachedSlideShowObject.pws_ha_fadeInImage()},38);

}

pws_ha_slideShow.prototype.pws_ha_resetCanvas=function(){
  this.opacityDegree=10;


}

pws_ha_slideShow.prototype.pws_ha_fadeInImage=function(){
  var buttonIndex;
  var oldButtonIndex;
  var buttonPath;
  var oldButtonPath;
  
  if (this.opacityDegree<100){
	this.opacityDegree = 100;
		
     
		buttonIndex = (this.nextImageIndex==0)?this.adImages.length:this.nextImageIndex;	

	 	buttonPath = new String(document.getElementById("buttonImage"+ buttonIndex).src);
		buttonPath = buttonPath.replace('-off-','-on-');
		document.getElementById("buttonImage"+buttonIndex).src = buttonPath;

		oldButtonIndex = (this.nextImageIndex==0)?this.adImages.length-1:this.nextImageIndex-1;
		if ((this.nextImageIndex==1))
			oldButtonIndex = this.adImages.length;
		
		oldButtonPath = new String(document.getElementById("buttonImage"+ oldButtonIndex).src);
		oldButtonPath = oldButtonPath.replace('-on-','-off-');
		document.getElementById("buttonImage"+oldButtonIndex).src = oldButtonPath;
	
  }
  else{
  
    clearInterval(this.timeIntervalHander); 
	this.pws_ha_drawImage(this.nextImageIndex);
    this.nextImageIndex=(this.nextImageIndex<this.cachedImages.length-1)? this.nextImageIndex+1 : 0;
	if(this.nextImageIndex ==1)
		loopCounter++;
    var cachedSlideShowObject=this;
	if (loopCounter < 3)
		this.timeHander = setTimeout(function(){cachedSlideShowObject.pws_ha_rotateImage()}, this.timeInterval);

  }
  
 }
 
function pws_ha_redirectLink()
{
	document.location = adImages[cuurentInd].targetURL;
}

pws_ha_ini();