$.fn.jqScroll = function(options){
	
  return this.each(function(i){
		
		var trackHeight, buttonHeight, scrollHeight, contentHeight,
				increment = 50,
				scrolling = false,
				scrollTimer = null,
				pagecontent = $(this),
				outer = $('<div class="jqScrollWrap"/>'),
				inner = $('<div class="innerscroll"/>'),
				scroller = $('<div class="scroller" />'),
				scrollbar = $('<div class="scrollbar" />').appendTo(scroller),
				scrollbot = $('<div class="bottom" />').appendTo(scrollbar),
				track = $('<div class="track" />').appendTo(scrollbot),
				button = $('<div class="button" />').appendTo(track),
				up = $('<div class="up" />').appendTo(scrollbot),
				down = $('<div class="down" />').appendTo(scrollbot);
		
		pagecontent.wrap(outer);
		pagecontent.wrapInner(inner);
		pagecontent.after(scroller);

		initvars();
		
		pagecontent.mousewheel(function(objEvent, intDelta){
			if(intDelta > 0) scrollPage('up')
			else if (intDelta < 0) scrollPage('down');
		});
		
		$.each([up,down],function(){
			$(this).mousedown(function(){
				var button = $(this);
				scrolling = true;
				scrollTimer = setInterval(function(){
					button.hasClass('up')? scrollPage('up', 10) : scrollPage('down', 10);
				}, 50);
			}).mouseup(clearScroll).click(clearScroll);
		});
		
		button.draggable({
			axis:'y',
			containment:'parent',
			drag: function(event, ui){ buttonScrollContent(ui.position.top); }
		});
		
		function clearScroll(){
			clearInterval(scrollTimer);
			scrolling = false;
			testheight();
		}
			
		function buttonScrollContent(buttonTop){
			var p = Math.round((buttonTop/(trackHeight-buttonHeight))*100);
			var diff = Math.round(((contentHeight - scrollHeight)*p)/100);
			pagecontent.scrollTop(diff);
		}
		
		function scrollPage(dir, inc){
			var i = inc || increment;
			var t = pagecontent.scrollTop();
			var p = dir=='up'? t-i : t+i;
			pagecontent.scrollTop(p);
			if(p <=0 || p >= contentHeight - scrollHeight) clearScroll();
			updateButton();
		}
		
		function updateButton(){
			var d = contentHeight - scrollHeight;
			var t = pagecontent.scrollTop();
			var p = Math.round((t/d)*100);
			var b = Math.round((trackHeight-buttonHeight)*(p/100));
			button.css({top:b});
		}
		
		function initvars(){
			scrollbar.show();
			var h = pagecontent.outerHeight(true);
			scrollbar.css({height: h});
			trackHeight = track.height();
			buttonHeight = button.height();
			scrollHeight = scrollbar.height();
			contentHeight = pagecontent[0].scrollHeight;
			pagecontent.scrollTop(0);
			if(contentHeight <= scrollHeight) scrollbar.hide();
		}
		
		function testheight(){
			if(contentHeight != pagecontent[0].scrollHeight) initvars();
		}

	});
	
}

$(function(){
	
	$('#col2 .scrollable').jqScroll();
	$('#info_content').jqScroll();

	$('a.popup').click(function(){
		var url;
		if( $(this).attr('href').match(/get_resource/)){
			url = this.href.replace(/\w+.php/,'get_resource.php');
		}else{
			url = this.href.replace(/\w+.php/,'get_info.php');
		}
		$.get(url, function(data){
			$('#info_popup .innerscroll').html(data);
			$('#info_popup').fadeIn(400, function(){
				$('#info_popup .up').click();
			});
		});
		return false;
	});
	
	$('#info_popup .close').click(function(){ $('#info_popup').fadeOut(); });

	
});