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;
		}
		
		// 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');

		if (!this.test(POP.Config.Syos.FlashVersion)) {
			// Deal with invalid flashplayer version (upgrade)
			this.onInvalidPlayerVersion();	
		}
		else{
			// Browser has adequate flash version installed
			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() {
		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 header = $('hdr_select_seating');
		var syosInstructions = $('syos_instructions');
		var bestInstructions = $('best_instructions');
		
		if (sWhich == 'syos') {
			this.UI.Syos.style.display = 'block';
			this.UI.Best.style.display = 'none';
			if (header) header.innerHTML = "Select Your Own Seats";
			if (syosInstructions) syosInstructions.style.display = 'block';
			if (bestInstructions) bestInstructions.style.display = 'none';
			this.UI.lnkBest.style.display = (bHideLink) ? 'none' : 'block';
			this.UI.lnkSyos.style.display = 'none';
			this.write();
		} else if (sWhich == 'best') {
			this.UI.Syos.style.display = 'none';
			this.UI.Best.style.display = 'block';
			if (header) header.innerHTML = "Reserve Tickets";
			if (syosInstructions) syosInstructions.style.display = 'none';
			if (bestInstructions) bestInstructions.style.display = 'block';
			this.UI.lnkSyos.style.display = (bHideLink) ? 'none' : 'block';
			this.UI.lnkBest.style.display = 'none';
		}
		
		// 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,
			"wmode": config.WMode
		};
		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,
			"refreshURL": config.refreshURL
			// "ga": config.ga // for GA tracking
		};
		// 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);
			}
		}
	}
}
