jQuery.noConflict();
jQuery(document).ready(function(){
								
//	get locale from akamai header cookie xglsgg - contains country code 
    	var cntryVar = 'US';
		var xglscookievalue =  GetCookie('xglsgg');
		if (xglscookievalue != null) {
			cntryVar = xglscookievalue;  // cntry code is the first variable passed in 
		}
		var localeVar = langVar + "_" +  cntryVar;
		
// check to see if the default locale pref cookie has been set for this visitor 
// if default cookie is set - use this as the locale and also set a omniture flag to true 
		var defaultcookievalue =  GetCookie('hasggrd_defaultLocale');
		if (defaultcookievalue != null) {
			// if set, use this locale instead of what was detected 
			localeVar = defaultcookievalue;
			if (calledfromVar != 'worldwide')
				s.prop43 = "true";
		  }	
		else
		{
			if (calledfromVar != 'worldwide')
				s.prop43 = "false";
		}

		var localeLen = localeVar.length;     

// 	pull in sitedata.json file		
		var sitedataURL = "/includes/sitedata/sitedata.json";
		var nbrReg2 = 0;
		jQuery.ajax({
				url: sitedataURL,
				type: "get",
				dataType: "json",	
				success: function(data, textStatus) {
							//loop through site data to find site 
							//build locales for page 
							//if locale is found in the list - set it to be highlighted 
							if (data != null) 
							{
								   var localetohighlight = ''; 
								   var us_locale = '';
								   var siteData=data.sites; 
								   for (var i=0; i<siteData.length; i++){
									if (siteData[i].guid == siteGuidVar)
										{
										   var loc = siteData[i].locales; 
										   for (var x=0; x< loc.length; x++){
													
													if (loc[x].country == 'United States')
															{
																us_locale = loc[x].locale;
															}	

													if (loc[x].locale == localeVar)
													{
														if (defaultcookievalue != null && calledfromVar != 'worldwide')
														{
														// locale exists for this site and locale is default pref 
														// need to write cookie for this site/locale
														// and redirect to that url 

															var sitecookievalue = siteCookieVar; 
															SetCookie(sitecookievalue, loc[x].url, expiry);
															localefound = true;
															window.location=loc[x].url;
															return; 
														}
														localetohighlight = loc[x].locale;
													}	
													else if (localetohighlight == '' && loc[x].county_code == cntryVar)
														{
															localetohighlight = loc[x].locale;
														}	
													else if (loc[x].country == 'United States')
														{
															us_locale = loc[x].locale;
														}	
													
										   		if (loc[x].region_order == 2)  /// if this is Europe, count the european locales and 
												{							   /// if more than 21 start a second column 		   																		
														nbrReg2 += 1;
														if (nbrReg2 > 21)
															var ulregion = "ul#li_cont" + loc[x].region_order + "b"; 
														else
															var ulregion = "ul#li_cont" + loc[x].region_order; 
														
												}
												else
												{
													var ulregion = "ul#li_cont" + loc[x].region_order; 
												}
											   addRow(ulregion, loc[x].region_order, loc[x].country_lang, loc[x].url, loc[x].locale, loc[x].country);

											}
											if (localetohighlight != '')
											{
												var hl = document.getElementById(localetohighlight);
												hl.setAttribute("class","highlight");
												hl.setAttribute("className","highlight");
												var harrowid = localetohighlight + 'bg_arrow'; 
												var harrow = document.getElementById(harrowid); 
												harrow.setAttribute("class","bg-arrow-highlight");
												harrow.setAttribute("className","bg-arrow-highlight");
											}
											else
											if (us_locale)
											{
												var hl = document.getElementById(us_locale);
												hl.setAttribute("class","highlight");
												hl.setAttribute("className","highlight");
												var harrowid = us_locale + 'bg_arrow'; 
												var harrow = document.getElementById(harrowid); 
												harrow.setAttribute("class","bg-arrow-highlight");
												harrow.setAttribute("className","bg-arrow-highlight");
											}
											
											
												var region1 = document.getElementById('li_cont1');
												if (region1.getElementsByTagName('li').length > 1)
												{
													region1.style.visibility = "visible";
												}
												var region2 = document.getElementById('li_cont2');
												if (region2.getElementsByTagName('li').length > 1)
												{
													region2.style.visibility = "visible";
												}
												var region3 = document.getElementById('li_cont3');
												if (region3.getElementsByTagName('li').length > 1)
												{
													region3.style.visibility = "visible";
												}
												var region4 = document.getElementById('li_cont4');
												if (region4.getElementsByTagName('li').length > 1)
												{
													region4.style.visibility = "visible";
												}
												var region1line = document.getElementById('div1line').style.visibility = "visible";
												document.getElementById('div2line').style.visibility = "visible";
											break; 
										}
								   }

								   
							   }
				},

				error: function(jqXHR,textStatus,errorThrown) {  }
			   });
});


function addRow(tableId, col, cntrylang, url, locale, country){
	var bgarrowid = locale + 'bg_arrow';
	var row = jQuery("<li ><span class='bg-arrow' id=" + bgarrowid + ">&nbsp;</span><a class='locale'"  + " id=" + locale + " href=javascript:SelectLocale('" + url + "','" + locale + "','" + escape(country) + "')>" + cntrylang + "</a></li>");
	jQuery(tableId).append(row);


}

function SelectLocale(url,locale, cntry){
	// locale is selected from list - if not on worldwide page set omniture variables and set tracking 
	// set visitor to the locale that they selected 
	if (calledfromVar != 'worldwide')  // if not on the worldwide page - attempt to set the site and default cookie 
	{
		s.eVar41 = trackingBrand + ":" + locale; 
		s.eVar42 = trackingBrand + ":" + cntry; 
		s.prop41 = trackingBrand + ":" + cntry; 
		s.prop42 = trackingBrand + ":" + locale; 

		gatewayClickTrack("",locale); 

		SetCookie(siteCookieVar, url, expiry);
		var defaultcookie =  GetCookie('hasggrd_defaultLocale');
		if (defaultcookie == undefined)
		{
			SetCookie("hasggrd_defaultLocale", locale, expiry);
		}
	}
	window.location=url;
}


//---
// Click tracking
//---

//wrapper function for click tracking
function gatewayClickTrack(link,text) {
	// send omniture call when clicking on locale 
	var omEvent = "None";
	var omFriendlyName = "";

	omEvent = "event61";
	omFriendlyName = "Gateway Locale Click";

	// Build Ominture Call
	s.linkTrackVars = "prop41,prop42,prop43,eVar41,eVar42,events";
	s.linkTrackEvents = omEvent;
	s.events = omEvent;
	
	s.tl(this,"o",omFriendlyName);
}


