﻿//<![CDATA[

Models_Scroller = function() { }

Models_Scroller.prototype.totNumProducts = 0;
Models_Scroller.prototype.displayItems = 3;
Models_Scroller.prototype.startIndex = 0;
Models_Scroller.prototype.currentIndex = 0;

Models_Scroller.prototype.products = new Array();

Models_Scroller.prototype.currentPanel = null;
Models_Scroller.prototype.visualIndex = null;

Models_Scroller.prototype.btnPrev = null;
Models_Scroller.prototype.btnNext = null;

Models_Scroller.prototype.running = false;

Models_Scroller.prototype.init = function(panelSelector, productsNo) {
    var s = this;

    this.totNumProducts = productsNo;
    this.currentPanel = panelSelector;

    this.currentIndex = this.startIndex;    

    // init visual Index
    this.visualIndex = $(s.currentPanel + ' .visualIndex').html('');
    var i = 0;

    // init products
    $(s.currentPanel + ' .product-item').each(function() {
        s.products[s.products.length] = $(this);

        var circle = $('<img />')
            .attr('id', 'index_' + (s.startIndex + i))
            .attr('src', '/_common/img/circle_empty.gif')
            .attr('alt', '');

        s.visualIndex.append(circle).append('&nbsp;');

        i++;
    });

    this.setVisualIndex();

    // init buttons
    this.btnPrev = $(s.currentPanel + ' .prev');
    this.btnNext = $(s.currentPanel + ' .next');

    this.btnPrev.click(function() { return s.btnPrevClick(); });
    this.btnNext.click(function() { return s.btnNextClick(); });

    this.setupButtons();
};

Models_Scroller.prototype.showNext = function() {
    var s = this;

    this.running = true;

    /*s.products[s.currentIndex].animate({
        marginLeft: '-187px'
    }, 500, function() {
        s.running = false;
    });*/

    $('.pnl-models-list').animate({
        marginLeft: '-=188px'
    }, 500, function() {
        s.running = false;
    });

    this.currentIndex++;

    this.setupButtons();
};

Models_Scroller.prototype.showPrev = function() {
    var s = this;

    this.running = true;

    /*s.products[s.currentIndex - 1].animate({
        marginLeft: '0px'
    }, 500, function() {
        s.running = false;
    });*/

    $('.pnl-models-list').animate({
        marginLeft: '+=188px'
    }, 500, function() {
        s.running = false;
    });

    this.currentIndex--;

    this.setupButtons();
};

Models_Scroller.prototype.btnPrevClick = function() {
    var s = this;

    if (!this.running) {
        this.showPrev();
        this.setVisualIndex();
    } /*else {
        setTimeout(function() {
            s.btnPrevClick();
        }, 200);
    }*/

    return false;
};

Models_Scroller.prototype.btnNextClick = function() {
    var s = this;
    
    if (!this.running) {
        this.showNext();
        this.setVisualIndex();
    } /*else {
        setTimeout(function() {
            s.btnNextClick();
        }, 200);
    }*/

    return false;
};

Models_Scroller.prototype.setVisualIndex = function() {
    var s = this;

    this.visualIndex.children('img')
        .attr('src', '/_common/img/circle_empty.gif');

    for (var i = s.currentIndex; i < s.currentIndex + s.displayItems; i++) {
        this.visualIndex.children('img#index_' + i)
            .attr('src', '/_common/img/circle_full.gif');
    }
};

Models_Scroller.prototype.setupButtons = function() {
    var s = this;

    if (this.currentIndex >= (this.totNumProducts - this.displayItems) || this.totNumProducts == 1) {
        this.setPrevBtn(false);
        this.setNextBtn(false);
    } else {
        this.setNextBtn(true);
    }

    if (this.currentIndex == this.startIndex) {
        this.setPrevBtn(false);
    } else {
        this.setPrevBtn(true);
    }
};

Models_Scroller.prototype.setPrevBtn = function(enable) {
    var s = this;

    if (enable) {
        $(this.btnPrev)
            .css('cursor', 'pointer')
            .unbind('click')
            .bind('click', function() { return s.btnPrevClick(); })
            .children('img').attr('src', '/_common/img/red_arrow_left.gif');
    } else {
        $(this.btnPrev)
            .css('cursor', 'default')
            .unbind('click')
            .bind('click', function() { return false; })
            .children('img').attr('src', '/_common/img/red_arrow_left_disabled.gif');
    }
};

Models_Scroller.prototype.setNextBtn = function(enable) {
    var s = this;

    if (enable) {
        $(this.btnNext)
            .css('cursor', 'pointer')
            .unbind('click')
            .bind('click', function() { return s.btnNextClick(); })
            .children('img').attr('src', '/_common/img/red_arrow_right.gif');
    } else {
        $(this.btnNext)
            .css('cursor', 'default')
            .unbind('click')
            .bind('click', function(e) { return false; })
            .children('img').attr('src', '/_common/img/red_arrow_right_disabled.gif');
    }
}

//]]>