/**
 * The Zapatec DHTML Menu
 *
 * Copyright (c) 2004-2005 by Zapatec, Inc.
 * http://www.zapatec.com
 * 1700 MLK Way, Berkeley, California,
 * 94709, U.S.A.
 * All rights reserved.
 *
 * Windows Widget
 * $$
 *
 */
/// Window object constructor.
Zapatec.Window = function (config) {
	this.winDiv = null;
	this.titleArea = null;
	this.titleText = null;
	this.minButton = null;
	this.maxButton = null;
	this.closeButton = null;
	this.contentArea = null;	
	this.statusArea = null;
	this.resizeArea = null;
	this.winNumber = 0;
	this.config = {};
	this.config.showTitle = true;
	this.config.showRestoreButton = false;
	this.config.minWidth = 180;
	this.config.minHeight = 70;
	this.config.state = "simple";
	this.config.titleWidth = 0;
	this.config.contentWidth = 0;
	this.config.statusWidth = 0;
	this.config.heightDiff = 0;
	this.config.left = 0;
	this.config.top = 0;
	this.config.width = 0;
	this.config.height = 0;
	this.setConfig(config);
}

Zapatec.Window.prototype.setConfig = function (config) {
	this.config.showMinButton = (typeof config.showMinButton != "undefined") ? config.showMinButton : true;
	this.config.showMaxButton = (typeof config.showMaxButton != "undefined") ? config.showMaxButton : true;
	this.config.showCloseButton = (typeof config.showCloseButton != "undefined") ? config.showCloseButton : true;
	this.config.showStatus = (typeof config.showStatus != "undefined") ? config.showStatus : true;
	this.config.canResize = (typeof config.showResize != "undefined") ? config.showResize : true;
	this.config.raiseOnlyOnTitle = (typeof config.raiseOnlyOnTitle != "undefined") ? config.raiseOnlyOnTitle : true;
	this.config.canDrag = (typeof config.canDrag != "undefined") ? config.canDrag : true;
	this.config.modal = (typeof config.modal != "undefined") ? config.modal : false;
	this.onContentLoad = (typeof config.onLoad != "undefined") ? config.onLoad : function () {};
}

Zapatec.Window.setup = function (config) {
	if (!config) config = {};
	if (config.popup) {
		var win = new Zapatec.Window(config);
		if (typeof config.popup == "string") {
			config.popup = document.getElementById(config.popup);
		}
		if (!config.popup) {
			alert("You specified wrong trigger element!");
			win.destroy();
			return;
		}
		if (!config.triggerEvent) {
			config.triggerEvent = "click";
		}
		if (!config.align) {
			config.align = null;
		}
		el = config.popup;
		el["on" + config.triggerEvent] = function (ev) {
			if (!win.winDiv) {	
				win.create(0, 0, config.width || win.config.minWidth, config.height || win.config.minHeight);
				if (config.title) {
					win.setTitle(config.title);
				}
				if (config.content) {
					win.setContent(config.content);
				}
				if (config.divContent) {
					win.setDivContent(config.divContent);
				}
				if (config.urlContent) {
					win.setContentUrl(config.urlContent);
				}
			}
			if (!win.config.visible) {
				win.showAtElement(this, config.align);
			}
			
			return false;
		}
		
		return;
	} else {
		var win = new Zapatec.Window(config);
		win.create(config.left || 0, config.top || 0, config.width || win.config.minWidth, config.height || win.config.minHeight);
		if (config.title) {
			win.setTitle(config.title);
		}
		if (config.content) {
			win.setContent(config.content);
		}
		if (config.divContent) {
			win.setDivContent(config.divContent);
		}
		if (config.urlContent) {
			win.setContentUrl(config.urlContent);
		}
		win.show();
	}
	
	return win;
} 

