var CookieName = 'shopPreference';

// Get url querystring param by name
$.urlParam = function(name){
var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
if (!results) { return 0; }
return results[1] || 0;}

$(document).ready(function() {

    // Hide the page stops it displaying temporarily...
    $('.jsEnabled').hide();

    // Hook all href events in the links div
    $('.countrySelect a').click(function() {
        var SelectLink = $(this).attr('href');

        if ($('#chkLanguage').is(':checked'))
            SetCookie(SelectLink); 				// Persist selection

        window.location.href = SelectLink;
        return false;
    });

    var qsval = $.urlParam('c');

    if (qsval) {
        // Clear cookie
        ClearCookie();
        $('.jsEnabled').show();
    } else {
        // Get the current cookie value (if present)
        var curLink = GetCookie();
        if (curLink) {
            // validate the cookie value (can ONLY be one of the listed values else simply present list as if cookie is missing)
            $('.countrySelect a').each(function() {
                var thisLink = $(this).attr('href');
                if (thisLink == curLink)
                    window.location.href = curLink;
            });
        } else {
            $('.jsEnabled').show();
        }
    }
});

// Set the cookie
function SetCookie(linkref) {
    // Set the preference
    $.cookie(CookieName, linkref, { expires: 365, path: '/' });
}

// Clear the cookie
function ClearCookie() {
	$.cookie(CookieName,null,{path: '/' });
}

// Get the cookie
function GetCookie() {
	// Getting the preference cookie:
	return $.cookie(CookieName);
}
