jQuery(function(){
		
	//only if the google ClientLocation works and gets a region...
	if(google.loader.ClientLocation && google.loader.ClientLocation.address.region){
		var detectedRegion = google.loader.ClientLocation.address.region;
		var country_code = google.loader.ClientLocation.address.country_code;
    var newCountry = '';

    var href = location.href;


		$('a[href^="https://www.yoursole.com"]').each(function(){ //for all links that point to the store...
			var href = $(this).attr('href');
			if(country_code === 'CA'){
			  var newHref = href.replace(/us/, "ca"); //we change the store link to point to the Canadian store instead of the default (US)
			  $(this).attr('href', newHref);
			}
			else if(country_code === 'US'){
				//for visitors from the US we just leave the default
				return false;
			} 
      else if(country_code === 'GB' || country_code === 'IE'){
			  var newHref = href.replace(/us/, "uk"); //we change the store link to point to the UK store instead of the default (US)
			  $(this).attr('href', newHref);
      }
      else {
				$(this).attr('href', "http://www.yoursole.com/where-to-buy/international/");
			}
		});

  $('#set-affil-store').each(function(){
    var formAction = $(this).attr('action');
    if(country_code === 'CA'){
      var newAction = formAction.replace(/us/, "ca"); //we change the store link to point to the Canadian store instead of the default (US)
      $(this).attr('action', newAction);
    }
    else {
      //for visitors from the US we just leave the default
      return false;
    }
  });

  $('#store-link-header').click(function(){
    var affiliate = $('#set-affil-store input').val();
    if( affiliate) {
      //$('#set-affil-store').submit();
      document.getElementById('set-affil-store').submit();
      return false;
    }
  });

  $('#pn-buy-now').click(function() {
    var phref = $(this).attr('href');
    var affiliate = $('#set-affil-store input').val();
    if (affiliate) {
      $('#set-affil-store').attr('action', phref);
      // $('#set-affil-store').submit();
      document.getElementById('set-affil-store').submit();
      return false;
    }
  });

	} else { 
		//otherwise do nothing
		return false; 
	}

});
