/*
 * Галерея 1.0.0 (2011.07.15);
 * Copyright (c) Nullweb (http://www.nullweb.ru/projects/carousel/);
 */
(function($) {
    $.fn.carousel = function(o) {
        return this.each(function() {
            new $.carousel(this, o);
        });
    };
    // Настройка по умолчанию;
    var defaults = {
        width: 150,
        height: 150,
        size: 4,
        current: 1,
        time: 1000
    };
    $.carousel = function(e, o) {
        // Настройка галереи;
        this.options = $.extend(defaults, o);
        // Описание блоков галереи;
        var container = $('<div class="list-container"></div>');
        var content = $('<div class="list-content"></div>');
        var list = $(e);
        var item = $("div.list-item",$(e));
        var arrow_left = $('<div class="list-arrow-left"></div>');
        var arrow_right = $('<div class="list-arrow-right"></div>');
        // Переменные;
        var count = item.size();
        var size = (this.options.size > count) ? count : this.options.size;
        var current = this.options.current;
        var width = this.options.width;
        var height = this.options.height;
        var time = this.options.time;
        // Формирование оболочки галереи;
        content.append(arrow_left,arrow_right);
        list.after(content).detach().prependTo(content);
        content.after(container).detach().prependTo(container);
        // Оформление блоков;
        container.css({
            'width': width * size + 'px',
            'height': height + 'px',
            'position': 'relative'
        });
        content.css({
            'width': width * size + 'px',
            'height': height + 'px',
            'overflow': 'hidden'
        });
        list.css({
            'width': width * count + 'px',
            'height': height + 'px',
            'position': 'relative',
            'left': '-' + (width * (current - 1)) + 'px'
        });
        item.css({
            'width': width + 'px',
            'height': height + 'px',
            'float': 'left',
            'overflow': 'hidden'
        });
        arrow_left.css({
            'cursor': 'pointer'
        });
        arrow_right.css({
            'cursor': 'pointer'
        });
        // Управление слайдами;
        $(arrow_left, container).bind("click", function() {
            current = current - 1;
            move();
        });
        $(arrow_right, container).bind("click", function() {
            current = current + 1;
            move();
        });
        // Движение слайдов;
        function move() {
            // Обработка текущего слайда;
            if (current < 1) current = 1;
            if (current > (count - size + 1)) current = count - size + 1;
            // Обработка кнопок управления;
            if (current == 1) {
                arrow_left.addClass("disabled");
            } else {
                arrow_left.removeClass("disabled");
            }
            if (current == count - size + 1) {
                arrow_right.addClass("disabled");
            } else {
                arrow_right.removeClass("disabled");
            }
            // Анимация слайда;
            list.animate({ 'left': '-'+(width * (current - 1))+'px' }, time);
        }
        // Установка начальной позиции слайдов;
        move();
    };

})(jQuery);