Zapatec.Window.mouseDown = function (ev, win, target) {
	if (!win.config.raiseOnlyOnTitle) {
		win.raise();
	} else {
		if (target && ((target.buttonType == "move") || (target.buttonType == "min") || (target.buttonType == "max") || (target.buttonType == "close") || (target.buttonType == "restore"))) {
			win.raise();		
		}
	}
	if (target) {
		switch (target.buttonType) {
			case "move" : {
				if (!target.customMouseDown) {
					if (win.config.canDrag && win.config.state != "min" && win.config.state != "max") {
						win.showStatus('Drag the window', 'temp');
						var posX = ev.pageX || ev.clientX + window.document.body.scrollLeft || 0;
						var posY = ev.pageY || ev.clientY + window.document.body.scrollTop || 0;
						var L = parseInt(win.winDiv.style.left) || 0;
						var T = parseInt(win.winDiv.style.top) || 0;
						win.winDiv.xOffs = (posX - L);
						win.winDiv.yOffs = (posY - T);
						win.titleArea.style.cursor = "move";
					}
				} else {
					target.customMouseDown(ev, win, target);
				}
				break;
			}

			case "resize" : {
				if (!target.customMouseDown) {
					if (win.config.canResize && win.config.state != "min" && win.config.state != "max") {
						win.showStatus('resize the window', 'temp');
						win.winDiv.xOffs = parseInt(win.winDiv.style.left) || 0;
						win.winDiv.yOffs = parseInt(win.winDiv.style.top) || 0;
					}
				} else {
					target.customMouseDown(ev, win, target);
				}
				break;
			}

			case "close" : {
				if (!target.customMouseDown) {
				} else {
					target.customMouseDown(ev, win, target);
				}
				break;
			}

			case "min" : {
				if (!target.customMouseDown) {
				} else {
					target.customMouseDown(ev, win, target);
				}
				break;
			}

			case "max" : {
				if (!target.customMouseDown) {
				} else {
					target.customMouseDown(ev, win, target);
				}
				break;
			}

			case "restore" : {
				if (!target.customMouseDown) {
				} else {
					target.customMouseDown(ev, win, target);
				}
				break;
			}
		}
	}
}

Zapatec.Window.mouseMove = function (ev, win, target) {
	if (target) {
		switch (target.buttonType) {
			case "move" : {
				if (!target.customMouseMove) {
					if (win.config.canDrag && win.config.state != "min" && win.config.state != "max") {
						var posX = ev.pageX || ev.clientX + window.document.body.scrollLeft || 0;
						var posY = ev.pageY || ev.clientY + window.document.body.scrollTop || 0;
						var L = posX - win.winDiv.xOffs, T = posY - win.winDiv.yOffs;
						win.setPos(L, T);
					}
				} else {
					target.customMouseMove(ev, win, target);
				}
				break;
			}

			case "resize" : {
				if (!target.customMouseMove) {
					if (win.config.canResize && win.config.state != "min" && win.config.state != "max") {
						var posX = ev.pageX || ev.clientX + window.document.body.scrollLeft || 0;
						var posY = ev.pageY || ev.clientY + window.document.body.scrollTop || 0;

						var style = win.winDiv.style;
					
						var newWidth =  posX - win.winDiv.xOffs;
						if (newWidth < win.config.minWidth) { 
							newWidth = win.config.minWidth;
						}
					
						var newHeight = posY - win.winDiv.yOffs - win.config.heightDiff;
						if (newHeight < win.config.minHeight) { 
							newHeight = win.config.minHeight;
						}
					
						win.setSize(newWidth, newHeight);
					}
				} else {
					target.customMouseMove(ev, win, target);
				}
				break;
			}
		}
	}
}

