﻿function Carousel(IsSliding, GlvClientId, GlvId, PagerOptions, SlidingOptions){
    this.isSliding = IsSliding;
    
    this.glvClientId = GlvClientId;
    this.glvId = GlvId;
    
    this.currentSlideNumber = PagerOptions.startPage;
    
    //this.totalItems = PagerOptions.totalItem;
    
    if(this.isSliding){
        //var pagingOptions = {"startPage" : 1, "nbPages":2, "nb" : [1, 6], "pagerInfo": ["Showing  ", "  of  ", "  items"]};
        this.totalItems = 0;
        this.nbPages = PagerOptions.nbPages;
        for(var i = 0 ; i < PagerOptions.nb.length; i++) {this.totalItems += PagerOptions.nb[i];}
        this.nbItemOnPages = PagerOptions.nb;
    }
    else{
        //var pagingOptions = {"startPage" : 1, "nbPages" : 4, "totalItem" : 16, "maxItemPerPage" : 4, "pagerInfo": ["Showing  ", "  of  ", "  items"]};
        this.totalItems = PagerOptions.totalItem;
        this.pagerSize = PagerOptions.maxItemPerPage; 
        if(null !== document.getElementById(this.glvClientId + '_hfTotalElts')){
            this.totalItems = parseInt(document.getElementById(this.glvClientId + '_hfTotalElts').value);
            this.nbPages = Math.floor(this.totalItems / this.pagerSize) + ((this.totalItems % this.pagerSize) == 0 ? 0 : 1);
        }
               
        this.lastPagerSize = this.totalItems - ((this.nbPages-1) * this.pagerSize);
    }
        
    /* Elements of carousel */
    this.leftArrow = null;
    this.rightArrow = null;
    this.pageNavigation = [];
    this.slides = [];
    this.pagingInfo = null;
    
    this.showPagerInfo = false;
    if('' != PagerOptions.pagerInfo[0]){
        this.pagingInfoPart1 = PagerOptions.pagerInfo[0];
        this.pagingInfoPart2 = PagerOptions.pagerInfo[1];
        this.pagingInfoPart3 = PagerOptions.pagerInfo[2];
        
        this.showPagerInfo = true;
    }
    
    if(this.isSliding ){this.currentNavButtonBaseId = 'PageNavigationSubhead' + this.glvId ;}
    else {this.currentNavButtonBaseId = this.glvClientId + '_PageNavigation' + this.glvId ;}
    
    if( this.isSliding){
        //var SlidingOptions = {"WrapperId" : "Carousel_glv0", "WrapperWidth" : 720, "AnimationDuration" : 200, "FramePerSeconds" : 50};
        
        this.wrapper = document.getElementById(SlidingOptions.WrapperId );
        this.wrapperWidth = SlidingOptions.WrapperWidth;
        this.animationDuration = SlidingOptions.AnimationDuration;
        this.framePerSeconds = SlidingOptions.FramePerSeconds;
        this.framePerMilliseconds = this.framePerSeconds / 1000;
        this.delayBetweenFrames = (1/this.framePerMilliseconds);
        this.nbFrame = this.animationDuration / this.delayBetweenFrames ;
        this.loopSlideScale = this.wrapperWidth / this.nbFrame;
    }
    
    this.manageArrow = function(){
        if(this.currentSlideNumber == this.nbPages ) { this.rightArrow.style.visibility='hidden'; }
        else {this.rightArrow.style.visibility='visible'; }
        
        if(this.currentSlideNumber == 1)  { this.leftArrow.style.visibility='hidden';}
        else { this.leftArrow.style.visibility='visible'; }  
    };
    
    this.getPagingInfo = function(){
        var beginnavigation, endnavigation;
        if(this.isSliding){
            beginnavigation = 0;
            for(var i = 0; i < this.currentSlideNumber - 1; i++) {beginnavigation += this.nbItemOnPages[i];}
            beginnavigation += 1;
            endnavigation = beginnavigation + this.nbItemOnPages[this.currentSlideNumber - 1] - 1;
        }
        else{
            //alert('currentSlide ' +  this.currentSlideNumber);
            beginnavigation = (( this.currentSlideNumber - 1) * this.pagerSize ) + 1;  
            endnavigation = this.currentSlideNumber  * this.pagerSize ; 
            if(endnavigation > this.totalItems) {
                endnavigation = this.totalItems;    
            }
        }
        return this.pagingInfoPart1 + beginnavigation + '-' + endnavigation + this.pagingInfoPart2 + this.totalItems + this.pagingInfoPart3; 
    };
    
    this.managePagingInfo = function(){
        if(this.showPagerInfo){
            var lbl = this.getPagingInfo();
            if(this.isSliding){
                this.pagingInfo[0].innerHTML = lbl;
                this.pagingInfo[1].innerHTML = lbl;
            }
            else{
                this.pagingInfo.innerHTML = lbl;
            }
        }
    };
    
    this.showSlideStatic = function(idToShow){
        for(var i = 1; i <=  this.nbPages; i++){
            if(this.pageNavigation[i - 1]) this.pageNavigation[i - 1].className = 'PageNavigation';
            if(this.slides[i - 1]) this.slides[i - 1].style.display='none';
        }
        if(this.pageNavigation[idToShow - 1]) this.pageNavigation[idToShow - 1].className = 'CurrentPage';
        if(this.pageNavigation[idToShow - 1]) this.slides[idToShow - 1].style.display='block';
        this.currentSlideNumber = idToShow;
        this.manageArrow();
        this.managePagingInfo();
    };
    
    this.showNextSlideStatic = function(){
        var nextId = parseInt(this.currentSlideNumber) + 1;
        this.showSlideStatic(nextId);
    };
    
    this.showPreviousSlideStatic = function(){
        this.showSlideStatic(this.currentSlideNumber - 1);
    };
    
    this.slideElement = function(slideToNext, elt, leftLoop){
        if(elt !== null){
            var carousel  = this;
            var newLeft = parseInt(elt.style.left.replace('px', ''), 10);
            if(slideToNext){newLeft = newLeft - this.loopSlideScale; }
            else {newLeft = newLeft + this.loopSlideScale;}
            var mySLeftValue = newLeft.toString() + 'px';
            elt.style.left = mySLeftValue; 
            if( leftLoop > 1) {
                var tmpLoop = leftLoop -1;
                var slideDel = function(){
                    carousel.slideElement(slideToNext, elt, tmpLoop);
                };
                window.setTimeout( slideDel, this.delayBetweenFrames);
            }
        }
    };//function slideElt()
    
    this.showSlideSliding = function(idToShow){
        
        if(this.currentSlideNumber != idToShow ){
            if(this.currentSlideNumber > idToShow){ //go to previous
                while( this.currentSlideNumber > idToShow ){
                    this.showPreviousSlideSliding();
                }
            }
            else{//go to next
                while( this.currentSlideNumber < idToShow ){
                    this.showNextSlideSliding();
                }
            }
        }
        
        for(var i = 0; i < this.nbPages; i++){
            this.pageNavigation[i].className = 'PageNavigation';
        }
        this.pageNavigation[idToShow - 1].className = 'CurrentPage';
        this.currentSlideNumber = idToShow;
        this.manageArrow();
        this.managePagingInfo();
    };
    
    this.showNextSlideSliding = function(){
        this.slideElement(true, this.wrapper, this.nbFrame);
        this.currentSlideNumber++;
        for(var i = 0; i < this.nbPages; i++){
            this.pageNavigation[i].className = 'PageNavigation';
        }
        this.pageNavigation[this.currentSlideNumber - 1].className = 'CurrentPage';
        this.manageArrow();
        this.managePagingInfo();
    };
    
    this.showPreviousSlideSliding = function(){
        this.slideElement(false, this.wrapper, this.nbFrame);
        this.currentSlideNumber--;
        for(var i = 0; i < this.nbPages; i++){
            this.pageNavigation[i].className = 'PageNavigation';
        }
        this.pageNavigation[this.currentSlideNumber - 1].className = 'CurrentPage';
        this.manageArrow();
        this.managePagingInfo();
    };
    
    this.addListener = function(Elt, EventName, functionName) {
     try{
        if (Elt.addEventListener) { Elt.addEventListener(EventName, functionName, false); }
        else if (window.attachEvent) {   Elt.attachEvent("on" + EventName, functionName); } 
     }
     catch(error){
        
     }

    }; //function addListener  
    
    this.addListenerToNav = function(Elt, carousel){        
       var navButtonId = Elt.id.replace(carousel.currentNavButtonBaseId, "");
       var showSlideDel = function(currentId){
            if(carousel.isSliding) {carousel.showSlideSliding(navButtonId);}
            else {carousel.showSlideStatic(navButtonId);}
            return false;
       };
       carousel.addListener(Elt, 'click', showSlideDel );    
    };
    
    this.init = function(){
        var me = this;
        var previousClickDel = function(){
            if(me.isSliding){me.showPreviousSlideSliding(); } 
            else{ me.showPreviousSlideStatic(); } 
            return false;
        };
    
        var nextClickDel = function(){
            if(me.isSliding){  me.showNextSlideSliding();} 
            else{ me.showNextSlideStatic(); } 
            return false;
        }; 
        this.leftArrow = document.getElementById(this.glvClientId + '_ileftArrowFeaturedItems' + this.glvId );
        this.addListener(this.leftArrow, 'click', previousClickDel);
        this.rightArrow = document.getElementById(this.glvClientId + '_irightArrowFeaturedItems' + this.glvId );
        this.addListener(this.rightArrow, 'click', nextClickDel);                
        
        for(var i = 1; i <=  this.nbPages; i++){
            var curId = this.currentNavButtonBaseId + i.toString();
            var currentNavButton = document.getElementById(curId); 
             
            if(currentNavButton){
                this.pageNavigation.push(currentNavButton);
                this.addListenerToNav(currentNavButton, me);
                this.slides.push(document.getElementById(this.glvClientId + '_slide' + this.glvId  + i.toString()));
            }
        }
        this.manageArrow();
       
        if(this.isSliding){
            this.pagingInfo = [];
            this.pagingInfo.push(document.getElementById('lPagingInfoHeader' + this.glvId));
            this.pagingInfo.push(document.getElementById('lPagingInfoFooter' + this.glvId));
        }
        else{
            this.pagingInfo = document.getElementById(this.glvClientId + "_lPagingInfo");
        }
        
        this.managePagingInfo();
    };
    
    this.init();
}