/**
 * Namespace: MN.ci
 * Custom installer for the unified installer
 */
MN.ci = {
	// Properties
	"installqmp" : null,
	"upgrade" : false,
	"os" : null,
	"browser" : null,
	"redirectUrl" : null,
	
	/**
	  * Function: WindowOnLoad
	  * Load installer when the window or iframe has loaded
	  */
	"WindowOnLoad" : function() {
		// debug
		MN.Log.logLevel = MN.Log.LogLevels.DEBUG;
		
		if(!MN.QMPInstall.CanPlay()){
			MN.ci.InstallInit();
			window.focus();
		}
		else{
			//Create confirmation video
			MN.CSS.RemoveClass($('ciPlayer'), 'ciHidden');
			MN.QVT.CreatePlayer("ciPlayer", MN.ci.OnInstallPlayerLoaded, "100%", "100%");
		}
	},
	
	/**
	  * Function: InstallInit
	  * Initialize custom messages - localization would happen here when we put it in
	  */
	"InstallInit" : function() {
		MN.ci.determineInstaller();
		MN.QMPInstall.MSG_BADOS = '';
		MN.QMPInstall.MSG_BADBROWSER = '';
		MN.QMPInstall.MSG_NEEDREINSTALL = '';
		MN.QMPInstall.MSG_UPGRADE_WIN_MOZILLA = '';
		MN.QMPInstall.MSG_UPGRADE_BASE_WIN_MOZILLA = '';
		MN.QMPInstall.MSG_UPGRADEFAILED = '';
		MN.QMPInstall.MSG_CANINSTALL = '';
		MN.QMPInstall.MSG_INSTALL_win_mozilla = '';
		MN.QMPInstall.MSG_INSTALL_win_ie = '';
		MN.QMPInstall.MSG_INSTALL_java = '';
		MN.QMPInstall.MSG_INSTALLING = '<div id="moveplay_progressouter"><div id="moveplay_progressinner"></div></div><span id="moveplay_installprogressmsg">Loading...</span>';
		MN.QMPInstall.MSG_BASE_INSTALLING_PREFIX = '<div id="moveplay_progressouter"><div id="moveplay_progressinner"></div></div><span id="moveplay_installprogressmsg">Loading...</span>';
		MN.QMPInstall.MSG_BASE_INSTALLING_POSTFIX = '<div id="moveplay_progressouter"><div id="moveplay_progressinner"></div></div><span id="moveplay_installprogressmsg">Loading...</span>';
		MN.QMPInstall.MSG_NOINSTALLMETHOD = '';
		MN.QMPInstall.MSG_NEEDRESTART = '';
	},
	
	/**
	  * Function: determineInstaller
	  * Display install or upgrade options and restrictions
	  */
	"determineInstaller" : function() {
		MN.ci.os = MN.QMPInstall.OS();
		MN.ci.browser = MN.QMPInstall.Browser();
		MN.ci.upgrade = MN.QMPInstall.UpgradeRequired();
		MN.ci.supportedOS = MN.QMPInstall.OSIsGood();
		
		if(MN.ci.upgrade){
			MN.CSS.RemoveClass($('ciUpgrade'), 'ciHidden');
			MN.CSS.RemoveClass($('ciPlayer'), 'ciHidden');
			MN.CSS.AddClass($('ciPlayer'), 'ciPlayerProgress');
			MN.CSS.RemoveClass($('ciUpgradeInst'), 'ciHidden');
			MN.ci.StartUpgrade();
		}
		else if(MN.ci.supportedOS){
			if(MN.ci.os == "mac")
			{	
				if(MN.QMPInstall.BrowserIsGood()){
					MN.CSS.RemoveClass($('ciGeneral'), 'ciHidden');
					MN.CSS.RemoveClass($('ciMacChoices'), 'ciHidden');
					MN.CSS.RemoveClass($('ciInstructions'), 'ciHidden');
					MN.CSS.RemoveClass($('ciMacInstructions'), 'ciHidden');
					MN.CSS.RemoveClass($('ciMacAlt'), 'ciHidden');
					if(MN.ci.browser == "safari")
					{
						MN.CSS.AddClass($('ciInstall'), 'ciPicMacSaf');
					}
					else if(MN.ci.browser == "mozilla")
					{
						MN.CSS.AddClass($('ciInstall'), 'ciPicMacFF');
					}
					
					// Player needs to be visible for install methods to execute
					MN.CSS.AddClass($('ciPlayer'), 'ciPlayerHidden');
					MN.CSS.RemoveClass($('ciPlayer'), 'ciHidden');
					MN.QVT.CreatePlayer("ciPlayer", MN.ci.OnInstallPlayerLoaded, "100%", "100%");
				}
				else{
					MN.CSS.RemoveClass($('ciNoMac'), 'ciHidden');
				}
			}
			else
			{
				if(MN.QMPInstall.BrowserIsGood()){
					MN.CSS.RemoveClass($('ciGeneral'), 'ciHidden');
					MN.CSS.RemoveClass($('ciPCChoices'), 'ciHidden');
					MN.CSS.RemoveClass($('ciInstructions'), 'ciHidden');
					if(MN.ci.browser == "ie")
					{
						MN.CSS.RemoveClass($('ciPCIEInstructions'), 'ciHidden');
						MN.CSS.AddClass($('ciInstall'), 'ciPicPCIE');
					}
					else if(MN.ci.browser == "mozilla")
					{
						MN.CSS.RemoveClass($('ciPCFFInstructions'), 'ciHidden');
						MN.CSS.AddClass($('ciInstall'), 'ciPicPCFF');
					}
					
					// Player needs to be visible for install methods to execute
					MN.CSS.AddClass($('ciPlayer'), 'ciPlayerHidden');
					MN.CSS.RemoveClass($('ciPlayer'), 'ciHidden');
					MN.QVT.CreatePlayer("ciPlayer", MN.ci.OnInstallPlayerLoaded, "100%", "100%");
				}
				else{
					MN.CSS.RemoveClass($('ciNoPC'), 'ciHidden');
				}
			}
		}
		else{
			MN.CSS.RemoveClass($('ciNoOS'), 'ciHidden');
		}
		
		if(!MN.ci.upgrade){
			MN.CSS.RemoveClass($('ciPreInstall'), 'ciHidden');
		}
	},

	/**
	  * Function: StartUpgrade
	  * Upgrade the user by creating a player, which will auto upgrade
	  */
	"StartUpgrade" : function() {
		MN.QVT.CreatePlayer("ciPlayer", MN.ci.OnInstallPlayerLoaded, "100%", "100%");
	},
	
	/**
	  * Function: startInstall
	  * Call into the SDK to StartInstall if the user chose the Java installer for Mac, otherwise redirect to the correct installer
	  *
	  * Parameter:
	  * installer:Boolean - download based install if true, java based (mac only) if false
	  */
	"startInstall" : function(installer) {
		// If DMG or EXE, set up callback for when install is completed
		if(installer){
			MN.QMPInstall.controller.WaitForInstall(MN.ci.installFinished);
		}
		
		// If Java, show player so loading/progress is shown
		if(!installer){
			MN.CSS.RemoveClass($('ciPlayer'), 'ciPlayerHidden');
			MN.CSS.AddClass($('ciPlayer'), 'ciPlayerProgress');
			MN.CSS.AddClass($('ciMacJavaBeginInstall'), 'ciHidden');
		}
		
		// Start install
		MN.QMPInstall.StartInstall(installer);
	},
	
	/**
	  * Function: installFinished
	  * Install process has completed, show and create the player
	  */
	"installFinished" : function() {
		MN.QVT.CreatePlayer("ciPlayer", MN.ci.OnInstallPlayerLoaded, "100%", "100%");
		
		if (MN.ci.browser == "mozilla" && MN.ci.os != "mac"){
			history.go(0);
		}
	},
	
	/**
	  * Function: OnInstallPlayerLoaded
	  * Player has finished being created, enlarge video and set timeout for confirmation to give video time to load
	  */
	"OnInstallPlayerLoaded" : function(player) {
		if(!player)
		{
			return;
		}

		MN.CSS.RemoveClass($('ciPlayer'), 'ciPlayerHidden');
		MN.CSS.RemoveClass($('ciPlayer'), 'ciPlayerProgress');
		MN.CSS.AddClass($('ciPreInstall'), 'ciHidden');
	
		MN.ci.installqmp = player;
		
		// Set the player to cap the bit rate of the player based on player stage size
		MN.ci.installqmp.autoSelectByStageSize(true);
		
		// Set video to play
		MN.ci.installqmp.Play('http://cms014.qec12.qcg5.qcn1.movenetworks.com/cms/publish3/folder/assetqvt/1041/7498.qvt');
		setTimeout(MN.ci.ShowInstallComplete, 2000);
	},
	
	/**
	  * Function: ShowInstallComplete
	  * Show confirmation buttons for video playing
	  */
	"ShowInstallComplete" : function() {
		if(MN.ci.upgrade){
			MN.CSS.AddClass($('ciUpgrade'), 'ciHidden');
			MN.CSS.RemoveClass($('ciPostUpgradeTitle'), 'ciHidden');
		}
		else{
			MN.CSS.RemoveClass($('ciPostInstallTitle'), 'ciHidden');
		}
		MN.CSS.RemoveClass($('ciPostInstall'), 'ciHidden');
	},
	
	/**
	  * Function: ShowIECloseMessage
	  * 
	  */
	"ShowIECloseMessage" : function() {
		MN.CSS.RemoveClass($('ciPostIEInstall'), 'ciHidden');
	},
	
	/**
	  * Function: CanSeeVideo
	  * Send success message to page if user clicked Yes, otherwise show Support page
	  * 
	  * Property:
	  * b:Boolean - Flag if the user saw video or not
	  */
	"CanSeeVideo" : function(b) {
		if(b) // The user said that they CAN see the video
		{
			MN.CSS.AddClass($('ciPostInstall'), 'ciHidden');
			if(MN.ci.installqmp){
				MN.ci.installqmp.Stop();
			}
			MN.CSS.AddClass($('ciPlayer'), 'ciHidden');
			try {
				opener.history.go(0);
				window.close();
			} catch (e) {
				MN.ci.ShowIECloseMessage();
			}
		}
		else // The user said that they CAN NOT see the video
		{
			document.location.href = 'http://player.movenetworks.com/moveps/support/faq.html'; // Redirect to FAQ
		}
	},
	
	
	/**
	  * Function: Popup
	  * Utility function to popup the Support pages
	  * 
	  * Property:
	  * url:String - url of the page
	  * width:Number - width of the popup
	  * height:Number - height of the popup
	  * top:Number - distance from the top of the screen
	  * left:Number - distance from the left of the screen
	  */
	"Popup" : function(url, width, height, top, left) {
		if(!height) var height = 518;
		if(!width) var width = 785;
		if(!top) var top = 0;
		if(!left) var left = 0;
	
		var popup = null;
	
		var popupString = "height=%s,width=%s,toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,top=%s,left=%s".format(height, width, top, left);
		
		if(url != null)
		{
			popup = window.open(url, '_blank', popupString);
		}
		
		// Check to see if the window actually came up
		if(popup == null || typeof(popup) == "undefined")
		{
			// In case of a popup blocker
			alert('popup blocked');
			return null;
		}
		return popup;
	}
};

// Load the installer
MN.Event.Observe(window,"load",MN.ci.WindowOnLoad);