

var WindowHelpers = {};

WindowHelpers.init = function(objectId)
{
	this.m_objectId = objectId;
	
	try
	{
	    this.m_width = Math.max(400, Math.ceil(window.screen.width * 0.75));
	}
	catch(e)
	{
	    this.m_width = 600;
	}
}

WindowHelpers.openWorkItemEditor= function(url, name)
{
    var _params = new Array();
    
    _params.push("scrollbars=yes");
    _params.push("resizable=yes");
    _params.push("status=yes");

    if(JSConfig.showBrowserToolbar)
        _params.push("toolbar=yes");
    
    if(typeof(name) == "undefined")
	    name = "WND_" + Math.abs(JsUtility.getHashCode(JsUtility.getSessionHashCode() + url));
	
    if(JSConfig.blankWorkItemEditorWindowName)
        name = "_blank";
        
	return WindowManager.openWindowWH(url, name, false, this.m_width, 600, true, _params.join(","));
};
WindowHelpers.openWorkItemEditorWithId= function(id)
{
    return this.openWorkItemEditor(CommonUrls.WorkItemEditor + "?id=" + id);
};
WindowHelpers.newWorkItem= function(wit)
{
    if(typeof(PopupManager) != "undefined")
    {
	    PopupManager.hide();
	    hidePopups();
	}
	
	WindowHelpers.openWorkItemEditor(CommonUrls.WorkItemEditor
				+ "?wit=" + encodeURIComponent(wit),
				"_blank");
	
	if(typeof(QuickLaunch) != "undefined")
	{
	    QuickLaunch.refreshNewWorkItem(wit);			
	}
}
WindowHelpers.openQueryEditor= function(url, name)
{
     var _params = new Array();
    
    _params.push("scrollbars=yes");
    _params.push("resizable=yes");
    _params.push("status=yes");

    if(JSConfig.showBrowserToolbar)
        _params.push("toolbar=yes");
        
    if(typeof(name) == "undefined")
	    name = "WND_" + Math.abs(JsUtility.getHashCode(JsUtility.getSessionHashCode() + url));

	return WindowManager.openWindowWH(url, name, false, this.m_width, 600, true, _params.join(","));
};
WindowHelpers.openNewQueryEditor= function(scope, project)
{
	return this.openNewWindow(CommonUrls.QueryEditor
				+ "?pid=" + project
				+ "&scope=" + scope, 
				"_blank");
};
WindowHelpers.openBatchEditor= function(url, workItems)
{
    var _params = new Array();
    
    _params.push("menubar=no");
    _params.push("resizable=no");
    _params.push("status=no");

    if(JSConfig.showBrowserToolbar)
        _params.push("toolbar=yes");
    else _params.push("toolbar=no");
        
	return WindowManager.openWindowWH(url + "?workitems=" + encodeURIComponent(workItems),
				WindowManager.getNextWindowName(),
				false,
				550,
				487,
				true,
				_params.join(","));
};
WindowHelpers.openSendMailWindow= function(url, requestName, requestValue)
{
    var _params = new Array();
    
    _params.push("menubar=no");
    _params.push("resizable=yes");
    _params.push("status=yes");
    _params.push("scrollbars=yes");

    if(JSConfig.showBrowserToolbar)
        _params.push("toolbar=yes");
    else _params.push("toolbar=no");
    
	return WindowManager.openWindowWH(url,
				WindowManager.getNextWindowName(),
				false,
				700,
				670,
				true,
				_params.join(","));
};

WindowHelpers.openNewWindow= function(url, name)
{
	var _name;
	
	if(name)
		_name = name;
	else _name = "WND_" + Math.abs(JsUtility.getHashCode(JsUtility.getSessionHashCode() + url));
	
	var _params = new Array();
    
    _params.push("resizable=yes");
    _params.push("status=yes");
    _params.push("scrollbars=yes");

    if(JSConfig.showBrowserToolbar)
        _params.push("toolbar=yes");
	
	return WindowManager.openWindowWH(url, 
				_name, 
				false, 
				this.m_width, 
				600, 
				true, 
				_params.join(","));
};
WindowHelpers.viewFavoriteItem= function(itemType, itemId, project)
{
	if(itemType == "WorkItem")
		this.openWorkItemEditorWithId(itemId);
	else if(itemType == "StoredQuery")
		window.location.href = CommonUrls.QueryResult + "?pid=" + project + "&qid=" + encodeURIComponent(itemId);	
	else if(itemType == "Report")
		this.viewReport(itemId, project);
};
WindowHelpers.viewReport= function(reportId, project)
{
	return this.openNewWindow(CommonUrls.ReportView 
					+ "?pid=" + project 
					+ "&id=" + encodeURIComponent(reportId));
};
WindowHelpers.openBuildResult= function(buildId)
{
	var _uri = "vstfs:///Build/Build/" + buildId;
	return WindowHelpers.openNewWindow(CommonUrls.BuildReport
			+ "?builduri=" + encodeURIComponent(_uri));
};

WindowHelpers.openMainWindow= function(urlParams)
{
    if(window.mainWindow && !window.mainWindow.closed) 
    {
        window.mainWindow.focus();
        return window.mainWindow;
    }
    else
    {
        window.mainWindow = window.open(JsUtility.joinUriComponents(urlParams, CommonUrls.Default, "?", false));
        return window.mainWindow;
    }
};

WindowHelpers.init("WindowHelpers");
