﻿// if JavaScript is enabled, hide the 'no-script' list of links, and show the ajax-driven menu
// these two lines MUST be below the elements they reference in the DOM (towards the bottom of the page)
$(".no-script").hide();
$(".sections").show();

// namespace
if (typeof HAT == "undefined" || !HAT) {
	var HAT = {};
}

HAT.Languages = {
	lang: [
		{
			formfreecode:"ara",
			textdirection:"rtl",
			culture:"ar_AE",
			countryval:"/arabic/index.htm",
			countrylang:["Algeria - Arabic", "Bahrain - Arabic", "Egypt - Arabic", "Iraq - Arabic", "Jordan - Arabic", "Kuwait - Arabic", "Lebanon - Arabic", "Morocco - Arabic", "Mauritania - Arabic", "Oman - Arabic", "Qatar - Arabic", "Saudi Arabia - Arabic", "Tunisia - Arabic", "UAE - Arabic", "Yemen - Arabic"],
			fonttype:"HAT-font2"
		},
		{
			formfreecode:"ara",
			textdirection:"rtl",
			culture:"ar_AR",
			countryval:"",
			countrylang:[],
			fonttype:"HAT-font2"
		},
		{
			formfreecode:"aze",
			textdirection:"ltr",
			culture:"az_AZ",
			countryval:"",
			countrylang:[],
			fonttype:"HAT-font6"
		},
		{
			formfreecode:"ces",
			textdirection:"ltr",
			culture:"cs_CZ",
			countryval:"/cd/corporate/europe/emea/ces/index.htm",
			countrylang:["Czech Republic - Czech"],
			fonttype:"HAT-font1"
		},
		{
			formfreecode:"deu",
			textdirection:"ltr",
			culture:"de_DE",
			countryval:"GlobalGateway_Germany_German",
			countrylang:["Austria - German", "Europe - German", "Germany - German", "Liechtenstein - German", "Switzerland - German"],
			fonttype:"HAT-font1"
		},
		{
			formfreecode:"eng",
			textdirection:"ltr",
			culture:"en_AP",
			countryval:"GlobalGateway_APAC_English",
			countrylang:["Asia Pacific - English", "Hong Kong - English", "Indonesia - English", "Malaysia - English", "New Zealand - English", "Pakistan - English", "Philippines - English", "Singapore - English", "Vietnam - English"],
			fonttype:"HAT-font1"
		},
		{
			formfreecode:"eng",
			textdirection:"ltr",
			culture:"en_AU",
			countryval:"GlobalGateway_Australia_English",
			countrylang:["Australia - English"],
			fonttype:"HAT-font1"
		},
		{
			formfreecode:"eng",
			textdirection:"ltr",
			culture:"en_IN",
			countryval:"GlobalGateway_India_English",
			countrylang:["India - English"],
			fonttype:"HAT-font1"
		},
		{
			formfreecode:"sin",
			textdirection:"ltr",
			culture:"en_SG",
			countryval:"",
			countrylang:[],
			fonttype:"HAT-font1"
		},
		{
			formfreecode:"eng",
			textdirection:"ltr",
			culture:"en_UK",
			countryval:"GlobalGateway_UnitedKingdom_English",
			countrylang:["Africa - English", "Belgium - English", "Czech Republic - English", "Denmark - English", "Estonia - English", "Europe - English", "Finland - English", "Gambia - English", "Greece - English", "Hungary - English", "Iceland - English", "Ireland - English", "Kenya - English", "Latvia - English", "Lithuania - English", "Luxembourg - English", "Nigeria - English", "Norway - English", "Romania - English", "Rwanda - English", "Tanzania - English", "United Kingdom - English", "Zimbabwe - English"],
			fonttype:"HAT-font1"
		},
		{
			formfreecode:"eng",
			textdirection:"ltr",
			culture:"en_US",
			countryval:"GlobalGateway_UnitedStates_English",
			countrylang:["United States - English", "Canada - English"],
			fonttype:"HAT-font1"
		},
		{
			formfreecode:"spa",
			textdirection:"ltr",
			culture:"es_ES",
			countryval:"GlobalGateway_Spain_Spanish",
			countrylang:["Spain - Spanish"],
			fonttype:"HAT-font1"
		},
		{
			formfreecode:"esla",
			textdirection:"ltr",
			culture:"es_LA",
			countryval:"GlobalGateway_LatinAmerica_Spanish",
			countrylang:["Argentina - Spanish", "Chile - Spanish", "Colombia - Spanish", "Costa Rica - Spanish", "Latin America - Spanish", "Mexico - Spanish", "Venezuela - Spanish"],
			fonttype:"HAT-font1"
		},
		{
			formfreecode:"espanol",
			textdirection:"ltr",
			culture:"es_MX",
			countryval:"",
			countrylang:[],
			fonttype:"HAT-font1"
		},
		{
			formfreecode:"fra",
			textdirection:"ltr",
			culture:"fr_FR",
			countryval:"GlobalGateway_France_French",
			countrylang:["Canada - French", "France - French", "Belgium – French"],
			fonttype:"HAT-font1"
		},
		{
			formfreecode:"heb",
			textdirection:"rtl",
			culture:"he_IL",
			countryval:"/cd/corporate/europe/emea/heb/287256.htm",
			countrylang:["Israel - Hebrew"],
			fonttype:"HAT-font1"
		},
		{
			formfreecode:"hun",
			textdirection:"ltr",
			culture:"hu_HU",
			countryval:"/cd/corporate/europe/emea/hun/index.htm",
			countrylang:["Hungary - Hungarian"],
			fonttype:"HAT-font1"
		},
		{
			formfreecode:"bah",
			textdirection:"ltr",
			culture:"id_ID",
			countryval:"",
			countrylang:[],
			fonttype:"HAT-font1"
		},
		{
			formfreecode:"ita",
			textdirection:"ltr",
			culture:"it_IT",
			countryval:"GlobalGateway_Italy_Italian",
			countrylang:["Italy - Italian"],
			fonttype:"HAT-font1"
		},
		{
			formfreecode:"jpn",
			textdirection:"ltr",
			culture:"ja_JP",
			countryval:"GlobalGateway_Japan_Japanese",
			countrylang:["Japan - Japanese"],
			fonttype:"HAT-font1"
		},
		{
			formfreecode:"kor",
			textdirection:"ltr",
			culture:"ko_KR",
			countryval:"GlobalGateway_Korea_Korean",
			countrylang:["Korea - Korean"],
			fonttype:"HAT-font5"
		},
		{
			formfreecode:"nla",
			textdirection:"ltr",
			culture:"nl_NL",
			countryval:"GlobalGateway_Netherlands_Dutch",
			countrylang:["Netherlands - Dutch", "Belgium – Netherlands"],
			fonttype:"HAT-font1"
		},
		{
			formfreecode:"pol",
			textdirection:"ltr",
			culture:"pl_PL",
			countryval:"GlobalGateway_Poland_Polish",
			countrylang:["Poland - Polish"],
			fonttype:"HAT-font1"
		},
		{
			formfreecode:"portugues",
			textdirection:"ltr",
			culture:"pt_BR",
			countryval:"GlobalGateway_Brazil_Portuguese",
			countrylang:["Brazil - Portuguese"],
			fonttype:"HAT-font1"
		},
		{
			formfreecode:"rom",
			textdirection:"ltr",
			culture:"ro_RO",
			countryval:"",
			countrylang:[],
			fonttype:"HAT-font1"
		},
		{
			formfreecode:"rus",
			textdirection:"ltr",
			culture:"ru_RU",
			countryval:"GlobalGateway_Russia_Russian",
			countrylang:["Russia - Russian"],
			fonttype:"HAT-font4"
		},
		{
			formfreecode:"swe",
			textdirection:"ltr",
			culture:"sv_SE",
			countryval:"GlobalGateway_Sweden_Swedish",
			countrylang:["Sweden - Swedish"],
			fonttype:"HAT-font1"
		},
		{
			formfreecode:"tur",
			textdirection:"ltr",
			culture:"tr_TR",
			countryval:"GlobalGateway_Turkey_Turkish",
			countrylang:["Turkey - Turkish"],
			fonttype:"HAT-font1"
		},
		{
			formfreecode:"tha",
			textdirection:"ltr",
			culture:"th_TH",
			countryval:"GlobalGateway_Thailand_Thai",
			countrylang:["Thailand - Thai"],
			fonttype:"GG-Thai-font"
		},
		{
			formfreecode:"vie",
			textdirection:"ltr",
			culture:"vi_VN",
			countryval:"",
			countrylang:[],
			fonttype:"HAT-font1"
		},
		{
			formfreecode:"zho",
			textdirection:"ltr",
			culture:"zh_CN",
			countryval:"GlobalGateway_China_SimplifiedChinese",
			countrylang:["China - Simplified Chinese"],
			fonttype:"HAT-font3"
		},
		{
			formfreecode:"ukr",
			textdirection:"ltr",
			culture:"uk_UA",
			countryval:"/cd/corporate/europe/emea/rus/ukraine/index.htm",
			countrylang:["Ukraine - Ukrainian"],
			fonttype:"HAT-font1"
		},
		{
			formfreecode:"zht",
			textdirection:"ltr",
			culture:"zh_TW",
			countryval:"GlobalGateway_Taiwan_TraditionalChinese",
			countrylang:["Taiwan - Traditional Chinese"],
			//fonttype:"HAT-font3"
			fonttype:"GG-Taiwan-font"
		}
	]
}

