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.');
			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;
		}
		if (!this.test(POP.Config.Syos.FlashVersion)) {
			// Deal with invalid flashplayer version (upgrade)
			this.onInvalidPlayerVersion();
			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);

		this.addViewButton('lnk_Best','best');
		this.addViewButton('lnk_Syos','syos');
		this.showView(POP.Config.Syos.DefaultView);
	},
	/**
	 * Most clients will want to fail silently. We could implement a notice here
	 * or auto-upgrade them via SWFObject.
	 */
	onInvalidPlayerVersion: function() {
		$$('.syos_instructions').each(function(item, i) {			item.hide();		});		this.showView('best');		this.debug("The Flash Player version requirement has not been met.");
		this.debug("--Installed: " + $H(deconcept.SWFObjectUtil.getPlayerVersion()).toJSON());
		this.debug("--Required: " + $H(new deconcept.PlayerVersion(POP.Config.Syos.FlashVersion.split("."))).toJSON());
	},
	/**
	 * 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]
	 * @return void
	 */
	showView: function(sWhich){
		if (sWhich == 'syos') {
			this.UI.Best.style.display = 'none';
			this.UI.Syos.style.display = 'block';
			this.write();
		} else if (sWhich == 'best') {
			this.UI.Syos.style.display = 'none';
			this.UI.Best.style.display = 'block';
		}
		// Update the syos state so postback returns us to the same view
		if (this.UI.SyosState)
		{
			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) {
		// Get the installed flashplayer version
		var detect = deconcept.SWFObjectUtil.getPlayerVersion();
		// Parse our requirements into a hash
		var required = new deconcept.PlayerVersion(sVersion.split("."));
		// Compare our required version
		return detect.versionIsValid(required);
	},
	/**
	 * Writes the SWF to the page
	 */
	write: function() {
		var config = POP.Config.Syos;
		var swfobj = new deconcept.SWFObject(
			config.ViewerSWF,
			'syos_embed',
			config.Width,
			config.Height,
			config.FlashVersion
		);
		swfobj.addVariable("xmlPath",config.XMLConfig);
		swfobj.addVariable("apiPath",config.APIPath);
		swfobj.addVariable("swfPath",config.SWFPath);
		swfobj.addVariable("imgPath",config.SeatPhotoPath);
		swfobj.addVariable("cartPath",config.CartPath);
		swfobj.addVariable("refreshURL",location.href);
		swfobj.addVariable("serverRoot", POP.Config.Root);
		swfobj.addVariable("perfNum", config.PerfNo);
		swfobj.addVariable("packNum", config.PackageNumber);
		swfobj.addVariable("imosNum", config.Mos);
		swfobj.write('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
	 * TODO Allow for any element to be passed, even without an ID
	 */
	addViewButton: function(sElementId, sView) {
		var App = this;
		Event.observe($(sElementId), 'click', function(e) {
			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);
			}
		}
	}
}