//<![CDATA[
// load handler
$(document).ready(function () {
	GlobalHeaders.init();
}); 

/**
 * Class to manage the global headers
 */
var GlobalHeaders = 
{
	init: function () {
		/**
		 * This sets an 'off' class on the element preceded by the active nav
		 * element, which will have an 'on' class. This is to cause CSS to remove
		 * the unnecessary vertical divider line
		 **/
		$('#h-global-channels li.on + li').addClass('off');
		 
		/**
		 * Check language for Japanese
		 **/
		if (this.isJapanese() == false) {
			this.doSIFR();
		}
		 
		// enable styleswitcher
		StyleSwitcher.init();
	}
	,
	isJapanese: function() {
		var metas = $('head meta');
		for (var i=0; i < metas.length; i++) {
			var content = metas[i].getAttribute('content');
			if (content != undefined && content.length > 0) {
				var rgx = new RegExp('.*(shift[-_]{1}jis).*','ig');
				var test = rgx.test(content);
				if (test != false) {
					return true;
				}
			}
		}
		return false;
	}
	,
	doSIFR: function () {
		
	}
}

/**
 * Global Template superclass
 */
var Template =
{
	init: function () {
		this.autoInit();
		this.renderStraplines();
		this.renderTitles();
	}
	,
	// this will always run, irrespective of subclassing
	autoInit: function() {
		var that = this;
		StyleSwitcher.onNarrowband = function() {
			that.fixIELowGraphics();
		}
	}
	,
	extend: function() {
		function F () {};
		F.prototype = this;
		var O = new F();
		O.autoInit();
		return O;
	}
	,
	renderStraplines: function() {
		if (GlobalHeaders.isJapanese() == true) return;
		if (typeof sIFR == "function")
		{
			if ($('#strapline1 h2').length > 0) {
				var strapline1 = named({
					sFlashSrc:	"flash/AgendaLt.swf",
					sColor:		rgb2hex($('#strapline1 h2').css('color')),
					sBgColor:	rgb2hex($('#strapline1 h2').css('background-color')),
					sFlashVars:	"textalign=left"
				});
				sIFR.replaceElement('#strapline1 h2', strapline1);
			}
			
			if ($('#strapline2 h2').length > 0)
			{
				var strapline2 = named({
					sFlashSrc:	"flash/AgendaLt.swf",
					sColor:		rgb2hex($('#strapline2 h2').css('color')),
					sBgColor:	rgb2hex($('#strapline2 h2').css('background-color')),
					sFlashVars:	"textalign=left"
				});
				sIFR.replaceElement('#strapline2 h2', strapline2);
			}
			
			if ($('#straplinered1 h2').length > 0) {
				var strapline1 = named({
					sFlashSrc:	"flash/AgendaLt.swf",
					sColor:		rgb2hex($('#straplinered1 h2').css('color')),
					sBgColor:	rgb2hex($('#straplinered1 h2').css('background-color')),
					sFlashVars:	"textalign=center"
				});
				sIFR.replaceElement('#straplinered1 h2', strapline1);
			}
			
			if ($('#straplinered2 h2').length > 0) {
				var strapline2 = named({
					sFlashSrc:	"flash/AgendaLt.swf",
					sColor:		rgb2hex($('#straplinered2 h2').css('color')),
					sBgColor:	rgb2hex($('#straplinered2 h2').css('background-color')),
					sFlashVars:	"textalign=center"
				});
				sIFR.replaceElement('#straplinered2 h2', strapline2);
			}
						
		}
	}
	,
	renderTitles: function(selector, font, color) {
		if (GlobalHeaders.isJapanese() == true) return;
		if (typeof sIFR == "function")
		{
			// set defaults for arguments
			selector = (selector == undefined || selector == null) ? '#content h3' : selector;
			font = (font == undefined || font == null) ? 'Agenda' : font;
			color = (color == undefined || color == null) ? color = "#5d8db1" : color;
		
			var moduleTitle = named({
				sFlashSrc:	'flash/'+font+'.swf',
				sColor:		color,
				sWmode:		"transparent",
				sFlashVars:	""
			});
			sIFR.replaceElement(selector, moduleTitle);
		}
	}
	,
	fixIELowGraphics: function() {
		$('.sIFR-replaced').removeClass('sIFR-replaced').addClass('sIFR-replaced-lg');
	}
}

/**
 * Class to handle stylesheet switching
 **/
var StyleSwitcher = 
{
	mode: 'hi',
	element: null,
	lows: null,
	highs: null,
	onNarrowband: null,
	onBroadband: null
	,
	init: function () {
		this.element = $('a#styleswitcher');
		this.element.attr('href', 'javascript:void(0)'); 
		this.element.bind('click',	function() {StyleSwitcher.toggle()});
		this.lows = $('link[@title=low]');
		this.highs = $('link[@title=high]');
		
		// enable high by default
		this.enableBroadband();
	}
	,
	// switch between style modes
	toggle: function () {
		if (this.mode == 'hi') {
			this.enableNarrowband();
		} else {
			this.enableBroadband();
		}
	}
	,
	// switch to narrowband styles
	enableNarrowband: function () {
		this.highs.attr('disabled','disabled');
		this.lows.attr('disabled','');
		this.mode = 'low';
		this.element.text('High graphics');
		if (this.onNarrowband) {
			//this.onNarrowband();
			// delay this callback by 1ms to allow IE6 to "catch up"
			var f = this.onNarrowband;
			var interval = setInterval(function() {
				f();
				clearInterval(interval);
			}, 1);
		}
	}
	,
	// switch to broadband (default) styles
	enableBroadband: function () {
		this.highs.attr('disabled','');
		this.lows.attr('disabled','disabled');
		this.mode = 'hi';
		this.element.text('Low graphics');
		if (this.onBroadband) {
			// delay this callback by 1ms to allow IE6 to "catch up"
			var f = this.onBroadband;
			var interval = setInterval(function() {
				f();
				clearInterval(interval);
			}, 1);
		}
	}
}

/**
 * Printing Helper class
 **/
var PrintHelper = 
{
	print: function () {
		window.print();
	}
}


/*===========[ Utility methods ]================================================================*/
/**
 * Function: rgb2hex
 * Used to convert Firefox's computed css colour values from rgb to standard hex
 * e.g. rgb(255, 255, 255) = #FFFFFF;
 **/
function rgb2hex(input) {
	if (input == undefined) return '';
	if (/#[\dabcdef]{3,6}/i.test(input) == true) return input;
	var rgb = input.substring(4,input.length-1).split(',');
	if (rgb.length !== 3) return false;
	var output = '#' + dec2hex(rgb[0]) +  dec2hex(rgb[1]) +  dec2hex(rgb[2]);
	return output;
}
/**
 * Function: dec2hex
 * Converts any decimal number to it's hexidecimal equivalent
 **/
function dec2hex(dec) {
	var hexChars = "0123456789ABCDEF";
	dec = parseInt(dec);
	var a = dec % 16;
	var b = (dec - a) / 16;
	var hex = "" + hexChars.charAt(b) + hexChars.charAt(a);
	return hex;
}
//]]>
