﻿/*
  Scroller class
  
  Created: ML from SenseNet support
  Date: 2011.11.29
    
    Usage:
        required:
            1 set ulElem throught setUlElem
            2 set rigthButton throught setRightButton
            3 set leftButton throught setLeftButton
            4 set showedWidth throught showedWidth
            5 call refreshButtons()
        optional:
            speed
            scrollStep
            rightCorrection
            
    Example:
        var slider = new UlSlider();
        var elem = $('.toplistRatings').first().find('#menuUl').first();
        slider.setUlElem(elem);
        slider.showedWidth = $('.toplistRatings').first().width();
        var leftButton =  $('.toplistRatings').first().find('#sleft').first();
        var rightButton = $('.toplistRatings').first().find('#sright').first();
        slider.setRightButton(leftButton);
        slider.setLeftButton(rightButton);
        slider.refreshButtons();
        
    //----------------------
  Changeset:

    //----------------------
*/

function UlSlider() {
    this.test;
  /*variables*/
  var _rightButton;
  var _leftButton;
  //ul elem to scroll
  var _ulElem;
  
  //actual visible width
  this.showedWidth = 0;

  //
  this.rightCorrection = 0;
  
  //scroll speed
  this.speed = 0.4;
  this.scrollStep = 5;

  /*privates*/
  //whole width
  this.sumWidth = 0;
  //scroll direction
  this.direction;
  //true if scroll has to stop
  this.stopScroll;
}
UlSlider.prototype.getLeftButton = function(){
    return this._leftButton;
};
UlSlider.prototype.setLeftButton = function(val) {
    this._leftButton = val;
    $(this._leftButton).bind("mouseenter", { obj: this }, function(event) {

        var obj = event.data.obj;
        obj.canScroll = true;
        obj.direction = '+';
        obj.stopScroll = false;

        obj.scrollUl();

    }).bind("mouseleave", { obj: this }, function(event) {
        var obj = event.data.obj;
        obj.stopScroll = true;
    });
};
UlSlider.prototype.getRightButton = function(){
    return this._rightButton;
};
UlSlider.prototype.setRightButton = function(val) {
    this._rightButton = val;
    $(this._rightButton).bind("mouseenter", { obj: this }, function(event) {
        var obj = event.data.obj;
        obj.canScroll = true;
        obj.direction = '-';
        obj.stopScroll = false;
        obj.scrollUl();

    }).bind("mouseleave", { obj: this }, function(event) {
        var obj = event.data.obj;
        obj.stopScroll = true;
    });
};
UlSlider.prototype.getUlElem = function(val){
    return this._ulElem;
}
UlSlider.prototype.setUlElem = function(val) {
    this._ulElem = val;
    this.sumWidth = 0;
    var w = 0;
    $(this._ulElem).find('li').each(function() {
        w += $(this).outerWidth();
    });
    this.sumWidth = w;
}
/*getter settters*/

/*-------------------------------*/
UlSlider.prototype.refreshButtons = function() {
    //hide buttons if doesn't need (fit)
    if (this.showedWidth >= this.sumWidth) {
        $(this._leftButton).hide();
        $(this._rightButton).hide();
        return false;
    }

    //get actual offset
    var marginLeft = $(this._ulElem).css('margin-left');
    if (marginLeft != null && marginLeft != '')
        marginLeft = parseInt(marginLeft.replace('px', ''));
    else
        marginLeft = 0;

    //if completely right
    if (marginLeft >= 0) {
        if (this.direction == '+') {
            this.canScroll = false;
        }
        //set margin left to 0px
        $(this._ulElem).css('margin-left', '0px');
        //set buttons visibility
        $(this._leftButton).hide();
        $(this._rightButton).show();
    }
    //if completely left
    else if (marginLeft <= (-(this.sumWidth - this.showedWidth) - this.rightCorrection)) {
        if (this.direction == '-') {
            this.canScroll = false;
        }
        //set margin left
        $(this._ulElem).css('margin-left', -(this.sumWidth - this.showedWidth) - this.rightCorrection + 'px');
        //set buttons visibility
        $(this._leftButton).show();
        $(this._rightButton).hide();
    }
    else {
        //set buttons visibility
        $(this._leftButton).show();
        $(this._rightButton).show();
    }

    return true;
}
UlSlider.prototype.scrollUl = function() {


    if (!this.refreshButtons())
        return;

    if (this.canScroll == true) {
        if (this.stopScroll) {
            //stop scroll
            this.canScroll = false;
            this.stopScroll = false;
        }
        else {
            //scroll
            var o = this;
            $(this._ulElem).animate({ 'margin-left': this.direction + '=' + this.scrollStep }, this.speed, 'linear',
        function() {
            o.scrollUl();
        }
      );
        }
    }
    else
        this.stopScroll = false;

}
