/**
 * @author joseph
 * 
 * Show/Hide Flash Player for Scion Flex App
 */

var _setToggle = true;
var _oldLocation = location.href;
//constants for CSSP page requests
var _csspRegisterFlow = "REGISTER"; 
var _csspUpdateProfile = "UPDATE_PROFILE"; 
var _csspVinUpdate = "VIN_UPDATE"; 
var _csspVehicleUpdate = "VEHICLE_UPDATE";
// constants for CSSP process completion - to inform the Flex app of what just happened in CSSP
var _csspDisplayOnly = "DISPLAY_ONLY"; 
var _csspRegisterSuccess = "REGISTER_SUCCESS";

function swfToggle(pageFlow, vinNumber, flexPage) {
	var _flashContainer = document.getElementById('scionContainer');
	var _csspFrame = document.getElementById('ccspFrame');
	var requestURL;
	
	if( pageFlow != undefined )
	{
		// pageFlow exists, so this should be a Flex request to display CSSP
		// vinNumber will usually be null, unless it's the _vinUpdate flow 
		switch(pageFlow) 
		{ 
			case _csspRegisterFlow: 
				// display register JSP 
				requestURL = "/scion/ssl/user/myscion/registerForm.do";
			break;
			case _csspUpdateProfile:
				// display profile update JSP 
				requestURL = "/scion/auth-ssl/user/myscion/profileForm.do";
			break;
			case _csspVinUpdate: 
				// display VIN Landing JSP
				requestURL = "/scion/auth-ssl/owners/startOwners.do";
			break;
			case _csspVehicleUpdate: 
				// display vehicle update JSP 
				requestURL = "/scion/auth-ssl/owners/startOwners.do?vinNumber=" + vinNumber;
			break;
		}
	}
	
	if (pageFlow) {
		switch(_csspFrame.style.display) 
		{ 
			case "none": 
				// display Flex app
				if( requestURL != "" )
				{
					// navigate CSSP frame to the appropriate place
					_csspFrame.src = requestURL;
				}
				_flashContainer.style.width = '1px';
				_flashContainer.style.height = '1px';
				_csspFrame.style.display = 'block';
				_setToggle = false;
			break;
			case "block": 				
				if (pageFlow == _csspRegisterSuccess) {
					mainSWFControlCutoverFromCSSP(pageFlow);
				}				
				//flexPage is for links in the CSSP app that take the user to another part of the flex app.
				if (flexPage) {
					window.document.location.href = '/#'+flexPage;
				}				
				_flashContainer.style.width = '100%';
				_flashContainer.style.height = '100%';
				_csspFrame.style.display = 'none';
				_csspFrame.src = "";
				_setToggle = true;
			break;
		}
	}
}


/**
 * Fires the swfToggle method if the URL hash changes while Flex App is hidden
 */
setInterval(function() {
    if (_setToggle == false) {
    	if(location.href != _oldLocation) {
    		swfToggle();
            _oldLocation = location.href;
       }
    }
}, 1000);

/**
 * Handles informing the main Flex app of the current status of control, from CSSP
 */
function mainSWFControlCutoverFromCSSP(pageFlow)
{
	//swfInstance = window.document[event.swf]; // SEE mapbridge.js
	swfInstance.returnCutoffControlFromCSSP(pageFlow);
}
