// JScript File

//function debug(msg) {
//  document.getElementById("debug").innerHTML = document.getElementById("debug").innerHTML + '<br />' + msg
//}

function banner(bodyObj, loadingMsg){
  function _show() {
    var me = this;
    
    function continueAnimation() {
      for (var i=0; i<me.options.length; i++) {
        me.linkOption(i);
      }
      
      me.selectNode(0);
      me.LoadingMessage.style.display='none';
    }
    
    var aImages = new Array();
    
    for (var i=0; i<this.options.length; i++) {
      aImages.push(this.options[i].NormalImage);
      aImages.push(this.options[i].MouseOverImage);
      aImages.push(this.options[i].MainImage);
    }
    
    var a = new ImagePreloader(aImages, continueAnimation);
  }
 
 // Initializing an option
 
  function _addOption(buttonObj, normalImage, liteImage, rotateImage, mainImage, navigateUrl) {
    // Creating a new node
    var node = new bannerNode(this, this.options.length, buttonObj, normalImage, liteImage, rotateImage, mainImage, navigateUrl, false);
    
    this.options[this.options.length] = node;
  }
  
  function _linkOption(index) {
    var node = this.options[index];
    var img = this.BodyImg;
    
    // Showing the button
    node.ButtonObj.src = node.NormalImage;
       
    // Adding events
	  addEvent(node.ButtonObj, 'mouseover', 
	    function() {
	      node.BannerObj.selectNode(node.Index);
	      if (node.BannerObj.RotationStatus == node.BannerObj.RotationStatusValues.Started)
	        node.BannerObj.pauseRotation();
	    });
	  
	  
    addEvent(node.ButtonObj, 'mouseout', 
	    function() {
	      if (node.BannerObj.RotationStatus == node.BannerObj.RotationStatusValues.Paused)
	        node.BannerObj.beginRotation();	              
	    });
	    
	  
  };
  
  
  function _selectNode(index, useRotateImage) {
  
//    debug('SelectNode');
  
  
    var o = this.options[index];
    
    useRotateImage = useRotateImage || false;
    
    o.ButtonObj.src = (useRotateImage)?o.RotateImage:o.MouseOverImage;
    
  
    //this.BodyImg.src = o.MainImage;
    blendimage(this.BodyImg.parentNode.id, this.BodyImg.id, o.MainImage, 150);
    
    
    if (this.SelectedIndex > -1 && this.SelectedIndex != index) 
      this.options[this.SelectedIndex].ButtonObj.src = this.options[this.SelectedIndex].NormalImage;
    
    this.SelectedIndex = index;
  } 
  
  function _beginRotation() {
    this.RotationStatus = this.RotationStatusValues.Started;
	  var myClass = this;
	  
	  function timerRelay() {	  
	    var index = myClass.SelectedIndex;
	    if (myClass.options.length == ++index) {
	      index=0;
	    }
	    myClass.selectNode(index, true);
	    
	    myClass._RotateId=setTimeout(timerRelay, myClass.RotationTime);
	  }
	  
	  myClass._RotateId=setTimeout(timerRelay, myClass.RotationTime);
	}
	
	function _pauseRotation() {
	  clearTimeout(this._RotateId);
	  this.RotationStatus = this.RotationStatusValues.Paused;
	}
	
	function _cancelRotation() {
	  clearTimeout(this._RotateId);
	  this.RotationStatus = this.RotationStatusValues.Stopped;
	}
  

  // ENUMS
  this.RotationStatusValues = {Started:0, Paused:1, Stopped:2};

  // Properties
	this.options = new Array();
	this.SelectedIndex = -1;
	this.RotationTime = 5000;
	this.BodyImg = bodyObj;
	this.RotationStatus = this.RotationStatusValues.Stopped;
	this.LoadingMessage = loadingMsg;
	
	// Attributes
	this._RotateId;
	
	// Methods
	this.addOption = _addOption;
	this.selectNode = _selectNode;
	this.beginRotation = _beginRotation;
	this.cancelRotation = _cancelRotation;
	this.pauseRotation = _pauseRotation;	
	this.show = _show;
	this.linkOption = _linkOption;
	
	// Contructor?
	loadingMsg.style.display='';
	
	var me = this;
  addEvent(bodyObj, 'mouseover', 
    function() {
      if (me.RotationStatus == me.RotationStatusValues.Started)
        me.pauseRotation();
    });
    
  addEvent(bodyObj, 'mouseout', 
    function() {
      if (me.RotationStatus == me.RotationStatusValues.Paused)
        me.beginRotation();
    });
    
  addEvent(bodyObj, 'click', 
    function() {
      var o = me.options[me.SelectedIndex];
      window.location = o.NavigateUrl;
    });    
}

