


//Country script

function logg(str)
{
	if(console && console.log)
	{
		console.log(str);	
	} else {
		jQuery('<p style="background:white;padding:15px;">' + str + '</p>').appendTo('body');	
	}
}

function getCountry()
{
	var sUrl = window.location.toString();
	var sCountry = null;	
	switch(true)
	{
		case sUrl.match(/appleandbeenz.co.nz/i) != null://New Zealand
			sCountry = 'NZ';
			break;
		case sUrl.match(/appleandbeeusa.com/i) != null://USA
			sCountry = 'USA';
			break;
		case sUrl.match(/appleandbeeshop.com/i) != null://AUS
			sCountry = 'AUS';
			break;
		case sUrl.match(/appleandbee.worldsecuresystems.com/i) != null://CART
			if(sUrl.match(/site_country=/i) != null)
			{
				sUrl = sUrl.split('?')[1].split('&');
				for(var i = 0; i < sUrl.length; i++)
				{
					if(sUrl[i].match(/site_country=/i) != null)
					{
						sCountry = sUrl[i].split('=')[1];
					}
				}
			} else {
				if(sUrl.match(/PaymentProcessDPS.aspx/i) != null)sCountry = 'NZ';		
			}
			break;	
		default:	
	}
	return sCountry;	
}



$(document).ready(function(){ // --------------------------------------------------------------------------------------------------------------------- START - DOCUMENT READY
	
	var sCountry = getCountry();
	
	
	
	//Set country links according to site country	  
	if(sCountry)//If country is set
	{
		switch(sCountry)
		{
			case 'USA':
				var sCountryAnchorId = 'currency-us';
				break;
			case 'NZ':
				var sCountryAnchorId = 'currency-nz';	
				break;
			default:
				var sCountryAnchorId = 'currency-au';
		}
		$('#' + sCountryAnchorId).addClass('current-url');	
	}
	
	//Add country string to url in click event for check out button in cart
	$('a#catshopbuy').click(function(){
		if($(this).attr('href').match(/site_country=/i) == null)
		{
			$('a#catshopbuy').attr('href',$('a#catshopbuy').attr('href') + '&site_country=' + sCountry);	//Add country to url in check out button
		}
	});
	
	$('#ShippingOptions').change(function(){
		$('a#catshopbuy').attr('href',$('a#catshopbuy').attr('href') + '&site_country=' + sCountry);	//Add country to url in check out button	
	});
	
	//Add country string to action attribute of the order submission form
	$('#catCartDetails form').attr('action', $('#catCartDetails form').attr('action') + '&site_country=' + sCountry);
	
	//Update anchor tags on pages according to country
	
	//List of selectors to be excluded from the anchors to be updated with the current country
	var notUpdate = ['#currency-au','#currency-nz','#currency-us'];
	
	if(window.location.toString().match(/appleandbee.worldsecuresystems.com/i) != null)
	{
		switch(sCountry)
		{
			case 'AUS': var sNewLocation = 	'appleandbeeshop.com'; break;
			case 'NZ': var sNewLocation = 'appleandbeenz.co.nz'; break;
			case 'USA': var sNewLocation = 'appleandbeeusa.com'; break;
			default: var sNewLocation = '';
		}
		
		$('a').not(notUpdate.join(',')).each(function(){
			if($(this).attr('href') && $(this).attr('href').match(/appleandbeeshop.com/i) != null)
			{
				$(this).attr('href',$(this).attr('href').replace(/appleandbeeshop.com/i,sNewLocation));
			}
			
			if($(this).attr('href') && $(this).attr('href').length > 0 && $(this).attr('href').substr(0,1) == '/')
			{
				$(this).attr('href',$(this).attr('href').replace('/','http://www.' + sNewLocation + '/'));	
			}
		});	
	}
	
	
// --------------------------------------------------------------------------------------------------------------------- START 	- DETAILED VIEW COUNTRIES

	var sCountry = $.trim($('#flags a.current-url').attr('title')).toLowerCase();//Get current country
	var shopMain = $('.shop-main');
	var smallProductTable = shopMain.find('table.productSmall');
	
	
	
	shopMain.attr('id',sCountry); //shop id with country
	
	switch(sCountry)//Set collection of countries strings to be used as flags to exclude countries
	{
		case 'aud':
			var countryArray = ['aud','au','aus'];
			break;
		case 'nzd':
			var countryArray = ['nzd','nzd','nz'];
			break;
		case 'usd':
			var countryArray = ['us','usd','usa'];
			break;
		default:
			var countryArray = [];	
	}
	
	var productArray = new Array();
	
	smallProductTable.find('td').each(function($key,$value){//Loop through each table cell
	
		var oTd = $(this);
		
		var oTdChild = oTd.find('.shop-product-small');//Get reference to small product parent inside the table cell
		
		if(oTdChild.length > 0)//If a product has been found
		{
			
			var childClasses = $.trim(oTdChild.attr('class')).split(/,+|\s+|;+/gi);//split the class attribute into an array
			
			oTdChild.attr('class',childClasses.join(' '));
			
			var countryFound = false;//Used to check if a product with a country that needs to removed has been found
			
			for(var i = 0; i < childClasses.length; i++)//Loop through classes of small product
			{
				if($.inArray(childClasses[i].toLowerCase(),countryArray) != -1)countryFound = true;	//If small product belongs to country that needs to be removed
			}
			//logg('childClasses: [' + childClasses.join('][') + ']     found: ' + countryFound.toString());
			if(!countryFound)//If country has not been found
			{
				productArray.push(oTd.detach());//Keep it
			} else {
				oTd.remove();
			}
		} else {
			oTd.remove();
		}
		
		
	});

	var n = 0; //Used in the second for loop to advance the smallProduct array in groups of 4 items
	
	for(var i = 0; i < Math.ceil(productArray.length / 4); i++)
	{
		var oTr = smallProductTable.find('tr').eq(i);
		for(var x = 0; x < 4; x++)
		{
			if(typeof productArray[n + x] != 'undefined')oTr.append(productArray[n + x]);	
		}
		n = n + 4;
	}
	


// --------------------------------------------------------------------------------------------------------------------- END 	- DETAILED VIEW COUNTRIES

	
}); // --------------------------------------------------------------------------------------------------------------------- END - DETAILED VIEW COUNTRIES





