/*!
 * Custom Scrollbar
 * 
 * http://www.atrivis.de/
 * Copyright 2010, atrivis GmbH 
 * Author: Bettina Mohr
 * 
 * Date: Fri Jul 16 2010
 */
function Scrollbar(){
	
	var scrollerWidth = 50;
	var innerHeight = 0;
	var outerHeight = 0;
	var scrollAreaHeight = 0;
	var scrollKnobHeight = 0;
	var scrollHeight = 0;
	
    this.init = function(){
    	(function($) {
    		innerHeight = $('#content').innerHeight();
			outerHeight = $('#contentScroll').height();
    		if(innerHeight >= outerHeight ) {
	    		self.showScrollbar();
	    		innerHeight = $('#content').innerHeight();
				outerHeight = $('#contentScroll').height();	    		
				scrollAreaHeight = $('#scrollArea').height();
				scrollKnobHeight = $('#scrollKnob').height();
				scrollHeight = innerHeight - outerHeight;
		    	self.initWheel();
    		}
    	})(jQuery);
    }
    
    this.initWheel = function() {
    	(function($) {
	    	$('#contentWrapper').bind('mousewheel', function(event, delta) {
	    		scrollbar.scrollByWheel(delta);
	 
	    	});
    	})(jQuery);
    }
    
	this.showScrollbar = function(){
		(function($) {
	
				//set content width
				$('#contentScroll').css('width', $('#contentScroll').width() - scrollerWidth);
				// set overflow-y
				$('#contentScroll').css('overflow-y', "hidden");
				//display scrollArea
				$('#scrollArea').css('display', "block");
				
				//register dragable
				$( "#scrollKnob" ).draggable({ 
					containment: 'parent',
					scroll: false,
					drag: function(event, ui) { scrollbar.drag(event, ui) }
				});

		})(jQuery);
    }
	this.scrollByKnob = function(knobTop) {
		(function($) {
			var ratio = knobTop  / (scrollAreaHeight - scrollKnobHeight);
			$('#contentScroll').scrollTop(Math.floor(scrollHeight * ratio));
		})(jQuery);
	}
	
	
	this.scrollByWheel = function(delta) {
		(function($) {

			
			var currentScrollTop = $('#contentScroll').scrollTop();
			$('#contentScroll').scrollTop(Math.floor(currentScrollTop - 15 * delta));
			currentScrollTop = $('#contentScroll').scrollTop();
			
			var ratio = currentScrollTop / scrollHeight;
			$('#scrollKnob').css('top', Math.floor(ratio * (scrollAreaHeight - scrollKnobHeight)));
			
		})(jQuery);
	}
		
	this.drag = function(event, ui) {
		(function($) {
			scrollbar.scrollByKnob(ui.position.top);
		})(jQuery);
	}
    
    var self = this;
}

var scrollbar = new Scrollbar();



(function($) {
	
	$(document).ready(function(){
		scrollbar.init();
	});

})(jQuery);
	



