/* script for shop page and the "Shop Online" link, require jQuery, jQuery.cookie */
var targetCountry = jQuery.cookie('selected-country');
var url = window.location.pathname;

var doCheck = (url + '').indexOf("au-shop", 0) > 0 || (url + '').indexOf("shop-au", 0) > 0
	|| (url + '').indexOf("us-shop", 0) > 0 || (url + '').indexOf("shop-us", 0) > 0;

//console.info(url);
if (targetCountry == null || doCheck) {
	//console.info('checking...');
	if ((url + '').indexOf("shop-au", 0) > 0) {
		targetCountry = "au";
	} else if ((url + '').indexOf("shop-us", 0) > 0 || (url + '').indexOf("us-shop", 0) > 0) {
		targetCountry = "us";
	} else {
		targetCountry = "-";
	}
//console.info(targetCountry);
}
if (window.location.search == "?source=au") {
	targetCountry = "au";
	jQuery.cookie('selected-country', 'au');
}
//console.info(targetCountry);

var shopTitle = 'Shop Online'; //this title must match the menu item title
var shopLink = null; //link of shop online
function duplicateList(){
	var selection = jQuery('#minicountryswitcher'); //use exists list
	var div = document.createElement('div');
	div.id = 'country-list';
	div.innerHTML = '<ul class="sub_nav_list" style="display:none;list-style:none;">'+selection.html().replace(/<option/gi, '<li><a').replace(/option>/gi, 'a></li>').replace(/value/gi, 'href')+'</ul>';
	jQuery(div).insertAfter(jQuery(shopLink));
	jQuery('#country-list ul li a').click(function(e){
		_gaq.push( ['_trackPageview', 'Shop - '+jQuery(this).html()] ); //track link clicks on Google Analytics
	});
	jQuery('#country-list ul li a:contains("'+targetCountry+'")').click(function(e){ //target country is clicked
		jQuery.cookie('selected-country', targetCountry);
		window.location = shopLink.attr('href');
		e.stopPropagation();
		return false; //ignore this country url
	});
	jQuery('#country-list ul li a:contains("Country")').closest('li').remove(); //it's just a title
}
var isListShown = false;
function showList(){
	if(!jQuery.exists(jQuery("#country-list")))
		duplicateList();
	if(isListShown)
		jQuery('#country-list').children('ul').slideUp(200);
	else
		jQuery('#country-list').children('ul').slideDown(200);
	isListShown = !isListShown;
}
function showShop(){ //only call this on shop page
	if(jQuery.cookie('selected-country')==targetCountry){
		jQuery('#country-list').css('display', 'none');
		jQuery('ul.sub_nav_list').insertAfter(jQuery("ul.menu li a span:contains('"+shopTitle+"')").closest('li')).wrap('<li class="sub_nav" />');
	} else {
		showList();
		jQuery('.moduletable_products').css('display', 'none');
	}
}

jQuery(document).ready(function() {
	/*jQuery.cookie('selected-country', 'null'); //for testing*/
	jQuery.exists = function(selector) {return (jQuery(selector).length > 0);}
	if(!jQuery.exists(jQuery("ul.menu li a span:contains('"+shopTitle+"')"))) //make sure this item is there.
		return;
	shopLink = jQuery("ul.menu li a span:contains('"+shopTitle+"')").closest('a');
	shopLink.click(function(e){
		if(jQuery.cookie('selected-country')!=targetCountry){ //if country is not selected, show the list
			showList();
			e.stopPropagation();
			return false;
		}
	});
	if( jQuery.exists(jQuery(".moduletable_products")) ){
		showShop();
	}
	/*if( jQuery.exists(jQuery("li#current a span:contains('"+shopTitle+"')")) ){ //if current page is shop
		showShop();
	} else {
		jQuery('.moduletable_products').css('display', 'none');
	}*/
});