Zapatec.Window.mouseUp = function (ev, win, target) {
	if (target) {
		switch (target.buttonType) {
			case "move" : {
				if (!target.customMouseUp) {
					if (win.config.canDrag) {
						win.showStatus('', 'restore');
						win.titleArea.style.cursor = "default";
					}
				} else {
					target.customMouseUp(ev, win, target);
				}
				break;
			}

			case "resize" : {
				if (!target.customMouseUp) {
					if (win.config.canResize) {
						win.showStatus('', 'resize');
					}
				} else {
					target.customMouseUp(ev, win, target);
				}
				break;
			}

			case "close" : {
				if (!target.customMouseUp) {
					win.close();
				} else {
					target.customMouseUp(ev, win, target);
				}
				break;
			}

			case "min" : {
				if (!target.customMouseUp) {
					if (win.config.showMinButton && win.state != "min" && target == (ev.srcElement || ev.target)) {
						if (win.contentArea) {
							var style = win.titleArea.nextSibling.style;
							style.display = "none";
						}
						if (win.statusArea || win.resizeArea) {
							style = win.titleArea.nextSibling.nextSibling.style;
							style.display = "none";
						}
						target.buttonType = "restore";
						target.className = "zpWinRestoreButton";
						win.winDiv.style.width = Zapatec.Window.minWinWidth + "px";
						if (win.titleText) win.titleText.style.width = (Zapatec.Window.minWinWidth - (win.config.width - win.config.titleWidth)) + "px";
						win.winDiv.style.left = Zapatec.Window.minimizeLeft + "px";
						
						win.winDiv.style.top = "";
						win.winDiv.style.bottom = "0px";
						win.config.showMinButton = false;
						if (win.config.state == "max") {
							win.config.showMaxButton = true;
							win.maxButton.className = "zpWinMaxButton";
							win.maxButton.buttonType = "max";
						} else {
							win.config.showRestoreButton = true;
						}
						win.config.state = "min";
						if (Zapatec.Window.lastActive) {
							Zapatec.Window.lastActive.setCurrent();
						}
						Zapatec.Utils.setupWCH(win.WCH, Zapatec.Window.minimizeLeft, 0, Zapatec.Window.minWinWidth, win.config.minHeight);
						if (win.WCH) {
							win.WCH.style.top = "";
							win.WCH.style.bottom = "0px";
						}
						Zapatec.Window.minimizeLeft += Zapatec.Window.minWinWidth + 5;
					}
				} else {
					target.customMouseUp(ev, win, target);
				}
				break;
			}
			
			case "max" : {
				if (!target.customMouseUp) {
					if (win.config.showMaxButton && win.state != "max" && target == (ev.srcElement || ev.target)) {
						var sizes = Zapatec.Utils.getWindowSize();
						var windowWidth = sizes.width;
						var windowHeight = sizes.height;
						var style = win.winDiv.style;
						style.left = "0px";
						style.top = "0px";
						var diff = win.config.width - windowWidth;
						style.width = windowWidth + "px";
						if (win.titleText) win.titleText.style.width = (win.config.titleWidth - diff) + "px";
						if (win.contentArea) win.contentArea.style.width = (win.config.contentWidth - diff) + "px";
						if (win.statusArea) win.statusArea.style.width = (win.config.statusWidth - diff) + "px";
						win.contentArea.style.height = (windowHeight - win.config.heightDiff) + "px";
						Zapatec.Utils.setupWCH(win.WCH, 0, 0, windowWidth, windowHeight);

						if (win.config.state == "min") {
							if (win.contentArea) {
								var style = win.titleArea.nextSibling.style;
								if (!Zapatec.is_ie) {
									if (style.display != "table-row") {
										style.display = "table-row";
									}
								} else {
									if (style.display != "block") {
										style.display = "block";
									}
								}
							}
							
							if (win.statusArea || win.resizeArea) {
								var style = win.titleArea.nextSibling.nextSibling.style;
								if (!Zapatec.is_ie) {
									if (style.display != "table-row") {
										style.display = "table-row";
									}
								} else {
									if (style.display != "block") {
										style.display = "block";
									}
								}
							}
							win.winDiv.style.bottom = "";
							win.config.showMinButton = true;
							win.minButton.className = "zpWinMinButton";
							win.minButton.buttonType = "min";
							Zapatec.Window.sortMin(win);
						} else {
							win.config.showRestoreButton = true;
						}
						win.config.showMaxButton = false;
						win.config.state = "max";
						target.buttonType = "restore";
						target.className = "zpWinRestoreButton";
					}
				} else {
					target.customMouseUp(ev, win, target);
				}
				break;
			}

			case "restore" : {
				if (!target.customMouseUp) {
					if (win.config.showRestoreButton && win.state != "simple" && target == (ev.srcElement || ev.target)) {
						if (win.contentArea) {
							var style = win.titleArea.nextSibling.style;
							if (!Zapatec.is_ie) {
								if (style.display != "table-row") {
									style.display = "table-row";
								}
							} else {
								if (style.display != "block") {
									style.display = "block";
								}
							}
						}
						
						if (win.statusArea || win.resizeArea) {
							var style = win.titleArea.nextSibling.nextSibling.style;
							if (!Zapatec.is_ie) {
								if (style.display != "table-row") {
									style.display = "table-row";
								}
							} else {
								if (style.display != "block") {
									style.display = "block";
								}
							}
						}
						
						win.winDiv.style.bottom = "";
						win.config.showRestoreButton = false;
						if (win.config.state == "min") {
							win.config.showMinButton = true;
							target.buttonType = "min";
							target.className = "zpWinMinButton";
							Zapatec.Window.sortMin(win);
						} else if (win.config.state == "max") {
							win.config.showMaxButton = true;
							target.buttonType = "max";
							target.className = "zpWinMaxButton";
						}
						win.config.state = "simple";
						win.setPosAndSize(win.config.left, win.config.top, win.config.width, win.config.height);
					}
				} else {
					target.customMouseUp(ev, win, target);
				}
				break;
			}
		}
	}
}

