$(document).ready(function () {

	$.getJSON('/json/ipgeo', (function (data) {
		console.log(data);
		var ipcode = data.ipgeocode;
		console.log('API called and the ipcode is ' + ipcode);

		if (ipcode == 'GB') { //UK Pound		
			$('#local-telephone').html('<i class="icon-phone"></i> +44 1480 396 395');
			if ($.cookie('exp__currency') === null || $.cookie('exp__currency') === "" || $.cookie('exp__currency') === "null" || $.cookie('exp__currency') === undefined) {
				var CookieSet = $.cookie('exp__currency', 'GB', {
					path: '/'
				});
			}
			if ($.cookie('exp__country') === null || $.cookie('exp__country') === "" || $.cookie('exp__country') === "null" || $.cookie('exp__country') === undefined) {
				var CookieSet = $.cookie('exp__country', 'GBR', {
					path: '/'
				});
			}

			//$('.currEx i').removeClass("icon-dollar-sign icon-euro-sign").addClass("icon-sterling-sign");
			var currenCookieSet = $.cookie('exp__currenCook', 'GBP', { path: '/' });

		} else if (ipcode == 'US' || ipcode == 'CA' || ipcode == 'MX') { //US Dollar			

			$("#local-telephone").html("Tel: +1 800 591 2796 (Toll Free)");
			if ($.cookie('exp__currency') === null || $.cookie('exp__currency') === "" || $.cookie('exp__currency') === "null" || $.cookie('exp__currency') === undefined) {
				var CookieSet = $.cookie('exp__currency', 'US', {
					path: '/'
				});
			}
			if ($.cookie('exp__country') === null || $.cookie('exp__country') === "" || $.cookie('exp__country') === "null" || $.cookie('exp__country') === undefined) {
				var CookieSet = $.cookie('exp__country', 'USA', {
					path: '/'
				});
			}
			//$('.currEx i').removeClass("icon-sterling-sign icon-euro-sign").addClass("icon-dollar-sign");
			var currenCookieSet = $.cookie('exp__currenCook', 'USD', { path: '/' });

		} else if (ipcode == 'AD' || ipcode == 'AT' || ipcode == 'BE' || ipcode == 'CY' || ipcode == 'EE' || ipcode == 'FI' || ipcode == 'FR' || ipcode == 'DE' || ipcode == 'GR' || ipcode == 'IE' || ipcode == 'IT' || ipcode == 'XK' || ipcode == 'LV' || ipcode == 'LU' || ipcode == 'MT' || ipcode == 'MC' || ipcode == 'ME' || ipcode == 'NL' || ipcode == 'PT' || ipcode == 'SM' || ipcode == 'SK' || ipcode == 'SI' || ipcode == 'ES' || ipcode == 'VA') {
			$('#local-telephone').html('<i class="icon-phone"></i> +44 1480 396 395');
			if ($.cookie('exp__currency') === null || $.cookie('exp__currency') === "" || $.cookie('exp__currency') === "null" || $.cookie('exp__currency') === undefined) {
				var CookieSet = $.cookie('exp__currency', 'EU', {
					path: '/'
				});
			}
			if ($.cookie('exp__country') === null || $.cookie('exp__country') === "" || $.cookie('exp__country') === "null" || $.cookie('exp__country') === undefined) {
				var CookieSet = $.cookie('exp__country', 'EUR', {
					path: '/'
				});
			}
			//$('.currEx i').removeClass("icon-dollar-sign icon-sterling-sign").addClass("icon-euro-sign");
			var currenCookieSet = $.cookie('exp__currenCook', 'EUR', { path: '/' });

		} else { //USDollar - catch all 			
			$('#local-telephone').html('<i class="icon-phone"></i> +44 1480 396 395');
			if ($.cookie('exp__currency') === null || $.cookie('exp__currency') === "" || $.cookie('exp__currency') === "null" || $.cookie('exp__currency') === undefined) {
				var CookieSet = $.cookie('exp__currency', 'US', {
					path: '/'
				});
			}
			if ($.cookie('exp__country') === null || $.cookie('exp__country') === "" || $.cookie('exp__country') === "null" || $.cookie('exp__country') === undefined) {
				var CookieSet = $.cookie('exp__country', 'ROW', {
					path: '/'
				});
			}
			//$('.currEx i').removeClass("icon-sterling-sign icon-euro-sign").addClass("icon-dollar-sign");
			var currenCookieSet = $.cookie('exp__currenCook', 'USD', { path: '/' });
		}

		$.cookie('exp__distCookie', ipcode, { path: '/' });

	}));

	$("a.currgb").click(function () { CookieSet = $.cookie('exp__currency', 'GB', { path: '/' }); location.reload(true); });
	$("a.currus").click(function () { CookieSet = $.cookie('exp__currency', 'US', { path: '/' }); location.reload(true); });
	$("a.curreu").click(function () { CookieSet = $.cookie('exp__currency', 'EU', { path: '/' }); location.reload(true); });

	if ($.cookie('exp__currency') === 'GB') {
		$('.currEx i').removeClass("icon-dollar-sign icon-euro-sign").addClass("icon-sterling-sign");
		var currenCookieSet = $.cookie('exp__currenCook', 'GBP', { path: '/' });
	}
	if ($.cookie('exp__currency') === 'US') {
		$('.currEx i').removeClass("icon-sterling-sign icon-euro-sign").addClass("icon-dollar-sign");
		var currenCookieSet = $.cookie('exp__currenCook', 'USD', { path: '/' });
	}
	if ($.cookie('exp__currency') === 'EU') {
		$('.currEx i').removeClass("icon-dollar-sign icon-sterling-sign").addClass("icon-euro-sign");
		var currenCookieSet = $.cookie('exp__currenCook', 'EUR', { path: '/' });
	}

	//Cart JSON call and cookie cuttr



	$.getJSON('/json/global_cart', (function (data) {
		console.log(data);
		if (data.order_qty == 0) {
			$(".cartupdate").text(" 0 items");
		} else if (data.order_qty == 1) {
			$(".cartupdate").text(" 1 item");
			$(".cartSubTot").text("(" + data.order_subtotal + ")");
		} else if (data.order_qty >= 2) {
			$(".cartupdate").text(" " + data.order_qty + " items");
			$(".cartSubTot").text("(" + data.order_subtotal + ")");
		}
	}));

	var str = $(".cartSubTot").text();
	str = str.slice(0, - 3);
	$(".cartSubTot").text(str);

	//tooltip for lang and curr		

	$('li.popover-markup a.langBut').each(function () {

		$(this).tooltip({
			placement: 'left',
			title: $(this).attr("tooltip-title")
		});
	});
	$('li.popover-markup a.currencyBut').each(function () {
		$(this).tooltip({
			placement: 'right',
			title: $(this).attr("tooltip-title")
		});

	});

});