// Country Selector Codes
// Last Edit 11.21.11

/*
11.21.11 - final updates for HBR
*/

var langSelectDefault = '';


$(window).load(function () {
 	
	langSelectDefault = $('#selectlanguage').html();

	
	$('#selectcountry').change(function() {
	  	onCountryChange($(this));
	});
	
	$('.btn-cta').live('click', function (e) {
		setCountryAndGo();
	});
	
	$('.errormsg').hide();
	
	//If page is refreshed:
	onCountryChange($('#selectcountry'));
	
	
	//IP Country Selector Cookie (Pre select country)
	IPCountryCode = 'unknown';
	var getIPcookie = getCookie('SAP.SITE.COOKIE');
	if(getIPcookie != undefined) {
		crumbs = getIPcookie.split('&');
		for (var i = 0; i < crumbs.length; i++) {
			var crumb = crumbs[i].split("=");
			if (crumb[0] == 'IPCountryCode') {
				IPCountryCode = crumb[1];
			}
		}
	
		if (IPCountryCode != 'unknown') {
			theCountryS = $('.' + IPCountryCode).parent().attr('id');
			theCountryID = theCountryS.substr(9);
			
			//Preselect the right country
			$("#selectcountry option[value='" + theCountryID + "']").attr('selected', 'selected');
			onCountryChange($('#selectcountry'));
		}
	}
	
	
	//make the footer text bold. Yeah, I know it's bad practice. What are you gonna do about it?
	$('#footer-leftbtm li a').css({'font-weight' : 'bold', 'color' : 'white', 'padding-top' : '3px'});
	$('#footer-leftbtm li').css({'width' : 'auto', 'padding-right' : '30px'});
	
});


function areaEnter(getRel) {
    if (userAgent == 'default') {
        if ($('#worldmaptitle-' + getRel).attr('class') !== 'on') { $('#worldmaptitle-' + getRel).addClass('huvver'); }
        $('#worldmap').prepend('<div id="worldmap-hover-' + getRel + '"></div>');
    }
}

function areaLeave(getRel) {
    if (userAgent == 'default') {
        if ($('#worldmaptitle-' + getRel).attr('class') !== 'on') { $('#worldmaptitle-' + getRel).removeClass('huvver'); }
        $('#worldmap-hover-' + getRel).remove();
    }
}


// MAP SWITCH

function mapSwitch(getRel) {
	$('#worldlist-intro').hide();
	$('#worldmap-right .worldlist').hide();
	$('#worldlist-' + getRel).fadeIn(400);
	$('#worldmap-left').css('background', 'url(/global/ui/images/backgrounds/bkg-worldmap-' + getRel + '-current.png) no-repeat 8px 10px');
	$('#worldmap-left a.on, #worldmap-left area.on').removeClass('on');
	$('#worldmaptitle-' + getRel).addClass('on');
	if ($.browser.webkit) { $('area').focus(function () { $(this).blur(); }); }
}



function onCountryChange(me) {
	cID = $(me).val(); //Country ID
	if (cID != 'NULL') {
		newSelectData = $('#selector_' + cID).find('#ldata').html();
		$('#selectlanguage').html(newSelectData);
		$('#selectlanguage').removeAttr('disabled');
		$('#selectlanguage').css('opacity', 1);
	} else {
		$('#selectlanguage').attr('disabled', 'disabled');
		$('#selectlanguage').html(langSelectDefault);
		$('#selectlanguage').css('opacity', 0.6);
	}	
}

function setCountryAndGo() {
	selectedCountryPath = $("#selectlanguage").val();
	if (selectedCountryPath != 'null') {
		set_current_country(selectedCountryPath); //set the country cookie (aka "SelectedCountryPath")
		if($("#selectlanguage").val() == '/canada/index.epx' && $("#selectlanguage option:selected").text() == 'Français') {
			set_cookie('SAP.SITE.COOKIE', 'PC.LNG=French'); //no exp date = session cookie
		}
		//Go there:
		window.location = selectedCountryPath;
	} else {
		//Show the error message since they didn't pick a country	
		$('.errormsg').show();
	}
}


