/*!
 * Custom Scrollbar
 * 
 * http://www.atrivis.de/
 * Copyright 2010, atrivis GmbH 
 * Author: Bettina Mohr
 * 
 * Date: Fri Jul 16 2010
 */
function Scrollbar(){
	
	var scrollerWidth = 20;
	var innerHeight = 0;
	var outerHeight = 0;
	var scrollAreaHeight = 0;
	var scrollKnobHeight = 0;
	var scrollHeight = 0;
	var scrollDownTimeout = null;
	var scrollUpTimeout = null;
	var scrollPx = 1; 
	var maxScroll = 0;
	
    this.init = function(secondPass){
    	(function($) {
    		innerHeight = $('#content').innerHeight();
			outerHeight = $('#bookPageRight').height();
			
			maxScroll = innerHeight - outerHeight;

    		if(maxScroll > 0 ) {
    			if(!secondPass) {
		    		self.showScrollbar();
	    		}
	    		innerHeight = $('#content').innerHeight();
				outerHeight = $('#bookPageRight').height();	  
				
				scrollAreaHeight = $('#scrollArea').height();
				$('#scrollKnob').height(Math.ceil(outerHeight / innerHeight * scrollAreaHeight));
				scrollKnobHeight = $('#scrollKnob').height();
				scrollHeight = innerHeight - outerHeight;
				
		    	self.initWheel();
				self.setScrollKnob();
    		}
    		
    		maxScroll = innerHeight - outerHeight;
    		
    		if(!secondPass) {
    			self.init(true);
    		}
    	})(jQuery);
    }
    
    this.initWheel = function() {
    	(function($) {
	    	$('#bookPageRight').bind('mousewheel', function(event, delta) {
	    		scrollbar.scrollByWheel(delta);
	    	});
    	})(jQuery);
    }
    
	this.showScrollbar = function(){
		(function($) {
	
				//set content width
				$('#bookPageRight').css('width', $('#bookPageRight').width() - scrollerWidth);
				// set overflow-y
				$('#bookPageRight').css('overflow-y', "hidden");
				//display scrollArea
				$('#scrollArea').css('display', "block");
				//display scrollArrows
				$('#arrowUp').css('display', "block");
				$('#arrowDown').css('display', "block");
				
				//register dragable
				$( "#scrollKnob" ).draggable({ 
					containment: 'parent',
					scroll: false,
					drag: function(event, ui) { scrollbar.drag(event, ui) }
				});

		})(jQuery);
    }
	this.checkScrolltop = function(scrollTop) {
		if(scrollTop <= 0){
			scrollbar.scrollUpStop();
			
			return 0;
		}
		else if(scrollTop >= maxScroll){
			self.scrollDownStop();
			
			return maxScroll;
		}
		else {
			window.clearTimeout(scrollDownTimeout);
			window.clearTimeout(scrollUpTimeout);
			return scrollTop;
		}
	}
	
	this.scrollByKnob = function(knobTop) {
		(function($) {
			var ratio = knobTop  / (scrollAreaHeight - scrollKnobHeight);

			$('#bookPageRight').scrollTop(scrollbar.checkScrolltop(Math.floor(scrollHeight * ratio)));
		})(jQuery);
	}
	
	this.setScrollKnob = function(delta){
		currentScrollTop = $('#bookPageRight').scrollTop();
		var ratio = currentScrollTop / scrollHeight;
		$('#scrollKnob').css('top', Math.floor(ratio * (scrollAreaHeight - scrollKnobHeight)));
	}
	
	this.scrollByWheel = function(delta) {
		(function($) {
			var currentScrollTop = $('#bookPageRight').scrollTop();
			$('#bookPageRight').scrollTop(scrollbar.checkScrolltop(Math.floor(currentScrollTop - 15 * delta)));
			currentScrollTop = $('#bookPageRight').scrollTop();
			
			var ratio = currentScrollTop / scrollHeight;
			$('#scrollKnob').css('top', Math.floor(ratio * (scrollAreaHeight - scrollKnobHeight)));
			
		})(jQuery);
	}
	
	this.scrollDown = function(isiDevice) {
		(function($) {
			
			if(isiDevice)
			scrollPx = 50;
			
			var currentScrollTop = $('#bookPageRight').scrollTop();
			$('#bookPageRight').scrollTop(scrollbar.checkScrolltop(Math.floor(currentScrollTop + Math.ceil(scrollPx))));
			currentScrollTop = $('#bookPageRight').scrollTop();
			
			var ratio = currentScrollTop / scrollHeight;
			$('#scrollKnob').css('top', Math.floor(ratio * (scrollAreaHeight - scrollKnobHeight)));
			
			scrollPx += 0.1;
			scrollDownTimeout = window.setTimeout("scrollbar.scrollDown()", 1);
			
		})(jQuery);
	}
	
	this.scrollDownStop = function() {
		(function($) {
			scrollPx = 1;
			window.clearTimeout(scrollDownTimeout);
		})(jQuery);
	}
	
    this.scrollUp = function(isiDevice) {
		(function($) {
			
			if(isiDevice)
			scrollPx = 50;
			
			var currentScrollTop = $('#bookPageRight').scrollTop();
			$('#bookPageRight').scrollTop(scrollbar.checkScrolltop(Math.floor(currentScrollTop - Math.ceil(scrollPx))));
			currentScrollTop = $('#bookPageRight').scrollTop();
			
			var ratio = currentScrollTop / scrollHeight;
			$('#scrollKnob').css('top', Math.floor(ratio * (scrollAreaHeight - scrollKnobHeight)));
			
			scrollPx += 0.1;
			scrollUpTimeout = window.setTimeout("scrollbar.scrollUp()", 1);
			
		})(jQuery);
	}
	
	this.scrollUpStop = function() {
		(function($) {
			scrollPx = 1;
			window.clearTimeout(scrollUpTimeout);
		})(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();
		$( "#bookOpen" ).click(function() {
		  /*
$( "#bookOpen" ).draggable();
*/
		});
	});

})(jQuery);
	



