/////////////////////////////////////////////////////////////////////////
//     This scrollZoom script was designed by Erik Arvidsson for WebFX //
//                                                                     //
//     For more info and examples see: http://www.eae.net/webfx/       //
//     or send mail to erik@eae.net                                    //
//                                                                     //
//     Feel free to use this code as long as this disclaimer is        //
//     intact.                                                         //
/////////////////////////////////////////////////////////////////////////

var	offset = 10; //The distance from the edge that is dragable in pixels
var xMin = 30; // The minumum x width
var yMin = 30;

var theobject = null; //This gets a value as soon as a resize start

function resizeObject() {
	this.el;			//pointer to the object
	this.dir = "";		//type of current resize (n, s, e, w, ne, nw, se, sw)
	this.grabx;			//Some useful values
	this.graby;
	this.width;
	this.height;
	this.left;
	this.top;
	this.type;
}

function scrollZoomDown() {
	var el = getReal(window.event.srcElement, "className", "scrollZoomHandle");	//Traverse the element tree
	if(el.className == "scrollZoomHandle") {

		theobject = new resizeObject(); //This is a global reference to the current dragging object
				
		theobject.el = el;
		theobject.dir = getDirection(el);
		theobject.type = el.getAttribute("type");			//Find the type

		theobject.grabx = window.event.clientX;
		theobject.graby = window.event.clientY;
		theobject.width = el.style.pixelWidth;
		theobject.height = el.style.pixelHeight;
		theobject.left = el.style.pixelLeft;
		theobject.top = el.style.pixelTop;
		theobject.parentWidth = el.parentElement.clientWidth;
		theobject.parentHeight = el.parentElement.clientHeight;

		window.event.returnValue = false;
		window.event.cancelBubble = true;
	}
	else {
		theobject = null;
	}	
}

function scrollZoomUp() {
	if (theobject) {
		theobject = null;
	}
}

function scrollZoomMove() {
	var el, xPos, yPos, str;

	el = getReal(event.srcElement, "className", "scrollZoomHandle");

	if (el.className == "scrollZoomHandle" && theobject == null) {	// Just hovering!
		str = getDirection(el);
		//Fix the cursor	
		if (str == "") str = "default";
		else str += "-resize";
		el.style.cursor = str;
	}
	
	if(theobject && theobject.dir == "") {	// Just scrolling
		if (theobject.type=="y") {
			if(event.clientY >= 0) {
				if ((event.clientY - theobject.graby + theobject.top >= 0) && (event.clientY - theobject.graby + theobject.top <= theobject.el.parentElement.clientHeight - theobject.el.style.pixelHeight)) {
					theobject.el.style.top = event.clientY - theobject.graby + theobject.top;
				}
				if (event.clientY - theobject.graby + theobject.top < 0) {
					theobject.el.style.top = "0";
				}
				if (event.clientY - theobject.graby + theobject.top > theobject.el.parentElement.clientHeight - theobject.el.style.pixelHeight - 0) {
					theobject.el.style.top = theobject.el.parentElement.clientHeight - theobject.el.style.pixelHeight;
				}

				handleFakeEvent(theobject.el,"y");
			}
		}
		else {
			if(event.clientX  >= 0) {
				if ((event.clientX  - theobject.grabx + theobject.left >= 0) && (event.clientX - theobject.grabx + theobject.left <= theobject.el.parentElement.clientWidth - theobject.el.style.pixelWidth)) {
					theobject.el.style.left = event.clientX - theobject.grabx + theobject.left;
				}
				if (event.clientX - theobject.grabx + theobject.left < 0) {
					theobject.el.style.left = "0";
				}
				if (event.clientX - theobject.grabx + theobject.left > theobject.el.parentElement.clientWidth - theobject.el.style.pixelWidth - 0) {
					theobject.el.style.left = theobject.el.parentElement.clientWidth - theobject.el.style.pixelWidth;
				}

				handleFakeEvent(theobject.el,"x");
			}
		}
		
		window.event.returnValue = false;
		window.event.cancelBubble = true;
	}
	
	//Resizing starts here
	if(theobject && theobject.dir != "") {
		if (theobject.dir.indexOf("s") != -1) {
			var tmpHeight = window.event.clientY - theobject.graby + theobject.height;
			theobject.el.style.height = Math.min(theobject.parentHeight - theobject.top, Math.max(yMin, tmpHeight));
			handleFakeEvent(theobject.el,"y");
		}
		if (theobject.dir.indexOf("n") != -1) {
			theobject.el.style.top = Math.min(theobject.top + theobject.height - yMin, Math.max(0,window.event.clientY - theobject.graby + theobject.top));
			theobject.el.style.height = Math.max(yMin, Math.min(theobject.top + theobject.height, theobject.graby - window.event.clientY + theobject.height));
			handleFakeEvent(theobject.el,"y");
		}
		if (theobject.dir.indexOf("e") != -1) {
			var tmpWidth = window.event.clientX - theobject.grabx + theobject.width;
			theobject.el.style.width = Math.min(theobject.parentWidth - theobject.left, Math.max(xMin, tmpWidth));
			handleFakeEvent(theobject.el,"x");
		}
		if (theobject.dir.indexOf("w") != -1) {
			theobject.el.style.left = Math.min(theobject.left + theobject.width - xMin, Math.max(0,window.event.clientX - theobject.grabx + theobject.left));
			theobject.el.style.width =  Math.max(xMin, Math.min(theobject.left + theobject.width, theobject.grabx - window.event.clientX + theobject.width));
			handleFakeEvent(theobject.el,"x");
		}				
		
		window.event.returnValue = false;
		window.event.cancelBubble = true;
	} 
}