HAT.Countries = [
	{
		deeplink: 'en_US_01',
		name: 'United States - English',
		menu: '/homepage/xml/GlobalGateway_UnitedStates_English.xml'
	},
	{
		deeplink: 'en_CA_01',
		name: 'Canada - English',
		menu: '/homepage/xml/GlobalGateway_UnitedStates_English.xml'
	},
	{
		deeplink: 'fr_CA_01',
		name: 'Canada - French',
		localizedName: 'Canada - Français',
		menu: '/homepage/xml/GlobalGateway_France_French.xml'
	}
];


// section hover behavior
$(document).ready(function() {
	var s = "fast";
	// fixes some occasional visual glitches when user moves mouse over/out of the section
	function resetHeight() { $(this).height('auto'); }
	$(".section").hover(function() {
		$(this)
			.find('h2').stop().slideUp(s, resetHeight).end()
			.find('.contents').stop().slideDown(s, resetHeight);
	}, function() {
		$(this)
			.find('h2').stop().slideDown(s, resetHeight).end()
			.find('.contents').stop().slideUp(s, resetHeight);
	});
});

var default_country_name;
var default_country;
var deeplink = location.hash.substr(2); // flash-style deeplinking: "#/country_code"
if (!deeplink) {
	default_country_name = param("country") || cookie("countrylang"); // passed-in param or cookie
}

