/**
##############################################################################################
#
#		Title: Flash switcher
#
#		Description: Behaviour for an alternative way of dealing with flash/html versions for RaW
#
##############################################################################################
*/

var FlashSwitcher = {
	basicVersion: 0, // set default value

	retryTimeout: 1000, // Time in ms to retry initialising if page is not yet rendered. 
	retryAttempts: 3, // Number of times to retry initialisation

	/* get user preference from Prefs Store */
	init: function() {
		var CurrentPrefs = PrefsStore.getPrefs();
		if (CurrentPrefs) {
			if ((typeof(CurrentPrefs.basicVersion) != 'undefined') && (CurrentPrefs.basicVersion != this.basicVersion)) {
				/* and switch between versions if the user preference is different from the default */
				this.basicVersion = CurrentPrefs.basicVersion;
			}
			if (this.basicVersion == 1) {
				bbcjs.dom.addClassName(document.documentElement,'basic-version'); // display HTML alternative and hide Flash content
			}
		}
	},
	initButtons: function() {
		//This is a workaround for an IE race condition where FlashSwitcher.initButtons is sometimes executed before the DOM has finished loading	
		if (!$('btm-flashswitch')){
			if (FlashSwitcher.retryAttempts > 0){
				FlashSwitcher.retryAttempts--;
				window.setTimeout("FlashSwitcher.initButtons()", FlashSwitcher.retryTimeout);					
			}
			return;
		}

		/* add event listener to button to intercept mouse click */
		bbcjs.dom.addEventListener($('btm-flashswitch'), 'click', FlashSwitcher.handleFlashSwitchClick);
	},
	/* handles the mouse click */
	handleFlashSwitchClick: function(e) {
		bbcjs.dom.stopEvent(e);	// stop the event to prevent link to '#'
		FlashSwitcher.switchFlash();
		FlashSwitcher.updateButtons();
	},
	/* switch from Flash to HTML version and vice versa */
	switchFlash: function() {
		if (this.basicVersion == 1) {
			this.setPref(0); // set the user preference to Flash ( hide flash = false )
			FlashEmbed.embedFlash();
			bbcjs.dom.removeClassName(document.documentElement,'basic-version'); // display Flash content and hide HTML alternative
		} else {
			this.setPref(1); // set the user preference to HTML ( hide flash = true )
			bbcjs.dom.addClassName(document.documentElement,'basic-version'); // display HTML alternative and hide Flash content
			if ($('flash-content') && !$('noflash-message')) {
				bbcjs.dom.empty($('flash-content'));
			}
		}
	},
	/* update button labels and styling for both the Flash/HTML and print preview buttons */
	updateButtons: function() {
		if (this.basicVersion == 1) {
			$('btm-flashswitch').innerHTML = 'Full version';
			bbcjs.dom.addClassName($('btm-flashswitch'),'left');
			$('btm-printpreview').style.display = 'block';
		} else {
			$('btm-flashswitch').innerHTML = 'Basic version';
			bbcjs.dom.removeClassName($('btm-flashswitch'),'left');
			$('btm-printpreview').style.display = 'none';
		}
	},
	/* set the Flash preference in Prefs Store */
	setPref: function(pref) {
		this.basicVersion = pref;
		var StorePrefs = {
			basicVersion: FlashSwitcher.basicVersion
		}
		PrefsStore.mergePrefs(StorePrefs);
	}
}


/**
 * Compile-time executed code
 */

FlashSwitcher.init();
bbcjs.addOnLoadItem(FlashSwitcher.initButtons);

/**
  * END
  */