/**************************************************
 * dom-drag.js
 * 09.25.2001
 * www.youngpup.net
 * Script featured on Dynamic Drive (http://www.dynamicdrive.com) 12.08.2005
 **************************************************
 * 10.28.2001 - fixed minor bug where events
 * sometimes fired off the handle, not the root.
 **************************************************/

/* added by Lionways.com */
var orig_onmousemove,orig_onmouseup;
/* end Lionways code */

var Drag = {

	obj : null,

	init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
	{
		o.onmousedown	= Drag.start;

		o.hmode			= bSwapHorzRef ? false : true ;
		o.vmode			= bSwapVertRef ? false : true ;

		o.root = oRoot && oRoot != null ? oRoot : o ;

		if (o.hmode  && isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "0px";
		if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";
		if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";
		if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";

		o.minX	= typeof minX != 'undefined' ? minX : null;
		o.minY	= typeof minY != 'undefined' ? minY : null;
		o.maxX	= typeof maxX != 'undefined' ? maxX : null;
		o.maxY	= typeof maxY != 'undefined' ? maxY : null;

		o.xMapper = fXMapper ? fXMapper : null;
		o.yMapper = fYMapper ? fYMapper : null;

		o.root.onDragStart	= new Function();
		o.root.onDragEnd	= new Function();
		o.root.onDrag		= new Function();
	},

	start : function(e)
	{
		var o = Drag.obj = this;
		e = Drag.fixE(e);
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		o.root.onDragStart(x, y);

		o.lastMouseX	= e.clientX;
		o.lastMouseY	= e.clientY;

		if (o.hmode) {
			if (o.minX != null)	o.minMouseX	= e.clientX - x + o.minX;
			if (o.maxX != null)	o.maxMouseX	= o.minMouseX + o.maxX - o.minX;
		} else {
			if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
			if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
		}

		if (o.vmode) {
			if (o.minY != null)	o.minMouseY	= e.clientY - y + o.minY;
			if (o.maxY != null)	o.maxMouseY	= o.minMouseY + o.maxY - o.minY;
		} else {
			if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
			if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
		}

		/* added by Lionways.com */
		orig_onmousemove=document.onmousemove;
		orig_onmouseu=document.onmouseup;
		/* end Lionways code */
		document.onmousemove	= Drag.drag;
		document.onmouseup		= Drag.end;

		return false;
	},

	drag : function(e)
	{
		e = Drag.fixE(e);
		var o = Drag.obj;

		var ey	= e.clientY;
		var ex	= e.clientX;
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		var nx, ny;

		if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
		if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
		if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
		if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);

		nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
		ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));

		if (o.xMapper)		nx = o.xMapper(y)
		else if (o.yMapper)	ny = o.yMapper(x)

		Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
		Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
		Drag.obj.lastMouseX	= ex;
		Drag.obj.lastMouseY	= ey;

		Drag.obj.root.onDrag(nx, ny);
		/* added by Lionways.com */
		getMouseXY(e);	// keep mouse tracking during drag
		/* end Lionways code */
		return false;
	},

	end : function()
	{
		/* changed by Lionways.com */
		document.onmousemove = orig_onmousemove;
		document.onmouseup   = orig_onmouseu;
		/* end Lionways code */
		Drag.obj.root.onDragEnd(	parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]),
									parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
		Drag.obj = null;
	},

	fixE : function(e)
	{
		if (typeof e == 'undefined') e = window.event;
		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
		if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
		return e;
	}
};


/*************************************************/



//==== variables ====
var IE=document.all?true:false;
var container, handle, scrollcont, accel, offsetTop;
var mouseY=0;
var scrollVal=0;
var scrollDest=0
var scrollStart=0;
var handle_height =207;

//==== do the scroll ====
function doScroll(a,mode){		// mode=false: scroll By, mode=true: scroll To
	var top=a;
	if (!mode) top+=parseInt(scrollcont.style.top);	// add the current position if scroll By
	if (top>0) top=0;		// constrain top scrolling
	if (top<container.clientHeight-scrollcont.clientHeight) top=container.clientHeight-scrollcont.clientHeight;		// constrain bottom scrolling
	scrollcont.style.top=top+'px';	// do the scroll
	handle.style.top=Math.floor(handle_height*(top/-(scrollcont.clientHeight-container.clientHeight)))+'px';	// update the scroll handle
}

//==== set scroll value/direction ====
function setScroll(v){
	scrollVal=v;
	scrollDest=0;	// stop auto-scrolling
}