var redirect = (!param("country") && !deeplink); // don't (initially) redirect if a country has been passed in
var initialTracking = false;
var omniture_page_name;

$.each(HAT.Countries, function(i, country) {
	// for convenience, set up a direct link the the appropriate item in HAT.Languages.lang
	$.each(HAT.Languages.lang, function(i, language) {
		if ($.inArray(country.name, language.countrylang) >= 0) {
			country.language = language;
			return false;
		}
	});
	
	// set up the default country value and an analytics tracking name
	if (deeplink == country.deeplink || default_country_name == country.name) {
		default_country = country;
		// set omniture page name, but NOT for the default (index 0, en_US) selection
		if (i && country.language) {
			omniture_page_name = country.language.countryval.replace(/_/g, ":");
		}
	}
});

$(document).ready(function() {
	// parse the country list into a select box when loaded
	$.each(HAT.Countries, function(i, n) {
		$("<option value='" + n.name + "'>")
			.html(n.localizedName || n.name)
			.attr("selected", (n == default_country ? "selected" : ""))
			.appendTo("#country-selector");
	});
	
	// show the country selector, and add a change handler
	$("#country-selector").show().change(function(e) {
		var country = HAT.Countries[this.selectedIndex];
		
		//document.cookie = "countrylang=" + country.name + ";path=/;max-age=" + (60*60*24*365);
		//IE6 Cookie Fix 2/1/2009
		var nextyear = new Date();
        nextyear.setFullYear(nextyear.getFullYear() + 1);
        var cookie_value = "countrylang=" + encodeURIComponent(country.name) + "; path=/; expires=" + nextyear.toGMTString();
        document.cookie = cookie_value;

		
		if (country.link && redirect) {
			location.href = country.link;
		} else if (country.menu && country.language) {
			location.hash = "/" + country.deeplink;
			var header = "sites/sitewide/HAT/30recode/"+ country.language.culture + "/json/global_header_gateway.js";
			var footer = "sites/sitewide/HAT/30recode/" + country.language.culture + "/" + country.language.textdirection + "_Standards_W1024_HTTP_UTF8/footer.inc";
			var languageClasses = "HAT-wide " + country.language.fonttype + (country.language.textdirection == "rtl" ? " HAT-rtl" : "");
			var styleLink = "/sites/sitewide/HAT/30recode/css/" + country.language.textdirection + "_header.css";
			
			$.get(country.menu, parseCountryXML, "xml");
			$.getJSON(header, parseHeaderJSON);
			$("#footerdiv").load(footer);
			
			$("#HAT-linkStyle").attr('href', styleLink);
			$("#HAT-globalheader, #ggw").attr('class', languageClasses);
			$("#ggw").css('background-image', (country.image ? 'url(' + country.image + ')' : ''));
			
			// track change after the language updates for better user experience
			// but only after the initial page load
			if (initialTracking) {
				var omniture_page_name = country.language.countryval.replace(/_/g, ":").toLowerCase();
				waTrackAsLink("global gateway selection: " + country.name, 'o', 'wa_prop14=cim:' + omniture_page_name);
			}
		}
	});
	
	// load the selected language if it is not the first (0th)
	// this assumes that the default language is already set up
	if ($("#country-selector").get(0).selectedIndex) {
		$("#country-selector").trigger('change');
	}
	
	redirect = true; // after the initial language is set up, re-enable redirects
	initialTracking = true;
	
	function parseCountryXML(xml) {
		var $xml = $(xml);
		function update(selector, textnode, linknode) {
			$(selector).html($.trim($xml.find(textnode).text()) || "&nbsp;"); // insert a hard space even if item is empty, to preserve HTML layout
			if (linknode) { $(selector).attr('href', $xml.find(linknode).text()); }
		}
		update(".choose-location h2", "headercopy");
		update(".section.work h1 a", "worktitle", "worklink");
		update(".section.play h1 a", "playtitle", "playlink");
		update(".section.about h1 a", "abouttitle", "aboutlink");
		update(".section.work h2", "worksubtitle");
		update(".section.play h2", "playsubtitle");
		update(".section.about h2", "aboutsubtitle");
		update(".section.work .contents", "workbody");
		update(".section.play .contents", "playbody");
		update(".section.about .contents", "aboutbody");
	}
	
	function parseHeaderJSON(json) {
		$("#logo_link").attr('href', json.header.logo.href + "?iid=" + json.header.logo.iid);
		$("#logo_img")
			.attr('alt', json.header.logo.value)
			.attr('title', json.header.logo.value);
		$("#lbl_search").html(json.header.search.button.value);
		$("#culture").val(json.header.search.culture);
		$("#searchsubmit")
			.attr('alt', json.header.search.button.value)
			.attr('title', json.header.search.button.value)
			.attr('src', "/sites/sitewide/HAT/30recode/pix/searchbtn_" + json.header.search.button.language + ".gif");
	
		HAT.SearchFilterLUT.head = json.header.search.filter.filterhead;
		HAT.SearchFilterLUT.qs = $.map(json.header.search.filter.term, function(n) {
			return { filter: n.query, term: n.value };
		});

		$("#HAT-filter-menu").remove();
		setTimeout(function() {
			$("#HAT-filter-menu").remove();
			HAT.FilterMenu.BuildFilter();
			HAT.FilterMenu.AttachEvents();
		}, 50);

		$("#supportbtn_link").attr('href',json.header.support.link.href + "?iid=" + json.header.support.link.ggiid);
		$("#supportbtn").text(json.header.support.link.value);
		
	}
	function parseSupportJSON(sjson) {
		var lang = cookie("culture");
		$("#supportbtn_link").attr('href',sjson.support[lang].support.href);
		$("#supportbtn").text(sjson.support[lang].support.button.value);
	}
});