Zapatec.Window.prototype.addEvents = function () {
	var self = this, target = null;
	Zapatec.Utils.addEvent(this.winDiv, "mousedown", function (ev) {
		ev = ev || window.event; 
		target = Zapatec.Utils.getTargetElement(ev);
		while(!target.buttonType && (target != self.winDiv)) {
			target = target.parentNode;
		}
		if (!target.buttonType) target = null;
		Zapatec.Window.mouseDown(ev, self, target);
		if (target) return Zapatec.Utils.stopEvent(ev);
	});
	Zapatec.Utils.addEvent(window.document, "mousemove", function (ev) {
		ev = ev || window.event; 
		Zapatec.Window.mouseMove(ev, self, target);
		if (target) return Zapatec.Utils.stopEvent(ev);
	});
	Zapatec.Utils.addEvent(window.document, "mouseup", function (ev) {
		ev = ev || window.event; 
		Zapatec.Window.mouseUp(ev, self, target);
		target = null;
		if (target) return Zapatec.Utils.stopEvent(ev);
	});
}

/*
 * For internal use only. Calculates some sizes needed to avoid IE's strange behaviour.
 */
Zapatec.Window.prototype.calculateSizes = function () {
	this.winDiv.style.display = "block";
	if (this.titleArea) {
		this.config.titleWidth = this.config.width - (this.winDiv.offsetWidth - this.titleText.offsetWidth);
	}
	
	if (this.contentArea) {
		this.config.contentWidth = this.config.width - (this.winDiv.offsetWidth - this.contentArea.offsetWidth);
	}
	
	if (this.statusArea) {
		this.config.statusWidth = this.config.width - (this.winDiv.offsetWidth - this.statusArea.offsetWidth);
	}

	this.config.heightDiff = this.winDiv.offsetHeight - this.config.height;
	this.winDiv.style.display = "none";
}

