$(document).ready(function () {

	$.getJSON('/json/ipgeo', (function (data) {
		console.log(data);
		var ipcode = data.ipgeocode;
		console.log("API called. 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: '/'
		});
	});
	$("a.currus").click(function () {
		CookieSet = $.cookie('exp__currency', 'US', {
			path: '/'
		});
	});
	$("a.curreu").click(function () {
		CookieSet = $.cookie('exp__currency', 'EU', {
			path: '/'
		});
	});

	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 ");
		} else if (data.order_qty == 1) {
			$(".cartupdate").text(" 1 ");
			$(".cartSubTot").text("(" + data.order_subtotal + ")");
		} else if (data.order_qty >= 2) {
			$(".cartupdate").text(" " + data.order_qty + " ");
			$(".cartSubTot").text("(" + data.order_subtotal + ")");
		}
	}));

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

	//tooltip for lang and curr

	$('[data-toggle=modal-popover]').each(function () {
		$(this).popover({
			content: $(this).attr("popover-content"),
			title: $(this).attr("popover-title")
		})
		$(this).tooltip({
			placement: 'right',
			title: $(this).attr("tooltip-title")
		})
	});

	// Read a page's GET URL variables and return them as an associative array.
	function getUrlVars() {
		var vars = [],
			hash;
		var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
		for (var i = 0; i < hashes.length; i++) {
			hash = hashes[i].split('=');
			vars.push(hash[0]);
			vars[hash[0]] = hash[1];
		}
		return vars;
	}

	//create function to select correct 360
	function setThreeSixty() {
		//initialise product model selection on page load
		const model = getUrlVars()['model'];
		const lightboxlink = document.getElementById("threesixlight");
		if (model === '9404A-06') {
			lightboxlink.setAttribute("href", "/products/9404a-06-threesixty");
		} else if (model === '9404A-16') {
			lightboxlink.setAttribute("href", "/products/9404a-16-threesixty");
		} else if (model === '9404A-25') {
			lightboxlink.setAttribute("href", "/products/9404a-25-threesixty");
		} else if (model === '9404A-33') {
			lightboxlink.setAttribute("href", "/products/9404a-33-threesixty");
		}
	}

	setThreeSixty();

	document.onclick = function () {setThreeSixty();};

	$.getJSON('/json/sdk_release', (function (data) {
		console.log(data);

		var win_sdk_32_version = data.sdk_release[0].version;
		var win_sdk_64_version = data.sdk_release[1].version;
		var macos_sdk_version = data.sdk_release[2].version;

		if ($.cookie('win_sdk_32') === null || $.cookie('win_sdk_32') === "" || $.cookie('win_sdk_32') === "null" || $.cookie('win_sdk_32') === undefined) {
			var sdk_32_cookie = $.cookie('win_sdk_32', win_sdk_32_version, {
				path: '/'
			});
		}
		if ($.cookie('win_sdk_64') === null || $.cookie('win_sdk_64') === "" || $.cookie('win_sdk_64') === "null" || $.cookie('win_sdk_64') === undefined) {
			var sdk_64_cookie = $.cookie('win_sdk_64', win_sdk_64_version, {
				path: '/'
			});
		}
		if ($.cookie('macos_sdk') === null || $.cookie('macos_sdk') === "" || $.cookie('macos_sdk') === "null" || $.cookie('macos_sdk') === undefined) {
			var mac_sdk_cookie = $.cookie('macos_sdk', macos_sdk_version, {
				path: '/'
			});
		}

	}));

});