function cookie(key) {
	//return parseParams(document.cookie, /; ?/)[key];
	//IE6 Cookie Fix 2/1/2009
	return decodeURIComponent(parseParams(document.cookie, /; ?/)[key]);
}

function param(key) {
	var params = parseParams(location.search.substr(1), "&");
	return params[key] ? unescape(params[key]) : null;
}

// can be used for cookies or URL search strings
function parseParams(str, delimiter) {
	var x = {};
	$.each(str.split(delimiter), function(i, n) {
		var keyVal = n.split(/=/);
		if (keyVal[0] && keyVal[1]) { x[keyVal[0]] = keyVal[1]; }
	});
	return x;
}

// utility function to generate the 'no script' list of country links
function createNoscriptList() {
	var $div = $(".no-script").empty().show();
	var $list = $("<ul>").appendTo($div);
	$.each(HAT.Countries, function(i, n) {
		var value = n.link;
		if (n.menu) {
			$.ajax({ async: false, url: n.menu,
				success: function(countryXML) { value = $(countryXML).find("worklink").text(); }
			});
		}
		$list.append("<li><a href='" + value + "'>" + (n.localizedName || n.name) + "<\/a><\/li>");
		if ((i+1) % 16 == 0) {
			if (i+1 == HAT.Countries.length) {
				$list.css("text-align", "right");
				$div.append("<div class='clear'>");
			} else {
				$list = $("<ul>").appendTo($div);
			}
		}
	});
}
