﻿
/**
 * @author Dan Entous
 * @version 2010-03-26 18.00 GMT +1
 */

function Carousel( elm ) {
    
    this.margin =                0;
    this.attributes =            { marginTop:  { from: 30, to: 10, unit: 'px' } };
    this.carousel_container =    elm;
    this.carousel =              null;
    this.carousel_ul =           null;
    this.carousel_li =           null;
    this.carousel_li_max =       0;
    this.carousel_nav =          null;
    this.carousel_nav_next =     null;
    this.carousel_nav_prev =     null;

    this.initialize = function() {
        
        if ( YAHOO.util.Dom.get( this.carousel_container ) ) {
            
            this.carousel_container   = YAHOO.util.Dom.get( elm );
            this.carousel             = this.carousel_container.getElementsByClassName( 'carousel' );
            this.carousel_nav         = this.carousel_container.getElementsByClassName( 'carouselNav' );
            this.carousel_nav_next    = this.carousel_container.getElementsByClassName( 'navNext' );
            this.carousel_nav_prev    = this.carousel_container.getElementsByClassName( 'navPrev' );
            
            this.carousel_ul          = this.carousel_container.getElementsByTagName( 'ul' );
            this.carousel_li          = this.carousel_container.getElementsByTagName( 'li' );
            this.carousel_li_max      = ( this.carousel_li.length - 2 ) * -160;            
            
            if ( this.carousel_li.length <= 2 ) {
            
                YAHOO.util.Dom.setStyle(
                    this.carousel_nav,
                    'display',
                    'none'
                );
                
                if ( this.carousel_li.length == 1 ) {
                    
                    YAHOO.util.Dom.setStyle(
                    this.carousel,
                    'height',
                    '144px'
                );
                
                }
                
            } else {
            
                this.addListeners();
                
            }
        
        }
        
    };

    this.addListeners = function() {        
        
        var self = this;
        
    	YAHOO.util.Event.addListener( this.carousel_nav_prev, 'click', function(e) {
    	
            YAHOO.util.Event.preventDefault(e);
            self.attributes.marginTop.from = self.margin;
            
            if ( self.margin != 0 ) {
            	self.margin = self.margin + 160;
            }

            self.navPositions();

            self.attributes.marginTop.to = self.margin;
            self.anim = new YAHOO.util.Anim(
           		self.carousel_ul,
           		self.attributes,
                .3
            );
            
            self.anim.animate();
        });
        
        YAHOO.util.Event.addListener( this.carousel_nav_next, 'click', function(e) {
            
            YAHOO.util.Event.preventDefault(e);
            
            self.attributes.marginTop.from = self.margin;
            if ( self.margin != self.carousel_li_max ) {  
        	    self.margin = self.margin - 160;
            }
            
            self.navPositions();
            
            self.attributes.marginTop.to = self.margin;
            self.anim = new YAHOO.util.Anim(
       		    self.carousel_ul,
       		    self.attributes,
                .3
            );
            
            self.anim.animate();
            
        });
        
    };
    
    this.navPositions = function() {
        
        if ( this.margin == 0 ) {
        
            YAHOO.util.Dom.setStyle( this.carousel_nav_prev, 'background-position', '0 -2050px');
            YAHOO.util.Dom.setStyle( this.carousel_nav_next, 'background-position', '0 -800px');
            
        } else if( this.margin == this.carousel_li_max ) {
        
            YAHOO.util.Dom.setStyle( this.carousel_nav_prev, 'background-position', '0 -750px');
            YAHOO.util.Dom.setStyle( this.carousel_nav_next, 'background-position', '0 -2100px');
            
        } else {
        
            YAHOO.util.Dom.setStyle( this.carousel_nav_prev, 'background-position', '0 -750px');
            YAHOO.util.Dom.setStyle( this.carousel_nav_next, 'background-position', '0 -800px');
            
        }
        
    };
    
}
