var actY;
var activeDrag=false;
var obj;
var objY;
var limitX_1, limitX_2;
var limitY_1, limitY_2;
var contentBar;
var contentScroll;
var scrolling=false;
var actY=0;
var parentScroll;
var contentScrollHeight;
var limitScroll=50;
var ie=true;

var bAgent=navigator.userAgent.toLowerCase();
if(bAgent.indexOf("msie")>0){
	ie=true;
	document.onmousemove=function(){
		if(activeDrag){
			document.selection.empty();
			moveBar(event);
		}
	}
}else if(bAgent.indexOf("gecko")>0){
	ie=false;
	document.onmousemove=function(event){
		if(activeDrag){
			window.getSelection().removeAllRanges();
			moveBar(event);
		}
	}
}

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

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

function getTop(){
	var topPos=0;
	aTag = contentBar;
	do {
		aTag = aTag.offsetParent;
		topPos += aTag.offsetTop;
	} while(aTag.tagName!="BODY");
	return topPos;
	// topPos=topPos+1-171;
}

function startDrag(barID, content, content_bar, event){
	if(scrolling) return false;
	actY=event.clientY;
	obj=document.getElementById(barID);
	parentScroll=document.getElementById(content)
	contentScroll=document.getElementById(content+"_sc")
	if(ie) contentScrollHeight=contentScroll.offsetHeight;
	else contentScrollHeight=parseInt(document.getElementById(content+"_sch").value);
//	alert(contentScrollHeight)
	contentBar=document.getElementById(content_bar);
	if(contentScrollHeight<=parentScroll.offsetHeight) return false
	/* document.body.style.cursor="pointer"; */
	activeDrag=true;
	maxTop=getTop();
	actPos=parseInt(obj.style.top);
	if(isNaN(actPos)){
		actPos=0;
	}else{
		actPos-=maxTop;
	}
	objY=event.clientY-maxTop-actPos;
	limitX_1=getLeft()-limitScroll;
	limitX_2=getLeft()+contentBar.offsetWidth+limitScroll;
	limitY_1=getTop()-limitScroll;
	limitY_2=getTop()+contentBar.offsetHeight+limitScroll;
	maxBottom=maxTop+contentBar.offsetHeight-parseInt(obj.style.height)
}

function moveOnClick(barID, content, cbar, event){
	if(!activeDrag && !scrolling){
		obj=document.getElementById(barID);
		contentBar=document.getElementById(cbar)
		parentScroll=document.getElementById(content)
		contentScroll=document.getElementById(content+"_sc")
		if(ie) contentScrollHeight=contentScroll.offsetHeight;
		else contentScrollHeight=parseInt(document.getElementById(content+"_sch").value);
	//alert(contentScroll.offsetHeight+" - "+parentScroll.offsetHeight)
		if(contentScrollHeight<=parentScroll.offsetHeight) return false
		/* document.body.style.cursor="pointer"; */
		maxTop=getTop();
		actPos=parseInt(obj.style.top);
		if(isNaN(actPos)){
			actPos=0;
		}else{
			actPos-=maxTop;
		}
		objY=maxTop+actPos;
		if(event.clientY==objY) return false;
		if(event.clientY<objY) dir="up";
		else dir="down";
		limit=parseInt(event.clientY-parseInt(obj.style.height)/2)
		limitY_1=getTop();
		limitY_2=getTop()+contentBar.offsetHeight;
		moveBar2(objY, limit, dir)
	}
}





function stopDrag(){
	/* document.body.style.cursor="default"; */
	activeDrag=false;
}

function moveBar(evt){
	if((evt.clientY-objY)>=maxTop && (evt.clientY-objY)<=maxBottom){
		obj.style.top=evt.clientY-objY;
		setY=limitY_1+limitScroll-parseInt(obj.style.top)
		scrVal=parseInt(((contentScrollHeight-parentScroll.offsetHeight)*setY)/(contentBar.offsetHeight-parseInt(obj.style.height)))
	//	window.status="ContScroll: "+contentScrollHeight+" SET: "+setY+" BA: "+contentBar.offsetHeight+" TOT: "+scrVal
		scrollDiv(scrVal);
	//	window.status=limitY_1+limitScroll-parseInt(obj.style.top)
	}
	if(!(evt.clientY>limitY_1 && evt.clientY<limitY_2)){
		stopDrag();
	}
	if(!(evt.clientX>limitX_1 && evt.clientX<limitX_2)){
		stopDrag();
	}
}

function moveBar2(actY, limit, dir){
	if((actY<limitY_1) && dir=="up"){
		stopDrag();
		scrolling=false;
		return false;
	}
	if((actY+parseInt(obj.style.height)>limitY_2) && dir=="down"){
		stopDrag();
		scrolling=false;
		return false;
	}

	if(actY!=limit){
		scrolling=true;
		obj.style.top=actY;
		if(dir=="down") actY++;
		else actY--;
		setY=limitY_1-actY
		scrVal=parseInt(((contentScrollHeight-parentScroll.offsetHeight)*setY)/(contentBar.offsetHeight-parseInt(obj.style.height)))
		//window.status="SC: "+contentScrollHeight+" SET: "+setY+" BA: "+contentBar.offsetHeight+" TOT: "+limitY_1
		scrollDiv(scrVal);
		setTimeout("moveBar2("+actY+", "+limit+", '"+dir+"')", 10)
	}else{
		scrolling=false;
	}
}

function scrollDiv(posY){
	contentScroll.style.top=posY
}