/// Creates all HTML elements of the window. This function takes in account
/// this.config object, trough its properties you can disable the following elements:
///  - The whole title (currently don't work) - this.showTitle property;
///  - Any of 3 buttons (min, max, close) - this.show(Min|Max|Close)Button property;
///  - The status area - this.showStatus property;
///  - The resize icon - this.canResize property;
/// Also calls this.addEvents to assign event handlers to the main div and creates WCH 
/// (http://www.aplus.co.yu/WCH/).
/// This function defines the following properties for HTML elements, needed for event handlers:
///  - buttonType - one of three buttons or resize icon;
///
/// @param x [integer] x coordinate.
/// @param y [integer] y coordinate.
/// @param width [integer] width of the window.
/// @param height [integer] height of the window.v
///
/// @return [object] { x, y } containing the position.
Zapatec.Window.prototype.create = function (x, y, width, height)  {
	var noButtons = !((this.config.showMinButton == true) || (this.config.showMaxButton) || (this.config.showCloseButton));
	var config = this.config;
	//creating of the top level div to control the width and position of the window
	var div = this.winDiv = Zapatec.Utils.createElement("div", null, true);
	div.style.position = "absolute";
	div.style.width = (this.config.width = width) + (width == "auto" ? "" : "px");
	div.style.display = "none";
	this.config.visible = false;
	this.addEvents();
	
	//creating of another div with zpWin class
	div = Zapatec.Utils.createElement("div", div, true);
	div.className = "zpWin";
	
	//table with all the elements
	var table = Zapatec.Utils.createElement("table", div, true);
	table.border = 0;
	table.cellPadding = "0";
	table.cellSpacing = "0";
	
	var tbody = Zapatec.Utils.createElement("tbody", table, true);
	
	//title area of the window
	if (config.showTitle == true) {
		var tr = this.titleArea = Zapatec.Utils.createElement("tr", tbody);
		tr.buttonType = "move";
		tr.className = "zpWinTitleArea";
		
		//additional cell for ronded borders in title
		var td = Zapatec.Utils.createElement("td", tr);
		div = Zapatec.Utils.createElement("div", td);
		div.innerHTML = "&nbsp;";

		//cell with title text
		td = Zapatec.Utils.createElement("td", tr);
		td.style.width = "100%";
		
		div = this.titleText = Zapatec.Utils.createElement("div", td);
		div.className = "zpWinTitleText";
		div.style.overflow = "hidden";
		div.style.width = "100%";
		div.style.height = "100%";
		div.appendChild(document.createTextNode(""));
			
		//cell with minimize button
		td = Zapatec.Utils.createElement("td", tr);
		
		if (config.showMinButton == true) {	
			div = this.minButton = Zapatec.Utils.createElement("div", td);
			div.style.overflow = "hidden";
			div.buttonType = "min";
			div.className = "zpWinMinButton";
		}
		
		//cell with maximize button
		td = Zapatec.Utils.createElement("td", tr);
		
		if (config.showMaxButton == true) {		
			div = this.maxButton = Zapatec.Utils.createElement("div", td);
			div.style.overflow = "hidden";
			div.buttonType = "max";
			div.className = "zpWinMaxButton";
		} 
					
		//cell with close button
		td = Zapatec.Utils.createElement("td", tr);
		
		if (config.showCloseButton == true) {		
			div = this.closeButton = Zapatec.Utils.createElement("div", td);
			div.style.overflow = "hidden";
			div.buttonType = "close";
			div.className = "zpWinCloseButton";
		} else if (!Zapatec.is_ie && noButtons) {
			//a workaround for Gecko's behaviour(we need to create at least one button cell for the window to be shown correctly).
			div = Zapatec.Utils.createElement("div", td);
			div.innerHTML = "&nbsp;";
		}
		tr.firstChild.id = "titleFirstCell";
		tr.lastChild.id = "titleLastCell";
	}
	
	//creating content area
	tr = Zapatec.Utils.createElement("tr", tbody, true);
		
	td = Zapatec.Utils.createElement("td", tr, true);
	td.colSpan = 5;
	td.id = "contentCell";
		
	div = this.contentArea = document.createElement("div");
	td.appendChild(div);
	div.style.height = (this.config.height = height) + (height == "auto" ? "" : "px");
	div.style.width = "100%";
	div.style.overflow = "auto";
	div.className = "zpWinContent";
	div.appendChild(document.createTextNode(""));
		
	//creating of status area
	if ((config.showStatus == true) || (config.canResize == true)) {
		tr = Zapatec.Utils.createElement("tr", tbody);
		
		td = Zapatec.Utils.createElement("td", tr);
		td.colSpan = 5;
		td.id = "statusCell";
			
		table = Zapatec.Utils.createElement("table", td);
		table.height = "100%";
		table.cellPadding = "0";
		table.cellSpacing = "0";
			
		tbody = Zapatec.Utils.createElement("tbody", table);
			
		tr = Zapatec.Utils.createElement("tr", tbody);
		
		//status text
		td = Zapatec.Utils.createElement("td", tr);
		td.style.verticalAlign = "bottom";
		td.style.width = "100%";
				
		if (config.showStatus == true) {
			div = this.statusArea = Zapatec.Utils.createElement("div", td);
			div.style.overflow = "hidden";
			div.style.width = "100%";
			div.className = "zpWinStatus";
			div.appendChild(document.createTextNode(""));
		}
			
		//resize button
		if (config.canResize == true) {
			td = Zapatec.Utils.createElement("td", tr);
			td.style.verticalAlign = "bottom";
				
			div = this.resizeArea = Zapatec.Utils.createElement("div", td);
			div.style.overflow = "hidden";
			div.buttonType = "resize";
			div.className = "zpWinResize";
		}
	}

	window.document.body.appendChild(this.winDiv);
	//Seting widths of title, content and status elements for IE to proceed overflows.
	this.calculateSizes();
	var size = Zapatec.Utils.getWindowSize(); 
	var br = {};
	if (Zapatec.is_ie) {
		br.y = window.document.body.scrollTop;
		br.x = window.document.body.scrollLeft;
	} else {
		br.y = window.scrollY;
		br.x = window.scrollX;
	}
 	if (x == 'center') { 
 		if (screen.width) { 
 			x = (size.width -  this.config.width)/2 + br.x; 
 		} 
 	} 
 	if (y == 'center') { 
 		if (screen.height) { 
			y = (size.height - (this.config.height + this.config.heightDiff))/2 + br.y; 
 		} 
 	}
	this.winDiv.style.left = (this.config.left = x) + "px";
	this.winDiv.style.top = (this.config.top = y) + "px";
	if (this.titleText) this.titleText.style.width = this.config.titleWidth + "px";
	if (this.contentArea) this.contentArea.style.width = this.config.contentWidth + "px";
	if (this.statusArea) this.statusArea.style.width = this.config.statusWidth + "px";
	
	//Creating WCH
	this.WCH = Zapatec.Utils.createWCH();
	
	//Activating window.
	if (this.config.modal == true) {
		this.modalLayer = Zapatec.Utils.createElement("DIV", document.body);
		var st = this.modalLayer.style;
		st.display = "none";
		st.position = "absolute";
		st.top = br.y + "px";
		st.left = br.x + "px";
		var dim = Zapatec.Utils.getWindowSize();
		st.width = dim.width + "px";
		st.height = dim.height + "px";
		st.zIndex = Zapatec.Window.maxNumber++;
		this.modalLayer.className = "zpWinModal";
		Zapatec.ScrollWithWindow.register(this.modalLayer);
	}
	this.setNumber();
	this.winDiv.style.zIndex = Zapatec.Window.maxNumber;
	Zapatec.Window.winArray.push(this);
	this.setCurrent();
	//alert(document.getElementById("1").innerHTML);
}	

