if(!window)
    window = this;
    
window.__isWindow = true;

if(window.dialogArguments)
{
    if(typeof(window.dialogArguments.__isCollection) != "undefined")
    {
        if(typeof(window.dialogArguments.mainWindow) != "undefined")
            window.mainWindow = window.dialogArguments.mainWindow;
            
        if(typeof(window.dialogArguments.dialogArguments) != "undefined")
            window._dialogArguments = window.dialogArguments.dialogArguments;
        else window._dialogArguments = null;
    }
    else window._dialogArguments = window.dialogArguments;
}
else window._dialogArguments = null;

var WindowManager = {};

WindowManager.init= function(objectId)
{
	this.m_objectId = objectId;
	this.m_modalWindow = null;
	this.m_lastModalResult = null;
	this.m_resultHandler = null;
	this.m_windowLeft = document.all ? "left" : "screenX";
	this.m_windowTop = document.all ? "top" : "screenY";
	this.m_windowSeed = 0;
}

WindowManager.getMainWindow= function()
{
    if(window.mainWindow && !window.mainWindow.closed)
        return window.mainWindow;
    
    if(window.opener && !window.opener.closed)
        return window.opener;
    
    return window;
}
	
WindowManager.openWindow= function(url, name, reload, params)
{
	var _windowHost = this.getMainWindow();
	
	var _window = null;
	
	if(reload)
	{
		if(params)
		    _window = _windowHost.open(url, name, params);
	    else _window = _windowHost.open(url, name);
	}
	else
	{
	    if(params)
		    _window = _windowHost.open("", name, params);
	    else _window = _windowHost.open("", name);
	    
	    if(_window)
	    {
	        _window.mainWindow = _windowHost;
	        
	        if(_window.location.href == "about:blank")
	        {
		        _window.location.href = url;
    			
		        if(typeof(window.event) != "undefined" && window.event)
		            window.event.returnValue = false;
	        }
	        else 
	        {
	            _window.focus();
	        }
	    }
	}
	
	return _window;
}
WindowManager.openWindowWH= function(url, name, reload, width, height, center, params)
{
	var _params = "width=" + width + ",height=" + height;
	
	if(center)
	{
		_params += "," + this.m_windowLeft + "=" + Math.floor((window.screen.width - width) / 2);
		_params += "," + this.m_windowTop + "=" + Math.floor((window.screen.height - height) / 2);
	}
	
	if(params)
		_params += "," + params;
	
	return this.openWindow(url, name, reload, _params);
}
WindowManager.getNextWindowName= function()
{
	var _result = "_W_" + this.m_windowSeed++;

	if(window.name)
	{
		return  window.name + _result;
	}	
	else 
	{
	    return _result + Math.random();
	}
}
WindowManager.openDialog= function(url, args, features, resultHandler)
{
	try
	{
		this.m_lastModalResult = null;
		
		var _dlgArgs = {__isCollection:true, dialogArguments: args, mainWindow: this.getMainWindow()};
				
		if(resultHandler)
		{ 
		    if(typeof(resultHandler) == "function")
			    this.m_resultHandler = resultHandler;
			else throw "Invalid parameter! Resulthandler must be a function pointer.";
		}	
		else this.m_resultHandler = null;
		
		if(window.showModalDialog)
		{
		    url = JsUtility.addTimeStampToUrl(url);
			
			this.m_lastModalResult = window.showModalDialog(url, _dlgArgs, features);
			this.setModalResult(this.m_lastModalResult);
			return this.m_lastModalResult;
		}
		else
		{
			features = features.replace(/ /gi,'');
			
			var _features = features.split(";");
			var _params = "directories=0,menubar=0,titlebar=0,toolbar=0,modal=1";
			var _height = 300;
			var _width = 400;
			
			for(key in _features)
			{
				var _feature = _features[key].split(":");
				var _featureName = _feature[0].toLowerCase();
				var _featureValue = _feature[1];

				switch (_featureName)
				{
					case "dialogheight":
						_height = JsUtility.getUnitAsPixel(_featureValue);
						_params += ",height=" + _height;
						break;
					case "dialogwidth":
						_width = JsUtility.getUnitAsPixel(_featureValue);
						_params += ",width=" + _width;
						break;
					case "dialogtop":
						_params += "," + _windowTop + "=" + JsUtility.getUnitAsPixel(_featureValue);
						break;
					case "dialogleft":
						_params += "," + _windowLeft + "=" + JsUtility.getUnitAsPixel(_featureValue);
						break;
					case "resizable":
						_params += ",resizable=" + _featureValue;
						break;
					case "status":
						_params += ",status=" + _featureValue;
						break;
					case "center":
						if(_featureValue.toLowerCase() == "yes" || _featureValue == "1")
						{
							_params += "," + this.m_windowLeft + "=" + Math.floor((window.screen.width - _width)/2);
							_params += "," + this.m_windowTop + "=" + Math.floor((window.screen.height - _height)/2);
						}
						break;
				}//switch
			}//for
			
			
		    this.m_modalWindow = window.open(url, "_blank", _params);
			
		    if(this.m_modalWindow)
		    {
			    this.m_modalWindow.dialogArguments = _dlgArgs;
			    this.m_modalWindow.isModal = true;
			    this.m_modalWindow.suppressParentFocus = false;
			    this.m_modalWindow.mainWindow = this.getMainWindow();
		    }
		}//else
	}
	catch(exception)
	{
		alert(exception.message);
	}
	
    return null;
}
WindowManager.onWindowFocus= function(e)
{
	try
	{
		JsUtility.writeLog("onWindowFocus");
		if(typeof(window.suppressParentFocus) != "undefined"
			&& !window.suppressParentFocus
			&& window.isModal 
			&& window.opener 
			&& !window.opener.closed)
		{
			JsUtility.writeLog("try to focus parent");
			window.opener.suppressModalFocus = true;
			window.opener.focus();
			window.opener.suppressModalFocus = false;
			window.suppressParentFocus = true;
			window.focus();
			window.suppressParentFocus = false;
		}
		
		if(!window.suppressModalFocus 
			&& this.m_modalWindow)
		{
			JsUtility.writeLog("try to focus modal");

			if(this.m_modalWindow.closed)
				this.m_modalWindow = null;
			else this.m_modalWindow.focus();
		}
	}
	catch(exception)
	{
		this.m_modalWindow = null;
	}
}

