// This hotfix makes older versions of jQuery UI drag-and-drop work in IE9
//(function($){var a=$.ui.mouse.prototype._mouseMove;$.ui.mouse.prototype._mouseMove=function(b){if($.browser.msie&&document.documentMode>=9){b.button=1};a.apply(this,[b]);}}(jQuery));
(function($) {
    var a = $.ui.mouse._mouseMove;
    $.ui.mouse._mouseMove = function(b) {
        if ($.browser.msie && document.documentMode >= 9) {
            b.button = 1
        };
        a.apply(this, [b]);
    }
} (jQuery));
// Set up the default block UI for loading external content
$.blockUI.defaults.message = '<img src="/themes/default/images/loader_bar.gif" alt="Loading" width="43" height="11" />';
$.blockUI.defaults.css.background = 'none';
$.blockUI.defaults.css.border = 'none';
// Loads content from a specified external file into a specified target node
function loadContent(strTargetNode, strPath, fncCallback) {
	$(strTargetNode).block();
	$(strTargetNode).load(strPath, function() { 
		$(strTargetNode).unblock();
		if (typeof fncCallback == 'function') fncCallback();
	});
}
// Opens an alert fly-out window
function alertUser(strTitle, strDescription, fncCallBack) {
	$('body').append('<div id="alert-user" class="dialog" title="' + strTitle + '"><div>' + strDescription + '</div></div>');
	$('#alert-user').dialog({ draggable: false, resizable: false, width: 400, height: 220, modal: true, close: function() { $(this).remove(); }, buttons: {
		'OK' : function() {
			if (typeof fncCallback == 'function') fncCallback();
			$(this).remove();
		}
	}});
}
// Opens a confirmation fly-out window
function confirmedAction(strTitle, strDescription, strPathAction, fncSuccess, fncFailure) {
	$('body').append('<div id="confirm-action" class="dialog" title="' + strTitle + '"><div>' + strDescription + '</div></div>');
	$('#confirm-action').dialog({ draggable: false, resizable: false, width: 400, height: 220, modal: true, close: function() { $(this).remove(); }, buttons: {
		'Continue' : function() {
			$.get(strPathAction, function(data) {
				if (data == 'success') {
					if (typeof fncSuccess == 'function') fncSuccess();
					$('#confirm-action').remove();
				} else {
					if (typeof fncFailure == 'function') fncFailure();
					$('#confirm-action').html(data);
				}
			});
		},
		'Cancel' : function() { $(this).remove(); }
	}});
}
// Executes a remote script and opens an alert fly-out window only if an error is returned
function unconfirmedAction(strPathAction, fncSuccess, fncFailure) {
	$.get(strPathAction, function(data) {
		if (data == 'success') {
			if (typeof fncSuccess == 'function') fncSuccess();
		} else {
			alertUser('An Error Has Occurred', data, fncFailure);
		}
	});
}
// 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);
// Used to toggle lists of check boxes 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"); });
	});
}
// Flyout for Help System
function showHelpInfo(objNode) {
	$("body").append('<div id="dialog-instructions" class="dialog"></div>');
	$("#dialog-instructions")
		.load($(objNode).attr("href"))
		.dialog({ title: "<img src='/themes/default/images/icon_help.png' width='14' height='14' border='0' /> Please oh please, tell me what this is...", modal: true, draggable: false, resizable: false, width: 400, height: 400, close: function() { $("#dialog-instructions").remove() }
	});
}
// Flyout for Templete Preview
function showTemplateInfo(objNode) {
	$("body").append('<div id="dialog-instructions" class="dialog"></div>');
	$("#dialog-instructions")
		.load($(objNode).attr("href"))
		.dialog({ title: "Template Preview", modal: true, draggable: false, resizable: false, width: 800, height: 600, close: function() { $("#dialog-instructions").remove() }
	});
}
// Class for handling thumbnail slider boxes
function sliderBox(objOptions) {
	// Set up default options
	var objDefaults = {
		'node' : '',
		'contentPath' : '',
		'imagePath' : '/themes/default/',
		'header' : '&nbsp;',
		'description' : '',
		'maxDisplay' : 3,
		'callBack' : '',
		'nolayout' : false
	};
	// Set up object properties, overriding default values with those passed in the options argument
	for(var strKey in objDefaults) {
		this[strKey] = (typeof objOptions[strKey] == 'undefined') ? objDefaults[strKey] : objOptions[strKey];
	}
	this.animating = false;
	this.nodeSliderInner = this.node + ' div.slider-inner';
	// Method for sliding the thumbnails
	this.slide = function(objOptions) {
		var intThumbsWidth = $(this.nodeSliderInner).children('div:first').outerWidth(true);
		if (!this.animating) {
			// Get the current left margin for use in our calculations
			var intMarginLeftOld = parseInt($(this.nodeSliderInner).css('margin-left'));
			// Calculate the new left margin based on the direction of the slide, and the current position
			var intMarginLeftNew;
			if (objOptions.direction == 'next') {
				if (intMarginLeftOld > (this.maxDisplay * intThumbsWidth) - ($(this.nodeSliderInner).children('div').length * intThumbsWidth)) {
					intMarginLeftNew = intMarginLeftOld - (this.maxDisplay * intThumbsWidth);
				} else {
					intMarginLeftNew = intMarginLeftOld;
				}
			} else {
				if (intMarginLeftOld < 0) {
					intMarginLeftNew = intMarginLeftOld + (this.maxDisplay * intThumbsWidth);
				} else {
					intMarginLeftNew = intMarginLeftOld;
				}
			}
			// Slide the thumbnails by animating the left margin
			this.animating = true;
			var objSelf = this;
			$(this.nodeSliderInner).animate({ marginLeft: intMarginLeftNew }, 'slow', function() {
				objSelf.animating = false;
				// Switch to grayed-out arrow images if at beginning or end of list
				var objImgNode = $(objSelf.nodeSliderInner).parent().parent().parent().find('img.left');
				if (parseInt($(objSelf.nodeSliderInner).css('margin-left')) == 0) {
					if ($(objImgNode).attr('src').indexOf('_off') == -1) {
						var arrImgName = $(objImgNode).attr('src').split('.');
						$(objImgNode).attr('src', arrImgName[0] + '_off.' + arrImgName[1]);
					}
				} else {
					$(objImgNode).attr('src', $(objImgNode).attr('src').replace(/_off/, ''));
				}
				objImgNode = $(objSelf.nodeSliderInner).parent().parent().parent().find('img.right');
				if (parseInt($(objSelf.nodeSliderInner).css('margin-left')) <= (objSelf.maxDisplay * intThumbsWidth) - ($(objSelf.nodeSliderInner).children('div').length * intThumbsWidth)) {
					if ($(objImgNode).attr('src').indexOf('_off') == -1) {
						var arrImgName = $(objImgNode).attr('src').split('.');
						$(objImgNode).attr('src', arrImgName[0] + '_off.' + arrImgName[1]);
					}
				} else {
					$(objImgNode).attr('src', $(objImgNode).attr('src').replace(/_off/, ''));
				}
			});
		}
	};
	// Load the thumbnail contents
	this.loadThumbs = function(strContentPath) {
		if (typeof strContentPath != 'undefined') this.contentPath = strContentPath;
		$(this.node + ' div.inner').block();
		var objSelf = this;
		$(this.nodeSliderInner).load(this.contentPath, function() { objSelf.resetThumbs(objSelf); });
	}
	this.resetThumbs = function(objSelf) {
		var objLocal = (objSelf === null || objSelf === undefined) ? this : objSelf;
		$(objLocal.nodeSliderInner).width($(objLocal.nodeSliderInner + ' div.thumb-holder:first').outerWidth(true) * $(objLocal.nodeSliderInner + ' div.thumb-holder').length);
		$(objLocal.node + ' img.slide-left').click(function() { objLocal.slide({ 'direction' : 'previous' }); });
		$(objLocal.node + ' img.slide-right').click(function() { objLocal.slide({ 'direction' : 'next' }); });
		if ($(objLocal.nodeSliderInner + ' div.thumb-holder').length <= objLocal.maxDisplay) $(objLocal.node + ' img.slide-left, ' + objLocal.node + ' img.slide-right').hide();
		$(objLocal.node + ' div.inner').unblock();
		if(typeof objLocal.callBack == 'function') objLocal.callBack(); 
	}
	// Method for initializing the slider
	this.init = function(strImagePath) {
		if (typeof strImagePath != 'undefined') this.imagePath = strImagePath;
		var strDescription = (this.description == '') ? '' : '<div style="padding: 5px;">' + this.description + '</div>';
		if (!this.nolayout) {
			$(this.node).find('.slider-auto').remove();
			$(this.node)
				.addClass('recent-slider')
				.prepend('<h2 class="slider-auto"><img class="slide-left slide-left-img left" src="' + this.imagePath + 'images/slider_recent_left_off.gif" alt="Previous" width="30" height="18" /><img class="slide-right slide-right-img right" src="' + this.imagePath + 'images/slider_recent_right.gif" alt="Next" width="30" height="18" />' + this.header + '</h2>' + strDescription + '<div class="inner slider-auto" style="padding: 0px;"><div class="slider"><div class="slider-inner">&nbsp;</div></div><div class="height-fixer">&nbsp;</div></div>')
				.append('<div class="height-fixer slider-auto">&nbsp;</div>')
				.show()
			;
		}
		this.loadThumbs();
	}
}
// 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() {
	$("#nav-imagemanager").click(function() { mcImageManager.browse(); return false; });
	$("#nav-filemanager").click(function() { mcFileManager.browse(); return false; });
	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;
	});
	// Flyout for Help System
	$("a.help-link").click(function() { showHelpInfo(this); return false; });
	// Flyout for Help System
	$("a.preview-link").click(function() { showTemplateInfo(this); return false; });
});