// Global that keeps track of man number of windows
Zapatec.Window.winArray = [];
Zapatec.Window.maxNumber = 0;
Zapatec.Window.currentWindow = null;
Zapatec.Window.minimizeLeft = 0;
Zapatec.Window.minWinWidth = 120;
Zapatec.Window.lastActive = null;

Zapatec.Window.sortMin = function (raised) {
	var place = parseInt(raised.winDiv.style.left, 10), win;
	for (var i in Zapatec.Window.winArray) {
		win = Zapatec.Window.winArray[i];
		if (win && win.config.state == "min") {
			var left = parseInt(win.winDiv.style.left, 10);
			if (left > place) {
				left -= Zapatec.Window.minWinWidth + 5;
				win.winDiv.style.left = left + "px";
				if (win.WCH) win.WCH.style.left = win.winDiv.style.left;
				
			}
		}
	}
	Zapatec.Window.minimizeLeft -= Zapatec.Window.minWinWidth + 5;
}

/*
 * For internal use only.
 * Increment the max number of windows
 */
Zapatec.Window.prototype.setNumber = function() {
	this.winNumber = ++Zapatec.Window.maxNumber;
};

/*
 * For internal use only.
 * Keep track of which is the current window
 */

Zapatec.Window.prototype.setCurrent = function(deb) {
	var win = this;
	if (Zapatec.Window.currentWindow && //if it is not the first one
			win != Zapatec.Window.currentWindow) { //and it is not the same one as before
		//Show the previous window as not being the current window anymore
		if (Zapatec.Window.currentWindow.winDiv) {
			Zapatec.Utils.removeClass(Zapatec.Window.currentWindow.winDiv, 'zpWinFront');
			Zapatec.Utils.addClass(Zapatec.Window.currentWindow.winDiv, 'zpWinBack');
		}
		if (Zapatec.Window.currentWindow.winDiv && Zapatec.Window.currentWindow.config.state != "min") {
			Zapatec.Window.lastActive = Zapatec.Window.currentWindow;
		} else {
			var zIndex = 0;
			Zapatec.Window.lastActive = null;
			for(i in Zapatec.Window.winArray) {
				if (Zapatec.Window.winArray[i] && (parseInt(Zapatec.Window.winArray[i].winDiv.style.zIndex, 10) > zIndex) && (Zapatec.Window.winArray[i].config.state != "min") && (Zapatec.Window.winArray[i] != win)) {
					zIndex = parseInt(Zapatec.Window.winArray[i].winDiv.style.zIndex, 10);
					Zapatec.Window.lastActive = Zapatec.Window.winArray[i];
				}
			}
		}
	}
	//And set the current window
	Zapatec.Window.currentWindow = win;
	Zapatec.Utils.removeClass(win.winDiv, 'zpWinBack');
	Zapatec.Utils.addClass(win.winDiv, 'zpWinFront');
}

/* 
 * Set the content of a window.
 * @param type [string] the HTML data to set the content to.
 */

Zapatec.Window.prototype.setContent = function(text) {
	this.contentArea.innerHTML = text;
}

/* 
 * Set the content of a window from HTML element(DIV).
 * @param div [string]or [object] the HTML data to set the content to.
 */

Zapatec.Window.prototype.setDivContent = function(div) {
	if (typeof div == "string") {
		div = document.getElementById(div);
	}
	if (typeof div == "object") {
    this.contentArea.appendChild(div);
	}
}

/* 
 * Set the content of a window from url.
 * @param div [string]or [object] the HTML data to set the content to.
 */

