﻿//<![CDATA[

Spec_Scroller = function() { }

Spec_Scroller.prototype.totNumProducts = 0;
Spec_Scroller.prototype.displayItems = 3;
Spec_Scroller.prototype.startIndex = 2;    // skip features col
Spec_Scroller.prototype.currentIndex = 0;

Spec_Scroller.prototype.currentPanel = null;
Spec_Scroller.prototype.visualIndex = null;

Spec_Scroller.prototype.btnPrev = null;
Spec_Scroller.prototype.btnNext = null;

Spec_Scroller.prototype.running = false;

Spec_Scroller.prototype.init = function(panelSelector, productsNo) {
    var s = this;

    this.totNumProducts = productsNo;
    this.currentPanel = panelSelector;

    this.currentIndex = this.startIndex;

    if (this.totNumProducts < this.displayItems) 
        this.displayItems = this.totNumProducts;

    // init visual Index
    this.visualIndex = $(s.currentPanel + ' .visualIndex').html('');

    for (var i = 0; i < s.totNumProducts; i++) {

        var circle = $('<img />')
            .attr('id', 'index_' + (s.startIndex + i))
            .attr('src', '/_common/img/circle_empty.gif')
            .attr('alt', '');

        s.visualIndex.append(circle).append('&nbsp;');
    }

    this.setVisualIndex();

    // hide products
    for (var i = s.startIndex + s.displayItems; i <= s.totNumProducts + 1; i++)
        $('.tbl-spec th:nth-child(' + i + '), .tbl-spec td:nth-child(' + i + ')').hide();

    // 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();
};

Spec_Scroller.prototype.showNext = function() {
    var s = this;

    this.running = true;
    
    this.hideColumn(this.currentIndex);
    this.showColumn(this.currentIndex + this.displayItems);

    this.running = false;
    
    this.currentIndex++;

    this.setupButtons();
};

Spec_Scroller.prototype.showPrev = function() {
    var s = this;

    this.running = true;

    this.hideColumn(this.currentIndex + this.displayItems - 1);
    this.showColumn(this.currentIndex - 1);

    this.running = false;
    
    this.currentIndex--;

    this.setupButtons();
};

Spec_Scroller.prototype.hideColumn = function(index) {
    $('.tbl-spec th:nth-child(' + index + '), .tbl-spec td:nth-child(' + index + ')').hide();
};

Spec_Scroller.prototype.showColumn = function(index) {
    $('.tbl-spec th:nth-child(' + index + '), .tbl-spec td:nth-child(' + index + ')').show();
};

Spec_Scroller.prototype.btnPrevClick = function() {
    var s = this;

    if (!this.running) {
        this.showPrev();
        this.setVisualIndex();
    } /*else {
        setTimeout(function() {
            s.btnPrevClick();
        }, 200);
    }*/

    return false;
};

Spec_Scroller.prototype.btnNextClick = function() {
    var s = this;

    if (!this.running) {
        this.showNext();
        this.setVisualIndex();
    } /*else {
        setTimeout(function() {
            s.btnNextClick();
        }, 200);
    }*/

    return false;
};

Spec_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');
    }
};

Spec_Scroller.prototype.setupButtons = function() {
    var s = this;
    
    if ((this.currentIndex + 1) >= this.totNumProducts || 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);
    }
};

Spec_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');
    }
};

Spec_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');
    }
}

//]]>