$(document).ready(function () {
	//console.log("Visitor Country Code: " + country_code);
	//console.log("URL Country Code: " + url_country_code);

	var _host = document.location.host;
	var _url = document.location.href.substring(document.location.href.indexOf(_host)+_host.length);
	/***if(_url == "/select-currency.htm"){ select_currency("none"); }***/

	/***
		Visitor URL detection. This function is called on every single page load. It checks to see if the country
		code of the URL matches the visitor's country code. If it does not match, it fires the select_currency 
		function. The select currency function checks the visitor's country code and re-directs them to the 
		proper website.
		
		Take into account that GoodBarry cannot make up its mind in the naming convention for England. Use the follow
		information to make the decision:
		URL Country Code: GB
		Visitor Country Code: UK
	***/
	if(!validate_address(country_code)){
		select_currency(_url);
	}

	/*
		Arrays of visitor country codes per website segment.
	*/
	var countries_eu = new Array("AT","BE","FI","FR","DE","GR","IE","IT","LU","NL","PT","SI","ES");
	var countries_uk = new Array("UK");
	var countries_na = new Array("US","CA");

	// Re-Direct explanar.biz to explanar.com
	if(_host == "explanar.biz" || _host == "www.explanar.biz"){
		document.location = "http://explanar.com" + _url;
	}

	// Re-Direct explanar.co.uk to uk.explanar.com
	if(_host == "explanar.co.uk"){
		document.location = "http://uk.explanar.com" + _url;
	}

	if(_host !== "explanargolf.worldsecuresystems.com"){
		if(!validate_address(country_code)){
			jQuery("#location-warning").show();
			jQuery("body").css({backgroundPosition: "left " + jQuery("#location-warning").outerHeight() + "px"});
		}
	}

	$("#currency-selection a").bind("click", function(e){
		if($(this).attr("rel") == "USD"){
			//alert("You selected USD");
			document.location = "http://explanar.com" + _url;
		}else if($(this).attr("rel") == "GBP"){
			//alert("You selected GBP");
			document.location = "http://uk.explanar.com" + _url;
		}else{
			//alert("You selected EUR");
			document.location = "http://eu.explanar.com" + _url;
		}
		return false;
	});

	$(".tabs ul li a").bind("click", function(e){
		$(".tabs ul li").each(function(){
			$(this).removeClass("selected");
		});
		$(".tab").each(function(){
			$(this).css({display:'none'});
		});
		$(this).parent().addClass("selected");
		$($(this).attr("href")).fadeIn(1500);
		return false;
	});

	initDropDownMenus();
	
	if($("#lnkZoom").attr("href") == "" || $("#lnkZoom").attr("href") == "#"){ $("#lnkZoom").remove(); }
	if($("#lnkPlayVideo").attr("href") == "" || $("#lnkPlayVideo").attr("href") == "#"){ $("#lnkPlayVideo").remove(); }
	
	/***
	//Cart Related Items
	if(url_country_code == "GB" && country_code == "UK"){
	}else if(url_country_code == "FR" && (country_code == "AT" || country_code == "BE" || country_code == "FI" || country_code == "FR" || country_code == "DE" || country_code == "IE" || country_code == "IT" || country_code == "LU" || country_code == "NL" || country_code == "PT" || country_code == "SI" || country_code == "ES" || country_code == "CY" || country_code == "IS" || country_code == "MT")){
	}else if(url_country_code == "US" && (country_code == "US" || country_code == "CA")){
	}else{
		$("#checkout-notice").slideDown("normal");
	}
	***/
	
	//Checkout Related Items
	$("#BillingShipping").bind("change", function(e){
		if($(this).checked){
			// unchecked
		}else{
			// checked
			$("#tblCheckoutShipping #ShippingAddress").val($("#tblCheckoutBilling #BillingAddress").val());
			$("#tblCheckoutShipping #ShippingCity").val($("#tblCheckoutBilling #BillingCity").val());
			$("#tblCheckoutShipping #ShippingState").val($("#tblCheckoutBilling #BillingState").val());
			$("#tblCheckoutShipping #ShippingZip").val($("#tblCheckoutBilling #BillingZip").val());
			$("#tblCheckoutShipping #ShippingCountry").val($("#tblCheckoutBilling #BillingCountry").val());
		}
	});
	
	$('#centre-detail tbody tr td:last-child').each(function(){
														  
		if (($(this).text() == "") || ($(this).text() == "BLANK")){
			$(this).parent().remove();
		}
	});
	
});