Zapatec.Window.prototype.setContentUrl = function(url) {
	var self = this;
	Zapatec.Transport.fetch({
		url : url, 
		onLoad : function (result) {
			//FIXED: <script>s didn't actually were executed on loaded piece of HTML, which I think was a problem
			var text = result.responseText, script, scripts = [], attrs;
			while (script = text.match(/<script([^>]*)>([^<]*)<\/script>/)) {
				text = text.replace(/<script[^>]*>[^<]*<\/script>/, "");
				scripts.push(script);
			}
			self.setContent(text);
			for(i in scripts) {
				// Evaluate code in global scope
				setTimeout(scripts[i][2], 0);
			}
			self.onContentLoad();
		},
		onError : function () {
			alert('Error while fetching data from the server');
		}
	});
}

/*
 * Set the title of the window
 * @param type [string] the title to set to
 */ 
Zapatec.Window.prototype.setTitle = function(text) {
	if (text == '') {
		this.titleText.innerHTML = 'Window ' + this.winNumber;
	} else {
		this.titleText.innerHTML = text;
	}
}

/**
 * Updates the window "WCH" (windowed controls hider).  A WCH is an
 * "invention" (read: "miserable hack") that works around one of the most
 * common and old bug in Internet Explorer: the SELECT boxes or IFRAMES show on
 * top of any other HTML element.  This function makes sure that the WCH covers
 * correctly the window element and another element if passed.
 */
Zapatec.Window.prototype.updateWCH = function() {
	if (this.WCH && this.WCH.style.bottom != "") {
		this.WCH.style.bottom = "";
	}
	Zapatec.Utils.setupWCH(this.WCH, this.config.left, this.config.top, this.config.width, this.config.height + this.config.heightDiff - 2);
}

/**
 * Sets the X coordinate of the window. Needed to synchronize some variables in
 * one place. Also updates the WCH for IE.
 *
 * @param left [integer] - X coordinate.
 */
Zapatec.Window.prototype.setLeft = function(left) {
	this.winDiv.style.left = (this.config.left = left) + "px";
	this.updateWCH();
}

/**
 * Sets the Y coordinate of the window. Needed to synchronize some variables in
 * one place. Also updates the WCH for IE.
 *
 * @param top [integer] - Y coordinate.
 */
Zapatec.Window.prototype.setTop = function(top) {
	this.winDiv.style.top = (this.config.top = top) + "px";
	this.updateWCH();
}

/**
 * Sets the width of the window. Needed to synchronize some variables in
 * one place. Also updates the WCH for IE.
 *
 * @param width [integer] - width of the window.
 */
Zapatec.Window.prototype.setWidth = function(width) {
	var diff = this.config.width - width;
	if (Zapatec.is_gecko) {
	}
	this.winDiv.style.width = (this.config.width = width) + "px";
	if (this.titleText) this.titleText.style.width = (this.config.titleWidth -= diff) + "px";
	if (this.contentArea) this.contentArea.style.width = (this.config.contentWidth -= diff) + "px";
	if (this.statusArea) this.statusArea.style.width = (this.config.statusWidth -= diff) + "px";
	this.updateWCH();
}

/**
 * Sets the height of the window. Needed to synchronize some variables in
 * one place. Also updates the WCH for IE.
 *
 * @param height [integer] - height of the window.
 */
Zapatec.Window.prototype.setHeight = function(height) {
	this.contentArea.style.height = (this.config.height = height) + "px";
	this.updateWCH();
}

/**
 * Sets the position of the window. 
 *
 * @param left [integer] - X coordinate.
 * @param top [integer] - Y coordinate.
 */
Zapatec.Window.prototype.setPos = function(left, top) {
	this.setLeft(left);
	this.setTop(top);
}

/**
 * Sets the sizes of the window. 
 *
 * @param width [integer] - width of the window.
 * @param height [integer] - height of the window.
 */
Zapatec.Window.prototype.setSize = function(width, height) {
	this.setWidth(width);
	this.setHeight(height);
}


/**
 * Sets the sizes and position of the window. 
 *
 * @param left [integer] - X coordinate.
 * @param top [integer] - Y coordinate.
 * @param width [integer] - width of the window.
 * @param height [integer] - height of the window.
 */
Zapatec.Window.prototype.setPosAndSize = function(left, top, width, height) {
	this.setPos(left, top);
	this.setSize(width, height);
}

/*
 * Raise this window so that it's above all other.
 * Bring it to the front.
 */ 
Zapatec.Window.prototype.raise = function() {
	Zapatec.Window.maxNumber++; //increment so it's more than all the others
	this.winDiv.style.zIndex = Zapatec.Window.maxNumber;
	this.setCurrent(this);
}

