 var objEvt = new Object();
objEvt.zIndex = 0;

function Browser() {
	var ua, s, i;

	this.isIE    = false;
	this.isNS    = false;
	this.version = null;

	ua = navigator.userAgent;

	s = "MSIE";
	if ((i = ua.indexOf(s)) >= 0) {
		this.isIE = true;
		this.version = parseFloat(ua.substr(i + s.length));
		return;
	}

	s = "Netscape6/";
	if ((i = ua.indexOf(s)) >= 0) {
		this.isNS = true;
		this.version = parseFloat(ua.substr(i + s.length));
		return;
	}

	s = "Gecko";
	if ((i = ua.indexOf(s)) >= 0) {
		this.isNS = true;
		this.version = 6.1;
		return;
	}
}

var browser = new Browser();

function swCur(curname) {
	document.getElementById("grabbox").style.cursor = "url(http://telekawaru.com/if/cursors/"+curname+".cur), auto";
	objEvt.curTool = curname;
	return true;
}

function init() {
	var pa = document.getElementById("grabbox");
	var lipdivHTML = '<div id="lips" onMouseDown="mouseDown(event);" /></div>';
	pa.innerHTML = lipdivHTML;
	var lipdiv = document.getElementById('lips');
	lipdiv.canPickUp = true;
	lipdiv.description = 'her hot lips';
	if (browser.isIE) {
		moveObj(lipdiv, lipdiv.offsetLeft + 1, lipdiv.offsetTop + 1);
	}
	objEvt.curTool = 'magnifier';
	writeStatus("You are looking at a hot chick");
}

function moveObj(obj, left, top) {
	obj.style.left = left + 'px';
	obj.style.top = top + 'px';
}

function writeStatus(strText) {
	var sb = document.getElementById('statusbox');
	if (sb.innerHTML.length == 0) {
		sb.innerHTML = strText;
	} else {
		sb.innerHTML = strText+'<br />'+sb.innerHTML;
	}
}

function mouseDown(event) {
	if (!event) {
		var event = window.event;
	}
	if (event.target) {
		targ = event.target;
	} else if (event.srcElement) {
		targ = event.srcElement;
	}
	if (targ.nodeType == 3) {
		targ = targ.parentNode;
	}
	switch (objEvt.curTool) {
		case 'pickup':
			if (targ.canPickUp) {
				objEvt.pickupEle = targ;
				objEvt.mouseStartX = mouseX(event);
				objEvt.mouseStartY = mouseY(event);
				objEvt.elStartLeft = parseInt(objEvt.pickupEle.offsetLeft, 10);
				objEvt.elStartTop = parseInt(objEvt.pickupEle.offsetTop, 10);
				if (isNaN(objEvt.elStartLeft)) objEvt.elStartLeft = 0;
				if (isNaN(objEvt.elStartTop)) objEvt.elStartTop = 0;
				objEvt.pickupEle.style.zIndex = ++objEvt.zIndex;
				if (browser.isIE) {
					document.attachEvent("onmousemove", doDrag);
					document.attachEvent("onmouseup",   doStop);
					window.event.cancelBubble = true;
					window.event.returnValue = false;
				}
				if (browser.isNS) {
					document.addEventListener("mousemove", doDrag, true);
					document.addEventListener("mouseup", doStop, true);
					event.preventDefault();
				}
			} else {
				writeStatus("You can't pick that up!");
				writeStatus("targ.canPickUp: " + targ.canPickUp);
			}
			break;
		case 'magnifier':
			if (targ.description) {
				writeStatus('You see '+targ.description);
			} else {
				writeStatus('There is nothing special about that');
			}
			break;
		default:
			writeStatus('You use the '+objEvt.curTool+' tool to no avail');
			break;
	}
}

function doDrag(event) {
	moveObj(objEvt.pickupEle, objEvt.elStartLeft + mouseX(event) - objEvt.mouseStartX, objEvt.elStartTop + mouseY(event) - objEvt.mouseStartY);
	if (browser.isIE) {
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	}
	if (browser.isNS)
		event.preventDefault();
}

function doStop(event) {
	objEvt.pickupEle = null;
	if (browser.isIE) {
		document.detachEvent("onmousemove", doDrag);
		document.detachEvent("onmouseup", doStop);
	}
	if (browser.isNS) {
		document.removeEventListener("mousemove", doDrag, true);
		document.removeEventListener("mouseup", doStop, true);
	}
}

function mouseX(event) {
	var x;
	if (browser.isIE) {
		x = window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
	}
	if (browser.isNS) {
		x = event.clientX + window.scrollX;
	}
	return x;
}

function mouseY(event) {
	var y;
	if (browser.isIE) {
		y = window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop;
	}
	if (browser.isNS) {
		y = event.clientY + window.scrollY;
	}
	return y;
}

