/* Copyright 2008 Marc Mongenet */

function W2Bar(title, content, posX, posY, borderWidth, sizeX, userClose)
{
	// From the Rhino book, p. 380
	function drag(elm, evt) {
		var dX = evt.clientX - elm.offsetLeft;
		var dY = evt.clientY - elm.offsetTop;
		if (document.addEventListener) {
			document.addEventListener("mousemove", moveHandler, true);
			document.addEventListener("mouseup", upHandler, true);
		} else {
			document.attachEvent("onmousemove", moveHandler);
			document.attachEvent("onmouseup", upHandler);
		}
		w2mlStopEvent(evt);
		function moveHandler(e) {
			if (!e) e = window.event;
			elm.style.left = (e.clientX - dX) + "px";
			elm.style.right = ""; /* Ensure that elm is left-positionned. */
			elm.style.top = (e.clientY - dY) + "px";
			w2mlStopEvent(e);
		}
		function upHandler(e) {
			if (!e) e = window.event;
			if (document.removeEventListener) {
				document.removeEventListener("mouseup", upHandler, true);
				document.removeEventListener("mousemove", moveHandler, true);
			} else {
				document.detachEvent("onmouseup", upHandler);
				document.detachEvent("onmousemove", moveHandler);
			}
			w2mlStopEvent(e);
		}
	}
	function isevtonelm(evt, elm) {
		return "srcElement" in evt && evt.srcElement == elm ||
		       "target" in evt && evt.target == elm;
	}
	function resize(bar, evt) {
		if (!isevtonelm(evt, bar.barBorder)) return;
		var inX = "offsetX" in evt ? evt.offsetX : evt.layerX - 1;
		var dX = evt.clientX - bar.barBorder.offsetLeft;
		var resizeLeft = bar.borderWidth > inX;
		if (!resizeLeft) {
			inX -= bar.barBorder.offsetWidth - 1;
			dX -= bar.barBorder.offsetWidth;
		}
		bar.resizing = true;
		bar.body.style.cursor = resizeLeft ? "w-resize" : "e-resize";
		if (document.addEventListener) {
			document.addEventListener("mousemove", moveHandler, true);
			document.addEventListener("mouseup", upHandler, true);
		} else {
			document.attachEvent("onmousemove", moveHandler);
			document.attachEvent("onmouseup", upHandler);
		}
		w2mlStopEvent(evt);
		function moveHandler(e) {
			if (!e) e = window.event;
			var oldWidth = bar.barBorder.offsetWidth;
			var posX = bar.barBorder.offsetLeft;
			
			/* First ensure that elm is left-positionned. */
			bar.barBorder.style.left = posX + "px";
			bar.barBorder.style.right = "";
			
			var dWidth = resizeLeft ? posX + dX - e.clientX : e.clientX - dX - oldWidth - posX;
			var newWidth = Math.max(oldWidth + dWidth, 2*bar.borderWidth + 1);
			bar.barBorder.style.width = newWidth + "px";
			if (resizeLeft) {
				bar.barBorder.style.left = (posX + oldWidth - newWidth) + "px";
			}
			w2mlStopEvent(e);
		}
		function upHandler(e) {
			if (!e) e = window.event;
			bar.body.style.cursor = bar.oldBodyCursor;
			bar.resizing = false;
			mouseonborder(bar, e);
			if (document.removeEventListener) {
				document.removeEventListener("mouseup", upHandler, true);
				document.removeEventListener("mousemove", moveHandler, true);
			} else {
				document.detachEvent("onmouseup", upHandler);
				document.detachEvent("onmousemove", moveHandler);
			}
			w2mlStopEvent(e);
		}
	}
	function mouseonborder(bar, evt) {
		if (isevtonelm(evt, bar.barBorder)) {
			var inX = "offsetX" in evt ? evt.offsetX : evt.layerX - 1;
			bar.body.style.cursor = bar.borderWidth > inX ? "w-resize" : "e-resize";
		}
		else mouseoutborder(bar);
	}
	function mouseoutborder(bar) {
		bar.body.style.cursor = bar.oldBodyCursor;
	}

	this.close = function() {
		this.barBorder.parentNode.removeChild(this.barBorder);
	}
	
	this.hide = function() {
		this.barBorder.style.display = "none";
	}

	this.borderWidth = borderWidth;
	this.userClose = userClose;
	this.resizing = false;
	this.body = document.getElementsByTagName("body")[0];
	this.oldBodyCursor = this.body.style.cursor;

	this.barClose = document.createElement("div");
	this.barClose.color = "#FFF";
	this.barClose.activeColor = "#AAF";
	this.barClose.style.backgroundColor = "#00F";
	this.barClose.style.position = "absolute";
	this.barClose.style.top = this.barClose.style.right = "0px";
	this.barClose.style.color = this.barClose.color;
	this.barClose.style.cursor = "default";
	this.barClose.bar = this;
	this.barClose.appendChild(document.createTextNode("X "));

	this.barTitle = document.createElement("div");
	this.barTitle.style.fontFamily = "sans-serif";
	this.barTitle.style.fontWeight = "bold";
	this.barTitle.style.fontSize = "smaller";
	this.barTitle.style.color = "#EEF";
	this.barTitle.style.backgroundColor = "#00F";
	this.barTitle.style.whiteSpace = "nowrap";
	this.barTitle.style.overflow = "hidden";
	this.barTitle.style.marginLeft = this.barTitle.style.marginRight = borderWidth + "px";
	this.barTitle.style.cursor = "default";
	this.barTitle.bar = this;
	this.barTitle.appendChild(document.createTextNode(" "+title+"    "));
	if (userClose) this.barTitle.appendChild(this.barClose);

	this.barContent = document.createElement("div");
	this.barContent.style.color = "#000";
	this.barContent.style.backgroundColor = "#DDD";
	this.barContent.style.overflow = "hidden";
	this.barContent.style.marginLeft = this.barContent.style.marginRight = borderWidth + "px";
	this.barContent.style.cursor = "";
	this.barContent.appendChild(content);

	this.barBorder = document.createElement("div");
	this.barBorder.style.backgroundColor = "#6495ed";
	if (sizeX != 0) this.barBorder.style.width = sizeX + 2*borderWidth + "px";
	this.barBorder.style.position = "fixed";
	this.barBorder.style.zIndex = ++W2Bar.lastZIndex;
	this.barBorder.style.right = posX + "px";
	this.barBorder.style.top = posY + "px";
	this.barBorder.bar = this;
	this.barBorder.appendChild(this.barTitle);
	this.barBorder.appendChild(this.barContent);

	this.body.appendChild(this.barBorder);

	if (navigator.appName.indexOf("Microsoft") != -1 && this.barBorder.offsetTop != posY) {
		/* Fixed positioning only works in IE7+ std compliant mode. */
		this.barBorder.style.position = "absolute";
	}

	this.barBorder.style.width = this.barBorder.offsetWidth + "px"; // For MSIE

	this.barClose.onmousedown = this.barClose.onmouseover = function(e) {
		this.style.color = this.activeColor;
		w2mlStopEvent(e ? e : window.event);
	}
	this.barClose.onmouseup = this.barClose.onmouseout = function(e) {
		this.style.color = this.color;
		w2mlStopEvent(e ? e : window.event);
	}
	this.barClose.onclick = function(e) {
		if (this.bar.userClose) {
			this.bar.userClose();
		}
		else {
			this.close();
			w2mlStopEvent(e ? e : window.event);
		}
	}
	this.barTitle.onmousedown = function(e) {
		this.bar.barBorder.style.zIndex = ++W2Bar.lastZIndex;
		drag(this.bar.barBorder, e ? e : window.event);
	}
	this.barBorder.onmousedown = function(e) {
		this.bar.barBorder.style.zIndex = ++W2Bar.lastZIndex;
		resize(this.bar, e ? e : window.event);
	}
	this.barBorder.onmouseover = function(e) {
		if (!this.bar.resizing) mouseonborder(this.bar, e ? e : window.event);
	}
	this.barBorder.onmouseout = function(e) {
		if (!this.bar.resizing) mouseoutborder(this.bar);
	}
}

W2Bar.lastZIndex = 0;
