/** 
 * Copyright (c) 2010 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 moodular lite version: 0.1
 *
 * Requires: jQuery 1.1 	// http://www.jquery.com
 * Compatible : Internet Explorer 6+, Firefox 1.5+, Safari 3+, Opera 9+, Chrome 0.9+
 */
jQuery(function($){
    $.fn.moodular = function(options){
        var el = null;
        var opts = $.extend({}, $.fn.moodular.defaults, options);
        this.each(function(){
            el = new $moodular(this, opts);
        });
        return opts.api ? el : null;
    };
    $.moodular = function(e, opts){
        this.e = $(e);
        $(e).html($(e).html() + $(e).html());
        this.aItems = null;
        this.nbItems = 0;
        this.current = 0;
        this.locked = false;
        this.dep = 0;
        this.timerMoving = null;
        this.opts = opts;
        this._init();
    };
    var $moodular = $.moodular;
    $moodular.fn = $moodular.prototype = {
        moodular: '0.1-lite'
    };
    $moodular.fn.extend = $moodular.extend = $.extend;
    $moodular.fn.extend({
        _init: function(){
            var self = this;
            this.e.wrap('<div></div>');
            this.e.parent().css({
                'position' : 'relative',
                'overflow' : 'hidden',
                'width'	: this.e.width(),
                'height' : this.e.height()
            });
            this.e.css({
                'position' : 'absolute',
                'left' : '0px'
            });
            s = 0;
            $('> li', this.e).each(function() {
                $(this).width(self.e.width());
                s += parseInt($(this).width());
            });
            this.e.css('width', s + 'px');
            this.aItems = $('> li', this.e);
            this.nbItems = this.aItems.length;
            if (this.opts.auto) {
                setTimeout(function(){
                    self._animate('next');
                }, self.opts.dispTimeout);
            }
        },
        _animate: function(dir){
            dir = (dir == undefined ? "next" : dir);
            if (!this.locked) {
                this.locked = true;
                clearTimeout(this.timerMoving);
                this.dep = this.dep == 0 ? 1 : this.dep;
                if (dir != 'next') {
                    this.dep *= -1;
                }
                this._move();
            }
        },
        _move: function(){
            if (this.dep < 0) {
                var size = 0;
                for (i = 0; i < Math.abs(this.dep); i++) {
                    var item = $('> li:last', this.e);
                    size += parseInt(item.css('width'));
                    $('> li:last', this.e).remove();
                    this.e.prepend(item);
                }
                this.e.css('left', -size);
            }
            var self = this;
            var size = 0;
            var i;
            if (this.dep > 0) {
                for (i = 0; i < this.dep; i++) {
                    size += parseInt(this.aItems.eq(this._realpos(this.current) + i).width());
                }
            }
            if (this.e.css('left') == 'auto') this.e.css('left', 0);
            var dest = parseInt(this.e.css('left')) + (this.dep > 0 ? -1 : 1) * size;
            var tempo = size / this.opts.speed;
            var t = setInterval(function () {
                self.e.css('left', parseInt(self.e.css('left')) - (this.dep > 0 ? -1 : 1) * tempo + 'px')
                if (Math.abs(parseInt(self.e.css('left'))) >= size) {
                    clearInterval(t);
                    self._afterMoving();
                }
            }, tempo);
        },
        _afterMoving: function() {
            var i;
            if (this.dep > 0) {
                for (i = 0; i < this.dep; i++) {
                    var item = $('> li:first', this.e);
                    $('> li:first', this.e).remove();
                    this.e.append(item);
                }
                this.e.css('left', 0);
            }
            var self = this;
            this.current = this.current + this.dep;
            if (this.current == -1) {
                this.current = this.nbItems - 1;
            }
            else {
                if (this.current == this.nbItems) {
                    this.current = 0;
                }
                else {
                    this.current = this._realpos(this.current);
                }
            }
            this.dep = 0;
            this.locked = false;
            if (this.opts.auto) {
                this.timerMoving = setTimeout(function(){
                    self._animate('next');
                }, this.opts.dispTimeout);
            }
        },
        _realpos: function(i){
            if (i < 0) i = (this.nbItems / 2) - i;
            return (i < (this.nbItems / 2) ? i : (i - (this.nbItems / 2)));
        },
        reanimate: function() {
        	if (!this.opts.auto) {
                this.locked = false;
                this.opts.auto = true;
                var self = this;
                this.timerMoving = setTimeout(function(){
                    self._animate('next');
                }, this.opts.dispTimeout);
            }
        },
        start: function(){
            if (!this.opts.auto) {
                this.locked = false;
                this.opts.auto = true;
                this._animate('next');
            }
            return false;
        },
        stop: function(){
            clearTimeout(this.timerMoving);
            this.opts.auto = false;
            return false;
        },
        next: function(){
            this._animate('next');
            return false;
        },
        prev: function(){
            this._animate('prev');
            return false;
        }
    });
    $.fn.moodular.defaults = {
        auto: true,
        speed: 100,
        dispTimeout: 5000,
        api: false
    };
});

