/*********
 *	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 Scrollbar(){

    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){
		$('scrollbarSliderWrapper').setStyle('display', 'block');
        //first, make sure scrollbarContent has overflow-y:hidden, so it will have the correct scrollHeight value
        $('scrollbarContent').setStyle('overflow-y', 'hidden');
        scrollbarHeight = $('scrollbarContent').getHeight();
        contentHeight = $('scrollbarContent').scrollHeight;
		
		//alert(scrollbarHeight);
		
        scrollbarSliderWrapper = $('scrollbarSliderWrapper');
        scrollbarSliderKnob = $('scrollbarSliderKnob');
        scrollbarSlider = $('scrollbarSlider');
        scrollbarContent = $('scrollbarContent');
        mouseWheelSensitiveElement = mouseWheelSensitiveElement2 || $(document.body);
       	scrollSpeed = scrollSpeed2 || 15;
		
        //alert(contentHeight + "-" + scrollbarHeight)
        //show scrollbar if neccessary
        if (contentHeight - scrollbarHeight > 0) {
			scrollbarSliderWrapper.setStyle('display', 'block');
			//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 = $('scrollbarSliderUp');
				scrollbarSliderDown = $('scrollbarSliderDown');
				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){
					scrollbar.slider.set(scrollbar.slider.step - 10);
				});
				
				scrollbarSliderDown.addEvent('mousedown', function(event){
					keepScrolling = true;
					self.waiterBottom();
				});
				
				scrollbarSliderDown.addEvent('click', function(event){
					scrollbar.slider.set(scrollbar.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('scrollbar.moveUp()', 500);
		}
	}

	this.waiterBottom = function() {

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

	
    this.moveUp = function(){
		if (keepScrolling) {
			this.slider.set(this.slider.step - scrollSpeed);
			window.setTimeout('scrollbar.moveUp()', 80);
        }

    }
	
    this.moveDown = function(){
		if (keepScrolling) {
        	this.slider.set(this.slider.step + scrollSpeed);
			window.setTimeout('scrollbar.moveDown()', 80);
        }
    }
	
    
    this.test = function(){
        alert("TEST!");
    }
    
    
    var self = this;
    
}

//var scrollbar = new Scrollbar();
var scrollbar = new Scrollbar();


window.addEvent('domready', function(){
    //only run if scrollbar is actually on page
	
	if ($('scrollbarWrapper'))
		scrollbar.init(true, $('scrollbarWrapper'), 10);
});


