var aUzScrollData = new Array();

//
// Presets
//

// Preset for scroll_when_press
//var uzListScrollMode = "pic_by_click";    // scroll_when_press|pic_by_click
//var uzScrollSpeedMs = 5;
//var uzScrollSpeedPx = 5;


// Preset for pic_by_click
var uzListScrollMode = "pic_by_click";    // scroll_when_press|pic_by_click
var uzScrollSpeedMs = 5;
var uzScrollSpeedPx = 10;

var uzListScroll = function(){

    this.CreateList = function(divId, ulId, scrollBtnIdLeft, scrollBtnIdRight){
        id = this.getNewItemId();

        var tmpObj = new uzListScroll();
        tmpObj.init(divId, ulId, scrollBtnIdLeft, scrollBtnIdRight);

        aUzScrollData[id] = tmpObj;

        return id;
    };

    this.getNewItemId = function(){
        return aUzScrollData.length;
    };

    this.init = function(divId, ulId, scrollBtnIdLeft, scrollBtnIdRight){
        this.scrollDiv = $('#'+divId);
        this.scrollUl = $('#'+ulId);
        this.scrollLeftBtn = $('#'+scrollBtnIdLeft);
        this.scrollRightBtn = $('#'+scrollBtnIdRight);

        this.scrollOffset = 0;
        this.scrollSpeedPx = uzScrollSpeedPx;
        this.scrollSpeedMs = uzScrollSpeedMs;
        this.scrollDirection = "";
        this.ulPadding = 0;

        this.id = this.getNewItemId();

        // Remove scrollbars
        this.scrollDiv.css({overflow: 'hidden'});

        this.calcScrollRange();

        // Dealyed re-calc scroll range for IE and Chrome
        setTimeout("aUzScrollData["+this.id+"].calcScrollRange()", 500);
        setTimeout("aUzScrollData["+this.id+"].calcScrollRange()", 1000);
        setTimeout("aUzScrollData["+this.id+"].calcScrollRange()", 1500);
        setTimeout("aUzScrollData["+this.id+"].calcScrollRange()", 2500);
        

        // add handlers
        if(uzListScrollMode == "scroll_when_press"){
            eval("this.scrollRightBtn.mousedown(function(e){ aUzScrollData["+this.id+"].startScrollRight(); });");
            eval("this.scrollRightBtn.mouseup(function(e){ aUzScrollData["+this.id+"].stopScrollRight(); });");
            eval("this.scrollLeftBtn.mousedown(function(e){ aUzScrollData["+this.id+"].startScrollLeft(); });");
            eval("this.scrollLeftBtn.mouseup(function(e){ aUzScrollData["+this.id+"].stopScrollLeft(); });");
        }
        if(uzListScrollMode == "pic_by_click"){
            eval("this.scrollRightBtn.click(function(e){ aUzScrollData["+this.id+"].scrollRight(); });");
            eval("this.scrollLeftBtn.click(function(e){ aUzScrollData["+this.id+"].scrollLeft(); });");
        }

        
        // Hide in print version
        /*
        if(typeof("active_module_link") != "undefined"){
            if(active_module_link.indexOf("_print_version=1") != -1){
                this.scrollDiv.hide();
            }
        }
        //*/
        

    };

    this.calcScrollRange = function(){
        // Get width
        this.divWidth = this.scrollDiv.width();


        // Find last image container
        this.lastLi = this.scrollUl.find('li:last-child');

        if(typeof(this.lastLi[0]) != 'undefined'){
            this.ulWidth = this.lastLi[0].offsetLeft + this.lastLi.outerWidth() + this.ulPadding;
        } else {
            this.ulWidth = 0;
        }

        this.scrollRange = this.ulWidth - this.divWidth;
        if(this.scrollRange < 0){
            this.scrollRange = 0;
        }

        // Enable/disable scrillong arrows by current scrolling position
        if(this.scrollOffset == 0){
            this.scrollLeftBtn.find("img.icon_str").attr("src", "_img/str_l1.gif");
        } else {
            this.scrollLeftBtn.find("img.icon_str").attr("src", "_img/str_l.gif");
        }

        if(this.scrollOffset == this.scrollRange){
            this.scrollRightBtn.find("img.icon_str").attr("src", "_img/str_r1.gif");
        } else {
            this.scrollRightBtn.find("img.icon_str").attr("src", "_img/str_r.gif");
        }
        //alert("scrollRange="+this.scrollRange+", scrollOffset="+this.scrollOffset);
    }

    this.startScrollLeft = function(){
        this.scrollDirection = "left";
        this.scroll();
    };

    this.stopScrollLeft = function(){
        this.scrollDirection = "";
    };

    this.startScrollRight = function(){
        this.scrollDirection = "right";
        this.scroll();
    };

    this.stopScrollRight = function(){
        this.scrollDirection = "";
    };

    this.scroll = function(){
        this.calcScrollRange();
        if(this.scrollDirection != ""){
            if(this.scrollDirection == "left"){
                this.scrollOffset = this.scrollOffset - this.scrollSpeedPx;
                if(this.scrollOffset < 0){
                    this.scrollOffset = 0;
                }
                this.scrollDiv.scrollLeft(this.scrollOffset);
            }

            if(this.scrollDirection == "right"){
                if(this.scrollRange > 0){
                    this.scrollOffset += this.scrollSpeedPx;
                    if(this.scrollOffset > this.scrollRange){
                        this.scrollOffset = this.scrollRange;
                    }
                }
                this.scrollDiv.scrollLeft(this.scrollOffset);
            }
            
            setTimeout("aUzScrollData["+this.id+"].scroll()", this.scrollSpeedMs);
        }
    };

    this.scrollLeft = function(){

        // Calc target scroll position
        var posL = 0;

        // Find image containers
        var aLi = this.scrollUl.find('li');

        // Get new left offset
        var i = aLi.length -1;
        for(var i = 0; i < aLi.length; i++){
            if(typeof(aLi[i]) != 'undefined'){
                if(aLi[i].offsetLeft < this.scrollOffset){
                    posL = aLi[i].offsetLeft;
                }
                //alert(aLi[i].offsetLeft);
            }
        }

        //alert(posL+", "+this.scrollOffset);
        this.scrollTo(posL);
    };

    this.scrollRight = function(){
        // Calc target scroll position
        var posR = 0;

        // Find image containers
        var aLi = this.scrollUl.find('li');

        // Get new left offset
        var i = aLi.length -1;
        for(var i = 0; i < aLi.length; i++){
            if(typeof(aLi[i]) != 'undefined'){
                if(aLi[i].offsetLeft > this.scrollOffset){
                    posR = aLi[i].offsetLeft;
                    break;
                }
                //alert(aLi[i].offsetLeft);
            }
        }

        //alert(posR);
        this.scrollTo(posR);
    };

    this.scrollTo = function(targetPos){
        this.calcScrollRange();

        // Validate target position
        if(targetPos > this.scrollRange){
            targetPos = this.scrollRange;
        }


        if(targetPos != this.scrollOffset){
            //alert(targetPos+" != "+this.scrollOffset);
            if(targetPos < this.scrollOffset){
                this.scrollOffset = this.scrollOffset - this.scrollSpeedPx;
                if(this.scrollOffset < targetPos){
                    this.scrollOffset = targetPos;
                }
                this.scrollDiv.scrollLeft(this.scrollOffset);
            }

            if(targetPos > this.scrollOffset){
                if(this.scrollRange > 0){
                    this.scrollOffset += this.scrollSpeedPx;
                    if(this.scrollOffset > this.targetPos){
                        this.scrollOffset = this.targetPos;
                    }
                }
                this.scrollDiv.scrollLeft(this.scrollOffset);
            }
            
            setTimeout("aUzScrollData["+this.id+"].scrollTo("+targetPos+")", this.scrollSpeedMs);
        }
    };

}

var uzListScrollEngine = new uzListScroll();

