/**
##############################################################################################
#
#		Title: BBC Ouch - Accessibility Widget
#
#		Description: Behaviour for widget which allows customisable font size and colour schemes
#
##############################################################################################
#   $Source: $
#   $Author: $
#   $Revision: $
#   $Date: $ 
##############################################################################################
*/
// Array of instance initialisation functions to execute once AccessWidget initialisation is complete
if (typeof(InstanceInits) == "undefined") {
	InstanceInits = [];
}

AccessWidget = {
	/**
	 * Properties
	 */
	unhideAttempts: 20, // Number of times to attempt unhiding the content before aborting
	defaultTheme : "theme-default",
	fontSizes : ACP_FONT_SIZES, // font sizes are set access_widget_head include
	empSkin: null, // EMP Skin to use for current theme
	prevEmpSkin: null, // EMP Skin used by previous theme
	callbackFunctions: {
		changeTheme: [],
		changeFontSize: []
	},
	
	/**
	 * Function: init
	 *
	 * Description: Performs onLoad initialisation of accessibility widget
	 */
	init : function(){
		
		try {
			// Load cookie
			bbcjs.cookies.loadCookies();
			if (bbcjs.cookies.cookieData.ouchAccessPrefs) {
				AccessWidget.currentFontSize = bbcjs.cookies.cookieData.ouchAccessPrefs.s;
				AccessWidget.currentTheme = bbcjs.cookies.cookieData.ouchAccessPrefs.t;
			}else{
				AccessWidget.currentFontSize = "normal";
				AccessWidget.currentTheme = AccessWidget.defaultTheme;
			}						

			// Set current options
			AccessWidget.setFontSize(AccessWidget.currentFontSize);
			AccessWidget.setTheme();

			// Add common event listeners
			if($('access-widget')) {
				bbcjs.dom.addEventListener($('font-normal'), 'click', AccessWidget.handleChangeFontSize);
				bbcjs.dom.addEventListener($('font-bigger'), 'click', AccessWidget.handleChangeFontSize);
				bbcjs.dom.addEventListener($('font-biggest'), 'click', AccessWidget.handleChangeFontSize);
				bbcjs.dom.addEventListener($('theme-default'), 'click', AccessWidget.handleClickTheme);
				bbcjs.dom.addEventListener($('theme-blue'), 'click', AccessWidget.handleClickTheme);
				bbcjs.dom.addEventListener($('theme-hiviz'), 'click', AccessWidget.handleClickTheme);
				bbcjs.dom.addEventListener($('theme-soft'), 'click', AccessWidget.handleClickTheme);

				// Unhide the widget
				$('access-widget').style.display = 'block';
			}
			
			// Execute any instance initialisation functions
			for (var i=0; i<InstanceInits.length; i++){
				InstanceInits[i]();
			}

			// Unhide the page now the theme has been applied
			window.setTimeout("AccessWidget.showPage()", 100);
		} 
		catch (e) {
			// A JS error occurred so make sure we unhide the page
			AccessWidget.showPage();
		}
	},
	
	/**
	 * Function: handleChangeFontSize
	 *
	 * Description: Handles click of font size control
	 */
	handleChangeFontSize : function(e){
		var sizeName = e.target.id.split('-')[1];
		AccessWidget.setFontSize(sizeName);		
		bbcjs.dom.stopEvent(e);	// stop the event to prevent link to '#'
	},
	
	
	/**
	 * Function: handleClickTheme
	 *
	 * Description: Handles click of a theme control
	 */
	handleClickTheme : function(e){
		AccessWidget.currentTheme = e.target.id;
		AccessWidget.setTheme();		
		bbcjs.dom.stopEvent(e);	// stop the event to prevent link to '#'
	},
	

	/**
	 * Function: setTheme
	 *
	 * Description: Dynamically inserts a link tag into the page, 
	 * causing the currently specified theme stylesheet to be loaded
	 */
	setTheme : function(){
		$('current-theme').href = "/ouch/css/themes/" + AccessWidget.currentTheme + ".css";		
		if ($('current-messageboard-theme')) {
    	  	$('current-messageboard-theme').href = "/dnaimages/ouch/style/" + AccessWidget.currentTheme + ".css";	
		}
		var themeName = AccessWidget.currentTheme.split('-')[1];
				
		AccessWidget.prevEmpSkin = AccessWidget.empSkin;
		AccessWidget.empSkin = themeName == "hiviz" ? "black" : "silver";
		
		var cfns = AccessWidget.callbackFunctions["changeTheme"];
		if (cfns.length > 0){			
			for (var i=0; i<cfns.length; i++){
				cfns[i](themeName);	
			}
			
		}
		AccessWidget.saveCookie();
	},
	
	/**
	 * Function: setFontSize
	 *
	 * Description: Sets the relative font size of the document body
	 * 
	 * @param {String} size - Name of Font size to set
	 */
	setFontSize : function(sizeName){
		// make sure the size name exists in fontSizes otherwise set to default size (mainly required for cookies still holding the old percent sizes)
		if (AccessWidget.fontSizes[sizeName]) {
			AccessWidget.currentFontSize = sizeName;
		} else {
			sizeName = AccessWidget.currentFontSize = "normal";
		}
		var cfns = AccessWidget.callbackFunctions["changeFontSize"];
		if (cfns.length > 0){			
			for (var i=0; i<cfns.length; i++){
				cfns[i](sizeName);	
			}			
		}
		var size = AccessWidget.fontSizes[sizeName];
		if ($('blq-main'))
			$('blq-main').style.fontSize = size;
		if ($('popup'))
			$('popup').style.fontSize = size;
		AccessWidget.saveCookie();
	},
	
	/**
	 * Function: saveCookie
	 *
	 * Description: Saves cookie with current settings
	 */
	saveCookie : function(){
		bbcjs.cookies.setCookie('ouchAccessPrefs', {
				s: AccessWidget.currentFontSize,
				t: AccessWidget.currentTheme
			}, '+1y', '/ouch');
	},
	
	/**
	 * Function: showPage
	 *
	 * Description: Unhides the page
	 */
	showPage : function(){
		if ($('blq-main')){
			$('blq-main').style.display = 'block';	
		} else if (AccessWidget.unhideAttempts > 0){
			AccessWidget.unhideAttempts--;
			window.setTimeout("AccessWidget.showPage()", 100);
		}						
	},
	
	/**
	 * Function: registerChangeThemeCallback
	 *
	 * Description: Registers a function to call on theme change. This function is passed the name of the new theme.
	 * 
	 * @param {Function} fn - function to registers to callback when theme is changed
	 */
	registerChangeThemeCallback : function (fn){
		AccessWidget.callbackFunctions["changeTheme"].push(fn);
	},
	
	/**
	 * Function: registerChangeFontCallback
	 *
	 * Description: Registers a function to call on font size change. This function is passed the name of the new font size.
	 * 
	 * @param {Function} fn - function to registers to callback when font size is changed
	 */
	registerChangeFontCallback : function (fn){
		AccessWidget.callbackFunctions["changeFontSize"].push(fn);
	}
};

/**
 * Compile-time executed code
 */
try {
	bbcjs.addOnLoadItem(AccessWidget.init);
}
catch (e) {
	// A JS error occurred so make sure we unhide the page
	window.setTimeout("AccessWidget.showPage()", 100);
}
/**
  * END
  */