// This controls the zIndex of the window.  Shared by all windows hence it is global
var zIndex = new Object();
zIndex.value = 100;

/*
	new jwindow({
					title : "name",				required
					id : "id",					required
					div : "id",					required
				})
*/
function jwindow(obj)
{
	var title = obj.title;
	var id = obj.id;
	
	var width = 0;
	var height = 0;
	
	// boolean that checks if window is open
	var opened = false;
	// position of window
	var topX = 0;
	var topY = 0;
	// position of mouse when down
	var currX = 0;
	var currY = 0;
	// position of mouse movement
	var newX = 0;
	var newY = 0;
	
	var main = document.createElement('div');
	main.id = id;
	var head = document.createElement('div');
	head.id = id + '_header';
	var foot = document.createElement('div');
	foot.id = id + '_footer';
	
	// header tweaks
	var string = '<div style="overflow:hidden">' +
					'<div style="float:left;background:url(~assets/Img/jWindow/topLeft.gif) top left no-repeat;width:15px;height:29px">' +
					'</div>' +
					'<div id="'+id+'_headerMain" style="float:left;background:url(~assets/Img/jWindow/topMid.gif) top left repeat-x;height:24px;padding-top:5px">' +
						title +
					'</div>' +
					'<div style="float:right;background:url(~assets/Img/jWindow/topRight.gif) top left no-repeat;width:15px;height:29px">' +
					'</div>' +
				'</div>';
	head.innerHTML = string;
	
	//footer tweaks
	var string = '<div style="overflow:hidden">' +
					'<div style="float:left;background:url(~assets/Img/jWindow/bottomLeft.gif) top left no-repeat;width:10px;height:10px">' +
					'</div>' +
					'<div id ="'+id+'_footerMain" style="float:left;background:url(~assets/Img/jWindow/bottomMid.gif) top left repeat-x;height:10px">' +
					'</div>' +
					'<div style="float:right;background:url(~assets/Img/jWindow/bottomRight.gif) top left no-repeat;width:10px;height:10px">' +
					'</div>' +
				'</div>';
	foot.innerHTML = string;
		
	var content = document.createElement('div');
	content.id = id + '_content';
	
	// append head and content to main
	main.appendChild(head);
	main.appendChild(content);
	main.appendChild(foot);
	// append to container div
	document.body.appendChild(main);
	
	//set initial window settings
	
	//set the initial CSS settings
	main.style.left = 0 + 'px';
	main.style.top = 0 + 'px';
	main.style.position = 'absolute';
	main.style.display = 'none';
	main.style.width = 0 + 'px';
	
	head.style.fontWeight = "bold";
	head.style.fontSize = "14px";
	
	content.style.padding = "10px";
	content.style.borderRight = '1px solid #bcbcbc';
	content.style.borderLeft = '1px solid #bcbcbc';
	content.style.backgroundColor = "#fff";
	
	/* jwindow event handlers */
	// Mouse over on head will show the move cursor
	head.onmouseover = function () { head.style.cursor = "move";}
	// mouse down on head with start the window move procedure
	head.onmousedown = function init(e) {
		if (!e) 
			var e = window.event;

		main.style.zIndex = zIndex.value++;
			
		if (isNaN(parseInt(main.style.left)))
			topX = 0;
		else
			topX = parseInt(main.style.left);
		if (isNaN(parseInt(main.style.top)))
			topY = 0;
		else
			topY = parseInt(main.style.top);
		
		currX = posMouseX(e);
		currY = posMouseY(e);
		
		if (e.preventDefault)
            e.preventDefault(); 
		else {
    		window.event.cancelBubble = true;
    		window.event.returnValue = false;
		}

		document.onmousemove = function setPos(e) {
			if (!e) 
				var e = window.event;
				
			newX = posMouseX(e);
			newY = posMouseY(e);
			
			if (e.preventDefault)
				e.preventDefault(); 
			else {
				window.event.cancelBubble = true;
				window.event.returnValue = false;
			}
	
			main.style.left = topX + newX - currX + "px";
			main.style.top = topY + newY - currY + "px";
		}
		
		main.onmousedown = function () { main.style.zIndex = zIndex.value++; }
		document.onmouseup = function () { document.onmousemove = null; }
	}
	
	/* SETTERS */
	this.setHeaderInnerHTML = function(html) {
		head.innerHTML = '<div style="padding:0px 5px 0px 5px">' + html + '</div>';
	}
	
	this.setContentInnerHTML = function(html)  {
		content.innerHTML = html;
	}
	
	this.setSize = function(w,h) {
		main.style.width = w + "px";
		main.style.minHeight = h + "px";
		// more mpl tweaks
		
		width = w;
		height = h;
		
		/*get(id+'_headerMain').style.width = (w - 30) + 'px';
		get(id+'_footerMain').style.width = (w - 20) + 'px';*/
	}
	
	this.setPosition = function(x,y) {
		main.style.left = x + 'px';
		main.style.top = y + 'px';
	}
	
	this.setTitle = function(aTitle) {
		/*var string = '<div style="overflow:hidden">' +
						'<div style="float:left;background:url(~assets/Img/jWindow/topLeft.gif) top left no-repeat;width:15px;height:29px">' +
						'</div>' +
						'<div id="'+id+'_headerMain" style="float:left;background:url(~assets/Img/jWindow/topMid.gif) top left repeat-x;height:24px;padding-top:5px">' +
							aTitle +
						'</div>' +
						'<div style="float:right;background:url(~assets/Img/jWindow/topRight.gif) top left no-repeat;width:15px;height:29px">' +
						'</div>' +
					'</div>';
		head.innerHTML = string;*/
		title = aTitle;
	}
	
	/* DISPLAY FUNCTIONS */
	// shows the window at the last hidden part
	this.show = function() {
		var string = '<div style="overflow:hidden">' +
						'<div style="float:left;background:url(~assets/Img/jWindow/topLeft.gif) top left no-repeat;width:15px;height:29px">' +
						'</div>' +
						'<div id="'+id+'_headerMain" style="float:left;background:url(~assets/Img/jWindow/topMid.gif) top left repeat-x;height:24px;padding-top:5px;width:'+(width - 30)+'px">' +
							title +
						'</div>' +
						'<div style="float:right;background:url(~assets/Img/jWindow/topRight.gif) top left no-repeat;width:15px;height:29px">' +
						'</div>' +
					'</div>';
		head.innerHTML = string;
		
		//footer tweaks
		var string = '<div style="overflow:hidden">' +
						'<div style="float:left;background:url(~assets/Img/jWindow/bottomLeft.gif) top left no-repeat;width:10px;height:10px">' +
						'</div>' +
						'<div id ="'+id+'_footerMain" style="float:left;background:url(~assets/Img/jWindow/bottomMid.gif) top left repeat-x;height:10px;width:'+(width - 20)+'px">' +
						'</div>' +
						'<div style="float:right;background:url(~assets/Img/jWindow/bottomRight.gif) top left no-repeat;width:10px;height:10px">' +
						'</div>' +
					'</div>';
		foot.innerHTML = string;
		
		main.style.zIndex = zIndex.value++;
		main.style.display = "block";
		opened = true;
	}
	
	// shows the window at the clicked position
	this.showClick = function(x, y) {
		var string = '<div style="overflow:hidden">' +
						'<div style="float:left;background:url(~assets/Img/jWindow/topLeft.gif) top left no-repeat;width:15px;height:29px">' +
						'</div>' +
						'<div id="'+id+'_headerMain" style="float:left;background:url(~assets/Img/jWindow/topMid.gif) top left repeat-x;height:24px;padding-top:5px;width:'+(width - 30)+'px">' +
							title +
						'</div>' +
						'<div style="float:right;background:url(~assets/Img/jWindow/topRight.gif) top left no-repeat;width:15px;height:29px">' +
						'</div>' +
					'</div>';
		head.innerHTML = string;
		
		//footer tweaks
		var string = '<div style="overflow:hidden">' +
						'<div style="float:left;background:url(~assets/Img/jWindow/bottomLeft.gif) top left no-repeat;width:10px;height:10px">' +
						'</div>' +
						'<div id ="'+id+'_footerMain" style="float:left;background:url(~assets/Img/jWindow/bottomMid.gif) top left repeat-x;height:10px;width:'+(width - 20)+'px">' +
						'</div>' +
						'<div style="float:right;background:url(~assets/Img/jWindow/bottomRight.gif) top left no-repeat;width:10px;height:10px">' +
						'</div>' +
					'</div>';
		foot.innerHTML = string;
		
		main.style.left = x + 10 + "px";
		main.style.top = y + 10 + "px";
		
		main.style.zIndex = zIndex.value++;
		main.style.display = "block";
		opened = true;
	}
	
	// shows the window in the center of the screen
	this.showCenter = function() {
		// header tweaks
		var string = '<div style="overflow:hidden">' +
						'<div style="float:left;background:url(~assets/Img/jWindow/topLeft.gif) top left no-repeat;width:15px;height:29px">' +
						'</div>' +
						'<div id="'+id+'_headerMain" style="float:left;background:url(~assets/Img/jWindow/topMid.gif) top left repeat-x;height:24px;padding-top:5px;width:'+(width - 30)+'px">' +
							title +
						'</div>' +
						'<div style="float:right;background:url(~assets/Img/jWindow/topRight.gif) top left no-repeat;width:15px;height:29px">' +
						'</div>' +
					'</div>';
		head.innerHTML = string;
		
		//footer tweaks
		var string = '<div style="overflow:hidden">' +
						'<div style="float:left;background:url(~assets/Img/jWindow/bottomLeft.gif) top left no-repeat;width:10px;height:10px">' +
						'</div>' +
						'<div id ="'+id+'_footerMain" style="float:left;background:url(~assets/Img/jWindow/bottomMid.gif) top left repeat-x;height:10px;width:'+(width - 20)+'px">' +
						'</div>' +
						'<div style="float:right;background:url(~assets/Img/jWindow/bottomRight.gif) top left no-repeat;width:10px;height:10px">' +
						'</div>' +
					'</div>';
		foot.innerHTML = string;
	
		var x = (posLeft() + posRight()) / 2;
		var y = (posTop() + posBottom()-100) / 2;
		
		var x2 = parseInt(main.style.width) / 2;
		var y2 = parseInt(main.style.minHeight) / 2;
		
		main.style.left = Math.abs(x - x2) + 'px';
		main.style.top = Math.abs(y - y2) + 'px';
		
		main.style.zIndex = zIndex.value++;
		main.style.display = "block";
		opened = true;
	}
	
	// simply hides the window from view
	this.hide = function() {
		main.style.display = "none";
		opened = false;
	}
	
	// hides and clears content 
	this.hideClear = function() {
		main.style.display = "none";
		content.innerHTML = '';
		opened = false;
	}
	
	this.isOpen = function() {
		return opened;
	}
}
