Windower.prototype.buildWindow = WINDOW_buildWindow;
Windower.prototype.close = WINDOW_close;
Windower.prototype.open = WINDOW_open;
Windower.prototype.minimise = WINDOW_minimise;
Windower.prototype.setTitle = WINDOW_setTitle;
Windower.prototype.setStatus = WINDOW_setStatus;
Windower.prototype.setSize = WINDOW_setSize;
Windower.prototype.move = WINDOW_move;
Windower.prototype.setContent = WINDOW_setContent;
//
Windower.prototype.startDrag = WINDOW_startDrag;
Windower.prototype.endDrag = WINDOW_endDrag;
Windower.prototype.startResize = WINDOW_startResize;
Windower.prototype.endResize = WINDOW_endResize;
Windower.prototype.confineToRectangle = WINDOW_confineToRectangle;
//
Windower.prototype.addControlButton = WINDOW_addControlButton;
Windower.prototype.checkControlButtonOverflow = WINDOW_checkControlButtonOverFlow;
Windower.prototype.scrollButtonsLeft = WINDOW_scrollButtonsLeft;
Windower.prototype.scrollButtonsRight = WINDOW_scrollButtonsRight;
Windower.prototype.setStyle = WINDOW_setStyle;
//
Windower.prototype.getButtonsWidth = WINDOW_getButtonsWidth;
//
var bIsIE = (navigator.userAgent.indexOf("MSIE") != -1 );
function Windower() {
	var scope = this;
	this.bodyRoot = document.getElementsByTagName("BODY")[0];
	//
	this.width;
	this.height;
	this.x;
	this.y;
	this.contentFunction;
	this.title = "Window";
	this.controlButtons = new Array();
	this.status;
	this.minimised = false;
	this.dragable = true;
	this.session = randomSuffix();
	this.disallowedCSS = ["width", "height", "top", "left", "right", "bottom", "display", "position", "visibility"];
	this.scrollingControls = false;
	this.bounds;
	this.keepToBounds = false;
	this.buttonInnards = true;
	//
	this.container;
	this.titleBar;
	this.titleBarInner;
	this.controlBar;
	this.controlBarInner;
	this.controlBarScrollLeft;
	this.controlBarScrollRight;
	this.content;
	this.statusBar;
	this.minimiseBtn;
	this.minimiseBtnInner1;
	this.minimiseBtnInner2;
	this.closeBtn;
	this.closeBtnInner;
	this.resizeCorner;
	//
	this.onOpen;
	this.onClose;
	this.onMinimise;
	this.onMaximise;
	this.onResize;
	this.onMove;
	//
	this.minimiseBtnContainer;
	this.closeBtnContainer;
	//
	this.windowStyle;
	this.titleBarStyle;
	this.controlButtonsStyle;
	this.contentStyle;
}
function WINDOW_confineToRectangle(x, y, w, h) {
	this.keepToBounds = true;
	this.bounds = {x:x, y:y, w:w, h:h};
}
function randomSuffix() {
	var cs = ["A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "_", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"];
	var s = "";
	for(var z = 0; z<16; ++z) {
		s += cs[Math.round(Math.random()*cs.length-1)];
	}
	return s;
}
function $c(t) { return document.createElement(t); };
function WINDOW_buildWindow(x, y, w, h, d, config) { //isDragable, isResizeable, hasCloseBtn, hasMinimiseBtn, hasControlBar, hasStatusBar) {
	var scope = this;
	this.width = w;
	this.height = h;
	this.minWidth = w;
	this.minHeight = h;
	this.x = x;
	this.y = y;
	this.depth = d;
	this.dragable = (config.isDragable == undefined ? true : config.isDragable);
	this.resizeable = (config.isResizeable == undefined ? true : config.isResizeable);
	this.hasCloseBtn = (config.hasCloseBtn == undefined ? true : config.hasCloseBtn);
	this.hasMinimiseBtn = (config.hasMinimiseBtn == undefined ? true : config.hasMinimiseBtn);
	this.hasControlBar = (config.hasControlBar == undefined ? false : config.hasControlBar);
	this.hasStatusBar = (config.hasStatusBar == undefined ? true : config.hasStatusBar);
	this.buttonInnards = (config.supressButtonGraphics == undefined ? true : !config.supressButtonGraphics);
	this.swapDepthOnDrag = false;
	//
	this.container = $c("DIV");
	this.container.setAttribute("id", "windowcontainer"+this.session);
	this.container.style.position = "absolute";
	this.container.style.zIndex = this.depth;
	this.container.style.width = this.width+"px";
	this.container.style.height = this.height+"px";
	this.container.style.border = "1px #000000 solid";
	this.container.style.left = x+"px";
	this.container.style.top = y+"px";
	//
	this.titleBar = $c("DIV");
	this.titleBar.setAttribute("id", "windowtitlebar"+this.session);
	this.titleBar.style.position = "relative";
	this.titleBar.style.height = "26px";
	this.titleBar.style.background = "#000000";
	this.titleBar.style.color = "#FFFFFF";
	if(this.dragable) {
		this.titleBar.style.cursor = "move";
		this.titleBar.onmousedown = function(e) {
			if (e == null) { e = window.event, e.target = e.srcElement }
			//alert(e.target.id);
			if(e.target.id.indexOf("minimse") == -1 && e.target.id.indexOf("close") == -1) {
				if(scope.swapDepthOnDrag) {
					var elTopIndex = getTopIndex();
					if(scope.container.style.zIndex != elTopIndex){
						scope.container.style.zIndex = elTopIndex + 1;
					}
				}
				scope.startDrag(e);
			}
		};
		this.titleBar.onmouseup = function(e) {
			if (e == null) { e = window.event, e.target = e.srcElement }
			//alert(e.target.id);
			if(e.target.id.indexOf("minimse") == -1 && e.target.id.indexOf("close") == -1) {
				scope.endDrag(e);
			}
		};
	}
	//
	this.titleBarInner = $c("DIV");
	this.titleBarInner.setAttribute("id", "windowtitlebarinner"+this.session);
	this.titleBarInner.style.display = "inline";
	this.titleBarInner.style.width = "90%";
	this.titleBarInner.style.height = "26px";
	this.titleBarInner.style.color = "#FFFFFF";
	this.titleBarInner.style.background = "#000000";
	this.titleBarInner.innerHTML = this.title;
	//
	if(this.hasControlBar) {
		this.controlBar = $c("DIV");
		this.controlBar.setAttribute("id", "windowcontrolbar"+this.session);
		this.controlBar.style.position = "relative";
		this.controlBar.style.height = "20px";
		this.controlBar.style.background = "#9A9A9A";
		//
		this.controlBarScrollLeft = $c("DIV");
		this.controlBarScrollLeft.setAttribute("id", "windowcbsleft"+this.session);
		this.controlBarScrollLeft.style.position = "absolute";
		this.controlBarScrollLeft.style.width = "12px";
		this.controlBarScrollLeft.style.borderRight = "1px #000000 solid";
		this.controlBarScrollLeft.style.textAlign = "center"
		this.controlBarScrollLeft.style.fontFamily = "Arial, Helvetica, sans-serif";
		this.controlBarScrollLeft.style.fontWeight = "bolder";
		this.controlBarScrollLeft.style.left = "0px";
		this.controlBarScrollLeft.style.top = "0px";
		this.controlBarScrollLeft.style.left = "0px";
		this.controlBarScrollLeft.style.cursor = "pointer";
		this.controlBarScrollLeft.innerHTML = "&lt;";
		this.controlBarScrollLeft.style.visibility = "hidden";
		this.controlBarScrollLeft.onmousedown = function(e) {
			e = e || window.event;
			scope.scrollingControls = true;
			scope.scrollButtonsRight(scope);
		};
		this.controlBarScrollLeft.onmouseup = function(e) {
			e = e || window.event;
			scope.scrollingControls = false;
		};
		//
		this.controlBarScrollRight = $c("DIV");
		this.controlBarScrollRight.setAttribute("id", "windowcbsleft"+this.session);
		this.controlBarScrollRight.style.position = "absolute";
		this.controlBarScrollRight.style.width = "12px";
		this.controlBarScrollRight.style.textAlign = "center"
		this.controlBarScrollRight.style.borderLeft = "1px #000000 solid";
		this.controlBarScrollRight.style.fontWeight = "bolder";
		this.controlBarScrollRight.style.fontFamily = "Arial, Helvetica, sans-serif";
		this.controlBarScrollRight.style.right = "0px";
		this.controlBarScrollRight.style.top = "0px";
		this.controlBarScrollRight.style.cursor = "pointer";
		this.controlBarScrollRight.style.visibility = "hidden";
		this.controlBarScrollRight.innerHTML = "&gt;";
		this.controlBarScrollRight.onmousedown = function(e) {
			e = e || window.event;
			scope.scrollingControls = true;
			scope.scrollButtonsLeft(scope);
		};
		this.controlBarScrollRight.onmouseup = function(e) {
			e = e || window.event;
			scope.scrollingControls = false;
		};
		//
		this.controlBarInner = $c("DIV");
		this.controlBarInner.style.position = "absolute"
		this.controlBarInner.style.overflow = "hidden";
		this.controlBarInner.setAttribute("id", "windowcontrolbarinner"+this.session);
		//this.controlBarInner.style.width = this.width+"px";
		this.controlBarInner.style.height = "20px";
	}
	//this.controlBarInner.style.display = "inline";
	//
	this.content = $c("DIV");
	this.content.setAttribute("id", "windowcontent"+this.session);
	if(this.hasControlBar && this.hasStatusBar) {
		this.content.style.height = (this.height - 66)+"px";
	} else if(this.hasControlBar || this.hasStatusBar && this.hasControlBar != this.hasStatusBar) {
		this.content.style.height = (this.height - 46)+"px";
	} else if(!this.hasControlBar && !this.hasStatusBar) {
		this.content.style.height = (this.height - 26)+"px";
	}
	this.content.style.background = "#FFFFFF";
	this.content.style.overflow = "auto";
	//
	this.statusBar = $c("DIV");
	this.statusBar.setAttribute("id", "windowstatusbar"+this.session);
	//this.statusBar.style.position = "absolute";
	this.statusBar.style.height = "20px";
	//this.statusBar.style.bottom = "0px";
	this.statusBar.style.background = "#CCCCCC";
	//
	if(this.hasCloseBtn) {
		this.closeBtn = $c("DIV");
		this.closeBtn.setAttribute("id", "windowclosebtn"+this.session);
		this.closeBtn.style.width = "15px";
		this.closeBtn.style.height = "15px";
		this.closeBtn.style.border = "1px #FFFFFF solid";
		this.closeBtn.style.background = "#000000";
		this.closeBtn.style.position = "relative";
		bIsIE ? this.closeBtn.style.styleFloat = "right" : this.closeBtn.style.cssFloat = "right";
		this.closeBtn.style.marginLeft = "2px";
		this.closeBtn.style.cursor = "pointer";
		this.closeBtn.onclick = function() { scope.close(); };
		//
		if(this.buttonInnards) {
			this.closeBtnInner = $c("DIV");
			this.closeBtnInner.setAttribute("id", "windowclosebtninner"+this.session);
			this.closeBtnInner.style.position = "absolute";
			this.closeBtnInner.style.width = "7px";
			this.closeBtnInner.style.height = "7px";
			this.closeBtnInner.style.background = "#FF0000";
			this.closeBtnInner.style.left = "4px";
			this.closeBtnInner.style.top = "4px";
			this.closeBtn.appendChild(this.closeBtnInner);
		}
		this.titleBar.appendChild(this.closeBtn);
	}
	//
	if(this.hasMinimiseBtn) {
		this.minimiseBtn = $c("DIV");
		this.minimiseBtn.setAttribute("id", "windowminimsebtn"+this.session);
		this.minimiseBtn.style.position = "relative";
		this.minimiseBtn.style.background = "#000000";
		this.minimiseBtn.style.border = "1px #FFFFFF solid";
		this.minimiseBtn.style.width = "15px";
		this.minimiseBtn.style.height = "15px";
		bIsIE ? this.minimiseBtn.style.styleFloat = "right" : this.minimiseBtn.style.cssFloat = "right";
		this.minimiseBtn.style.cursor = "pointer";
		this.minimiseBtn.onclick = function() { scope.minimise(); };
		//
		if(this.buttonInnards) {
			this.minimiseBtnInner1 = $c("DIV");
			this.minimiseBtnInner1.setAttribute("id", "windowminimsebtninner1"+this.session);
			this.minimiseBtnInner1.style.position = "absolute";
			this.minimiseBtnInner1.style.background = "#FFFFFF";
			this.minimiseBtnInner1.style.width = "3px";
			this.minimiseBtnInner1.style.height = "15px";
			this.minimiseBtnInner1.style.left = "6px";
			this.minimiseBtnInner1.style.visibility = "hidden";
			//
			this.minimiseBtnInner2 = $c("DIV");
			this.minimiseBtnInner2.setAttribute("id", "windowminimsebtninner2"+this.session);
			this.minimiseBtnInner2.style.position = "absolute";
			this.minimiseBtnInner2.style.background = "#FFFFFF";
			this.minimiseBtnInner2.style.width = "15px";
			this.minimiseBtnInner2.style.height = "3px";
			this.minimiseBtnInner2.style.top = "6px";
			this.minimiseBtn.appendChild(this.minimiseBtnInner1);
			this.minimiseBtn.appendChild(this.minimiseBtnInner2);
		}
		this.titleBar.appendChild(this.minimiseBtn);
	}
	if(this.resizeable) {
		this.resizeCorner = $c("DIV");
		this.resizeCorner.setAttribute("id", "windowresizecorner"+this.session);
		this.resizeCorner.style.position = "absolute";
		this.resizeCorner.style.width = "15px";
		this.resizeCorner.style.height = "15px";
		this.resizeCorner.style.background = "#0066FF"
		this.resizeCorner.style.bottom = "0px";
		this.resizeCorner.style.right = "0px";
		this.resizeCorner.style.cursor = "se-resize";
		this.container.appendChild(this.resizeCorner);
		this.resizeCorner.onmousedown = function(e) {
			e = e || window.event;
			//alert("resizeClick");
			scope.startResize(e);
		};
	}
	//
	this.titleBar.appendChild(this.titleBarInner);
	//
	this.container.appendChild(this.titleBar);
	if(this.hasControlBar) {
		this.controlBar.appendChild(this.controlBarScrollLeft);
		this.controlBar.appendChild(this.controlBarInner);
		this.controlBar.appendChild(this.controlBarScrollRight);
		this.container.appendChild(this.controlBar);
		this.checkControlButtonOverflow();
	}
	this.container.appendChild(this.content);
	if(this.hasStatusBar) {
		this.container.appendChild(this.statusBar);
	}
	//
	this.open();
}
function WINDOW_setContent(htmlOrURL) {
	if(typeof(htmlOrURL) == "string") {
		if(htmlOrURL.match(/^http:\/\/\S*$/i)) {
			var pageFrame = $c("IFRAME");
			pageFrame.setAttribute("id", "pageframe");
			pageFrame.setAttribute("name", "pageframe");
			pageFrame.setAttribute("frameborder", "0");
			pageFrame.setAttribute("marginwidth", "0");
			pageFrame.setAttribute("marginheight", "0");
			pageFrame.setAttribute("scrolling", "auto");
			//pageFrame.setAttribute("width", "auto");
			//pageFrame.setAttribute("height", "auto");
			pageFrame.style.width = "100%";
			pageFrame.style.height = "100%";
			pageFrame.setAttribute("src", htmlOrURL);
			this.content.appendChild(pageFrame);
		} else {
			this.content.innerHTML = htmlOrURL;
		}
	} else {
		this.content.appendChild(htmlOrURL);
	}
}
function WINDOW_minimise() {
	if(this.minimised) {
		this.content.style.visibility = "visible";
		this.content.style.display = "block";
		if(this.hasControlBar) {
			this.controlBar.style.visibility = "visible";
			this.controlBar.style.display = "block";
		}
		if(this.hasStatusBar) {
			this.statusBar.style.visibility = "visible";
			this.statusBar.style.display = "block";
		}
		if(this.buttonInnards) this.minimiseBtnInner1.style.visibility = "hidden";
		this.container.style.height = this.height+"px";
		if(this.resizeable) {
			this.resizeCorner.style.visibility = "visible";
		}
		if(this.onMaximise != undefined) {
			this.onMaximise();
		}
	} else {
		this.content.style.visibility = "hidden";
		this.content.style.display = "none";
		if(this.hasControlBar) {
			this.controlBar.style.visibility = "hidden";
			this.controlBar.style.display = "none";
		}
		if(this.hasStatusBar) {
			this.statusBar.style.visibility = "hidden";
			this.statusBar.style.display = "none";
		}
		if(this.buttonInnards)this.minimiseBtnInner1.style.visibility = "visible";
		this.container.style.height = "26px"; 
		if(this.resizeable) {
			this.resizeCorner.style.visibility = "hidden";
		}
		if(this.onMinimise != undefined) {
			this.onMinimise();
		}
	}
	this.minimised = !this.minimised;
}
function WINDOW_close() {
	this.bodyRoot.removeChild(this.container);
	if(this.onClose != undefined) {
		this.onClose();
	}
}
function WINDOW_open() {
	this.bodyRoot.appendChild(this.container);
	if(this.onOpen != undefined) {
		this.onOpen();
	}
}
function WINDOW_setTitle(t) {
	this.title = t;
	this.titleBarInner.innerHTML = t;
}
function WINDOW_setStatus(t) {
	this.status = t;
	this.statusBar.innerHTML = t;
}
function WINDOW_setSize(w, h) {
	this.width = w;
	this.height = h;
	this.container.style.width = w+"px";
	this.container.style.height = h+"px";
	if(this.hasControlBar && this.hasStatusBar) {
		this.content.style.height = (this.height - 66)+"px";
	} else if(this.hasControlBar || this.hasStatusBar && this.hasControlBar != this.hasStatusBar) {
		this.content.style.height = (this.height - 46)+"px";
	} else if(!this.hasControlBar && !this.hasStatusBar) {
		this.content.style.height = (this.height - 26)+"px";
	}
	if(this.hasControlBar) {
		this.checkControlButtonOverflow();
	}
	if(this.onResize != undefined) {
		this.onResize();
	}
}
function WINDOW_move(x, y) {
	if(this.keepToBounds) {
		if(x < this.bounds.x) {
			x = this.bounds.x;
		} else if((x+this.width)>(this.bounds.x+this.bounds.w)) {
			x = ((this.bounds.x+this.bounds.w) - this.width);
		}
		if(y < this.bounds.y) {
			y = this.bounds.y;
		} else if((y+this.height)>(this.bounds.y+this.bounds.h)) {
			y = ((this.bounds.y+this.bounds.h) - this.height);
		}
	} 
	this.container.style.left = x+"px";
	this.container.style.top = y+"px";
	this.x = x;
	this.y = y;
	if(this.onMove != undefined) {
		this.onMove();
	}
}
function WINDOW_addControlButton(label, callBack, floatDir) {
	if(this.hasControlBar) {
		var button = $c("INPUT");
		button.setAttribute("id", "controlbutton"+label+this.session);
		button.setAttribute("type", "button");
		button.setAttribute("value", label);
		button.style.position = "absolute";
		button.style.padding = "0px";
		button.style.margin = "0px";
		if(bIsIE) {
			button.style.width = "100px";
		} else {
			//button.style.width = "auto";
		}
		//button.style.display = "inline";
		//bIsIE ? button.style.styleFloat = floatDir : button.style.cssFloat = floatDir;
		if(callBack != undefined && callBack != null) {
			button.onclick = callBack;
		}
		this.controlBarInner.appendChild(button);
		
		if(this.controlButtons.length > 0) {
			button.style.left = this.getButtonsWidth()+"px";
		}
		this.controlButtons.push(button);
		this.checkControlButtonOverflow();
	}
}
function WINDOW_getButtonsWidth(){
	var w = 0;
	for(var z = 0; z<this.controlButtons.length; ++z) {
		w += this.controlButtons[z].offsetWidth;
	}
	return w;
}
function WINDOW_checkControlButtonOverFlow() {
	var w = 0;
	for(var z = 0; z<this.controlButtons.length; ++z) {
		w += this.controlButtons[z].offsetWidth;
	}
	if(w > this.controlBar.offsetWidth) {
		this.controlBarInner.style.width = (this.width - 24)+"px";
		this.controlBarScrollLeft.style.visibility = "visible";
		this.controlBarScrollRight.style.visibility = "visible";
		this.controlBarInner.style.left = "12px";
	} else {
		this.controlBarInner.style.left = "0px";
		this.controlBarInner.style.width = this.width+"px";
		this.controlBarScrollLeft.style.visibility = "hidden";
		this.controlBarScrollRight.style.visibility = "hidden";
	}
}
function WINDOW_scrollButtonsLeft(scope) {
	var lastButton = scope.controlButtons[scope.controlButtons.length-1];
	if(lastButton.offsetLeft+lastButton.offsetWidth > scope.controlBarInner.offsetWidth && scope.scrollingControls) {
		for(var z = 0; z<scope.controlButtons.length; ++z) {
			scope.controlButtons[z].style.left = (scope.controlButtons[z].offsetLeft - 4)+"px";
		}
		setTimeout(scope.scrollButtonsLeft, 5, scope);
	}
}
function WINDOW_scrollButtonsRight(scope) {
	var firstButton = scope.controlButtons[0];
	if(firstButton.offsetLeft < 0 && scope.scrollingControls) {
		for(var z = 0; z<scope.controlButtons.length; ++z) {
			scope.controlButtons[z].style.left = (scope.controlButtons[z].offsetLeft + 4)+"px";
		}
		setTimeout(scope.scrollButtonsRight, 5, scope);
	}
}
/****************************
     DRAGGING FUNCTIONS
****************************/
function w_getScrollAmount() {
	var x,y;
	if (self.pageYOffset) {
		x = self.pageXOffset;
		y = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {
		x = document.documentElement.scrollLeft;
		y = document.documentElement.scrollTop;
	} else if (document.body) {
		x = document.body.scrollLeft;
		y = document.body.scrollTop;
	}
	return {x:x, y:y};
}
function mouseCoords(ev){
	var sa = w_getScrollAmount();
	if(ev.pageX || ev.pageY){
		return {x:ev.pageX, y:ev.pageY};
	}
	return {
		x:ev.clientX + sa.x - document.body.clientLeft,
		y:ev.clientY + sa.y  - document.body.clientTop
	};
}
function WINDOW_startDrag(e) {
	//changeBodyCursor("move");
	var scope = this
	e = e || window.event;
	document.onmouseup = scope.endDrag;
	dragTarget = this.container;
	dragOffset = {x:mouseCoords(e).x - dragTarget.offsetLeft, y:mouseCoords(e).y - dragTarget.offsetTop} 
	document.onmousemove = function(e) {
		e = e || window.event;
		var m = mouseCoords(e);
		scope.move( m.x - dragOffset.x,  m.y - dragOffset.y);
		/*dragTarget.style.left = m.x - dragOffset.x+"px";
		dragTarget.style.top = m.y - dragOffset.y+"px";
		scope.x = m.x - dragOffset.x;
		scope.y = m.y - dragOffset.y;
		if(scope.onMove != undefined) {
			scope.onMove();
		}*/
	};
}
function WINDOW_endDrag(e) {
	if (e == null) { e = window.event, e.target = e.srcElement; }
	//changeBodyCursor("default");
	document.onmouseup = document.onmousemove = null;
}


/****************************
     RESIZING FUNCTIONS
****************************/
function WINDOW_startResize(e) {
	var scope = this;
	document.onmouseup = scope.endResize;
	var cW, cH;
	//changeBodyCursor("se-resize");
	document.onmousemove = function(e) {
		e = e || window.event;
		var m = mouseCoords(e);
		var nW = m.x - scope.container.offsetLeft;
		var nH = m.y - scope.container.offsetTop;
		if(nW >= scope.minWidth) {
			cW = nW;
		} else {
			cW = scope.minWidth;
		}
		if(nH >= scope.minHeight) {
			cH = nH;
		} else {
			cH = scope.minHeight;
		}
		scope.setSize(cW, cH);
	}
}
function WINDOW_endResize() {
	//changeBodyCursor("default");
	document.onmouseup = document.onmousemove = null;
}

/****************************
     STYLING FUNCTIONS
****************************/
Array.prototype.getIndex = function(aItem) {
	var start = 0;
	var finish = this.length-1;
	var m;
	while (start<=finish) {
		m = Math.floor((start+finish)/2);
		if (aItem<this[m]) {
			finish = m-1;
		} else if (aItem>this[m]) {
			start = m+1;
		} else {
			return m;
		}
	}
	return -1;
}
function WINDOW_setStyle(element, attributes, values){
	var disAllowedAttributes = ["left", "right", "top", "bottom", "width", "height", "display", "visiblity", "position"];
	if(typeof(attributes) != typeof(values)) {
		alert("Styling for '"+element+"' failed, attributes' and values' datatypes do not match")
		return;
	}
	if(typeof(attributes) == "object" && typeof(values) == "object") {
		var discarded = "";
		for(var z = 0; z<disAllowedAttributes.length; ++z) {
			var pos = attributes.getIndex(disAllowedAttributes[z]);
			if(pos != -1) {
				discarded += attributes[z]+", ";
				attributes.splice(pos, 1);
				values.splice(pos, 1);
			}
			//this.setTitle(attributes);
		}
		if(discarded != "") alert(discarded+"are dissallowed styling attributes");
		if(attributes.length != values.length) {
			alert("Styling for '"+element+"' failed, Number of values does not match number of attributes");
			return;
		}
	} else if (typeof(attributes) == "string" && typeof(values) == "string") {
		var pos = disAllowedAttributes.getIndex(attributes);
		if(pos != -1) {
			alert(attributes+" is a disallowed styling attribute");
			return;
		}
	}
	switch (element) {
		case "titlebar" :
			if(typeof(values) != "object") {
				this.titleBar.style[attributes] = values;
				if(attributes[z] != "border") {
					this.titleBarInner.style[attributes] = values;
				} else {
					this.titleBarInner.style[attributes] = "";
				}
			} else {
				for(var z = 0; z<attributes.length; ++z) {
					this.titleBar.style[attributes[z]] = values[z];
					if(attributes[z] != "border") {
						this.titleBarInner.style[attributes[z]] = values[z];
					} else {
						this.titleBarInner.style[attributes[z]] = "";
					}
				}
			}
			break;
		case "closebutton" :
			if(this.hasCloseBtn) {
				if(typeof(values) != "object") {
					this.closeBtn.style[attributes] = values;
				} else {
					for(var z = 0; z<attributes.length; ++z) {
						this.closeBtn.style[attributes[z]] = values[z];
					}
				}
			}
			break;
		case "minimisebutton" :
			if(this.hasMinimiseBtn) {
				if(typeof(values) != "object") {
					this.minimiseBtn.style[attributes] = values;
				} else {
					for(var z = 0; z<attributes.length; ++z) {
						this.minimiseBtn.style[attributes[z]] = values[z];
					}
				}
			}
			break;
		case "resizecorner" :
			if(this.resizeable) {
				if(typeof(values) != "object") {
					this.resizeCorner.style[attributes] = values;
				} else {
					for(var z = 0; z<attributes.length; ++z) {
						this.resizeCorner.style[attributes[z]] = values[z];
					}
				}
			}
			break;
		case "controlbuttons" :
			if(this.hasControlBar) {
				for(var j = 0; j<this.controlButtons.length; ++j) {
					if(typeof(values) != "object") {
						this.controlButtons[j].style[attributes] = values;
					} else {
						for(var z = 0; z<attributes.length; ++z) {
							this.controlButtons[j].style[attributes[z]] = values[z];
						}
					}
				}
			}
			break;
		case "statusbar" :
			if(this.hasStatusBar) {
				if(typeof(values) != "object") {
					this.statusBar.style[attributes] = values;
				} else {
					for(var z = 0; z<attributes.length; ++z) {
						this.statusBar.style[attributes[z]] = values[z];
					}
				}
			}
			break;
		case "controlbar" :
			if(this.hasControlBar) {
				if(typeof(values) != "object") {
					this.controlBar.style[attributes] = values;
				} else {
					for(var z = 0; z<attributes.length; ++z) {
						this.controlBar.style[attributes[z]] = values[z];
					}
				}
			}
			break;
	}
}
/*function removeCSSAttributes(cssClass, attributes) {
	var cssRules = window.ActiveXObject ? "rules" : "cssRules";
	for (var z = 0; z < document.styleSheets.length; z++){
		for (var j = 0; j < document.styleSheets[z][cssRules].length; j++) {
			if (document.styleSheets[z][cssRules][j].selectorText == cssClass) {
				for(var a = 0; a<attributes.length; ++a) {
					document.styleSheets[z][cssRules][j].style.removeProperty(attributes[a]);
				}
			}
			break;
		}
	}
}*/
function $_td(element, tag) {
	var tags = element.getElementsByTagName(tag);
	var returnTags = new Array();
	for(var z = 0; z<tags.length; ++z) {
		if(tags[z].parentNode.nodeName == "BODY") {
			returnTags.push(tags[z]);
		}
	}
	return returnTags;
}
function getTopIndex() {
	var topTags = $_td(document.getElementsByTagName("BODY")[0], "*");
	var top = Number(topTags[0].style.zIndex);
	for(var z = 1; z<topTags.length; ++z) {
		if(Number(topTags[z].style.zIndex) > top) {
			top = Number(topTags[z].style.zIndex);
		}
	}
	return top;
}