﻿//------------------------- MultiImageClass ----------------------------
function MultiImage(controlID){
    this.selectedCSS = "MultiImageBarElement_Selected";
    this.anchorCSS = "MultiImageBarElement"
    this.ControlID = controlID;
    this.Image = document[this.ControlID+"_ImageTag"];
    this.sources = new Array();
    this.alts = new Array();
    this.BarElements = document.getElementsByName(this.ControlID +"_barElement");
    this.currentIndex = 0;
    this.nextAnchor = document.getElementById(this.ControlID+"_anchorNext");
    this.prevAnchor = document.getElementById(this.ControlID+"_anchorPrev");
    this.ShowImage = 
    function showImage(imageIndex){
        if(this.Image == null){
            this.Image = document[this.ControlID+"_ImageTag"];
        }
        if(this.Image == null ){
            throw "MultiImage : Cannot Find Image Tag (could be because of browser)";
        }
       
       
       this.Image.src = this.sources[imageIndex];
       this.Image.alt = this.alts[imageIndex];
       
       this.Image.width = 145
       this.Image.height = 111
    }
                        
   this.incrementBar = 
   function(){
        this.currentIndex = Math.min(this.currentIndex+1,this.sources.length-1) ;
        this.arrangeBarElements();
        this.ShowImage(this.currentIndex);
    }
    
    this.decrementBar = 
    function decrementBar(){
        this.currentIndex = Math.max(this.currentIndex-1,0) ;
        this.arrangeBarElements();
        this.ShowImage(this.currentIndex)
    }                
    
    this.arrangeBarElements = function arrangeBarElements(){
       var numArray = new Array();
       numArray.push(this.currentIndex+1)
       var i=this.currentIndex-1;
       var iterations = 0;
       var maxIterations = parseInt(this.BarElements.length/2);
       while(i >= 0 && iterations < maxIterations ){
            numArray.unshift(i+1);
            iterations++;
            i--;
       }
       
       i=this.currentIndex+1;
       maxIterations = this.BarElements.length - iterations - 1;
       //alert("numArray =" + numArray.toString() + "\niterations ="+iterations + "\nmaxIterations = " + maxIterations   );
       iterations = 0;
       
       while(i < this.sources.length  && iterations < maxIterations ){
            numArray.push(i+1);
            iterations++;
            i++;
       }
       while(iterations < maxIterations ){
            numArray.unshift(numArray[0]-1);
            iterations++;
       }
       
       for(var i=0; i<this.BarElements.length;i++){
            this.BarElements[i].innerHTML = numArray[i];
            this.BarElements[i].className = (numArray[i]-1 == this.currentIndex )?this.selectedCSS:this.anchorCSS;
       }
       
      this.setArrows();
    }
    
    this.setArrows = function setArrows(){
        if(this.currentIndex == this.sources.length-1){
        this.disableNext();
        }else{ 
        this.enableNext();
        }
        
       if(this.currentIndex == 0){
        this.disablePrev();
       }else{
        this.enablePrev();
       }
    }    
    this.disableNext = function disableNext(){
        hideElement(this.nextAnchor.firstChild);
        this.nextAnchor.href = "";
    }
    this.enableNext = function disableNext(){
        showElement(this.nextAnchor.firstChild);
        this.nextAnchor.href = "javascript:"+this.ControlID +".incrementBar();" ;
    }
    
    this.disablePrev = function disablePrev(){
        hideElement(this.prevAnchor.firstChild);
        this.prevAnchor.href = "";
    }
     this.enablePrev = function disablePrev(){
        showElement(this.prevAnchor.firstChild);
        this.prevAnchor.href = "javascript:"+this.ControlID +".decrementBar();" ;
    }
    this.selectBarElement = function selectBarElement(i){
        for(var j=0 ;j<this.BarElements.length;j++){
                this.BarElements[j].className = (j == i)?this.selectedCSS:this.anchorCSS;
        }
        this.currentIndex=parseInt(this.BarElements[i].innerHTML)-1;
        this.ShowImage(this.currentIndex);
        this.setArrows();
    }
    this.setArrows();
}
//----------------------------------------------------------------------