//Find out what kind of resize! Return a string that describes the directions
function getDirection(el) {
	var xPos, yPos, dir, tmpEl, type;

	if (theobject && theobject.dir)
		dir = theobject.dir;
	else {
		type = el.getAttribute("TYPE")
		dir = "";
	
		xPos = window.event.offsetX;
		yPos = window.event.offsetY;
		tmpEl = window.event.srcElement;
		
		while (tmpEl != el) {
			xPos += tmpEl.offsetLeft;
			yPos += tmpEl.offsetTop;
			tmpEl = tmpEl.offsetParent;
		}

		if (type == "y") {
			if (yPos<offset) dir += "n";
			else if (yPos > el.clientHeight-offset) dir += "s";
		}
		else {
			if (xPos<offset) dir += "w";
			else if (xPos >= el.clientWidth-offset) dir += "e";
		}
	
		return dir;
	}
}

function getReal(el, type, value) {
	var temp = el;
	while ((temp != null) && (temp.tagName != "BODY")) {
		if (eval("temp." + type) == value) {
			el = temp;
			return el;
		}
		temp = temp.parentElement;
	}
	return el;
}


function setValue(el, zoomValue, scrollValue) {
	if (zoomValue) el.zoomValue = zoomValue;
	if (scrollValue) el.scrollValue = scrollValue;

	if (el.getAttribute("TYPE") == "x") {
		if (el.zoomValue && el.zoomValue != 0)	// to prevent divide by zero
			el.style.width = el.parentElement.clientWidth / el.zoomValue;
		if (el.scrollValue)
			el.style.left =  el.scrollValue * (el.parentElement.clientWidth - el.style.pixelWidth);
	}
	else {
		if (el.zoomValue && el.zoomValue != 0)	// to prevent divide by zero
			el.style.height = el.parentElement.clientHeight / el.zoomValue;
		if (el.scrollValue)
			el.style.top =  el.scrollValue * (el.parentElement.clientHeight - el.style.pixelHeight);
	}
	eval(el.onchange.replace(/this/g, "el"))
}

function handleFakeEvent(el, dir) {
	var onchange;
	
	if (dir == "y") {
		el.scrollValue = el.style.pixelTop / (el.parentElement.clientHeight - el.style.pixelHeight + 1);	// to prevent divide by zero
		el.zoomValue = el.parentElement.clientHeight / el.style.pixelHeight;
		onchange = theobject.el.getAttribute("ONCHANGE");
		if (onchange)
			eval(onchange.replace(/this/g, "el"));
	}
	else {
		el.scrollValue = el.style.pixelLeft / (el.parentElement.clientWidth - el.style.pixelWidth + 1);	// to prevent divide by zero
		el.zoomValue = el.parentElement.clientWidth / el.style.pixelWidth;
		onchange = theobject.el.getAttribute("ONCHANGE");
		if (onchange)
			eval(onchange.replace(/this/g, "el"));
	}
}

document.onmousedown = scrollZoomDown;
document.onmouseup = scrollZoomUp;
document.onmousemove = scrollZoomMove;

document.write('<style type="text/css">\
				.scrollZoomHandle	{position: relative; cursor: default;}\
				</style>');

