tar_cscroll={
	init:function(){
		$(".tar_cscroll").each(function(){
			tar_cscroll.setup(this);
		});
	},
	setup:function(elem){
		var fadeblocking=($(elem).find(".fadeblocking").length)?$(elem).find(".fadeblocking"):"";
		$(elem).find(".fadeblocking").remove();
		var content=$(elem).html();
		var blockheight=parseInt($(elem).height());
		$(elem).empty().append('<div class="tar_scrollblock" style="height:'+blockheight+'px;"><div class="tar_scrollable"><div class="tar_scrollit"></div></div></div>');
		$(elem).append(fadeblocking);
		$(elem).find(".tar_scrollit").append(content);
		var hidebar=parseInt($(elem).find(".tar_scrollblock").outerWidth());
		$(elem).find(".tar_scrollable").css("width",hidebar+35);
		$(elem).find(".tar_scrollit").css("width",hidebar);
		
		var boxheight = parseInt($(elem).find(".tar_scrollable").outerHeight());
		var contentheight = parseInt($(elem).find(".tar_scrollit").outerHeight());
		if (contentheight>boxheight){
			$(elem).append('<div class="tar_scrollbar v"><div class="tar_scrollbutton less"></div><div class="tar_scrollbutton more"></div><div class="tar_scrollbound v"><div class="tar_scroller v"><div class="tar_scrollgrab"></div><div class="tar_scrollzone"></div></div></div></div>');
			var l_buttonheight = parseInt($(elem).find(".v .tar_scrollbutton.less").outerHeight());
			var m_buttonheight = parseInt($(elem).find(".v .tar_scrollbutton.more").outerHeight());
			var scrollheight = boxheight-l_buttonheight-m_buttonheight;
			var ratio = boxheight/contentheight;
			var barheight = Math.floor(scrollheight*(1-ratio));
			barheight=(barheight%2)?barheight+1:barheight;
			
			$(elem).find(".tar_scrollbound.v").css({"height":barheight,"top":l_buttonheight});
			$(elem).find(".tar_scroller.v").css({"height":scrollheight-barheight,"top":0});
			tar_cscroll.events(elem,ratio);
		}
	},
	events:function(elem,ratio){
		$(elem).find(".tar_scroller.v").mousedown(function(e){
			$(this).closest(".tar_scrollbar").css({"z-index":1001});
			$(this).closest(".tar_cscroll").append('<div id="blocker" style="top:-30px;left:-30px;width:140%;height:140%;position:absolute;z-index:1000;background:url(http://imgs.tarlabs.com/trans.gif);"></div>');
			scrollitheight=parseInt($(this).closest(".tar_cscroll").find(".tar_scrollit").outerHeight())-parseInt($(this).closest(".tar_cscroll").find(".tar_scrollable").height());
			$(this).addClass("scrolling").attr("rel",e.pageY+","+$(this).css("top")+","+scrollitheight);
			$("body").bind("mouseup",function(){
				$(".tar_scroller").removeClass("scrolling");
				$("body").unbind("mouseup");
				return false;
			});
			return false;
		}).mousemove(function(e){
			if($(this).hasClass("scrolling")){
				temp=$(this).attr("rel").split(",");
				barheight=parseInt($(this).closest(".tar_scrollbound").css("height"));
				
				startmouse=parseInt(temp[0]);
				startoffset=parseInt(temp[1]);
				scrollitheight=parseInt(temp[2]);
				
				mouse=parseInt(e.pageY);
				
				mouseoffset=mouse-startmouse;
				
				togo=startoffset+mouseoffset
				bottomlimit=barheight;
				togo=(togo>=0)?togo:0;
				togo=(togo<=bottomlimit)?togo:bottomlimit;
				
				percent=(togo/barheight)*100;
				percentage=Math.floor(scrollitheight*(percent/100));
					
				$(this).css({top:togo});
				$(this).closest(".tar_cscroll").find(".tar_scrollable").scrollTop(percentage);
				return false;
			}
		});
		/*$(elem).find(".tar_scrollzone").bind("mouseout",function(){
			$(this).closest(".tar_scroller").removeClass("scrolling");
			return false;
		}).bind("mouseup",function(){
			$(this).closest(".tar_scroller").removeClass("scrolling");
			setTimeout("tar_cscroll.removeblocker()",200);
			return false;
		});*/
		$(elem).find(".tar_scrollable").scroll(function(){
			var scrollitheight=parseInt($(this).find(".tar_scrollit").outerHeight())-parseInt($(this).outerHeight());
			var barheight=parseInt($(this).closest(".tar_cscroll").find(".tar_scrollbound").css("height"));
     	
			var percent=barheight/scrollitheight;
			
			var togo=Math.ceil(parseInt($(this).scrollTop())*percent);
			$(this).closest(".tar_cscroll").find(".tar_scroller.v").css({top:togo});
			return false;
		});
		$(elem).find(".tar_scrollbutton").click(function(){
			$(this).closest(".tar_cscroll").append('<div id="blocker" style="top:0;left:0;width:110%;height:110%;position:absolute;z-index:1000;"></div>');
			var adjust=($(this).hasClass("more"))?50:-50;
			var togo=parseInt($(this).closest(".tar_cscroll").find(".tar_scrollable").scrollTop())+adjust;
			$(this).closest(".tar_cscroll").find(".tar_scrollable").scrollTop(togo);
			setTimeout("tar_cscroll.removeblocker()",200);
			return false;
		}).bind("dblclick",function(){ return false; });
		$(elem).mouseup(function(){
			setTimeout("tar_cscroll.removeblocker()",200);
		});
		$(elem).hover(function(){
			tar_cscroll.removeblocker();
		},function(){
			setTimeout("tar_cscroll.removeblocker()",200);
		});
	},
	removeblocker:function(){
		$(".tar_scrollbar").removeAttr("style");
		$("#blocker").remove();
	}
}

