/*	WindowManager v1.0
	Copyright 2006 Refunk <http://www.refunk.com/> All rights reserved.
 */

var WindowManager = {
	
	// Set defaults
	_windowname: "popup",
	_width: 780,
	_height: 420,
	_toolbar: 0,
	_location: 0,
	_directories: 0,
	_menubar: 0,
	
	// Good usability
	_status: 1,
	_scrollbars: 1,
	_resizable: 1,
	
	open: function(WO) {
		if (typeof WO == "undefined" || typeof WO.url != "string") return; // Window Object including url name value pair is required
		
		var _ua = navigator.userAgent.toLowerCase();
		var _iewin = /msie/.test(_ua) && !/opera/.test(_ua) && /win/.test(_ua);
		var _awk = /applewebkit/.test(_ua);
		
		var WM = WindowManager;
		var _u = WO.url;
		var _n = typeof WO.windowname	== "string" ? WO.windowname		: WM._windowname; 
		var _w = typeof WO.width		== "number" ? WO.width			: WM._width;
		var _h = typeof WO.height		== "number" ? WO.height			: WM._height;
		var _p1 = typeof WO.toolbar		== "number" ? WO.toolbar		: WM._toolbar;
		var _p2 = typeof WO.location	== "number" ? WO.location		: WM._location;
		var _p3 = typeof WO.directories	== "number" ? WO.directories 	: WM._directories;
		var _p4 = typeof WO.menubar		== "number" ? WO.menubar 		: WM._menubar;
		var _p5 = typeof WO.status 		== "number" ? WO.status 		: WM._status;
		var _p6 = typeof WO.scrollbars 	== "number" ? WO.scrollbars 	: WM._scrollbars;
		var _p7 = typeof WO.resizable 	== "number" ? WO.resizable		: WM._resizable;
		
		var _p = "width=" + _w + ",height=" + _h + ",toolbar=" + _p1 + ",location=" + _p2 + ",directories=" + _p3 + ",menubar=" + _p4 + ",status=" + _p5 + ",scrollbars=" + _p6 + ",resizable=" + _p7;
		if (_iewin) {
			if (typeof WO.center != "undefined" && WO.center == true) _p += ",left=" + parseInt((screen.availWidth - _w) / 2, 10) + ",top=" + parseInt((screen.availHeight - _h) / 2, 10);
			if (typeof WO.maximize != "undefined" && WO.maximize == true) _p += ",fullscreen=1"; // Note: from IE6 XP SP2 onwards fullscreen means maximized, while earlier versions show a window without titlebar and menus
		}
				
		var _win = window.open(_u, _n, _p);
		
		if (_win && !_iewin) {
			if (typeof WO.center != "undefined" && WO.center == true && typeof screen != "undefined") {
				try { _win.moveTo(parseInt((screen.availWidth - _w) / 2, 10), parseInt((screen.availHeight - _h) / 2, 10)); }
				catch(e) {}	
			}
			if (typeof WO.maximize != "undefined" && WO.maximize == true && typeof screen != "undefined") {
				try {
					if (_awk) { // Order matters for Safari and Mozilla
						_win.moveTo(0, 0);
						_win.resizeTo(screen.availWidth, screen.availHeight);
					}
					else {
						_win.resizeTo(screen.availWidth, screen.availHeight);
						_win.moveTo(0, 0);
					}
				}
				catch(e) {}	
			}
			if (typeof _win.focus != "undefined") _win.focus();
		}		
	}
};
