/** This is high-level function.
 * It must react to delta being more/less than zero.
 */
var eWithFocus = window;

function setMyScroller(eScroller) {
	eWithFocus = eScroller;
	//alert(eScroller.id);
	return true;
}

function resetScroller(event) {
	if (!event) {
		event = window.event;
	}
		
	if (event.srcElement && event.srcElement.tagName == "DIV") {
		eWithFocus = window;
		//alert(event.srcElement.tagName);
	}else{
		if (event.target && event.target.tagName == "DIV") {
			eWithFocus = window;
		}else{
			eWithFocus = window;
		}
	}
}

function handle(delta) {
	if (eWithFocus && eWithFocus.scrollBy) {
		eWithFocus.scrollBy(0, -delta*30);
	}else{
		newtop = parseInt(eWithFocus.scrollTop, 10) - ((delta * eWithFocus.scrollHeight) / 40);
		if (newtop < 0) {
			newtop = 0;
		}else if (newtop > eWithFocus.scrollHeight) {
			newtop = eWithFocus.scrollHeight;
		}
		eWithFocus.scrollTop = newtop;
	}
}

/** Event handler for mouse wheel event.
 */
function wheel(event){
	var delta = 0;
	if (!event) event = window.event; /* For IE. */
	
	//if (event.srcElement) { alert('event.srcElement.tagName = ' + event.srcElement.tagName); }
	//if (event.target) { alert('event.target.tagName = ' + event.target.tagName); }
	
	if (event.srcElement) { eTarget = event.srcElement; }
	else if (event.target) { eTarget = event.target; }
	
	found = false;
	while (!found && eTarget.tagName != "HTML" && eTarget.parentNode) {
		if (eTarget.tagName == "SELECT") return true;
		
		if (eTarget.tagName == "DIV" && eTarget.className == "wheeler") {
			found = true;
		}else{
			eTarget = eTarget.parentNode;
		}
	}
	
	if (eTarget.tagName == "HTML") {
		eWithFocus = window;
	}else{
		eWithFocus = eTarget;
	}
			
	if (event.wheelDelta) { /* IE/Opera. */
		delta = event.wheelDelta/120;
		/** In Opera 9, delta differs in sign as compared to IE.
		 */
		if (window.opera) delta = -delta;
	} else if (event.detail) { /** Mozilla case. */
		/** In Mozilla, sign of delta is different than in IE.
		 * Also, delta is multiple of 3.
		 */
		delta = -event.detail/3;
	}
	
	/** If delta is nonzero, handle it.
	 * Basically, delta is now positive if wheel was scrolled up,
	 * and negative, if wheel was scrolled down.
	 */
	if (delta) handle(delta);
	
	/** Prevent default actions caused by mouse wheel.
	 * That might be ugly, but we handle scrolls somehow
	 * anyway, so don't bother here..
	 */
	if (event.preventDefault) event.preventDefault();
	 
	//event.cancelBubble = true;
	//if (event.stopPropagation) event.stopPropagation();
	
	event.returnValue = false;
	return false;
}

function getY(oElement) {
	var iReturnValue = 0;
	
	while( oElement != null ) {
		iReturnValue += oElement.offsetTop;
		oElement = oElement.offsetParent;
	}
	return iReturnValue;
}

/** Initialization code. 
 * If you use your own event management code, change it as required.
 */
if (window.addEventListener)
        /** DOMMouseScroll is for mozilla. */
        window.addEventListener('DOMMouseScroll', wheel, false);
/** IE/Opera. */
window.onmousewheel = document.onmousewheel = wheel;


