﻿/**
 * Magento Enterprise Edition
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Magento Enterprise Edition License
 * that is bundled with this package in the file LICENSE_EE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.magentocommerce.com/license/enterprise-edition
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    Varien
 * @package     js
 * @copyright   Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
 * @license     http://www.magentocommerce.com/license/enterprise-edition
 */

function horizScroller(opt){
    var d = 0;
    var container = $(opt.cont)
    var leftEl = $(opt.left)
    var rightEl = $(opt.right)
    
    
    
    opt.delay = opt.delay || 50;
    opt.dist = opt.dist || 1;
    leftEl.onmouseover = function() { d = -opt.dist; scroll(); }
    leftEl.onmouseout = function() { d = 0 }
    rightEl.onmouseover = function() { d = opt.dist; scroll(); }
    rightEl.onmouseout = function() { d = 0 }
    container.scrollLeft = 0;
    
    var slidesCount = container.getElementsBySelector('li').size()
    var sliderWidth = 0;
    for( var i = 0; i < slidesCount; i++ ){
        sliderWidth += container.getElementsBySelector('li')[i].getWidth()
    }
    container.down('ul').setStyle({'width': sliderWidth+'px'});
     
    function scroll(){
        if (!d) return;
        var x = container.scrollLeft+d, stop = false, rightBoundary = container.scrollWidth-container.offsetWidth;
        if (x<0) {
            addClassName(leftEl, 'disabled');
            x = 0;
            stop = true;
        } else {
            removeClassName(leftEl, 'disabled');
        }
        if (x>=rightBoundary) {
            addClassName(rightEl, 'disabled');
            x = rightBoundary;
            stop = true;
        } else {
            removeClassName(rightEl, 'disabled');
        }
        container.scrollLeft = x;
        if (stop) return;
        window.setTimeout(scroll, opt.delay);
    }

    function addClassName(el, className){
        el.className = el.className.replace(new RegExp(' ?'+className+'|$'), ' '+className);
    }
    
    function removeClassName(el, className){
        el.className = el.className.replace(new RegExp(' ?'+className+'( |$)'), '$1');
    }
}

