/**
 * Handle horizontal scrolling lists
 */
var HScrollList = Class.create();
HScrollList.prototype = {
    /**
     * Constructor
     */
    initialize: function(list, scrollWidth) {
        this.listContainer = $(list);
        this.scrollWidth = scrollWidth;
        this.listContents = this.listContainer.down('.piclist');
        this.leftButton = this.listContainer.down('.leftscroll');
        this.rightButton = this.listContainer.down('.rightscroll');
        this.intervalId = null;
        this.fps = 12;
        
        if ( this.listContents ) {
            this.targetX = this.listContents.scrollLeft;
        } else {
            this.targetX = 0;
        }

        // set up any buttons
        if (this.leftButton) this.leftButton.observe('click', this.scrollLeft.bind(this));
        if (this.rightButton) this.rightButton.observe('click', this.scrollRight.bind(this));

        this.updateButtons();
    },

    /**
     * Scroll to the left
     */
    scrollLeft: function() {
        var newX = (this.targetX + this.scrollWidth) > (this.listContents.scrollWidth - this.listContents.clientWidth) ?
                   this.listContents.scrollWidth - this.listContents.clientWidth :
                   this.targetX + this.scrollWidth;
        this.setTargetX(newX);
        this.updateButtons();
    },

    /**
     * Scroll to the right
     */
    scrollRight: function() {
        var newX = this.targetX > this.scrollWidth ? this.targetX - this.scrollWidth : 0;
        this.setTargetX(newX);
        this.updateButtons();
    },

    /**
     * Update the scroll buttons
     */
    updateButtons: function() {
        // left button
        if (this.leftButton) {
            if ((this.listContents.scrollWidth - this.listContents.clientWidth) > this.targetX)
                this.leftButton.addClassName('enabledButton');
            else
                this.leftButton.removeClassName('enabledButton');
        }

        // right button
        if (this.rightButton) {
            if (this.targetX > 0)
                this.rightButton.addClassName('enabledButton');
            else
                this.rightButton.removeClassName('enabledButton');
        }
    },

    /**
     * Set the value of targetX for scrolling
     */
    setTargetX: function(x) {
        this.targetX = x;
        if (this.targetX != this.listContents.scrollLeft) {
            this.intervalId = window.setInterval(this.onTick.bind(this), 1000/this.fps);
        }
    },

    /**
     * Called periodically when animating the scroll
     */
    onTick: function() {
        // determine the distance between the location and the target
        var diff = this.targetX - this.listContents.scrollLeft;

        // values less than our tolerance stop the animation
        if ((diff > -2) && (diff < 2)) {
            this.listContents.scrollLeft = this.targetX;
            window.clearInterval(this.intervalId);
            this.intervalId = null;
        }

        // otherwise, move part way
        var delta = Math.floor(diff / 2);
        this.listContents.scrollLeft += delta;
    }
};

/**
 * Set up the scrolling lists
 */
function setupScrolls() {
    // #product_rotator no longer exists except in caches -- remove later:
    $$('#product_rotator li').each(function(item) {
        new HScrollList(item, 110);
    });
    $$('body.catalog-product-view .alternatives_list').each(function(item) {
        if ( item.down('td') ) {
            new HScrollList(item, item.down('td').offsetWidth);
        }
    });
}

/**
 * Open a popup window with larger images for a product
 */
function productZoom(productId, uid) {
    var url = '/product.php?action=zoom_images&id='+escape(productId)+(uid ? '&uid='+escape(uid) : '');
    var win = window.open(url, 'product_zoom', 'left=20,top=40,height=550,width=650,menubar=no,toolbar=no,directories=no,status=no,titlebar=yes,scrollbars=no,resizable=yes');
    if (win)
        win.focus();
}

Event.observe(window, 'load', setupScrolls);
