/***************************************************************
 **              Scroller                                     **
 **                                                           **
 **         Author:   Pasquale Di Stasio                      **
 **         E-Mail:   info@pasqualedistasio.com               **
 **       Web-Site:   www.pasqualedistasio.com                **
 **                                                           **
 **           Date:   2006-04-24                              **
 **        Version:   2.0                                     **
 ***************************************************************/

var activeDrag = false;
var scrolling = false;
var objScroll = new Array();
var limitTop, limitBottom, actLimit, actBar;


var bAgent = navigator.userAgent.toLowerCase();
if (bAgent.indexOf("msie")>0) {
	bAgent = "ie";
	document.onmousemove=function(){
		if(activeDrag){
			document.selection.empty();
			moveBar(actBar, event);
		}else if(scrolling) {
			document.selection.empty();
		}
	}
	document.onmouseout = function(){
		if (event.toElement == null) {
			stopClick();
			stopDrag();
		}
	}
} else if (bAgent.indexOf("gecko")>0) {
	bAgent = "ff";
	document.onmousemove=function(event){
		if(activeDrag){
			window.getSelection().removeAllRanges();
			moveBar(actBar, event);
		}else if(scrolling) {
			window.getSelection().removeAllRanges();
		}
	}
	document.onmouseout = function(event){
		if (event.relatedTarget == null) {
			stopClick();
			stopDrag();
			return false;
		}
	}
} else {
	bAgent = false;	
}

document.onmouseup = function() {
	stopClick();
	stopDrag();
}


function init(){
	i = 1;
	if (bAgent == "ie") barW = "8px";
	else barW = "6px";
	while (document.getElementById("content_scroller"+i)) {
		obj = document.getElementById("content_scroller"+i);
		obj.style.overflow="hidden";
		w = parseInt(obj.style.width)-20;
		h = parseInt(obj.style.height);
		code  = "<table cellspacing='0' cellpadding='0' border='0'><tr>";
		code += "<td class='"+obj.className+"' valign='top'><div id='content_text"+i+"' style='position:relative;top:0px;width:"+w+"px;'>";
		code += obj.innerHTML;
		code += "</div><td>";
		code += "<td width='12'>&nbsp;</td>";
		code += "<td width='8' valign='top'>";
		code += "<div style='background-color:#E1E1E1;height:"+h+"px;' id='content_bar"+i+"' onMouseDown=\"moveOnClick("+i+", event)\" onMouseUp=\"stopClick("+i+")\">";
		code += "<div style='position:relative;width:"+barW+";background:url(images/arrow_scroller.gif) center center no-repeat #9A1F21;border:1px solid #3F0C0E;' id='bar"+i+"' onMouseDown=\"startDrag("+i+", event)\"></div>";
		code += "</div></td></tr></table>";
		obj.innerHTML = code;

		objScroll[i] = new Object();
		objScroll[i].contentScroll=document.getElementById("content_scroller"+i);
		objScroll[i].contentText=document.getElementById("content_text"+i);
		objScroll[i].contentBar=document.getElementById("content_bar"+i);
		objScroll[i].bar=document.getElementById("bar"+i);

		handleMouseWheel(objScroll[i].contentScroll);
		i++
	}
	setTimeout("setBar()", 100)
}

function checkButton(event){
	if(bAgent=="ie" && event.button=="1") return true;
	else if(bAgent=="ff" && event.button=="0") return true;
	return false;
}

function setBar(i){
	i = 1;
	while (document.getElementById("content_scroller"+i)) {
		contentText = objScroll[i].contentText;
		contentScroll = objScroll[i].contentScroll;
		contentBar = objScroll[i].contentBar
		bar = objScroll[i].bar;
		if (contentText.offsetHeight <= contentScroll.offsetHeight) {
			bar.style.height = contentScroll.offsetHeight;
		} else {
			hBar=Math.round(contentBar.offsetHeight * (contentScroll.offsetHeight / contentText.offsetHeight));
			bar.style.height = hBar;
		}
		i++;
	}
}

function startDrag(i, event){
	if (scrolling) return false;
	if (!checkButton(event)) return false;
	actBar = i;
	contentText = objScroll[i].contentText;
	contentScroll = objScroll[i].contentScroll;
	contentBar = objScroll[i].contentBar;
	bar = objScroll[i].bar;
	actY=event.clientY;
	if(contentText.offsetHeight<=contentScroll.offsetHeight) return false
	activeDrag=true;
	limitTop=getTop(contentBar);
	limitBottom = limitTop + parseInt(contentBar.style.height) - bar.offsetHeight
	barPos=parseInt(bar.style.top);
	if (isNaN(barPos)) barPos = 0;
	actLimit=event.clientY-limitTop-barPos;
}

