// Add a case-insensitive version of the :contains selector
(function($) {
	$.extend($.expr[':'], {
		Contains: function(a,i,m) {
			return (a.textContent || a.innerText || "").toLowerCase().indexOf(m[3].toLowerCase()) >= 0;
		}
	})
})(jQuery);
// Initialize the alphabet navigation
function initNavAlpha(bolFull) {
	if ($("table.list").outerHeight() > $("div.table-list-holder").innerHeight()) {
		$("ul.nav-alpha").show();
/*
		if (bolFull) {
			var arrAlpha = new Array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
			var strCurrent = "";
			for (var i = 0; i < arrAlpha.length; i++) {
				strLetter = arrAlpha[i];
				bolFound = false;
				$("table.list tbody tr:visible td.contact-name").each(function() {
					if ($(this).text().charAt(0).toUpperCase() == strLetter && !bolFound) {
						$("ul.nav-alpha a:contains('" + strLetter + "')").removeClass("empty");
						if (strCurrent.length == 0) strCurrent = strLetter;
						bolFound = true;
					}
				});
				if (!bolFound) {
					strTempHolder = $("ul.nav-alpha a:contains('" + strLetter + "')").text();
					$("ul.nav-alpha a:contains('" + strLetter + "')").parent().prepend(strTempHolder);
					$("ul.nav-alpha a:contains('" + strLetter + "')").remove();
				}
			}
			$("ul.nav-alpha a").removeClass("current");
			$("ul.nav-alpha a:contains('" + strCurrent + "')").addClass("current");
			$("div.table-list-holder").scrollTop(0);
			$("ul.nav-alpha a").click(function() { alphaScroller(this); return false; });
		}
*/
		$("div.table-list-holder").scrollTop(0);
		$("ul.nav-alpha a").click(function() { alphaScroller(this); return false; });
	} else {
		$("ul.nav-alpha").hide();
	}
}
// Handle the alphabet nav scrolling feature
function alphaScroller(objNode) {
	var strLetter = $(objNode).text().toUpperCase();
	var bolFound = false;
	$("table.list tbody tr:visible td.contact-name").each(function(i) {
		if ($(this).text().charAt(0).toUpperCase() == strLetter && !bolFound) {
			bolFound = true;
			$("ul.nav-alpha a.current").removeClass("current");
			$(objNode).addClass("current");
			intTopOffset = $(this).parent().position().top - $(this).parent().parent().position().top;
			$("div.table-list-holder").scrollTop(intTopOffset);
		}
	});
}
// Used to toggle lists of check obxes or radio buttons
function toggleFields(objNode, strSelector) {
	$(strSelector).filter(function() { return $(this).parents("tr").is(":visible"); }).attr("checked", $(objNode).attr("checked"));
}
// Add zebra stripes to list tables
function zebraStripe() {
	$("table.list tbody tr").removeClass("odd");
	$("table.list").each(function() { 
		$(this).find("tbody tr:visible").each(function(index) { if (index % 2 == 1) $(this).addClass("odd"); });
	});
}
// Initialize the search form, if present
var strMsgEmail = "Email Address";
var strMsgPassword = "Password";
var strMsgLocation = "Zip Code or City and State";
var strMsgPriceMin = "Min";
var strMsgPriceMax = "Max";
var strMsgMLS = "MLS Number";
$(document).ready(function() {
	if ($("#header-signin #hs_user_email").val() == "") { $("#header-signin #hs_user_email").val(strMsgEmail); } else { $("#header-signin #hs_user_email").addClass("filled"); }
	$("#header-signin #hs_user_email").focus(function() { if ($(this).val() == strMsgEmail) { $(this).val(""); $(this).addClass("filled"); }});
	$("#header-signin #hs_user_email").blur(function() { if ($(this).val() == "") { $(this).val(strMsgEmail); $(this).removeClass("filled"); }});
	$("#header-signin #hs_user_password_text").val(strMsgPassword);
	$("#header-signin #hs_user_password_text").focus(function() { $(this).hide(); $("#header-signin #hs_user_password").show().focus(); });
	$("#header-signin #hs_user_password").blur(function() { if ($(this).val() == "") { $(this).hide(); $("#header-signin #hs_user_password_text").show(); }});
	if ($("#lslocation").val() == "") { $("#lslocation").val(strMsgLocation); } else { $("#lslocation").addClass("filled"); }
	$("#lslocation").focus(function() { if ($(this).val() == strMsgLocation) { $(this).val(""); $(this).addClass("filled"); }});
	$("#lslocation").blur(function() { if ($(this).val() == "") { $(this).val(strMsgLocation); $(this).removeClass("filled"); }});
	if ($("#lsmin").val() == "") { $("#lsmin").val(strMsgPriceMin); } else { $("#lsmin").addClass("filled"); }
	$("#lsmin").focus(function() { if ($(this).val() == strMsgPriceMin) { $(this).val(""); $(this).addClass("filled"); }});
	$("#lsmin").blur(function() { if ($(this).val() == "") { $(this).val(strMsgPriceMin); $(this).removeClass("filled"); }});
	if ($("#lsmax").val() == "") { $("#lsmax").val(strMsgPriceMax); } else { $("#lsmax").addClass("filled"); }
	$("#lsmax").focus(function() { if ($(this).val() == strMsgPriceMax) { $(this).val(""); $(this).addClass("filled"); }});
	$("#lsmax").blur(function() { if ($(this).val() == "") { $(this).val(strMsgPriceMax); $(this).removeClass("filled"); }});
	if ($("#lsid").val() == "") { $("#lsid").val(strMsgMLS); } else { $("#lsid").addClass("filled"); }
	$("#lsid").focus(function() { if ($(this).val() == strMsgMLS) { $(this).val(""); $(this).addClass("filled"); }});
	$("#lsid").blur(function() { if ($(this).val() == "") { $(this).val(strMsgMLS); $(this).removeClass("filled"); }});
	$("#header-signin").submit(function() {
		if ($("#header-signin #hs_user_email").val() == strMsgEmail) $("#header-signin #hs_user_email").val("");
	});
	$("#listingsearch").submit(function() {
		if ($("#lslocation").val() == strMsgLocation) $("#lslocation").val("");
		if ($("#lsmin").val() == strMsgPriceMin) $("#lsmin").val("");
		if ($("#lsmax").val() == strMsgPriceMax) $("#lsmax").val("");
		if ($("#lsid").val() == strMsgMLS) $("#lsid").val("");
		if ($("#search-location").is(":hidden")) {
			$("#lslocation").val("");
		} else {
			$("#lsid").val("");
		}
	});
	$("a.search-toggle-mls").click(function() { $("#search-location").hide(); $("#search-mls").show(); return false; });
	$("a.search-toggle-location").click(function() { $("#search-mls").hide(); $("#search-location").show(); return false; });
	$("#header-signup").bind("click", function() {
		$("body").append('<div id="dialog-signup" class="dialog"></div>');
		$("#dialog-signup").load("/consumer_signup.php").dialog({ title: "Buyer/Seller Signup", modal: true, draggable: false, resizable: false, width: 400, close: function() { $("#dialog-signup").remove() } });
		return false;
	});
	$("a.new-assoc-link").bind("click", function() {
		$("body").append('<div id="dialog-new-assoc" class="dialog"></div>');
		$("#dialog-new-assoc").load("/consumer_associations.php").dialog({ title: "New Agent Associations", modal: true, draggable: false, resizable: false, width: 400, close: function() { $("#dialog-new-assoc").remove(); $("#messages div.warning").hide(); } });
		return false;
	});
	$("a.mailto").live("click", function() {
		$("body").append('<div id="dialog-contact" class="dialog"></div>');
		var intUserID = $(this).find("input.userid").val();
		var strOID = $(this).find("input.orgid").val();
		var strLID = $(this).find("input.listingid").val();
		$("#dialog-contact").load("/consumer_contact.php", { "user_id": intUserID, "oid": strOID, "lid": strLID }).dialog({ title: "Contact Form", modal: true, draggable: false, resizable: false, width: 600, close: function() { $("#dialog-contact").remove(); } });
		return false;
	});
});
