/*
Created by Baobaz, http://www.baobaz.com
*/


ProductsCarousel = Class.create({
    options: {
        mode: 'horizontal',
        fullscreen: false
    },
    initialize: function(carouselContainer, custom_options) {
        Object.extend(this.options, custom_options || {});
        this.carousel = carouselContainer;
        this.mode = (this.options.mode=='vertical') ? 1 : 0;        
        this.setDimensions(); 
        this.centerActive();
        this.addListeners();    
        this.updateNav();
    },
    
    setDimensions: function() {
        this.carouselUl = this.carousel.down('ul');
        this.carouselUlSize = this.carouselUl.childElements().length;
        this.carouselLi = this.carouselUl.down('li'); // change for CMS Newspaper (Barbara 28/07/2011) before : this.carouselLi = this.carousel.down('li'); 
        //if(this.options.fullscreen)
            //this.carouselLi.addClassName('item-active');
        if(this.mode) {
            this.carouselLiDimension = this.carouselLi.getHeight() 
                + parseInt(this.carouselLi.getStyle('marginTop')) 
                + parseInt(this.carouselLi.getStyle('marginBottom'));
            this.carouselAreaDimension = this.carousel.down('.view-area').getHeight();        
        }
        else {           
            this.carouselLiDimension = this.carouselLi.getWidth() 
                + parseInt(this.carouselLi.getStyle('marginRight')) 
                + parseInt(this.carouselLi.getStyle('marginLeft'));
            this.carouselAreaDimension = this.carousel.down('.view-area').getWidth();        
        } 
        this.carouselUlDimension = this.carouselLiDimension*this.carouselUlSize;
        this.prevBtn = this.carousel.down('span.prev-views');
        this.nextBtn = this.carousel.down('span.next-views');
        
        if(this.mode) {
            this.carouselUl.setStyle({height: this.carouselUlDimension + 'px'});              
        } 
        else {
            this.carouselUl.setStyle({width: this.carouselUlDimension + 'px'});      
        }       
    },
    
    centerActive: function() {
        if(this.options.fullscreen) {
            //this.initialOffset = Math.floor((document.viewport.getWidth() - this.carouselLiDimension)/2);
        	this.initialOffset = 195; //sidebar width
            this.carouselUl.setStyle({left: this.initialOffset + 'px'});
        }
        else
            this.initialOffset = 0;
    },
    
    addListeners: function() {
        this.prevBtn.observe('click', this.movePrev.bindAsEventListener(this));
        this.nextBtn.observe('click', this.moveNext.bindAsEventListener(this));    
    },
    
    movePrev: function() {
        if(this.prevBtn.hasClassName('prev-active')) {
            var moveX = 0, moveY = 0;
            var stepWidth = this.options.stepItems || 1;
            if(this.mode) {
                moveY = this.carouselLiDimension;
            }             
            else {
                moveX = this.carouselLiDimension * stepWidth;
            }           
        
            new Effect.Move(this.carouselUl, {
                x: moveX,
                y: moveY,
                mode: 'relative',
                duration: 0.4,
                queue: {
                    scope: 'carousel_scope',
                    limit: 1
                },
                afterFinish: function() {this.updateNav(); /*this.updateActive(true); removed due to Carousel changes*/}.bind(this)
            });
        }  
    },
    
    moveNext: function() {
        if(this.nextBtn.hasClassName('next-active')) {
            var moveX = 0, moveY = 0;
            var stepWidth = this.options.stepItems || 1;
            if(this.mode) {
                moveY = - this.carouselLiDimension;
            }             
            else {
                moveX = - this.carouselLiDimension * stepWidth;
            }           
        
            new Effect.Move(this.carouselUl, {
                x: moveX,
                y: moveY,
                mode: 'relative',
                duration: 0.4,
                queue: {
                    scope: 'carousel_scope',
                    limit: 1
                },
                afterFinish: function() {this.updateNav(); /*this.updateActive(false);*/}.bind(this)
            });
        }  
    },
    
    updateNav: function() {
        if(this.carouselUl.positionedOffset()[this.mode] >= this.initialOffset)
            this.prevBtn.removeClassName('prev-active');
        else
            this.prevBtn.addClassName('prev-active');
        
        if(this.carouselUl.positionedOffset()[this.mode] + this.carouselUlDimension <= this.carouselAreaDimension){
            this.nextBtn.removeClassName('next-active');
            //console.log(this.carouselUl.positionedOffset()[this.mode], this.carouselUlDimension, this.carouselAreaDimension, this.initialOffset);
        }
        else {
            this.nextBtn.addClassName('next-active');
        	//console.log(this.carouselUl.positionedOffset()[this.mode], this.carouselUlDimension, this.carouselAreaDimension, this.initialOffset);
        }
    },
    
    updateActive: function(isPrev) {
        if(this.options.fullscreen) {
            var activeLi = this.carouselUl.down('.item-active');
            if(isPrev) {
                var prevLi = activeLi.previous('li');
                if(prevLi) {
                    activeLi.removeClassName('item-active');
                    prevLi.addClassName('item-active');
                }
            }
            else {
                var nextLi = activeLi.next('li');
                if(nextLi) {
                    activeLi.removeClassName('item-active');
                    nextLi.addClassName('item-active');
                }        
            }
        }
    }
});