//==== scroll engine ====
function scrollMonitor(){
	if (scrollDest){
		var curTop=parseInt(scrollcont.style.top);
		var norm=180*Math.abs((curTop-scrollStart)/(scrollDest-scrollStart));	// normalize to 180 deg
		var factor=Math.floor(Math.abs(scrollDest-scrollStart)/200)+1;	// apply factor based on scroller height
		if (Math.sin(0.01745329252*norm)<0)	// detect end of scrolling
			scrollDest=0;	// overshoot - stop scrolling
		else if (norm>170)	 accel=1+Math.floor((180-norm)/5*factor);		// final approach to end
		else if (norm>160)	 accel=2*factor;
		else if (norm>155)	 accel=3*factor;
		else if (norm>140)	 accel=4*factor;
		else if (norm>110)	 accel=6*factor;	// start descend
		else if (norm>24)	 accel=8*factor;	// max speed
		else	accel=1+Math.floor(norm/3*factor);		// accelleration phase
		if (scrollDest){
			if (curTop>scrollDest)
				doScroll(-accel,false);
			else if (curTop<scrollDest)
				doScroll(accel,false);
		}
	}
	else if (scrollVal)
		doScroll(scrollVal*4,false)
}

//==== handle scroll click outside of handle ====
function scrollLineClick(){
	var y=mouseY-offsetTop-50;
	if (y<parseInt(handle.style.top) || y>(parseInt(handle.style.top)+26)) {
		scrollStart=parseInt(scrollcont.style.top);
		scrollDest=-Math.floor((scrollcont.clientHeight-container.clientHeight)*(mouseY-offsetTop-50)/(248));
	}
}

//==== keep track of mouse position ====
function getMouseXY(e) {
	if (IE)
		mouseY = event.clientY + document.body.scrollTop;
	else
		mouseY = e.pageY;
	if (mouseY<0) mouseY=0;
}

//==== mouse wheel handling ====
function handleWheel(delta) {
	if (delta){
		doScroll(delta*15,false);
		scrollDest=0;	// stop auto-scrolling
	}
}

//==== mouse wheel event handler ====
function wheel(event){
	var delta = 0;
	if (!event) // For IE
		event=window.event;

	if (event.wheelDelta) { // IE/Opera.
		delta=event.wheelDelta/120;
//		if (window.opera) delta=-delta;	// In Opera 9, delta differs in sign as compared to IE. *** update *** in opera 9.5 this problem seems to be solved
	}
	else if (event.detail) // In Mozilla, sign of delta is different than in IE. Also, delta is multiple of 3.
		delta= -event.detail/3;
	// delta is positive if wheel was scrolled up and negative if wheel was scrolled down.
	if (delta) handleWheel(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.returnValue=false;
}

//==== initialization ====
function initScroller(){
	scrollcont=document.getElementById("scrollCont");
	container=document.getElementById("scroller");
	handle=document.getElementById("scrollHandle")
	container.style.top='0px';
	container.style.height='400px';
	scrollcont.style.top='0px';
	handle.style.top='0px';
	offsetTop=findPos(container)[1];

	if (scrollcont.clientHeight<container.clientHeight)	// hide scrollbar if not enough content
		document.getElementById("scrollbar").style.display='none';
	else {
		document.getElementById("scrollbar").style.display='block';
		scrollcont.style.width=scrollcont.clientWidth-10+'px';

		//---- hook mouse event ----
		if (!IE) document.captureEvents(Event.MOUSEMOVE);
		document.onmousemove=getMouseXY;
		//---- hook to mouse wheel ----
		if (container.addEventListener)
			container.addEventListener('DOMMouseScroll', wheel, false);		// DOMMouseScroll is for mozilla
		container.onmousewheel=wheel;		// IE/Opera
		//---- create drag handle ----
		Drag.init(handle, null, 0, 0, 0, handle_height);
		handle.onDrag = function(x, y) {
			scrollcont.style.top=Math.floor(-(scrollcont.clientHeight-container.clientHeight)*y/handle_height)+'px';
		}
		//---- start scrolling engine ----
		setInterval('scrollMonitor()',20);
	}
}

function findPos(obj) {
// http://www.quirksmode.org/js/findpos.html
	var curleft = curtop = 0;
	if (obj.offsetParent) {
	do {
		curleft += obj.offsetLeft;
		curtop += obj.offsetTop;
	} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}
