// About Page Testimonials
function testimonials() {
	$("#testimonials .uk-slider-container").css("visibility", "inherit");
}

// Populate Account modal and show it
function accountModal(request){
	switch(request){
		case 'register':
			if(isMobile.any){
				window.open('https://taphandlesofficial.myshopify.com/account/register', '_blank');
			} else{
				$("#account_modal .uk-modal-dialog iframe").attr("src", "https://taphandlesofficial.myshopify.com/account/register");	
			}
		break;
		
		case 'account':
			if(isMobile.any){
				window.open('https://taphandlesofficial.myshopify.com/account', '_blank');
			} else{
				$("#account_modal .uk-modal-dialog iframe").attr("src", "https://taphandlesofficial.myshopify.com/account");
			}
		break;
		
		case 'cart':
			if(isMobile.any){
				window.open('https://taphandlesofficial.myshopify.com/cart', '_blank');
			} else{
				$("#account_modal .uk-modal-dialog iframe").attr("src", "https://taphandlesofficial.myshopify.com/cart");
			}
		break;
	}
	
	if(isMobile.any == false){UIkit.modal("#account_modal").show();}
}

function checkCart(){
	$.ajax({
		type: 'GET',
		url: 'https://taphandlesofficial.myshopify.com/cart.json',
		dataType: 'jsonp',
		success: function(data){
			//console.log(data); 
			//If there are items in cart
			if(data.item_count > 0){
				$("#site_header #shop.nav-cart").hide();
				$("#site_header #cart.nav-cart").text('Cart' + ' (' + data.item_count + ')').show();
				$("#mobile_cart_link").removeClass('uk-hidden').find('a').first().text('Cart' + ' (' + data.item_count + ')');
			} else{
				$("#site_header #cart.nav-cart").text('Cart').hide();
				$("#site_header #shop.nav-cart").show();
				$("#mobile_cart_link").addClass('uk-hidden').find('a').first().text('Cart');
			}
		}
	});
}

function checkLang() {
	// set endpoint and your access key
	var ip = '172.31.10.108';
	var uri = window.location.href;
	var access_key = '20fc72a1e723f68b760e05e793a7969b';

	// get the API result via jQuery.ajax
	$.ajax({
		type:"GET",
		url: 'https://ipapi.co/json/',
		success: function(json) {
			console.log(JSON.stringify(json))
			var uri = window.location.pathname.split('/');
			
			if (['en', 'es', 'it', 'eu', 'latam', 'spa'].indexOf(uri[1]) !== -1) {
				
				if (['NA', 'SA'].indexOf(json.continent_code) !== -1) {$('#lang_menu_desktop').hide();}
			
				return;
			}
			
			if ( (json.continent_code === 'EU' || json.continent_code === 'OC') && json.country !== 'ES' ) {window.location.replace('/eu/');} else if ( json.continent_code === 'SA' && json.country !== 'ES' ) {window.location.replace('/latam/');} else if (json.country === 'ES') {window.location.replace('/spa/');} else {window.location.replace('/en/');}

			// output the "capital" object inside "location"

		}
	});
}

var url = window.location.href,
	path = window.location.pathname,
	origin   = window.location.origin;
var checkfrt = path.split('/');
var newurl = ''; 
if(checkfrt[1] === 'it' ){
newurl = url.replace("com/it", "com/eu");
}else if(checkfrt[1] === 'es'){
newurl = url.replace("com/es", "com/spa");
}
console.log(checkfrt[1])
console.log(newurl)
if(newurl !== ''){
	window.location.href = newurl;
}
// MAIN
$(document).ready(function() {
	// Clear Account iframe src on close
	$('#account_modal').on('hide.uk.modal', function(){
        $("#account_modal .uk-modal-dialog iframe").attr("src", "");
        checkCart();
    });
    
    checkCart();
	checkLang();
});