Figment.Import( "Figment.EventHandler",Figment.getJSRoot() + "_framework/" );
Figment.Import( "Figment.DOM",Figment.getJSRoot() + "_framework/" );
Figment.Import( "Figment.Net.Request",Figment.getJSRoot() + "_framework/" );
Figment.Import( "Disney.WDPRO.UI.InterfaceBlocking",Figment.getJSRoot() + "_global/" );
Figment.Import('Disney.WDPRO.IBC.UI.DynamicPleaseWait',Figment.getJSRoot() + '_global/');

Figment.Namespace("Disney.WDPRO.IBC.Session");

Disney.WDPRO.IBC.Session = {
	// When the session will expire.  This should be overridden by the values 
	// in the SiteConfiguration.
	DEFAULT_TTL_IN_MINS: 2,
	// This is when the warning will appear.  It is time in minutes to be 
	// subtracted from the session expiration timeout.  This should be overridden
	// by the values in the SiteConfiguration.
	DEFAULT_TTL_WARN_IN_MINS: 1,
	// Location of the module that contains the content for the session timeout
	// layer
	WARNING_EXPIRING_LAYER_ID: "SessionTimeoutWarning_Module",
	// Location of the content to display when a warning is needed
	WARNING_EXPIRING_LAYER_CONTENT_ID: "sessionTimeout_sessionWarning_Module",
	// Location of the content that contains the text
	WARNING_EXPIRING_LAYER_TEXT_ID: "sessionWarningText",
	// Location of the content to display when the session has expired
	EXPIRED_SESSION_LAYER_ID: "sessionTimeout_sessionTimeout_Module",
	// Location of button to keep the session alive
	WARNING_KEEPALIVE_BUTTON: "sessionTimeoutModule_Continue_Btn",
	WARNING_SAVEOFFER_KEEPALIVE_BUTTON:"sessionTimeout_sessionWarning_dontSaveOffer_selectBtn",
	WARNING_SAVEOFFER_BUTTON:  "sessionTimeout_sessionWarning_saveOffer_selectBtn",
	// Location of the AJAX processor
	AJAX_PROCESSOR_LOCATION: Figment.getWebRoot() + "en_US/_framework/components/ajaxProcessor",
	
	// Reference id to session timer
	TIMER: null,
	// Reference for the Interface Blocker once it has been created
	BLOCKER: null,
	
	// Milliseconds is optional.  The default of the FULL time until session
	// warning will be used if not passed to this method.
	startTimer: function(evt,millis)
	{
		var useSessionExpiredLayer = typeof millis !== "undefined" ? true : false;
		// Convert the warning timeout period to milliseconds (use passed in
		// in millis if available, otherwise set to default.
		millis = (typeof millis !== "undefined" && millis !== null) ? millis : (Disney.WDPRO.IBC.Session.DEFAULT_TTL_IN_MINS - Disney.WDPRO.IBC.Session.DEFAULT_TTL_WARN_IN_MINS) * ( 60 * 1000 );
		// Determine if we need to show the session has expired or will expire
		// layers
		Disney.WDPRO.IBC.Session.TIMER = useSessionExpiredLayer ? window.setTimeout(Disney.WDPRO.IBC.Session.displayExpiredSession,millis) : window.setTimeout(Disney.WDPRO.IBC.Session.displayWarning,millis);
	},
	
	// Milliseconds is optional.  The default of the FULL time until session
	// warning will be used if not passed to this method.
	resetTimer: function(millis)
	{
		// Clear the current timer out
		Disney.WDPRO.IBC.Session.stopTimer();
		// Restart the timer again, since we sent a keep-alive to the MW for
		// this session already
		Disney.WDPRO.IBC.Session.startTimer(null,millis);
	},
	
	// Will stop the timer and clear the reference to the timer
	stopTimer: function()
	{
		if( Disney.WDPRO.IBC.Session.TIMER !== null )
		{
			// Clear the current timer out
			window.clearTimeout(Disney.WDPRO.IBC.Session.TIMER);
		}
		// Reset the timer reference id
		Disney.WDPRO.IBC.Session.TIMER = null;
	},
	
	// Method to call when the AJAX request has completed.  If the request was
	// successful, it will reset the timer, hide the warning layer, and remove
	// the PleaseWait layer (if present)
	mwCallBack: function(netRequest)
	{
		if( netRequest !== null && netRequest.wasSuccessful() )
		{
			// Now restart the timer
			Disney.WDPRO.IBC.Session.resetTimer();
			// Hide the please wait layer if it is present
			if( Figment.doesClassExist( "Disney.WDPRO.IBC.UI.DynamicPleaseWait" ) )
			{
				Disney.WDPRO.IBC.UI.DynamicPleaseWait.BLOCKING_INTERFACE.turnOff();

				// Show the select boxes
				Disney.WDPRO.IBC.Session.toggleSelectBoxes( "show" );
			}
		}
	},
	
	// Displays the warning layer to the guest
	displayWarning: function()
	{
		// Reset the timer (convert ttl_to_warn time to milliseconds)
		Disney.WDPRO.IBC.Session.resetTimer(Disney.WDPRO.IBC.Session.DEFAULT_TTL_WARN_IN_MINS * ( 60 * 1000 ));
		// Now show the content for the session warning layer
		var content = document.getElementById(Disney.WDPRO.IBC.Session.WARNING_EXPIRING_LAYER_CONTENT_ID);
		var warningText = document.getElementById(Disney.WDPRO.IBC.Session.WARNING_EXPIRING_LAYER_TEXT_ID);
		if( content !== null && warningText !== null)
		{
			// Replace text blocks with real numbers
			var text = warningText.innerHTML;
			text = text.replace("[##Number##]",Disney.WDPRO.IBC.Session.DEFAULT_TTL_IN_MINS);
			text = text.replace("[##Timer##]",Disney.WDPRO.IBC.Session.DEFAULT_TTL_WARN_IN_MINS);
			// Place the modified text back into the content
			warningText.innerHTML = text;
		}
		Disney.WDPRO.IBC.Session.BLOCKER = new Disney.WDPRO.UI.InterfaceBlocking();
		Disney.WDPRO.IBC.Session.BLOCKER.addContent( content );
		Disney.WDPRO.IBC.Session.BLOCKER.turnOn();
		content.style.display = "block";
		
		// Hide the select boxes
		Disney.WDPRO.IBC.Session.toggleSelectBoxes();
		
	},
	
	// Hides the warning layer either when the expired session layer is displayed
	// or when the session is kept-alive
	hideWarning: function()
	{
		// Clear the current timer out
		Disney.WDPRO.IBC.Session.stopTimer();
		// Now show the content for the session warning layer
		var content = document.getElementById(Disney.WDPRO.IBC.Session.WARNING_EXPIRING_LAYER_CONTENT_ID);
		if( content !== null && Disney.WDPRO.IBC.Session.BLOCKER !== null )
		{
			content.style.display = "none";
			Disney.WDPRO.IBC.Session.BLOCKER.turnOff();
			Disney.WDPRO.IBC.Session.BLOCKER = null;

			// Hide the select boxes
			Disney.WDPRO.IBC.Session.toggleSelectBoxes("show");
		}
	},

	/**
	 * Hide select boxes in order to prevent unsightly boxes showing through
	 * the blocking layer. Used to fix IE :-(.
	 */
	toggleSelectBoxes: function( strDisplay )
	{
		var objBody	= Figment.DOM.getElementsByTagName( "body" )[0];

		if( strDisplay === 'show' )
		{
			Figment.DOM.removeClassName( objBody, 'hideSelects' );
		}
		else
		{
			Figment.DOM.addClassName( objBody, 'hideSelects' );
		}
		
		// Garbage man!
		delete objBody;
	},

	
	// Displays the session has expired layer (this is as far you go)
	displayExpiredSession: function()
	{
		// Hide the warning layer if it is showing
		Disney.WDPRO.IBC.Session.hideWarning();
		// Now show the content for the session expired layer
		var content = document.getElementById(Disney.WDPRO.IBC.Session.EXPIRED_SESSION_LAYER_ID);
		if( content !== null )
		{
			Disney.WDPRO.IBC.Session.BLOCKER = new Disney.WDPRO.UI.InterfaceBlocking();
			Disney.WDPRO.IBC.Session.BLOCKER.addContent( content );
			Disney.WDPRO.IBC.Session.BLOCKER.turnOn();
			content.style.display = "block";
			
			// Hide the select boxes
			Disney.WDPRO.IBC.Session.toggleSelectBoxes();
		}
	},
	
	// Event to use when the guest wants to keep the session alive (after the
	// warning layer has displayed).  A keep-alive call will be made to the server
	// and the timer will reset.
	EVENT_keepAlive_onClick: function(evt)
	{
		var content = document.getElementById(Disney.WDPRO.IBC.Session.WARNING_EXPIRING_LAYER_CONTENT_ID);
		var module = document.getElementById(Disney.WDPRO.IBC.Session.WARNING_EXPIRING_LAYER_ID);
		if( content !== null && module !== null )
		{
			var moduleName = Figment.DOM.getRealModuleName( module );
			if( moduleName !== null )
			{
				// Send a keep-alive to the MW to keep this session going on the 
				// backend
				var netRequest = new Figment.Net.Request();
				netRequest.getOptions().method = "post";
				netRequest.addParameter( "keepAlive_Submit","true" );
				netRequest.addParameter( "timeToLive",Disney.WDPRO.IBC.Session.DEFAULT_TTL_IN_MINS.toString() );
				netRequest.addParameter( "strModuleName",moduleName );
				netRequest.getOptions().setUseCallbacks( true );
				netRequest.setCompleteCallBack(
					Disney.WDPRO.IBC.Session.mwCallBack
				);
				// Show the please wait layer if it is present
				if( Figment.doesClassExist( "Disney.WDPRO.IBC.UI.DynamicPleaseWait" ) )
				{
					var content = document.getElementById( Disney.WDPRO.IBC.UI.DynamicPleaseWait.getAsyncModuleName() );
					var pleaseWaitLayer = document.getElementById( Disney.WDPRO.IBC.UI.DynamicPleaseWait.ID_WAITING_LAYER );
					if( content !== null && pleaseWaitLayer !== null )
					{
						// Put up the blocking layer
						Disney.WDPRO.IBC.UI.DynamicPleaseWait.BLOCKING_INTERFACE.addContent(content);
						Disney.WDPRO.IBC.UI.DynamicPleaseWait.BLOCKING_INTERFACE.turnOn();
						// Get the please wait layer out of the content
						pleaseWaitLayer.style.display = "block";
					}
				}
				// Now hide the warning layer
				Disney.WDPRO.IBC.Session.hideWarning();
				// Send the keep-alive to the MW
				netRequest.send( Disney.WDPRO.IBC.Session.AJAX_PROCESSOR_LOCATION );
			}
		}
		// Show the select boxes
		Disney.WDPRO.IBC.Session.toggleSelectBoxes( 'show' );
	},
	
	EVENT_saveOffer:function(evt){
		
		Disney.WDPRO.IBC.Session.hideWarning();
	},
	
	// Event to use when the document has finished loading.  This will start the
	// timer immediately and then attach the EVENT to the warning layers' button.
	main: function(evt)
	{
		Disney.WDPRO.IBC.UI.DynamicPleaseWait.BLOCKING_INTERFACE = new Disney.WDPRO.UI.InterfaceBlocking();
		Disney.WDPRO.IBC.Session.startTimer();
		var btn = document.getElementById( Disney.WDPRO.IBC.Session.WARNING_KEEPALIVE_BUTTON );
		if( btn !== null )
		{
			// Attach the click event to the session warning layer keep-alive
			// button.
			Figment.EventHandler.addEvent(btn,"click",Disney.WDPRO.IBC.Session.EVENT_keepAlive_onClick);
		}
		
		btn = document.getElementById( Disney.WDPRO.IBC.Session.WARNING_SAVEOFFER_KEEPALIVE_BUTTON );
        if( btn !== null )
        {
            // Attach the click event to the session warning layer keep-alive
            // button.
            Figment.EventHandler.addEvent(btn,"click",Disney.WDPRO.IBC.Session.EVENT_keepAlive_onClick);
        }
		
		btn = document.getElementById(Disney.WDPRO.IBC.Session.WARNING_SAVEOFFER_BUTTON);
		if (btn !== null) {
			Figment.EventHandler.addEvent(btn, "click", Disney.WDPRO.IBC.Session.EVENT_saveOffer);
		}
        
	
        // Cleanup
        delete btn;
	},
	
	// Event to use when the document is unloaded.  This cleans up the timer so
	// we don't leak all over IE.
	EVENT_window_onUnload: function(evt)
	{
		// Cleanup
		Disney.WDPRO.IBC.Session.stopTimer();
	}
};

// Run this EVENT when the document is unloaded (to cleanup after ourselves)
Figment.EventHandler.addEvent(window,"unload",Disney.WDPRO.IBC.Session.EVENT_window_onUnload);
Figment.EntryPoint.add( Disney.WDPRO.IBC.Session );

