// Wrap code in an object so that it doesn't interfere with other code
var scroller = {
	init: function() {
		//collect variables
		scroller.doc_h = document.getElementById("fsd").offsetHeight;
		scroller.cont_h = document.getElementById("fsd_cont").offsetHeight;
		scroller.scrollcont_h = document.getElementById("popfsd_scrollcont").offsetHeight;
		
		//calculate height of scroller and resize the scroller div
		//(however, we make sure that it isn't too small for long pages)
		scroller.scroll_h = (scroller.cont_h * scroller.scrollcont_h) / scroller.doc_h;
		//if(scroller.scroll_h < 15) scroller.scroll_h = 15;
		document.getElementById("popfsd_scroller").style.height = Math.round(scroller.scroll_h) + "px";
		
		// reset scroller top;
		document.getElementById("popfsd_scroller").style.top="0px";
		
		//what is the effective scroll distance once the scoller's height has been taken into account
		scroller.scroll_dist = Math.round(scroller.scrollcont_h-scroller.scroll_h);
		
		//make the scroller div draggable
		// Drag.init(document.getElementById("popfsd_scroller"),null,0,0,-1,scroller.scroll_dist);		
		Drag.init(document.getElementById("popfsd_scroller"),null,0,0,0,scroller.scroll_dist);		
		
		//add ondrag function
		document.getElementById("popfsd_scroller").onDrag = function (x,y) {
			var scroll_y = parseInt(document.getElementById("popfsd_scroller").style.top);
			var doc_y = 0 - (scroll_y * (scroller.doc_h - scroller.cont_h) / scroller.scroll_dist);
			document.getElementById("fsd").style.top = doc_y + "px";
		}
	}
}

//============================================ mousewheel, =ebe
function scroll_wheel_handler(delta) {
	var scroll_y = parseInt(document.getElementById("popfsd_scroller").style.top);
	scroll_y = (delta < 0) ? (scroll_y + 2) : (scroll_y - 2);
	
	// test limits
	scroll_y = (scroll_y < 0) ? 0 : scroll_y;
	scroll_y = (scroll_y > scroller.scroll_dist) ? scroller.scroll_dist : scroll_y;
	
	// set new vals
	document.getElementById("popfsd_scroller").style.top=scroll_y + "px";
	var doc_y = 0 - (scroll_y * (scroller.doc_h - scroller.cont_h) / scroller.scroll_dist);
	document.getElementById("fsd").style.top = doc_y + "px";
}

function wheel(event){
	// g_content_code => global var; if set, we have a popup scrollable layer
	// g_scroll_flag => global var; if set and content exceeds display space and we have "scroll"
	// .. otherwise, scroll is returned to browser window
	if (g_content_code && g_scroll_flag) {
		var delta = 0;
		if (!event) {event = window.event;} ;
		if (event.wheelDelta) {
			delta = event.wheelDelta/120; 
			if (window.opera) {delta = -delta;} ;
		} 
		else if (event.detail) {
			delta = -event.detail/3;
		}
		if (delta) {
			scroll_wheel_handler(delta);
			if (event.preventDefault) { event.preventDefault(); } ;
			event.returnValue = false;
		}
	}
}

// 
function scroll_but_handler(delta){
	// global var => scroll_time_out
	clearTimeout(scroll_time_out);
	
	// do the scroll
	scroll_wheel_handler(delta);
	scroll_time_out = setTimeout("scroll_but_handler("+delta+")",20) ;
}

// clears timeout so the scroll stops: called onmouseup
function scroll_but_handler_kill(){
	clearTimeout(scroll_time_out);
}


// init window listener
if (window.addEventListener) {
	window.addEventListener('DOMMouseScroll', wheel, false);
}
window.onmousewheel = document.onmousewheel = wheel;