function moveBar(i, evt){
	actBarPos = evt.clientY-limitTop-actLimit;
	if (actBarPos >= 0 && actBarPos <= (limitBottom - limitTop)) {
		scrollObj(objScroll[i].bar, actBarPos);
		actTextPos = (objScroll[i].contentText.offsetHeight-objScroll[i].contentScroll.offsetHeight)/(objScroll[i].contentBar.offsetHeight-objScroll[i].bar.offsetHeight)*actBarPos*(-1);
		scrollObj(objScroll[i].contentText, actTextPos);
	}else if(actBarPos < 0) {
		scrollObj(objScroll[i].bar, 0);
		scrollObj(objScroll[i].contentText, 0);
	}else if(actBarPos > (limitBottom - limitTop)) {
		scrollObj(objScroll[i].bar, (limitBottom - limitTop));
		scrollObj(objScroll[i].contentText, (objScroll[i].contentScroll.offsetHeight-objScroll[i].contentText.offsetHeight));
	}
}

function stopDrag(){
	activeDrag=false;
}

function stopClick(i){
	try{
		clearTimeout(timer);
	}catch(e) {}
	scrolling=false;
}

function handleMouseWheel(node){
	if (node.addEventListener) {
		node.addEventListener("DOMMouseScroll", moveOnWheel, false);
	} else {
		node.onmousewheel = moveOnWheel;
	}
}

function moveOnWheel(event) {
	if (activeDrag || scrolling) return false;
	if(!event) event = window.event;
	if (bAgent == "ie") {
		i=event.srcElement.id.replace("content_text", "");
		if (i == "") i=event.srcElement.offsetParent.id.replace("content_text", "");

	} else {
		i=event.target.parentNode.id.replace("content_text", "");
	}
	if (isNaN(i) || i == "") return false;
	bar = objScroll[i].bar;
	barPos = parseInt(bar.style.top);
	if (isNaN(barPos)) barPos = 0;
	
	if(event.wheelDelta) delta = event.wheelDelta*(-1)
	else delta = event.detail;
	if (delta <= 0) {
		dir = "up";
		newBarPos = barPos - 20;
	} else {
		dir = "down";	
		newBarPos = barPos + 20;
	}
	actLimit = newBarPos;
	moveBar2(i, newBarPos, barPos, dir)
}

function moveOnClick(i, event) {
	if (!activeDrag && !scrolling) {
		if (!checkButton(event)) return false;
		contentText = objScroll[i].contentText;
		contentScroll = objScroll[i].contentScroll;
		contentBar = objScroll[i].contentBar;
		bar = objScroll[i].bar;
		if (contentText.offsetHeight <= contentScroll.offsetHeight) return false;

		limitTop = getTop(contentBar);
		limitBottom = limitTop + parseInt(contentBar.style.height)
		
		barPos = parseInt(bar.style.top);
		if (isNaN(barPos)) barPos = 0;
		actLimit = Math.round(parseInt(event.clientY)-limitTop-parseInt(bar.style.height)/2);
		
		
		if ((event.clientY-limitTop) > barPos+parseInt(bar.style.height)){
			dir = "down";
			newBarPos = barPos + 20;
		} else if((event.clientY-limitTop) < barPos) {
			dir ="up";
			newBarPos = barPos - 20;
		}else return false;
		moveBar2(i, newBarPos, barPos, dir)
	}
}

function moveBar2(i, newBarPos, actBarPos, dir){
	actTextPos = (objScroll[i].contentText.offsetHeight-objScroll[i].contentScroll.offsetHeight)/(objScroll[i].contentBar.offsetHeight-objScroll[i].bar.offsetHeight)*actBarPos*(-1);
	if (actBarPos < 0 && dir == "up") {
		stopClick();
		return false;
	}

	if ((actBarPos + parseInt(bar.style.height) > parseInt(objScroll[i].contentBar.style.height)) && dir == "down") {
		stopClick();
		return false;
	}
	if (actBarPos != actLimit) {
		scrolling = true;
		scrollObj (objScroll[i].contentText, actTextPos);
		scrollObj (objScroll[i].bar, actBarPos);
		if (dir == "down") actBarPos += 1;
		else actBarPos -= 1;
		timer=setTimeout("moveBar2("+i+", "+newBarPos+", "+actBarPos+", '"+dir+"')", 10)
	} else {
		stopClick();
		return false;
	}
}

function scrollObj(obj, val) {
	obj.style.top = val + "px";
}

function getLeft(obj){
	var leftPos=0;
	aTag = obj;
	do {
		aTag = aTag.offsetParent;
		leftPos += aTag.offsetLeft;
	} while(aTag.tagName!="BODY");
	return leftPos;
}

function getTop(obj){
	var topPos=0;
	aTag = obj;
	do {
		aTag = aTag.offsetParent;
		topPos += aTag.offsetTop;
	} while(aTag.tagName!="BODY");
	return topPos;
}