var Syos = Class.create();
Syos.prototype = {
	initialize: function() {
		if (!POP.Config.Syos.Enabled) {
			// Do nothing if SYOS is not enabled
			this.debug('SYOS is not enabled.');
			if ($('syos_content')) $('syos_content').remove();
			if ($('production_links')) $('production_links').remove();
			if ($('remove_alert')) $('remove_alert').remove();
			if ($('best_available')) $('best_available').style.display='block';
			if ($('main_content')) $('main_content').addClassName('main_content_best');
			return;
		}
		this.UI = {
			Wrapper: $(POP.Config.Syos.WrapperId),
			Best: $(POP.Config.Syos.BestAvailableId),
			Syos: $(POP.Config.Syos.SyosId)
		}
		if (!this.UI.Wrapper || !this.UI.Best || !this.UI.Syos) {
			// Needed HTML elements are not in place;
			this.onHTMLError();
			return;
		}
		
		// Add a hidden 'state' input to the page to store the current view;
		// Allows us to return to the page after postback and see the same 'view'
		this.UI.SyosState = document.createElement('input');
		this.UI.SyosState.setAttribute('type','hidden');
		this.UI.SyosState.setAttribute('id','uxSyosState');
		this.UI.SyosState.setAttribute('name','uxSyosState');
		this.UI.SyosState.setAttribute('value',POP.Config.Syos.DefaultView);
		this.UI.Wrapper.appendChild(this.UI.SyosState);
		
		// Setup the view toggling links
		this.UI.lnkBest = $('lnk_best');
		this.UI.lnkSyos = $('lnk_syos');
		this.addViewButton(this.UI.lnkBest, 'best');
		this.addViewButton(this.UI.lnkSyos, 'syos');

		// C.N.: was choking on '9', quick fix - hard-coded as '9.0.0', value is no longer being passed in.
		if (!this.test('9.0.0')) {
			// Deal with invalid flashplayer version (upgrade)
			this.onInvalidPlayerVersion();
			$('best_instructions').style.display='none';	
		}
		else{
			// Browser has adequate flash version installed
			this.showView(POP.Config.Syos.DefaultView);
		}
		
		// adjust some styles based on changing height of performance_info from messaging.
		if ($$('div.main_content_syos div#performance_info')) {
			
			var adjustmentValue = $('performance_info').getHeight() + 25; // 25 is arbitrary -- looks good
		

			// build variables from height
			var valueString = adjustmentValue + 'px';
			var negativeValueString = '-' + adjustmentValue + 'px';			
			var mainContentMargin = valueString + ' 0 0 0';
			
			// set styles on elements
			Element.setStyle('performance_info',{
				top: negativeValueString
			});
			
			;
			Element.setStyle('signin_select',{
				top: negativeValueString
			});

		}
	},
	/**
	 * Most clients will want to fail silently. We could implement a notice here
	 * or auto-upgrade them via SWFObject.
	 */
	onInvalidPlayerVersion: function() {
		this.showView("best", true);// Show the best avial view and pass arg to hide switching link.
		this.debug("The Flash Player version requirement has not been met.");
		this.debug("--Installed: " + $H(swfobject.getFlashPlayerVersion()).inspect());
		this.debug("--Required: " + POP.Config.Syos.FlashVersion.inspect());
	},
	/**
	 * Deal with missing HTML elements, or notify developer if not in place.
	 */
	onHTMLError: function() {
		this.debug("You are missing some required HTML Elements.");
		this.debug($H(this.UI).toJSON());
	},
	/**
	 * Displays the specified Selection view [syos|best] and hides the other.
	 * @param {String} sWhich The view to show [syos|best]
	 * @param {Boolean} bHideLink option to not show the view's link (used by invalid player version)
	 * @return void
	 */
	showView: function(sWhich, bHideLink){
		var main_content = $('main_content');
		var secondary_content = $('secondary_content');
		var production_links = $('production_links');
		var syos_instructions = $('syos_instructions');
		var best_instructions = $('best_instructions');
		var standing_room = $('standing_room');
		var chartSwf = $('chartSwf');
		var remove_alert = $('remove_alert');
		var nav_base = $('nav_base');
		
		if (sWhich == 'syos') {
			this.UI.Syos.style.display = 'block';
			this.UI.Best.hide();
			
			if ($('seatingChart')) $('seatingChart').style.display ='none';
			
			
			if (main_content)		{
				main_content.addClassName('main_content_syos');
				main_content.removeClassName('main_content_best');
			}	
			if (secondary_content)	{
				secondary_content.toggleClassName('secondary_content_syos');
				if (!secondary_content.empty() && standing_room) standing_room.addClassName('secondary_content_exists');
			}
			if (best_instructions)  best_instructions.hide();
			if (syos_instructions)  syos_instructions.show();
			if (chartSwf)			chartSwf.hide();
			if (production_links)	production_links.show();
			if (remove_alert)		remove_alert.style.display='block';
			
			if (nav_base) nav_base.addClassName('nav_base_syos');
						
			this.write();
		} else if (sWhich == 'best') {
			this.UI.Syos.hide();
			this.UI.Best.style.display='block';
			
			if ($('seatingChart')) $('seatingChart').style.display ='inline';
			
			if (main_content)		{
				main_content.removeClassName('main_content_syos');
				main_content.addClassName('main_content_best');
			}
			if (secondary_content)	secondary_content.toggleClassName('secondary_content_syos');
			
			if (syos_instructions)  syos_instructions.hide();
			if (best_instructions)  best_instructions.show();
			if (standing_room)		standing_room.removeClassName('secondary_content_exists');
			if (production_links)	production_links.hide();
			if (chartSwf)			chartSwf.show();	
			if (remove_alert)		remove_alert.hide();
			
			if (nav_base) nav_base.removeClassName('nav_base_syos');
		}
		
		// Update the syos state so postback returns us to the same view
		this.UI.SyosState.setAttribute('value',sWhich);
	},
	/**
	 * Test a version string ( 5 | 5.2 | 5.2.42 | etc ) against the installed flash player
	 * @param {String} sVersion The version of the player to test for
	 * @return {Bool}
	 */
	test: function(sVersion) {
		return swfobject.hasFlashPlayerVersion(sVersion);
	},
	/**
	 * Writes the SWF to the page
	 */
	write: function() {
		var config = POP.Config.Syos;
		var flashAtt = {
			"data": config.viewerSWF,
			"width": config.Width,
			"height": config.Height
		};
		var flashVars = {
			"xmlPath": config.xmlPath,
			"apiPath": config.apiPath,
			"swfPath": config.swfPath,
			"imgPath": config.imgPath,
			"cartPath": config.cartPath,
			"serverRoot": config.serverRoot,
			"perfNum": config.perfNum,
			"packNum": config.packNum,
			"imosNum": config.imosNum,
			"sr_count": config.sr_count, //  standing room ticket count in cart
			"refreshURL": config.refreshURL
			
			// "ga": config.ga // for GA tracking
		};
		var flashAtt = {
			"data": config.viewerSWF,
			"width": config.Width,
			"height": config.Height
		};
		// using createSWF instead of embedSWF to bypass unneeded install logic
		swfobject.createSWF(flashAtt, {"flashvars": Object.toQueryString(flashVars)}, 'syos_swf');
	},
	
	/**
	 * Adds functionality to an element to display a view
	 * @param {String} sElementId The ID of the element to attach functionality to
	 * @param {String} sView The view to show when the element is clicked
	 */
	addViewButton: function(sElementId, sView) {
		var App = this;
		Event.observe($(sElementId), 'click', function(e) {
			//App.debug(sView);
			var e = e || window.event;
			Event.stop(e);
			App.showView(sView);
		});
	},
			
	/**
	 * If debugging is enabled, display a debugging message
	 */
	debug: function(sMsg) {
		if (POP.Config.Syos.Debug) {
			if (typeof console != 'undefined' && typeof console.info != 'undefined') {
				console.info(sMsg);
			}
		}
	}
}