
$(function () {

	var url = String(window.location).toLowerCase();

	if (url.indexOf("http://www.gomassage.com") > -1 && (url.indexOf("/book") > -1 || url.indexOf("/bookgift") > -1 || url.indexOf("/bookgo") > -1 || url.indexOf("/bookconcierge") > -1 || url.indexOf("/concierge") > -1 || url.indexOf("/booklewis") > -1 || url.indexOf("/lewis") > -1 || url.indexOf("/signup") > -1 || url.indexOf("/login") > -1)) {
		url = String(window.location);
		url = url.replace("http://www.gomassage.com/", "https://gomassage.buildcentral.com/");
		window.location.replace(url);
	}
	else if (url.indexOf("https://gomassage.buildcentral.com") > -1 && (url.indexOf("/book") == -1 && url.indexOf("/bookgift") == -1 && url.indexOf("/bookgo") == -1 && url.indexOf("/bookconcierge") == -1 && url.indexOf("/concierge") == -1 && url.indexOf("/booklewis") == -1 && url.indexOf("/lewis") == -1 && url.indexOf("/signup") == -1 && url.indexOf("/login") == -1)) {
		url = String(window.location);
		url = url.replace("https://gomassage.buildcentral.com/", "http://www.gomassage.com/");
		window.location.replace(url);
	}

	if ($("#quick-booking input[name='PostalCode']").length) {

		// Load jquery-ui if needed
		if (typeof $.datepicker == "undefined") {
			$.getScript("Scripts/jquery-ui.min.js", function () {
				$("#quick-booking input[name='AppointmentDate']").datepicker();
				LoadLocationAutocomplete();
			});
		}
		else {
			$("#quick-booking input[name='AppointmentDate']").datepicker();
			LoadLocationAutocomplete();
		}

		/*if (typeof $.mask == "undefined") {
		$.getScript("Scripts/JQuery/plugins/jquery.maskedinput.min.js", function () {
		$("#quick-booking input[name='PostalCode']").mask("99999");
		});
		}
		else {
		$("#quick-booking input[name='PostalCode']").mask("99999");
		}*/

		$("#header-menu a").hover(function () {
			$("#quick-booking").css("display", "none");
			$("#massage-bookings-quotes").unbind("mouseenter mouseleave");

			if ($("#ways-to-book-massage h2").length) {
				$("#ways-to-book-massage h2").css("display", "block");
			}
		});

		$("#massage-bookings-quotes").click(function () {
			$("#quick-booking").css("display", "block");
			// Prevent panel from closing
			$("#massage-bookings-quotes").hover(function () {
				$("#quick-booking").css("display", "block");
			});

			if ($("#ways-to-book-massage h2").length) {
				$("#ways-to-book-massage h2").css("display", "none");
			}
		});

		$("#quick-booking #CancelButton").click(function () {
			$("#quick-booking").css("display", "none");
			$("#massage-bookings-quotes").unbind("mouseenter mouseleave");

			if ($("#ways-to-book-massage h2").length) {
				$("#ways-to-book-massage h2").css("display", "block");
			}
		});
	}

});

function LoadLocationAutocomplete() {

	// Set Autocomplete
	$("#quick-booking input[name='PostalCode']").autocomplete({
		minLength: 2,

		// define callback to format results
		source: function (req, add) {
			var searchVal = $("#quick-booking input[name='PostalCode']").val();
			var url = "/Home/GetCityState/?city=" + searchVal;

			// pass request to server
			$.getJSON(url, req, function (data) {

				// create array for response objects
				var suggestions = [];

				// process response
				$.each(data, function (i, val) {
					suggestions.push(val.CityName + ", " + val.StateAbbr);
				});

				// pass array to callback
				add(suggestions);
			});
		}
	});

}