function catSetSelectedCSSItem(menu_id)
{
	var _host = document.location.host;
	var _url = document.location.href.substring(document.location.href.indexOf(_host)+_host.length);

	$('#' + menu_id + ' a').each(function(){
		if($(this).attr('href') == _url){
			$(this).parent().addClass('selected');
		}
	});
	$('#breadcrumbs a').each(function(){
		if($(this).attr('href') == _url){
			$(this).addClass('selected');
		}
	});
}


function initDropDownMenus(){
	$('.menu li').bind("mouseover", function(e){
		$(this).addClass('sfhover');
	});
	$('.menu li').bind("mouseout", function(e){
		$(this).removeClass('sfhover');
	});
}

function select_currency(referring_page){
	var _host = document.location.host;
	var _url = document.location.href.substring(document.location.href.indexOf(_host)+_host.length);
	if(referring_page == "none"){
		if(_host !== "explanargolf.worldsecuresystems.com"){
			if(country_code === "UK" || country_code === "GI" || country_code === "GG" || country_code === "IM" || country_code === "JE"){
				document.location = "http://uk.explanar.com";
			}else if(country_code === "AT" || country_code === "BE" || country_code === "FI" || country_code === "FR" || country_code === "DE" || country_code === "IE" || country_code === "IT" || country_code === "LU" || country_code === "NL" || country_code === "PT" || country_code === "SI" || country_code === "ES" || country_code === "CY" || country_code === "IS" || country_code === "MT"){
				document.location = "http://eu.explanar.com/home.htm";
			}else{
				document.location = "http://explanar.com";
			}
		}
	}else{
		if(_host !== "explanargolf.worldsecuresystems.com"){
			if(country_code === "UK"){
				document.location = "http://uk.explanar.com" + _url;
			}else if(country_code === "AT" || country_code === "BE" || country_code === "FI" || country_code === "FR" || country_code === "DE" || country_code === "GR" || country_code === "IE" || country_code === "IT" || country_code === "LU" || country_code === "NL" || country_code === "PT" || country_code === "SI" || country_code === "ES"){
				document.location = "http://eu.explanar.com" + _url;
			}else{
				document.location = "http://explanar.com" + _url;
			}
		}
	}
}

function validate_address(visitor_country_code){
	
	var countries_eu = new Array("AT","BE","FI","FR","DE","GR","IE","IT","LU","NL","PT","SI","ES");
	var countries_uk = new Array("UK","GB");
	// The countries_us array isn't used. It is just here for posterity's sake. Eventually we may need to use it like the other lists are used in order to filter others to a different domain.
	var countries_us = new Array("US");
	
	// The FR country code is used as a generic definition of Europe to get the system to use Euros.
	if(url_country_code === "FR"){
		for(i = 0; i < countries_eu.length; i++){
			if(visitor_country_code === countries_eu[i]){
				return true;
			}
		}
	}
	// The GB country code is for Great Brittain. REMINDER: url_country_code uses GB while the visitor_country_code uses UK. Nice consistency, BusinessCatalyst.
	if(url_country_code === "GB"){
		for(i = 0; i < countries_uk.length; i++){
			if(visitor_country_code === countries_uk[i]){
				return true;
			}
		}
	}
	// The US country code is for United States. It is currently serving as a catch-all site for any visitor NOT in Europe or United Kingdom.
	if(url_country_code === "US"){
		// The person should be viewing the Europe site if they are in the Europe list.
		for(i = 0; i < countries_eu.length; i++){
			if(visitor_country_code === countries_eu[i]){
				return false;
			}
		}
		// The person should be viewing the United Kingdom site if they are in the United Kingdom list.
		for(i = 0; i < countries_uk.length; i++){
			if(visitor_country_code === countries_uk[i]){
				return false;
			}
		}
		// If the person is not in the Europe list or the United Kingdom list, they should be viewing the United States site.
		return true;
	}
	return false;
}