var currentFocus;
var zIndex = 0;
var mouse = {x:0,y:0};
//document.body.onmousedown = function(){ return false;}
if(document.addEventListener){
	document.addEventListener('mousemove',setMouse,false);
}else if(document.attachEvent){
	document.attachEvent('onmousemove',setMouse,false);
}
function setMouse(e){
	if(!e) e = window.event;
	mouse.x = e.clientX;
	mouse.y = e.clientY;
	return false;
}
function newDiv(parent){
	var div = document.createElement("div");
	div.name = "div";
	div.className = "display";
	div.parent = parent;
    parent.appendChild(div);
	div.x = 0;
	div.y = 0;
	div.width = 0;
	div.height = 0;
	div.dragging = false;
	div.mxoff = 0;
	div.myoff = 0;
	div.setPos = function(x,y){
		div.x = x;
		div.y = y;
		div.style.left = x+"px";
		div.style.top = y+"px";
	}
	div.setX = function(x){
		div.x = x;
		div.style.left = x+"px";
	}
	div.setY = function(y){
		div.y = y;
		div.style.top = y+"px";
	}
	div.setDims = function(width,height){
		div.width = width;
		div.height = height;
		div.style.width = width+"px";
		div.style.height = height+"px";
	}
	div.setWidth = function(width){
		div.width = width;
		div.style.width = width+"px";
	}
	div.setHeight = function(height){
		div.height = height;
		div.style.height = height+"px";
	}
	div.getWidth = function(){
		return div.offsetWidth;
	}
	div.getHeight = function(){
		return div.offsetHeight;
	}
	div.startDrag = function(){
		//alert("dragging");
		div.mxoff = div.x-mouse.x;
		div.myoff = div.y-mouse.y;
		div.dragging = true;
	}
	div.stopDrag = function(){
		div.dragging = false;
	}
	div.drag = function(){
		//alert(div.dragging);
		if(div.dragging){
			div.setPos(mouse.x+div.mxoff,mouse.y+div.myoff);
			if(div.x<0){
				div.setX(0);
				div.mxoff = div.x-mouse.x;
			}else if(div.x+div.offsetWidth>document.body.clientWidth){
				div.setX(document.body.clientWidth-div.offsetWidth);
				div.mxoff = div.x-mouse.x;
			}
			if(div.y<0){
				div.setY(0);
				div.myoff = div.y-mouse.y;
			}
		}
	}
	div.remove = function(){
		div.parent.removeChild(div);
	}
	return div;
}
function newImg(parent){
	var img = newDiv(parent);
	img.className = "img";
	img.img = document.createElement("img");
	img.appendChild(img.img);
	img.img.onmousedown = function(){ return false; }
	img.setDims = function(width,height){
		img.width = width;
		img.height = height;
		//img.style.width = width+"px";
		//img.style.height = height+"px";
		img.img.width = width;
		img.img.height = height;
	}
	img.setWidth = function(width){
		img.width = width;
		//img.style.width = width+"px";
		img.img.width = width;
	}
	img.setHeight = function(height){
		img.height = height;
		//img.style.height = height+"px";
		img.img.height = height;
	}
	img.set = function(src){
		img.img.src = src;
		img.src = src;
	}
	img.remove = function(){
		img.parent.removeChild(img);
	}
	return img;
}
function newWindow(parent){
	var win = newDiv(parent);
	win.className = "window";
	makeWindow(win);
	win.setTitle = function(newTitle){
		win.windowTitle = newTitle;
		win.bar.titleDiv.innerHTML = newTitle;
	}
	win.setContents = function(html){
		win.content.innerHTML = html;
	}
	currentFocus = win;
	win.style.zIndex = zIndex;
	zIndex++;
	win.hasFocus = function(){
		//var curZ = currentFocus.style.zIndex;
		//currentFocus.style.zIndex = win.style.zIndex;
		win.style.zIndex = parseInt(currentFocus.style.zIndex)+1;//curZ;
		currentFocus = win;
	}
	if(win.addEventListener){
		win.addEventListener("mousedown",win.hasFocus,false);
	}else if(win.attachEvent){
		win.attachEvent('onmousedown',win.hasFocus);
	}
	win.dragger = function(starter){
		if(win.addEventListener){
			starter.addEventListener("mousedown",win.startDrag,false);
			document.addEventListener("mousemove",win.drag,false);
			document.addEventListener("mouseup",win.stopDrag,false);
		}else if(win.attachEvent){
			starter.attachEvent('onmousedown',win.startDrag);
			document.attachEvent('onmousemove',win.drag);
			document.attachEvent('onmouseup',win.stopDrag);
		}
		starter.style.cursor = "move";
	}
	win.addButton = function(num){
		var buttons = win.bar.buttons;
		if(num == 0){
			var shade = document.createElement("div");
			shade.className = "shade_button";
			shade.onmouseup = win.shade;
			buttons.appendChild(shade);
			buttons.shade = shade;
		}else if(num == 1){
			var close = document.createElement("div");
			close.className = "close_button";
			close.onmouseup = win.close;
			buttons.appendChild(close);
			buttons.close = close;
		}else if(num == 2){
		}
	}
	win.close = function(){
		if(win.addEventListener){
			document.removeEventListener("mousemove",win.drag,false);
			document.removeEventListener("mouseup",win.stopDrag,false);
		}else if(win.attachEvent){
			document.detachEvent('onmousemove',win.drag);
			document.detachEvent('onmouseup',win.stopDrag);
		}
		win.remove();
		win.innerHTML = "";
	}
	win.shade = function(){
		if(win.content.style.height != "0px"){
			win.content.style.height = "0px"
			win.foot.style.visibility = "hidden";
		}else{
			win.content.style.height = ""
			win.foot.style.visibility = "visible";
		}
	}
	return win;
}
function makeWindow(win){
	win.innerHTML = '<div class="window_cap"><div class="window_cap_left"><div class="window_cap_right"><div class="window_cap_middle"></div></div></div></div><div class="window_bar"><div class="window_border_left"><div class="window_border_right"><div id="newWindowBarContent" class="window_bar_content"><div id="newWindowTitle" class="window_bar_title"></div><div id="newWindowButtons" class="window_bar_buttons"></div><div class="window_title_foot"></div></div></div></div></div><div class="window_middle"><div class="window_border_left"><div class="window_border_right"><div id="newWindowContent" class="window_content"></div></div></div></div><div id="newWindowFoot" class="window_foot"><div class="window_corner_left"><div class="window_corner_right"><div class="window_border_bottom"></div></div></div></div>';
	win.bar = document.getElementById("newWindowBarContent");
	win.bar.id = "";
	win.bar.titleDiv = document.getElementById("newWindowTitle");
	win.bar.titleDiv.id = "";
	win.bar.buttons = document.getElementById("newWindowButtons");
	win.bar.buttons.id = "";
	win.content = document.getElementById("newWindowContent");
	win.content.id = "";
	win.foot = document.getElementById("newWindowFoot");
	win.foot.id = "";
}
