jQuery.noConflict();
//Constants
var scrollerTopMargin=2;
var scrollerBottomMargin=365;
//Variables
var mX=0;
var mY=0;
var contentScrolling=0;
var contentScrollSpeed=24;
var contentScrollingEnabled=false;
var constPX=20; //used to move the #scrolling element that much px further than calculated for fine tuning.
var dontScroll=false;
jQuery(function(jQuery) {
	jQuery("#content").bind('mousewheel', function(event, delta) {
		if(!contentScrollingEnabled) return true;
		    if (delta > 0){
				contentDir=1;
				contentMousewheelScroll();
			}
		    else if (delta < 0){
				contentDir=-1;
				contentMousewheelScroll();
			}
			return false;
		});
});
function contentMousewheelScroll(){
	var speed=contentScrollSpeed;
	if(contentDir>0)
	{
		speed*=-1;
	}
	incScroll(speed);
}
function fixScroll(){
	st=document.getElementById('content').scrollTop;
	
	var ratio=-jQuery('.scrollable').outerHeight()+jQuery('#scrolling').outerHeight()+constPX;
	ratio=ratio/(scrollerBottomMargin-scrollerTopMargin);
	nTop=st/ratio;
	scrollTo(nTop);
	jQuery('#scroller').css('top',nTop+"px");
	/*var nTop=parseInt((parseInt(y)-scrollerTopMargin)*ratio);
	if(nTop<0)
	{
		nTop=0;
	}
	else if(nTop>-jQuery('.scrollable').outerHeight()+jQuery('#scrolling').outerHeight()+constPX)
	{
		nTop=-jQuery('.scrollable').outerHeight()+jQuery('#scrolling').outerHeight()+constPX;
	}*/
}
jQuery(document).ready(function(){
	if(dontScroll) return;
	if(jQuery('.scrollable').outerHeight()<jQuery('#scrolling').outerHeight())
	{
		setTimeout("fixScroll();",1000);
		
		contentScrollingEnabled=true;
		jQuery('#scrollLine').show();
	}
	jQuery('#scroller').bind('mousedown',function(event){
			contentScrolling=1;
		});
	jQuery('body').bind('mousemove',function(event){
			if(!contentScrolling){ return; }
			if (mY == 0) {
				saveMousePosition(event);
				return;
			}
			var y=mY;
			saveMousePosition(event);
			y=mY-y;
			if(y==0) return;
			incScroll(y);
		}).bind('mouseup',function(){
			contentScrolling=0;
			mX=0;
			mY=0;
		});
});
function incScroll(by)
{
	var top=jQuery('#scroller').css('top');
	top=top.substr(0,top.length-2);
	var nTop=parseInt(top)+by;
			
	if(nTop<scrollerTopMargin)
	{
		scrollTo(scrollerTopMargin);
		top=scrollerTopMargin;
	}
	else if(nTop>scrollerBottomMargin)
	{
		scrollTo(scrollerBottomMargin);
		top=scrollerBottomMargin;
	}
	else
	{
		scrollTo(nTop);
		top=nTop;
	}
	jQuery('#scroller').css('top',top+"px");
}
function saveMousePosition(e)
{
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		mX = e.pageX;
		mY = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		mX = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		mY = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
}
function scrollTo(y)
{
	var ratio=-jQuery('.scrollable').outerHeight()+jQuery('#scrolling').outerHeight()+constPX;
	ratio=ratio/(scrollerBottomMargin-scrollerTopMargin);
	var nTop=parseInt((parseInt(y)-scrollerTopMargin)*ratio);
	if(nTop<0)
	{
		nTop=0;
	}
	else if(nTop>-jQuery('.scrollable').outerHeight()+jQuery('#scrolling').outerHeight()+constPX)
	{
		nTop=-jQuery('.scrollable').outerHeight()+jQuery('#scrolling').outerHeight()+constPX;
	}
	document.getElementById('content').scrollTop=nTop;
}

