/**
* Slideshow function
*/
(function($) {
    $.fn.rzSlider = function(options) {
        var cfg = $.extend({}, $.fn.rzSlider.defaults, options);
        var slides = $(cfg.slider + ' > *').length;
        var current = 0;
        var autoInterval = false;
		
        
        /* Style or restyle slides */
        var rzStyle = function() {
            $(cfg.slider).css({
                width: cfg.slidesWidth * slides + 'px',
                left: '-' + ((current * $(cfg.slider).find(':first-child').width() - cfg.startPos) + 'px')
            });    
        };
        
        var updateButtons = function() {
            if ( cfg.cycle ) {
                if (slides <= 1) {
                    $(cfg.prev).addClass(cfg.hideClass);                
                    $(cfg.next).addClass(cfg.hideClass);
                }
                else {
                    $(cfg.prev).removeClass(cfg.hideClass);
                    $(cfg.next).removeClass(cfg.hideClass);
                }
            }
            else {
                if ( current == 0 || slides <= 1 ) {
                    $(cfg.prev).addClass(cfg.hideClass);
                }
                else {
                    $(cfg.prev).removeClass(cfg.hideClass);                
                }
                
                if ( current >= slides - cfg.stepBy ) {
                    $(cfg.next).addClass(cfg.hideClass);
                }
                else {
                    $(cfg.next).removeClass(cfg.hideClass);
                }
            
            }
        };
        
        /* Display legend of current item */
        var updateLegend = function() {
            if ( $(cfg.legend) ) {
                $(cfg.legend).not(":eq("+current+")").fadeOut(function(){ $(cfg.legend).eq(current).fadeIn(); })
            }
        };

        /* Disable auto slide */
        var disableAuto = function() {
            if (autoInterval) {
                clearInterval(autoInterval);
                autoIntverval = false;
            }        
        };
        
        /* Animation management */
        var rzAnimate = function(idx) {		
            var e = $(cfg.slider).find(":first-child");
            var left_val =  (-1 * ((idx * e.outerWidth() - cfg.startPos))) + 'px';
			
            $(cfg.slider).animate({
                left: left_val
            }, cfg.duration, 'easeInOutCubic');
        };
        
        /* Go to previous slide */
        var goPrevious = function() {
            if ( current == 0 && cfg.cycle == false ) {
                return false;
            }
                
            $(cfg.slider).stop();
        
            $('.list-box').hide();
        
            if ( current > 0 ) {
                current -= cfg.stepBy;
                updateButtons();
            } else {
                current = Math.floor((slides - 1) / cfg.stepBy) * cfg.stepBy;
            }
            rzAnimate(current);
            updateLegend();           
            
            return false;        
        };
        
        /* Go to next slide */
        var goNext = function() {
            if ( !cfg.auto && (current >= slides - cfg.stepBy) && cfg.cycle == false ) {
                return false;
            }
        
            $(cfg.slider).stop();

            $('.list-box').hide();
            
            if ( current < slides - cfg.stepBy ) {
                current += cfg.stepBy;
                updateButtons();
            }
            else {
                current = 0;
            }
            rzAnimate(current);            
            updateLegend();         
            
            return false;
        };
        
        /* Initialize slider */
        rzStyle();
        $(window).resize(function() {
            rzStyle();
        });
        updateLegend();
        updateButtons();
                
        /* Prev & next events */
        $(cfg.prev).click(function() {
            disableAuto();
            goPrevious();
            return false;
        });
        
        $(cfg.next).click(function() {
            disableAuto();
            goNext();            
            return false;
        });
        
        /* Auto slide */
        if ( cfg.auto )
            autoInterval = setInterval(goNext, 6000);

    };
    
    /* Slider defaults options */
    $.fn.rzSlider.defaults = {
        'slider': '.slider',
        'slidesWidth': 141,
        'stepBy': 5,
        'prev': '#prev a',
        'next': '#next a',
        'duration': 1500,
        'hideClass': 'hidden',
        'legend' : '',
        'auto': false,
        'cycle': false,
		'startPos' : 0
    };
})(jQuery);

/**
* Easing functions
*/
jQuery.extend(jQuery.easing,
{
    easeInOutCubic: function (x, t, b, c, d) {
        if ((t/=d/2) < 1) return c/2*t*t*t + b;
        return c/2*((t-=2)*t*t + 2) + b;
    }
});

 // Fix 10000 pixel bug in animate (jquery 1.4.4)
$.fx.prototype.cur = function(){
    if ( this.elem[this.prop] != null && (!this.elem.style || this.elem.style[this.prop] == null) ) {
      return this.elem[ this.prop ];
    }

    var r = parseFloat( jQuery.css( this.elem, this.prop ) );
    return typeof r == 'undefined' ? 0 : r;
}

