/**
 * Copyright (c) 2009 Sylvain Gougouzian (sylvain@gougouzian.fr)
 * MIT (http://www.opensource.org/licenses/mit-license.php) licensed.
 * GNU GPL (http://www.gnu.org/licenses/gpl.html) licensed.
 * jQuery carrouselLite v0.1 by Sylvain Gougouzian http://sylvain.gougouzian.fr
 */
jQuery(function($){
    $.fn.carrouselLite = function(options){
    
        var el = this.eq(0).data("carrouselLite");
        var opts = $.extend({}, {
            speed: 2000,
            scroll: 1,
            dispTimeout: 1000,
            dispNumber: 3,
            mode: 'img',
            htmlPrevButton: "prev",
            htmlNextButton: "next",
            api: false
        }, options);
        this.each(function(){
            el = new $carrouselLite(this, opts);
        });
        return opts.api ? el : null;
        
    };
    
    $.carrouselLite = function(e, opts){
        this.e = $(e);
        $(e).css('position', 'relative').wrap('<div><div></div></div>');
        var self = this;
        var tA = new Date();
        this.id = ($(e).attr('id') != "" ? 'carrousel_' + tA.getTime() : $(e).attr('id'));
        this.container = $(e).parent();
        this.container.parent().css('overflow', 'hidden').append("<a href='#' id='" + this.id + "_prev' class='carrousel_button carrousel_prev'>" + opts.htmlPrevButton + "</a><a href='#' id='" + this.id + "_next' class='carrousel_button carrousel_next'>" + opts.htmlNextButton + "</a><div id='" + this.id + "_legend' class='carrousel_legend'></div>");
        $('a#' + this.id + '_prev').hide().click(function(){
            self._animate('prev');
            return false;
        });
        $('a#' + this.id + '_next').hide().click(function(){
            self._animate('next');
            return false;
        });
        this.aItems = null;
        this.nbItems = 0;
        this.current = 0;
        this.locked = false;
        this.block = {
            width: 0,
            height: 0
        };
        this.margin = {
            width: 0,
            height: 0
        };
        this.dep = 0;
        this.timerMoving = null;
        this.opts = opts;
        this.direction = 'next';
        this.pos = 'left';
        this.dir = 1;
        this.vertical = false;
        this._init();
    };
    var $carrouselLite = $.carrouselLite;
    $carrouselLite.fn = $carrouselLite.prototype = {
        carrouselLite: '1.1.7'
    };
    $carrouselLite.fn.extend = $carrouselLite.extend = $.extend;
    $carrouselLite.fn.extend({
        _init: function(){
            var self = this;
            this.aItems = $('> li', this.e);
            this.nbItems = this.aItems.length;
            this.nbItems = this.nbItems * 2;
            var item = this.aItems.eq(0);
            this.block.width = item.outerWidth(true);
            this.block.height = item.outerHeight(true);
            this.margin.width = this.block.width - item.outerWidth();
            this.margin.height = this.block.height - item.outerHeight();
            this.container.parent().addClass('carrousel').attr('id', this.id + '_div');
            var width = (this.vertical ? 1 : parseInt(this.opts.dispNumber)) * this.block.width;
            this.container.addClass('carrousel_carrousel').attr('id', this.id + '_carrousel').css({
                'overflow': 'hidden',
                'position': 'relative'
            }).width(width + 'px').height((this.vertical ? parseInt(this.opts.dispNumber) : 1) * this.block.height + 'px').parent().width(width + 'px');
            $('ul > li > ' + this.opts.mode, this.container).each(function(i){
                $(this).wrap('<div class="carrousel_item" rel="' + i + '"></div>');
            });
            this.e.css(this.pos, '0px').width(this.block.width * (this.vertical ? 1 : this.nbItems));
            $('.carrousel_item img', this.container).load(function(){
                $this = $(this);
                $this.parent().width($this.width()).height($this.height());
            });
        },
        _animate: function(dir){
            dir = (dir == undefined ? this.direction : dir);
            if (!this.locked) {
                clearTimeout(this.timerMoving);
                this.locked = true;
                this.dep = this.dep == 0 ? this.opts.scroll : this.dep;
                if (this.dir == -1) {
                    if (dir == "next") {
                        dir = "prev";
                    }
                    else {
                        dir = "next";
                    }
                }
                if (dir != 'next') {
                    this.dep *= -1;
                }
                var cont = true;
                if (dir == 'next') {
                    if (this.current == ((this.nbItems / 2) - this.opts.dispNumber)) {
                        cont = false;
                        this.locked = false;
                    }
                }
                else {
                    if (this.current == 0) {
                        cont = false;
                        this.locked = false;
                    }
                }
                if (cont) {
                    var self = this;
                    var size = this.block.width;
                    var b = parseInt(this.e.css(this.pos));
                    this.e.stop(true, true).animate({
                        left: b - this.dep * this.block.width + 'px'
                    }, this.opts.speed, this.opts.easing, function(){
                        self.current = self.current + self.dep;
                        if (self.current == -1) {
                            self.current = self.nbItems - 1;
                        }
                        else {
                            if (self.current == self.nbItems) {
                                self.current = 0;
                            }
                            else {
                                self.current = (self.current < (self.nbItems / 2) ? self.current : (self.current - (self.nbItems / 2)));
                            }
                        }
                        self.dep = 0;
                        self.locked = false;
                    });
                }
            }
        },
        next: function(){
            this._animate('next');
            return false;
        },
        prev: function(){
            this._animate('prev');
            return false;
        }
    });
    
});

