//<![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 () {
		if (typeof sIFR == "function")
		{
			// define templates
			var h_national_menu_nav_on = named({sFlashSrc:"https://secure.cmax.americanexpress.com/Internet/International/sharedSBSCanada2008/flash/Agenda.swf", sColor:"#025395", sLinkColor:"#025395", sBgColor:"#FFFFFF", sHoverColor:"#025395", nPaddingTop:0, nPaddingBottom:0, sWmode: "transparent", sFlashVars:"textalign=center&offsetTop=3"});
			var h_national_menu_nav_off = named({sFlashSrc:"https://secure.cmax.americanexpress.com/Internet/International/sharedSBSCanada2008/flash/Agenda.swf", sColor:"#000000", sLinkColor:"#000000", sBgColor:"#FFFFFF", sHoverColor:"#025395", nPaddingTop:0, nPaddingBottom:0, sWmode: "transparent", sFlashVars:"textalign=center&offsetTop=3"});			
		
			// init nav elements by state
			$('#h-national-menu-nav>li.on>div').each(
				function (index) {
					sIFR.replaceElement('#' + $(this).attr('id'), h_national_menu_nav_on);
				}
			);
			$('#h-national-menu-nav>li:not(.on)>div').each(
				function (index) {
					sIFR.replaceElement('#' + $(this).attr('id'), h_national_menu_nav_off);
				}
			);
		};
	}
}

/**
 * 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:	"https://secure.cmax.americanexpress.com/Internet/International/sharedSBSCanada2008/flash/AgendaLt.swf",
					sColor:		rgb2hex($('#strapline1 h2').css('color')),
					sBgColor:	rgb2hex($('#strapline1 h2').css('background-color')),
					sFlashVars:	"textalign=center"
				});
				sIFR.replaceElement('#strapline1 h2', strapline1);
			}
			
			if ($('#strapline2 h2').length > 0)
			{
				var strapline2 = named({
					sFlashSrc:	"https://secure.cmax.americanexpress.com/Internet/International/sharedSBSCanada2008/flash/AgendaLt.swf",
					sColor:		rgb2hex($('#strapline2 h2').css('color')),
					sBgColor:	rgb2hex($('#strapline2 h2').css('background-color')),
					sFlashVars:	"textalign=center"
				});
				sIFR.replaceElement('#strapline2 h2', strapline2);
			}
			
			if ($('#straplinered1 h2').length > 0) {
				var strapline1 = named({
					sFlashSrc:	"https://secure.cmax.americanexpress.com/Internet/International/sharedSBSCanada2008/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:	"https://secure.cmax.americanexpress.com/Internet/International/sharedSBSCanada2008/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:	'https://secure.cmax.americanexpress.com/Internet/International/sharedSBSCanada2008/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]');
		this.mode = Get_DC_Cookie();
		if (this.mode == 'low') { 
			this.enableNarrowband();	
		} else {
			// if no cookie available or cookie value == high
			this.enableBroadband();
		}
		
	}
	,
	// switch between style modes
	toggle: function () {
		if (this.mode == 'low') {
			Set_DC_Cookie('hi');
			//window.location.reload();

		} else {
			//this.enableNarrowband();
			Set_DC_Cookie('low');
		}
		window.location.reload();
		
	}
	,
	// switch to narrowband styles
	enableNarrowband: function () {
		this.highs.attr('disabled','disabled');
		this.lows.attr('disabled','');
		this.mode = 'low';
		document.getElementById("styleswitcher").innerHTML = document.getElementById("hiGraphicsLabel").value;
		document.getElementById("styleswitcher").title = document.getElementById("hiGraphicsLabel").value;
		
			
		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';
		
		
		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;
}

function Get_DC_Cookie() {
	
	var Bwidth = Get_Cookie('bandwidthdetect');
	var bandwidth ="High";
	if(Bwidth == null) {
		Bwidth = "High";
	}

	bandwidth = new String(Bwidth)
	if(equalsIgnoreCase(bandwidth,"High") || equalsIgnoreCase(bandwidth,"Vhigh"))
	{ 
		//alert('Strings Match'); 
		return 'hi';
	}else if(equalsIgnoreCase(bandwidth,"Low") || equalsIgnoreCase(bandwidth,"Med"))
	{ 
		//alert('No Match');
		return 'low';
	} else {
		return 'hi';
	}

}

function Set_DC_Cookie( cok_value ) {
	
	
	if(cok_value == 'hi')
	{
		Set_Cookie( 'bandwidthdetect', 'Vhigh', '360', '/', '.americanexpress.com', '' );
		
	}
	if(cok_value == 'low')
	{
		Set_Cookie( 'bandwidthdetect', 'Low', '360', '/', '.americanexpress.com', '' );
		
	}

}
// To use, simple do: Get_Cookie('cookie_name'); 
// replace cookie_name with the real cookie name, '' are required
function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	
	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );
		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
		
		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found ) 
	{
		return null;
	}
}


/*
only the first 2 parameters are required, the cookie name, the cookie
value. Cookie time is in milliseconds, so the below expires will make the 
number you pass in the Set_Cookie function call the number of days the cookie
lasts, if you want it to be hours or minutes, just get rid of 24 and 60.

Generally you don't need to worry about domain, path or secure for most applications
so unless you need that, leave those parameters blank in the function call.
*/
function Set_Cookie( name, value, expires, path, domain, secure ) {
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	// if the expires variable is set, make the correct expires time, the
	// current script below will set it for x number of days, to make it
	// for hours, delete * 24, for minutes, delete * 60 * 24
	if ( expires )
	{
		expires = expires * 1000 * 60 * 60 * 24;
	}
	//alert( 'today ' + today.toGMTString() );// this is for testing purpose only
	var expires_date = new Date( today.getTime() + (expires) );
	//alert('expires ' + expires_date.toGMTString());// this is for testing purposes only

	document.cookie = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + //expires.toGMTString()
		( ( path ) ? ";path=" + path : "" ) + 
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}

//The first line assigns the MatchIgnoreCase function as an equalsIgnoreCase method of the String object

function equalsIgnoreCase(string1, string2)
{
	var str1 = string1;
	var str2 = string2;
	if(str1.toLowerCase()==str2.toLowerCase()){
		return true;
	}else {
		return false;
	}
} //End Function



//]]>
