// $Id: window-globals.js 5087 2006-11-14 11:23:07Z slip $
/**
 *
 * Copyright (c) 2006 by SouthProjects
 * http://www.southprojects.com
 * Santiago, Chile,
 * Version 1.2 Dec 2006.
 * All rights reserved.
 */

// Global that keeps track of max number of windows
southprojects.Window.maxNumber = 5;
// Global that holds the pointer to the current window
southprojects.Window.currentWindow = null;
// Global that holds the current position to minimize to
southprojects.Window.minimizeLeft = 0;
// Global that determines the minimal width of the window
southprojects.Window.minWinWidth = 120;
// Global that keeps last active window
southprojects.Window.lastActive = null;

/**
 * \internal 
 * Sorts all the minimized windows at the bottom when one of them was restored to the simple state.
 */
southprojects.Window.sortMin = function (raised) {
	var place = southprojects.Utils.getAbsolutePos(raised.container).x, win;
	for (var i in southprojects.Widget.all) {
		if (southprojects.Widget.all[i].widgetType == "window") {
			win = southprojects.Widget.all[i];
			if (win && win.state.state == "min") {
				var left = southprojects.Utils.getAbsolutePos(win.container).x;
				if (left > place) {
					left -= southprojects.Window.minWinWidth + 5;
					win.container.style.left = (left - (southprojects.Utils.bodyOffset ? southprojects.Utils.bodyOffset.left : 0)) + "px";
					if (win.WCH) win.WCH.style.left = win.container.style.left;
					
				}
			}
		}
	}
	win = null;
	southprojects.Window.minimizeLeft -= southprojects.Window.minWinWidth + 5;
}

/**
 * A function that is called to handle mousedown event for our window.
 *
 * This function handles the routines that should be done on mousedown. Target parameter determines
 * whether something was "pushed" and holds that "pushed" element. "pushed" means that mouse was down
 * on that element and was not released. buttonType property of the target element determines the action that will be made.
 * There is one usefull possibility: if the target element has the customMouseDown property and it holds 
 * function, this function will be called except the default action.
 *
 * @param ev [object] - event object.
 * @param win [object] - our window object.
 * @param target [object] - "pushed" element.
 */
southprojects.Window.mouseDown = function (ev, win, target) {
	if (!win.config.raiseOnlyOnTitle && !win.draggable.dragging) {
		win.activate();
	}
	var posX = ev.pageX || ev.clientX + window.document.body.scrollLeft || 0;
	var posY = ev.pageY || ev.clientY + window.document.body.scrollTop || 0;
	if (target) {
		switch (target.buttonType) {
			case "resize" : case "title" : {
				if (!target.customMouseDown) {
					if (win.state.state == "min" && !win.draggable.dragging) {
						win.activate();
					}
					if (win.config.canResize && win.state.state == "simple") {
						if (win.container.style.cursor.indexOf("resize") != -1) {
							win.draggable.dragEnd(ev);
							win.setStatus('resize the window', 'temp');
							win.resizing = true;
							//creating a div
							southprojects.Utils.cover.show(
								10000001,
								win.container.style.cursor,
								function(ev) {
									ev = ev || window.event; 
									southprojects.Window.mouseMove(ev, win, target);
									
									return southprojects.Utils.stopEvent(ev);
								},
								function(ev) {
									ev = ev || window.event; 
									southprojects.Window.mouseUp(ev, win, target);
									//destroying the cover
									southprojects.Utils.cover.hide();
									target = null;
									
									return southprojects.Utils.stopEvent(ev);
								}
							);
							win.container.style.cursor = "";
						}
						win.oldX = win.state.left || 0;
						win.oldY = win.state.top || 0;
						win.oldWidth = win.state.width;
						win.oldHeight = win.state.height;
						win.mouseX = posX;
						win.mouseY = posY;
					}
				} else {
					target.customMouseDown(ev, win, target);
				}
				break;
			}
		}
	}
	
	//return southprojects.Utils.stopEvent(ev);
}

/**
 * A function that is called to handle mousemove event for our window.
 *
 * This function handles the routines that should be done on mousedown. Target parameter determines
 * whether something was "pushed" and holds that "pushed" element. "pushed" means that mouse was down
 * on that element and was not released. buttonType property of the target element determines the action that will be made.
 * There is one usefull possibility: if the target element has the customMouseMove property and it holds 
 * function, this function will be called except the default action.
 *
 * @param ev [object] - event object.
 * @param win [object] - our window object.
 * @param target [object] - "pushed" element.
 */
