/**
 * @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;

/**
 * @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 ? wWidth : 1000;
	var h = wHeight ? 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}
	});		
}

/**
 * 
 * @param sceneId
 * @param showLoader
 * @param useCurrentWindow
 * @param projectId
 * @return
 */
function LoadSceneInPlanner (sceneId, showLoader, useCurrentWindow, projectId)
{		
	var url = CONTEXT_PATH + '/jsp/secure/planner2/prelaunch.jsp';
	var requestContent = 'sceneId=' + sceneId + '&projectId=' + projectId;
	var options	= { method: 'post'
		, asynchronous: false
		, postBody: requestContent
		, onSuccess: function (r) { eval(r.responseText); }
		, onFailure: function (t) { alert('Error ' + t.status + ' -- ' + t.statusText); }
		};
	
	var req = new Ajax.Request(url, options);
	
	var contentURL = CONTEXT_PATH + '/jsp/secure/planner2/planner.jsp?';

	if (showLoader == true)
		contentURL += 'showLoader=true&';
		
	// open the planner window
	if ( useCurrentWindow == true )
	{
		contentURL += 'newWindow=false&' + requestContent; 
		window.location.href = contentURL;
	} 
	else
	{
		contentURL += 'newWindow=true&';
		winPlanner = CreateChildWindow(contentURL, 'winPlanner', true);
		winPlanner.focus();
	}	
}

/**
 * 
 * @param duplicateSceneId
 * @param newSceneName
 * @param showLoader
 * @param useCurrentWindow
 * @param autoAccountType
 * @return
 */
function DuplicateSceneInPlanner (
		duplicateSceneId, 
		newSceneName, 
		showLoader, 
		useCurrentWindow, 
		autoAccountType,
		projectId)
{
	var url = CONTEXT_PATH + '/jsp/secure/planner2/prelaunch.jsp';
	var requestContent = 'duplicateSceneId=' + duplicateSceneId 
		+ '&projectId=' + projectId
		+ '&newSceneName=' + newSceneName 
		+ '&actype=' + autoAccountType
		;
	
	var options	= { method: 'post'
		, asynchronous: false
		, postBody: requestContent
		, onSuccess: function (r) { eval(r.responseText); }
		, onFailure: function (t) { alert('Error ' + t.status + ' -- ' + t.statusText); }
		};
	
	//alert('doing ajax request to: ' + url);
	new Ajax.Request(url, options);
	
	var contentURL = CONTEXT_PATH + '/jsp/secure/planner2/planner.jsp';

	if (showLoader == true)
		contentURL += '?showLoader=true';
		
	// open the planner window
	if (useCurrentWindow == true) {
		window.location.href = contentURL + '?' + requestContent;
	} else {
		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 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();
}