WindowManager.getLastModalResult= function()
{
	if(!this.m_lastModalResult)
	{
		if(this.m_modalWindow)
		{
			if(typeof(this.m_modalWindow.returnValue) != "undefined")
				this.m_lastModalResult = this.m_modalWindow.returnValue;
		}
	}
	
	return this.m_lastModalResult;
}
WindowManager.constructResult= function(args)
{
    if(args)
    {
        var _l = args.length;
        if(args.length > 1)
        {
            var _result = [];
            for(var i=0; i < _l; i++)
                _result[i] = args[i];
            
            return _result;
        }
        else return args[0];
    }
    else return void(0);
}
WindowManager.setModalResultDelayed= function()
{
	this.m_lastModalResult = this.constructResult(arguments);
	if(typeof(this.m_resultHandler) == "function")
	{
	    var _this = this;
	    var _arguments = arguments;
	    window.setTimeout(function()
	                  {
	                      _this.setModalResult.apply(_this, _arguments);
	                  }, 
	                  10);
    }
}
WindowManager.setModalResult= function()
{
    this.m_lastModalResult = this.constructResult(arguments);
    
    if(typeof(this.m_resultHandler) == "function")
        this.m_resultHandler.apply(this, arguments);
}
WindowManager.closeWithResult= function()
{
    window.returnValue = this.constructResult(arguments);
	window.closedWithResult = false;
	
	if(window.opener && window.opener.WindowManager)
	{
	    window.opener.WindowManager.setModalResultDelayed.apply(window.opener.WindowManager, arguments);
		window.opener.WindowManager.m_modalWindow = null;
	}
		
	window.setTimeout(function()
	                  {
	                    window.close();
	                  }, 
	                  300);
}
WindowManager.close= function()
{
	window.closedWithResult = false;
	
	if(window.opener && window.opener.WindowManager)
	{
		window.opener.WindowManager.m_modalWindow = null;
	}
	
	window.setTimeout(function()
	                  {
	                    window.close();
	                  }, 
	                  300);
}

WindowManager.init("WindowManager");