Zapatec.Window.prototype.showStatus = function(message, mode) {
	if (this.config.showStatus) {
		switch (mode) {
			case "temp" : {
				this.config.statusText = this.statusArea.innerHTML;
				break;
			}
			
			case "restore" : {
				message = this.config.statusText;
				break;
			}
			
		}
		this.statusArea.innerHTML = message;
	}
}

Zapatec.Window.prototype.show = function() {
	this.winDiv.style.display = "block";
	if (this.config.modal == true && this.modalLayer) {
		this.modalLayer.style.display = "block";
	}
	this.updateWCH();
	this.config.visible = true;	
}

Zapatec.Window.prototype.hide = function() {
	this.winDiv.style.display = "none";
	if (this.config.modal == true && this.modalLayer) {
		this.modalLayer.style.display = "none";
	}
	Zapatec.Utils.hideWCH(this.WCH);
	this.config.visible = false;	
}

Zapatec.Window.prototype.close = function() {
	Zapatec.Utils.destroy(this.winDiv);
	Zapatec.Utils.destroy(this.WCH);
	if (this.config.modal == true && this.modalLayer) {
		Zapatec.Utils.destroy(this.modalLayer);
	}
	delete this.winDiv;
	for (i in Zapatec.Window.winArray) {
		if (Zapatec.Window.winArray[i] == this) {
			Zapatec.Window.winArray[i] = null;
		}
	}
	if (Zapatec.Window.lastActive) {
		Zapatec.Window.lastActive.setCurrent();
	}
}

Zapatec.Window.prototype.destroy = function() {
	this.close();
}

/**
 * This function displays the calendar near a given "anchor" element, according
 * to some rules passed in \em opts.  The \em opts argument is a string
 * containing one or 2 letters.  The first letter decides the vertical
 * alignment, and the second letter decides the horizontal alignment relative
 * to the anchor element.  Following we will describe these options; in parens
 * we will use simple descriptions like "top to bottom" which means that the
 * top margin of the calendar is aligned with the bottom margin of the object.
 *
 * \b Vertical align:
 *
 * - T -- the calendar is completely above the element (bottom to top)
 * - t -- the calendar is above the element but might overlap it (bottom to bottom)
 * - C -- the calendar is vertically centered to the element
 * - b -- the calendar is below the element but might overlap it (top to top)
 * - B -- the calendar is completely below the element (top to bottom)
 *
 * \b Horizontal align (defaults to 'l' if no letter passed):
 *
 * - L -- the calendar is completely to the left of the element (right to left)
 * - l -- the calendar is to the left of the element but might overlap it (right to right)
 * - C -- the calendar is horizontally centered to the element
 * - r -- the calendar is to the right of the element but might overlap it (left to left)
 * - R -- the calendar is completely to the right of the element (left to right)
 *
 * @param el [HTMLElement] the anchor element
 * @param opts [string, optional] the align options, as described above.  Defaults to "Bl" if nothing passed.
 */
Zapatec.Window.prototype.showAtElement = function (el, opts) {
	var self = this;
	var p = Zapatec.Utils.getAbsolutePos(el);
	if (!opts || typeof opts != "string") {
		this.showAt(p.x, p.y + el.offsetHeight);
		return true;
	}
	this.winDiv.style.display = "block";
	var w = self.winDiv.offsetWidth;
	var h = self.winDiv.offsetHeight;
	self.winDiv.style.display = "none";
	var valign = opts.substr(0, 1);
	var halign = "l";
	if (opts.length > 1) {
		halign = opts.substr(1, 1);
	}
	// vertical alignment
	switch (valign) {
	    case "T": p.y -= h; break;
	    case "B": p.y += el.offsetHeight; break;
	    case "C": p.y += (el.offsetHeight - h) / 2; break;
	    case "t": p.y += el.offsetHeight - h; break;
	    case "b": break; // already there
	}
	// horizontal alignment
	switch (halign) {
	    case "L": p.x -= w; break;
	    case "R": p.x += el.offsetWidth; break;
	    case "C": p.x += (el.offsetWidth - w) / 2; break;
	    case "l": p.x += el.offsetWidth - w; break;
	    case "r": break; // already there
	}
	p.width = w;
	p.height = h;
	Zapatec.Utils.fixBoxPosition(p);
	self.showAt(p.x, p.y);
}

/**
 * Shows the calendar at a given absolute position (beware that, depending on
 * the calendar element style -- position property -- this might be relative to
 * the parent's containing rectangle).
 *
 * @param x [int] the X position
 * @param y [int] the Y position
 */
Zapatec.Window.prototype.showAt = function (x, y) {
	this.setPos(x, y);
	this.show();
}


