/**
 * @file windowManagement.js
 * 
 * This file is to contain scripts that will control the various windows
 *	opened and used by the application.
 */


// planner window
var winPlanner 		= null;
// children of the planner window
var winInfo 		= null;
var winMaterials 	= null;
var winReports 		= null;
var winScreenShots 	= null;


(function() {

	/**
	 */
	function loadScene ( sceneId, showLoader, useCurrentWindow, projectId ) {		
		var params 	= 'sceneId=' + sceneId + '&projectId=' + projectId;
		doLaunch( params, showLoader, useCurrentWindow );
	}

	/**
	 */
	function duplicateScene ( duplicateSceneId, newSceneName, 
			showLoader, useCurrentWindow, autoAccountType, projectId ) {
		var params 	= 'duplicateSceneId=' + duplicateSceneId 
					+ '&projectId=' + projectId
					+ '&newSceneName=' + newSceneName 
					+ '&actype=' + autoAccountType;
		doLaunch( params, showLoader, useCurrentWindow );
	}

	/**
	 */
	function doLaunch ( params, showLoader, useCurrentWindow ) {
		// Pre-Launch
		var url = CONTEXT_PATH + '/jsp/secure/planner2/prelaunch.jsp';	
		var options	= { method: 'post', asynchronous: false, postBody: params
			, onSuccess: function (r) { eval( r.responseText ); }
			, onFailure: function (t) { alert( 'Error ' + t.status + ' -- ' + t.statusText ); }
			};
		new Ajax.Request( url, options );
		
		// Launch
		url = CONTEXT_PATH + '/jsp/secure/planner2/planner.jsp?';

		if (showLoader == true) url += 'showLoader=true';
		
		if (useCurrentWindow == true)
			window.location.href = url + 'newWindow=false';
		else
		{
			winPlanner = CreateChildWindow( url + 'newWindow=true', 'winPlanner', true );
			winPlanner.focus();
		}
	}
	
	// Expose public methods
	window.DuplicateSceneInPlanner 	= duplicateScene;
	window.LoadSceneInPlanner 		= loadScene;
	
})();

/**
 * @func: 	CreateChildWindow (contentURL, wName, wHeight, wWidth)
 * @desc: 	creates a popup window.
 * @param:	contentURL 	the URL that will display in the window.
 * @param:	wName 		the name of the window.
 * @param:	max 		(optional, default null) if true, window opens maximized
 * @param:	wHeight 	(optional, default 600) height of the window.
 * @param:	wWidth 		(optional, default 800) the width of the window.
 * @return: handle to the created window.
 */
function CreateChildWindow (contentURL, wName, max, wHeight, wWidth)
{
	var w = wWidth  || 1000;
	var h = wHeight || 700;
	
	// figure out the top and left position to have the window centered.
	var wLeft = (screen.width/2)  - (w/2);
	var wTop  = (screen.height/2) - (h/2);
	
	// set the attributes string to pass to the window.open function
	var attr = "status=0, toolbar=0, location=0, menubar=0, resizable=1, scrollbars=1";	
	
//	if (max) attr += ", channelmode=1"; // "theater mode" a.k.a. maximized
	
	attr += ", height="  + h;
	attr += ", width="   + w;
	attr += ", screenX=" + wLeft;
	attr += ", screenY=" + wTop;
	
	return window.open(contentURL, wName, attr);
}

/**
 * @func: 	DisplayPlannerWindow (contentURL)
 * @desc: 	opens or brings to focus the planner window
 * @param:	contentURL	[in] the URL that will display in the window
 * @return: none
 */
function DisplayPlannerWindow (contentURL)
{
	// open the planner window
	winPlanner = CreateChildWindow(contentURL, 'winPlanner', true);
	winPlanner.focus();
}

/**
 * @func: DisplayMaterialPicker (contentURL)
 * @desc: opens or brings to focus the material picker window
 * @param:	contentURL	the URL that will display in the window
 * @return: none
 */
function DisplayMaterialPicker (contentURL)
{	
	// open the material picker window
	winMaterials = CreateChildWindow(contentURL, 'winMaterials');
	winMaterials.focus();
	
	// make the material picker close if the planner window changes
	winPlanner.onunload = function () { winMaterials.close(); winMaterials = null; }
}

/**
 * 
 * @return
 */
function CCinfo () {
	var ccUrl = CONTEXT_PATH + "/jsp/public/account/register/whyCC.jsp";
	window.open(ccUrl, "myWindow", "status = 1, height = 400, width = 400, resizable = 0" );
}
function DisplayCCInfo () { CCinfo(); }

/**
 * 
 * @param userId
 * @return
 */
function QuickScene (userId)
{
	var url = CONTEXT_PATH + "/jsp/secure/planner2/quickScene.jsp";
	new Ajax.Request(url, {
		method: 'get',
		parameters: {userId:userId}
	});		
}

/**
 * @func: DisplayMaterialPicker (contentURL)
 * @desc: opens or brings to focus the material picker window
 * @param:	contentURL	the URL that will display in the window
 * @return: none
 */
function DisplayReportsWindow (params)
{
	var url = CONTEXT_PATH + '/jsp/secure/reports/reports.jsp?' + params;
	
	// open the material picker window
	winReports = CreateChildWindow(url, 'winReports', true);//false, 650, 950);
	winReports.focus();
}

