/*********
 *	Scrollbar class
 *
 *	boolean hasUpDownButtons: if true, moveUp/moveDown will be added to click event
 *	element mouseWheelSensitiveElement (optional): element to be sensitive for mousewheel events. If not specified, mousewheel event will be applied to body
 *	integer scrollSpeed (optional): amount of steps to be made when scrolled by mousewheel or up/down buttons. If not specified, scrollSpeed is 15
 */
function Scrollbar2(){

    this.slider = null;
    var scrollbarHeight = 0;
    var contentHeight = 0;
    var scrollbarSliderWrapper = null;
    var scrollbarSliderKnob = null;
    var scrollbarSlider = null;
    var scrollbarContent = null;
    var mouseWheelSensitiveElement = null;
    var scrollSpeed = 0;
    var scrollbarSliderUp;
    var scrollbarSliderDown;
    var keepScrolling = false;
    var headlineHeight = 0;
	
    
    this.init = function(hasUpDownButtons, mouseWheelSensitiveElement2, scrollSpeed2){
              
        //first, make sure scrollbarContent has overflow-y:hidden, so it will have the correct scrollHeight value
        $('scrollbarContent2').setStyle('overflow-y', 'hidden');
        scrollbarHeight = $('scrollbarSliderWrapper2').scrollHeight;
        contentHeight = $('scrollbarContent2').scrollHeight;
		
        scrollbarSliderWrapper = $('scrollbarSliderWrapper2');
        scrollbarSliderKnob = $('scrollbarSliderKnob2');
        scrollbarSlider = $('scrollbarSlider2');
        scrollbarContent = $('scrollbarContent2');
        mouseWheelSensitiveElement = mouseWheelSensitiveElement2 || $(document.body);
       	scrollSpeed = scrollSpeed2 || 15;

        //show scrollbar if neccessary
        if (contentHeight - scrollbarHeight > 0) {
        
            //adjust scrollbarknobheight when using upDown buttons, so the knob doesn't overlap the buttons it certain cases
			if (hasUpDownButtons) {
				buttonHeights = $('scrollbarSliderUp').getHeight() + $('scrollbarSliderDown').getHeight();
				scrollbarSliderKnobHeight = scrollbarHeight / contentHeight * (scrollbarHeight - buttonHeights);
			}else 
				scrollbarSliderKnobHeight = scrollbarHeight / contentHeight * scrollbarHeight;
				
            scrollbarSliderKnob.setStyle('height', scrollbarSliderKnobHeight);
            
            this.slider = new Slider(scrollbarSlider, scrollbarSliderKnob, {
                steps: contentHeight - scrollbarHeight,
                offset: 0,
                mode: 'vertical',
                onChange: function(step){
                    scrollbarContent.scrollTop = step;
                }
            }).set(0);
            
            
            //add mousewheel event
            mouseWheelSensitiveElement.addEvent('mousewheel', function(event){
                event = new Event(event);

					if (event.wheel > 0) {
						self.wheelUp();
					}
					
					else {
					if (event.wheel < 0)
						self.wheelDown();
					}

			
			
			});
            
            
            //add up/down button events
            if (hasUpDownButtons) {
                scrollbarSliderUp = $('scrollbarSliderUp2');
                scrollbarSliderDown = $('scrollbarSliderDown2');
                scrollbarSliderUp.addEvent('mousedown', function(event){
                
                    keepScrolling = true;
                    self.waiterTop();
                });
                scrollbarSliderUp.addEvent('mouseup', function(event){
                    //TODO implement continuous scrolling on hold
                    keepScrolling = false;
                });
				
				scrollbarSliderUp.addEvent('click', function(event){
                    scrollbar2.slider.set(scrollbar2.slider.step - 10);
                });
				
                scrollbarSliderDown.addEvent('mousedown', function(event){
                    keepScrolling = true;
                    self.waiterBottom();
                });
				
				scrollbarSliderDown.addEvent('click', function(event){
                    scrollbar2.slider.set(scrollbar2.slider.step + 10);
                });
				
                scrollbarSliderDown.addEvent('mouseup', function(event){
                    //TODO implement continuous scrolling on hold
                    keepScrolling = false;
                });
            }
            
            /*TODO
             //add scrollEvent if content is scrolled through anchor tag
             //scrollbarContent.addEvent('scroll',function(event){
             
             console.log(scrollbarSliderKnob.getTop());
             console.log(Math.floor(scrollbarHeight * scrollbarHeight / contentHeight));
             console.log(contentHeight - scrollbarHeight);
             });*/
            //hide scrollbar
        }
        else 
            scrollbarSliderWrapper.setStyle('display', 'none');
        
    }
    
	this.wheelUp = function (){
		this.slider.set(this.slider.step - scrollSpeed);
	}
	
	this.wheelDown = function (){
		this.slider.set(this.slider.step + scrollSpeed);
	}
	
	
    this.waiterTop = function() {
		if (keepScrolling) {
			window.setTimeout('scrollbar2.moveUp()', 500);
		}
	}

	this.waiterBottom = function() {

		if (keepScrolling) {
			window.setTimeout('scrollbar2.moveDown()', 500);
		}
	}

	
    this.moveUp = function(){
		if (keepScrolling) {
			this.slider.set(this.slider.step - scrollSpeed);
			window.setTimeout('scrollbar2.moveUp()', 80);
        }
        else {
            this.slider.set(this.slider.step - scrollSpeed);
        }
    }
	
    this.moveDown = function(){
		if (keepScrolling) {
        	this.slider.set(this.slider.step + scrollSpeed);
			window.setTimeout('scrollbar2.moveDown()', 80);
        }
        else {
             this.slider.set(this.slider.step + scrollSpeed);
        }
    }
	   
    
    var self = this;
    
}


var scrollbar2 = new Scrollbar2();


window.addEvent('domready', function(){
    //set height of 
	
	if ($('leftText')) {
		scrollbar2.init(true, $('leftText'), 10);
	}
});

