//////////////////////////
// RE: THE FOLLOWING CODE:
//
//*** This code is copyright 2003 by Gavin Kistner, gavin@refinery.com
//*** It is covered under the license viewable at http://phrogz.net/JS/_ReuseLicense.txt

//The following are for browsers like NS4 or IE5Mac which don't support either attachEvent or addEventListener
function MyAttachEvent(obj,evt,fnc){
	if (!obj.myEvents) obj.myEvents={};
	if (!obj.myEvents[evt]) obj.myEvents[evt]=[];
	var evts = obj.myEvents[evt];
	evts[evts.length]=fnc;
}

function MyFireEvent(obj,evt){
	if (!obj || !obj.myEvents || !obj.myEvents[evt]) return;
	var evts = obj.myEvents[evt];
	for (var i=0,len=evts.length;i<len;i++) evts[i]();
}

function addCustomEvent(objElement,eventName,eventFunction){

	if(objElement.addEventListener){
		objElement.addEventListener (eventName,eventFunction,false);
	} else if(objElement.attachEvent) {
		objElement.attachEvent ('on' + eventName,eventFunction);
	} else {
		MyAttachEvent(objElement,eventName,eventFunction);
		objElement['on'+eventName]=function(){ MyFireEvent(objElement,eventName) };
	}

}

////////////////////

		var scroll_scrollbar, scroll_viewport, scroll_content, scroll_thumb_top, scroll_thumb_height, click_offset_y, scroll_viewport_top, within_bounds_thumb, within_bounds_content, scroll_ratio;
		scroll_thumb_top = 2;
		scroll_thumb_height = 30;

		addCustomEvent(window,'load',function(){
			scroll_viewport = document.getElementById('scroll_viewport');
			scroll_content = document.getElementById('scroll_content');
			scroll_scrollbar = document.getElementById('scroll_scrollbar');
			scroll_viewport_top = findPos(document.getElementById('scroll_viewport'))[1];

			// INITIALIZE
			if(scroll_viewport.clientHeight>scroll_content.clientHeight){
				scroll_scrollbar.style.display = 'none';
				return;
			}
			scroll_scrollbar.style.height = scroll_viewport.clientHeight + 'px';
			scroll_viewport.onselectstart = function(){ return false; }

			scroll_scrollbar.onmousemove = scroll_content.onmousemove = function(evt){

				if(!scroll_scrollbar.begin_drag||!(within_bounds_content||within_bounds_thumb)){
					scroll_scrollbar.begin_drag = false;
					return;
				}

				// DRAGGING
				evt = (evt||window.event);

				var distance_from_top = (evt.layerY||evt.y);
				scroll_thumb_top = ((distance_from_top)?distance_from_top:(evt.pageY - scroll_viewport_top)) - click_offset_y;
				set_scrolltop();
			}

			// SCROLL SCROLLBAR
			scroll_scrollbar.onmouseover = function(evt){
				evt = (evt||window.event);
				
				// CURSOR IS WITHIN BOUNDS OF SCROLLBAR
				within_bounds_thumb = true;
			}

			scroll_scrollbar.onmousedown = function(evt){
				evt = (evt||window.event);
				// START DRAGGING
				scroll_scrollbar.begin_drag = true;
				within_bounds_thumb = true;

				var distance_from_top = (evt.layerY||evt.y);

				if(distance_from_top){
					// IE, OPERA, SAFARI
					if(distance_from_top<scroll_thumb_top||distance_from_top>scroll_thumb_top+scroll_thumb_height){
						scroll_thumb_top = distance_from_top - (scroll_thumb_height / 2);
					}
					click_offset_y = distance_from_top - scroll_thumb_top;
				}else{
					// FF
					distance_from_top = evt.pageY;
					if(distance_from_top<scroll_thumb_top||distance_from_top>scroll_thumb_top+scroll_thumb_height){
						scroll_thumb_top = (distance_from_top - (scroll_thumb_height / 2)) - scroll_viewport_top;
					}

					click_offset_y = distance_from_top - scroll_thumb_top - scroll_viewport_top;
				}

				set_scrolltop();
			}

			scroll_scrollbar.onmouseout = scroll_scrollbar.onmouseup = function(){
				// CURSOR IS OUT OF SCROLLBAR BOUNDS
				window.setTimeout(function(){
					if(!within_bounds_content) within_bounds_thumb = false;
				},50);
			}

			// SCROLL CONTENT		
			scroll_content.onmouseup = function(){
				scroll_scrollbar.begin_drag = false;
			}

			scroll_content.onmouseout = function(){
				within_bounds_content = false;
			}

			scroll_content.onmouseover = function(){
				within_bounds_content = true;
			}

		});

		function set_scrolltop(){			
			try{	
				if(isNaN(scroll_thumb_top)) scroll_thumb_top = 0;
				if(scroll_thumb_top<2) scroll_thumb_top = 2;
				if(scroll_thumb_top + scroll_thumb_height > scroll_viewport.clientHeight - 2) scroll_thumb_top = scroll_viewport.clientHeight - scroll_thumb_height - 2;

				scroll_scrollbar.style.backgroundPosition = '0px ' + scroll_thumb_top + 'px';

				scroll_ratio = (scroll_viewport.clientHeight - scroll_content.clientHeight) / (scroll_viewport.clientHeight - scroll_thumb_height);

				scroll_content.style.marginTop = (scroll_ratio * scroll_thumb_top) + 'px';				
			}catch(err){
	
			}
		}

		function findPos(obj) {
			var curleft = curtop = 0;
			if (obj.offsetParent) {
				curleft = obj.offsetLeft
				curtop = obj.offsetTop
				while (obj = obj.offsetParent) {
					curleft += obj.offsetLeft
					curtop += obj.offsetTop
				}
			}
			return [curleft,curtop];
		}