// Banner Node Class
function bannerNode(bannerObj, index, buttonObj, normalImage, mouseOverImage, rotateImage, mainImage, navigateUrl, selected)
{
	this.NormalImage = normalImage;
	this.MouseOverImage = mouseOverImage;
	this.MainImage = mainImage;
	this.RotateImage = rotateImage;
	this.NavigateUrl = navigateUrl;
	this.Index = index;
	this.ButtonObj = buttonObj;
	this.BannerObj = bannerObj;
}

/** Not part of the BANNER - These are utility functions **/
function addEvent( obj, type, fn ) {
  if ( obj.attachEvent ) {
    obj['e'+type+fn] = fn;
    obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
    obj.attachEvent( 'on'+type, obj[type+fn] );
  } else
    obj.addEventListener( type, fn, false );
}
function removeEvent( obj, type, fn ) {
  if ( obj.detachEvent ) {
    obj.detachEvent( 'on'+type, obj[type+fn] );
    obj[type+fn] = null;
  } else
    obj.removeEventListener( type, fn, false );
}

function ImagePreloader(images, call_back)
{
   // store the call-back
   this.call_back = call_back;
 
   // initialize internal state.
   this.nLoaded = 0;
   this.nProcessed = 0;
   this.aImages = new Array;
 
   // record the number of images.
   this.nImages = images.length;
 
   // for each image, call preload()
   for ( var i = 0; i < images.length; i++ )
      this.preload(images[i]);
}

ImagePreloader.prototype.preload = function(image)
{
   // create new Image object and add to array
   var oImage = new Image;
   this.aImages.push(oImage);
  
   // set up event handlers for the Image object
   oImage.onload = ImagePreloader.prototype.onload;
   oImage.onerror = ImagePreloader.prototype.onerror;
   oImage.onabort = ImagePreloader.prototype.onabort;
  
   // assign pointer back to this.
   oImage.oImagePreloader = this;
   oImage.bLoaded = false;
  
   // assign the .src property of the Image object
   oImage.src = image;
}

ImagePreloader.prototype.onComplete = function()
{
   this.nProcessed++;
   if ( this.nProcessed == this.nImages )
   {
      this.call_back(this.aImages, this.nLoaded);
   }
}

ImagePreloader.prototype.onload = function()
{
   this.bLoaded = true;
   this.oImagePreloader.nLoaded++;
   this.oImagePreloader.onComplete();
}

ImagePreloader.prototype.onerror = function()
{
   this.bError = true;
   this.oImagePreloader.onComplete();
}

ImagePreloader.prototype.onabort = function()
{
   this.bAbort = true;
   this.oImagePreloader.onComplete();
}





function blendimage(divid, imageid, imagefile, millisec) {
  var speed = Math.round(millisec / 100);
  var timer = 0;
  
  //set the current image as background
  document.getElementById(divid).style.backgroundImage = "url(" + document.getElementById(imageid).src + ")";
  
  //make image transparent
  changeOpac(0, imageid);
  
  //make new image
  document.getElementById(imageid).src = imagefile;
  
  //Effect.Appear(imageid);

  //fade in image
  for(i = 0; i <= 100; i++) {
      setTimeout("changeOpac(" + i + ",'" + imageid + "')",(timer * speed));
      timer++;
  }
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
}


function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}
