//<![CDATA[
// load handler
$(document).ready(function () {
	GlobalHeaders.init();
}); 

/*****************************************************************************
* THIS FILE HAS BEEN MODIFIED FOR USE ONLY WITH THE 2037903 GOLD CHARGE ACQUISITION LANDING PAGE
*
*     --= DO NOT USE WITH ANY OTHER AMEX TEMPLATES =--
* 
* Class to manage Global Headers
*
* Older versions:
* -  v1.0: Dylan Smith
* -  v2.0: Richard O'Brien, 03 Nov 2008: Upgraded to implement sIFR v3.0
*            (see also sIFR 3.0 by Mike Davidson, Shaun Inman, Tomas Jogin and 
*			  Mark Wubben <http://novemberborn.net/sifr3>)
* -  v2.1: Richard O'Brien, 09 Sep 2009: Modified for Gold Card Acquisition
*
*****************************************************************************/
var GlobalHeaders = 
{
	FONTS: [
		{ src:"flash/Agenda_3_0.swf" },
		{ src:"flash/AgendaLt_3_0.swf" }
	]
	,
	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 () {
		
		var loadedFonts = this.FONTS;
		
		if (typeof sIFR == "object")	// **used to be "function"... will this affect anything?
		{
			/* updated for sIFR 3 */
			if( !sIFR.isActive ) {
				// activate sIFR (required for v3)
				sIFR.activate(loadedFonts);
			}
			
			// define templates
			var h_national_menu_nav_css_on = [".sIFR-root { color:#025395; background-color:#dbe9f4; text-align:center; text-decoration:none; text-transform:uppercase; font-size:12px; font-weight:bold; }",
											  "a { color: #025395; text-decoration: none; }",
											  "a:link { color: #025395; }",
											  "a:hover { color: #025395; }"];
			var h_national_menu_nav_css_off = [".sIFR-root { color:#000000; background-color:#FFFFFF; text-align:center; text-decoration:none; text-transform:uppercase; font-size:12px; font-weight:bold; }",
											  "a { color: #000000; text-decoration: none; }",
											  "a:link { color: #000000; }",
											  "a:hover { color: #000000; }"];
			// init nav elements by state
			$('#h-national-menu-nav>li.on>div').each(
				function (index) {
					sIFR.replace(loadedFonts[0], {
						selector: '#' + $(this).attr('id'), 
						css: h_national_menu_nav_css_on,
						wmode: "transparent",
						fitExactly:true,
						ratios: [10, 1.32, 16, 1.27, 23, 1.23, 24, 1.21, 25, 1.22, 31, 1.21, 38, 1.2, 42, 1.19, 43, 1.2, 62, 1.19, 66, 1.18, 67, 1.19, 113, 1.18, 114, 1.17, 119, 1.18, 120, 1.17, 1.18],
						tuneWidth:5,
						offsetTop:3
					});
				}
			);
			$('#h-national-menu-nav>li:not(.on)>div').each(
				function (index) {
					sIFR.replace(loadedFonts[0], {
						selector: '#' + $(this).attr('id'), 
						css: h_national_menu_nav_css_off,
						wmode: "transparent", 
						fitExactly:true,
						ratios: [10, 1.32, 16, 1.27, 23, 1.23, 24, 1.21, 25, 1.22, 31, 1.21, 38, 1.2, 42, 1.19, 43, 1.2, 62, 1.19, 66, 1.18, 67, 1.19, 113, 1.18, 114, 1.17, 119, 1.18, 120, 1.17, 1.18],
						offsetTop:3
					});
				}
			);
			/* end updated for sIFR 3 */
		};
	}
}

/**
 * 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;
					
		/* updated for sIFR 3 */
		if (typeof sIFR == "object")
		{
			if ($('#strapline1 h1').length > 0) {
				var strapline1 = ".sIFR-root { color:" + rgb2hex($('#strapline1 h1').css('color')) + "; background-color:" + rgb2hex($('#strapline1 h1').css('background-color')) + "; text-align:center; }";
				sIFR.replace(GlobalHeaders.FONTS[0], {
					selector: '#strapline1 h1', 
					css: strapline1,
					wmode: "transparent",
					fitExactly:true,
					offsetTop:0
				});
			}
			
			if ($('#strapline2 h1').length > 0)
			{
				var strapline2 = ".sIFR-root { color:" + rgb2hex($('#strapline2 h1').css('color')) + "; background-color:" + rgb2hex($('#strapline2 h1').css('background-color')) + "; text-align:center;}";
				sIFR.replace(GlobalHeaders.FONTS[1], {
					selector: '#strapline2 h1', 
					css: strapline2,
					wmode: "transparent",
					fitExactly:true,
					offsetTop:0
				});
			}
			
			if ($('#straplinered1 h1').length > 0) {
				var straplinered1 = ".sIFR-root { color:" + rgb2hex($('#straplinered1 h1').css('color')) + "; background-color:" + rgb2hex($('#straplinered1 h1').css('background-color')) + "; text-align:center;}";
				sIFR.replace(GlobalHeaders.FONTS[0], {
					selector: '#straplinered1 h1', 
					css: straplinered1,
					wmode: "transparent",
					fitExactly:true,
					offsetTop:0
				});
			}
			
			if ($('#straplinered2 h1').length > 0) {
				var straplinered2 = ".sIFR-root { color:" + rgb2hex($('#straplinered2 h1').css('color')) + "; background-color:" + rgb2hex($('#straplinered2 h1').css('background-color')) + "; text-align:center;}";
				sIFR.replace(GlobalHeaders.FONTS[1], {
					selector: '#straplinered2 h1', 
					css: straplinered2,
					wmode: "transparent",
					fitExactly:true,
					offsetTop:0
				});
			}
		}
		/* end updated for sIFR 3 */
	}
	,
	renderTitles: function(cssSelector, font, color) {
		if (GlobalHeaders.isJapanese() == true) return;
		
		/* updated for sIFR 3 */
		if (typeof sIFR == "object")
		{
			// set defaults for arguments
			cssSelector = (cssSelector == undefined || cssSelector == null) ? '#content h3' : cssSelector;
			
			if( font == undefined || font == null)
				font = GlobalHeaders.FONTS[0];
			else {
				for(var i=0; i<GlobalHeaders.FONTS.length; i++) {
					if( GlobalHeaders.FONTS[i].src.toLowerCase().indexOf(font.toLowerCase()) != -1 ) {
						font = GlobalHeaders.FONTS[i];
						break;
					}
				}
			}
			
			color = (color == undefined || color == null) ? color = "#5d8db1" : color;
			
			var ieTuneHeight = -3;	//set default tuneHeight
			if ( $.browser.msie && $.browser.version =="6.0" )
				ieTuneHeight = -7;	//set ie6 specific tuneHeight to fix bug

			sIFR.replace(font, {
				selector: cssSelector, 
				css: ['.sIFR-root { color:' + color + '; }',
        				'a {color: #9f7742; text-decoration: none;}',
        				'a:hover {color: #9f7742; text-decoration: none;}'
					],
				wmode: "opaque",
				tuneHeight: ieTuneHeight
			});
		}
		/* end updated for sIFR 3 */
	}
	,
	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;
}
//]]>

