/**
 * Display splash page country selector at the user's first visit
 */

jQuery(document).ready(function($) {
	var cookieName = 'barbour.com';
	
	if ((document.cookie.length > 0 && document.cookie.indexOf(cookieName + '=') != -1 && document.location.href.substring(0,10) == 'http://www') || (document.location.href.substring(0,10) != 'http://www')) {
        // do nothing, cookie already sent
	} else {
		// handle jQuery animation
		// set the cookie to show user has already visited
		document.cookie = cookieName + "=1";
		
		// Load country detection page
		var loadUrl = "barbour_custom/country_selector";  
		$("#country-select-panel").load(loadUrl);
		
		$('#country-select-panel').show();
		$('#overlay-bg').show();
	}
});;
/**
 * Keep search area selection
 */

jQuery(document).ready(function($) {
	var inFormOrLink;
	$('a').live('click', function() { inFormOrLink = true; });
	$('form').bind('submit', function() { inFormOrLink = true; });
	$(':checkbox').bind('change', function() { inFormOrLink = true; });
	$('select').bind('change', function() { inFormOrLink = true; });

	$(window).bind('beforeunload', function()
	{
		if (!inFormOrLink){
			document.cookie = "scope=;path=/";
		}
	});
	
	$('#edit-search-category').bind("change", function(e) {onSearchClicked(e);});
	function onSearchClicked() {
		var searchScopeValue = $('#edit-search-category option:selected').attr('value');
		document.cookie = "scope=" + searchScopeValue+";path=/";
	}
	// set default country if country not in cookie
	var currentSearch = readCookie('scope');
	$("select#edit-search-category option[value='"+currentSearch+"']").attr("selected","selected");

	function readCookie(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		var output=null;
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) output = c.substring(nameEQ.length,c.length);
		}
		return output;
	}	
	
});;