southprojects.Window.mouseMove = function (ev, win, target) {
	var posX = ev.pageX || ev.clientX + window.document.body.scrollLeft || 0;
	var posY = ev.pageY || ev.clientY + window.document.body.scrollTop || 0;
	if (target) {
		switch (target.buttonType) {
			case "resize" : case "title" : {
				if (!target.customMouseMove) {
					if (win.config.canResize && win.state.state == "simple" && win.resizing) {
						var style = win.container.style;
					
						var newWidth = win.oldWidth, newHeight = win.oldHeight, newLeft = win.oldX, newTop = win.oldY;
						var act = southprojects.Utils.cover.style.cursor.split("-");
						if (act[0].indexOf("w") != -1) {
							newWidth = win.oldWidth - (posX - win.mouseX);
							var over = 0;
							if (newWidth < win.config.minWidth) { 
								over = win.config.minWidth - newWidth;
								newWidth = win.config.minWidth;
							}
							newLeft = win.oldX + (posX - win.mouseX) - over;
						}
						if (act[0].indexOf("n") != -1) {
							newHeight = win.oldHeight - (posY - win.mouseY);
							var over = 0;
							if (newHeight < win.config.minHeight) { 
								over = win.config.minHeight - newHeight;
								newHeight = win.config.minHeight;
							}
							newTop = win.oldY + (posY - win.mouseY) - over;
						}
						if (act[0].indexOf("e") != -1) {
							newWidth = win.oldWidth + (posX - win.mouseX);
							if (newWidth < win.config.minWidth) { 
								newWidth = win.config.minWidth;
							}
						}
						if (act[0].indexOf("s") != -1) {
							newHeight = win.oldHeight + (posY - win.mouseY);
							if (newHeight < win.config.minHeight) { 
								newHeight = win.config.minHeight;
							}
						}
						var newDim = {
							"newWidth" : newWidth,
							"newHeight" : newHeight,
							"newLeft" : newLeft,
							"newTop" : newTop
						};
						win.fireEvent("resizeCallback", win, newDim);
						win.setPosAndSize(newDim.newLeft, newDim.newTop, newDim.newWidth, newDim.newHeight);
						win.fireEvent("onResize", win, newWidth, newHeight);
					}
				} else {
					target.customMouseMove(ev, win, target);
				}
				break;
			}
		}
	}
}

/**
 * A function that is called to handle mouseup event for our window.
 *
 * This function handles the routines that should be done on mousedown. Target parameter determines
 * whether something was "pushed" and holds that "pushed" element. "pushed" means that mouse was down
 * on that element and was not released. buttonType property of the target element determines the action that will be made.
 * There is one usefull possibility: if the target element has the customMouseUp property and it holds 
 * function, this function will be called except the default action.
 *
 * @param ev [object] - event object.
 * @param win [object] - our window object.
 * @param target [object] - "pushed" element.
 */
southprojects.Window.mouseUp = function (ev, win, target) {

	if (target) {
		switch (target.buttonType) {
			case "resize" : case "title" : {
				if (!target.customMouseUp) {
					if (win.config.canResize) {
						win.resizing = false;
						win.setStatus('', 'restore');
						win.fireEvent('resized');
					}
				} else {
					target.customMouseUp(ev, win, target);
				}
				break;
			}
		}
	}
}

/**
 * A function that is called to handle doubleclick event for our window.
 *
 * This function handles the routines that should be done on mousedown. Target parameter determines
 * whether something was "pushed" and holds that "pushed" element. "pushed" means that mouse was down
 * on that element and was not released. buttonType property of the target element determines the action that will be made.
 *
 * @param ev [object] - event object.
 * @param win [object] - our window object.
 * @param target [object] - "pushed" element.
 */
southprojects.Window.dblClick = function (ev, win, target) {
/*alert('aaa');*/
	if (target) {
		switch (target.buttonType) {
			case "title" : {
				switch (win.state.state) {
					case "min" : {
						win.restore();
						
						break;
					}
					case "max" : {
						win.restore();
						
						break;
					}
					case "simple" : {
						win.maximize();
						
						break;
					}
				}
				
				break;
			}
		}
		win.draggable.dragEnd(ev);
	}
}

