﻿$(document).ajaxStop($.unblockUI);

/* ----------------------- menus ----------------------- */

$(document).ready(function () {

	$("ul.subnav").parent().append("<span class='menu'></span>");

	$("ul.topnav li#help").click(function () {

		$(this).parent().find("ul.subnav").slideDown('fast').show();

		$(this).parent().hover(function () {
		}, function () {
			$(this).parent().find("ul.subnav").slideUp('slow');
		});


	}).hover(function () {
		$(this).addClass("subhover");
	}, function () {
		$(this).removeClass("subhover");
	});

});



/* ----------------------- smooth scrolling ----------------------- */

$(document).ready(function () {

	$('#rental-info a[href*=#]').click(function () {

		if ($(this).parent().hasClass('scrollable_navigation') || $(this).attr('href').indexOf('javascript') > -1) return;
		var duration = 750;
		var easing = 'swing';
		var newHash = this.hash;
		var oldLocation = window.location.href.replace(window.location.hash, '');
		var newLocation = this;

		// make sure it's the same location      
		if (oldLocation + newHash == newLocation) {
			// get target
			var target = $(this.hash + ', a[name=' + this.hash.slice(1) + ']').offset().top;

			// adjust target for anchors near the bottom of the page
			if (target > $(document).height() - $(window).height()) target = $(document).height() - $(window).height();

			// set selector
			if ($.browser.safari) var animationSelector = 'body:not(:animated)';
			else var animationSelector = 'html:not(:animated)';

			// animate to target and set the hash to the window.location after the animation
			$(animationSelector).animate({ scrollTop: target }, duration, easing, function () {

				// add new hash to the browser location
				window.location.href = newLocation;
			});

			// cancel default click action
			return false;
		}
	});

});


/* ----------------------- side scrolling toolbar ----------------------- */

$(function () {

	var $sidebar = $("#side-toolbar"),
		$window = $(window),
		offset = $sidebar.offset(),
		topPadding = 15;

	$window.scroll(function () {
		if ($window.scrollTop() > offset.top) {
			$sidebar.stop().animate({
				marginTop: $window.scrollTop() - offset.top + topPadding
			});
		} else {
			$sidebar.stop().animate({
				marginTop: 0
			});
		}
	});

});


/* ----------------------- extended textbox ----------------------- */

function PrepareForInput(sender, defaultText) {
	sender.style.backgroundColor = "#FFFFAA";
	if (sender.value == defaultText) {
		sender.value = "";
	}
}

function OnInputBlur(sender, defaultText) {
	sender.style.backgroundColor = "#ffc";
	if (sender.value == "") {
		sender.value = defaultText;
	}
}


/* ----------------------- unit search ----------------------- */

(function ($) {
	$.widget("ui.combobox", {
		_create: function () {
			var self = this,
					select = this.element.hide(),
					selected = select.children(":selected"),
					value = selected.val() ? selected.text() : "";
			var input = this.input = $("<input>")
					.insertAfter(select)
					.val(value)
					.autocomplete({
						delay: 0,
						minLength: 0,
						source: function (request, response) {
							var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
							response(select.children("option").map(function () {
								var text = $(this).text();
								if (this.value && (!request.term || matcher.test(text)))
									return {
										label: text.replace(
											new RegExp(
												"(?![^&;]+;)(?!<[^<>]*)(" +
												$.ui.autocomplete.escapeRegex(request.term) +
												")(?![^<>]*>)(?![^&;]+;)", "gi"
											), "<strong>$1</strong>"),
										value: text,
										option: this
									};
							}));
						},
						select: function (event, ui) {
							ui.item.option.selected = true;
							self._trigger("selected", event, {
								item: ui.item.option
							});
						},
						change: function (event, ui) {
							if (!ui.item) {
								var matcher = new RegExp("^" + $.ui.autocomplete.escapeRegex($(this).val()) + "$", "i"),
									valid = false;
								select.children("option").each(function () {
									if ($(this).text().match(matcher)) {
										this.selected = valid = true;
										return false;
									}
								});
								if (!valid) {
									$(this).val("");
									select.val("");
									input.data("autocomplete").term = "";
									return false;
								}
							}
						}
					})
					.addClass("ui-widget ui-widget-content ui-corner-left");

			input.data("autocomplete")._renderItem = function (ul, item) {
				return $("<li></li>")
						.data("item.autocomplete", item)
						.append("<a>" + item.label + "</a>")
						.appendTo(ul);
			};

			this.button = $("<button type='button'>&nbsp;</button>")
					.attr("tabIndex", -1)
					.attr("title", "Show All Items")
					.insertAfter(input)
					.button({
						icons: {
							primary: "ui-icon-triangle-1-s"
						},
						text: false
					})
					.removeClass("ui-corner-all")
					.removeClass("ui-state-default")
					.removeClass("ui-widget-content")
					.addClass("ui-corner-drop")
					.click(function () {
						if (input.autocomplete("widget").is(":visible")) {
							input.autocomplete("close");
							return;
						}

						$(this).blur();
						input.autocomplete("search", "");
						input.focus();
					});
		},

		destroy: function () {
			this.input.remove();
			this.button.remove();
			this.element.show();
			$.Widget.prototype.destroy.call(this);
		}
	});
})(jQuery);

$(function () {
	$("#combobox").combobox();
	$("#toggle").click(function () {
		$("#combobox").toggle();
	});
});

function RequestStart(sender, args) {
	if (args.EventTargetElement != null)
		args.EventTargetElement.disabled = true;
}

function ResponseEnd(sender, args) {
	if (args.EventTargetElement != null)
		args.EventTargetElement.disabled = false;
}

$(document).ready(function () {
	$(".smart-state input").attr("disabled", false);
	$("input.smart-state ").attr("disabled", false);
	$(".smart-state input").click(SmartState);
});

function SmartState() {
	$("a.smart-state").click(function (e) { e.preventDefault(); });
	$(".smart-state input").attr("disabled", true);
	$("input.smart-state ").attr("disabled", true);
	$('.smart-state option').each(function (i, option) { $(option).remove(); });
}

function SmartStateOverlay() {
	$.blockUI.defaults.css = {};
	setTimeout("$.blockUI({ message: $('#loading-overlay') })", 3500);
	setTimeout('$("#processing-text").html("Processing, please wait ...");', 3500);
	setTimeout('$("#processing-text").html("Processing extra data, please wait ...");', 7000);
	setTimeout('$("#processing-text").html("Processing extra data, please wait ... We are sorry it takes that long ...");', 15000);
	SmartState();
}

jQuery.fn.slideFadeToggle = function (speed, easing, callback) {
	return this.animate({ opacity: 'toggle', height: 'toggle' }, speed, easing, callback);
};

function NavigateRentalUnit() {
	var code = $('#combobox option:selected').val();

	if (code != "")
		document.location = "/rentals/unit_" + code + ".aspx";
}

function NavigateQuote() {
	var quote = $('#quotebox').val();

	if (quote != "")
		document.location = "/memberarea/allinone.aspx?mode=payment&quote=" + quote;
}

function roundNumber(rnum, rlength) {
	return Math.round(rnum * Math.pow(10, rlength)) / Math.pow(10, rlength);
}

function ValidateDate(input) {
	var re = /^\d{1,2}\/\d{1,2}\/\d{4}$/;

	if (input != '' && !input.match(re)) {
		return false;
	}
	return true;
}
