//<![CDATA[
// load handler
$(document).ready(function () {
	StyleSwitcher.init();
}); 

//------------------------Oglivy fix starts here -----------------------------

/**
 * Global Template superclass
 */
var Template =
{
	init: function () {
		this.renderStraplines();
		this.renderTitles();
	}
	,
	extend: function() {
		function F () {};
		F.prototype = this;
		return new F();
	}
	,
	renderStraplines: function() {
		
		if (StyleSwitcher.isJapanese() == true) return;
		if (StyleSwitcher.isCantanese() == true) return;
		if (typeof sIFR == "function")
		{
			if (StyleSwitcher.mode == 'hi') {
				if ($('#strapline1 h2').length > 0) {
					var strapline1 = named({
						sFlashSrc:	"https://secure.cmax.americanexpress.com/Internet/International/sharedDotCom/flash/AgendaLt.swf",
						sColor:		rgb2hex($('#strapline1 h2').css('color')),
						sBgColor:	rgb2hex($('#strapline1 h2').css('background-color')),
						sWmode:		"opaque",
						sFlashVars:	"textalign=center"
					});
					sIFR.replaceElement('#strapline1 h2', strapline1);
				}

				if ($('#strapline2 h2').length > 0)
				{
					var strapline2 = named({
						sFlashSrc:	"https://secure.cmax.americanexpress.com/Internet/International/sharedDotCom/flash/AgendaLt.swf",
						sColor:		rgb2hex($('#strapline2 h2').css('color')),
						sBgColor:	rgb2hex($('#strapline2 h2').css('background-color')),
						sWmode:	"opaque",
						sFlashVars:	"textalign=center"
					});
					sIFR.replaceElement('#strapline2 h2', strapline2);
				}

				//-----------------Start: AmexRED Specific Strapline ------------------
				if ($('#straplinered1 h2').length > 0) {
					var strapline1 = named({
						sFlashSrc:	"https://secure.cmax.americanexpress.com/Internet/International/sharedDotCom/flash/tradegothic.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/sharedDotCom/flash/tradegothic.swf",
						sColor:		rgb2hex($('#straplinered2 h2').css('color')),
						sBgColor:	rgb2hex($('#straplinered2 h2').css('background-color')),
						sFlashVars:	"textalign=center"
					});
					sIFR.replaceElement('#straplinered2 h2', strapline2);
				}
				//-----------------End: AmexRED Specific Strapline ------------------



			
			} else {
				var strapline = named({
					sFlashSrc:	"https://secure.cmax.americanexpress.com/Internet/International/sharedDotCom/flash/AgendaLt.swf",
					sColor:		"#FFFFFF",
					sBgColor:	"#000000",
					sWmode:		"opaque",
					sFlashVars:	"textalign=center"
				});
				sIFR.replaceElement('#strapline1 h2', strapline);			
				sIFR.replaceElement('#strapline2 h2', strapline);			
			}
		}
	}
	,
	renderTitles: function(selector, font, color) {
		
		if (StyleSwitcher.isJapanese() == true) return;
		if (StyleSwitcher.isCantanese() == true) return;
		if (typeof sIFR == "function")
		{
			// set defaults for arguments
			selector = (selector == undefined) ? '#content h3' : selector;
			font = (font == undefined) ? 'Agenda' : font;
			color = (color == undefined || color == null) ? color = "#5d8db1" : color;

			var moduleTitle = named({
				sFlashSrc:	'https://secure.cmax.americanexpress.com/Internet/International/sharedDotCom/flash/'+font+'.swf',
				sColor:		color,
				sWmode:		"transparent",
				sFlashVars:	""
			});
			sIFR.replaceElement(selector, moduleTitle);
		}
	}
}

//------------------------Oglivy fix Ends here -----------------------------



/**
 * 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();
		//alert(this.mode);
		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();
		
	}
	,
	// switch to broadband (default) styles
	enableBroadband: function () {
		this.highs.attr('disabled','');
		this.lows.attr('disabled','disabled');
		this.mode = 'hi';
		if (this.onBroadband) this.onBroadband();
	}
	,
	
	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;
	}
	,
	// check for Big5 charset and the omn_language.
	isCantanese: function(){
		var page_omn_language = '';
		if ( typeof omn_language != 'undefined' ) {
			page_omn_language = omn_language;
		}
		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('.*(big[5]{1}).*','ig');
				var test = rgx.test(content);
				if (test != false && page_omn_language != 'English') {
					return true;
				}
			}
		}
		return false;
	}

}

/**
 * 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 (/#[\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


//]]>
