function addFavorite(propertyID) {
	$.ajax({
		type: "POST",
		url: "/cfc/remote/CustomerRemote.cfc",
		data: "method=AddFavorite&guiPropertySummaryID=" + propertyID,
		success: function(message) {
			/* $("#myFavorites").load("/homefinder/tabs/ajax-my-favorites.cfm", null, function() {
				alert("here");
				sIFR.redraw();
			}); */
			/* $.ajax({
				type: "GET",
				url: "/homefinder/tabs/ajax-my-favorites.cfm",
				success:function( */
			if ($("#myFavorites").length > 0) {
				var href = document.location.href.replace(/\?[^\?]*/, "");
				href += "?selectTab=myFavorites";
				document.location.href = href;
			}
		}
	});
}

function updatePropertyView(viewmode) {
	var currentClass = $("#search-results-area").attr("class");
	$("#search-results-area").removeClass(currentClass);
	$("#search-results-area").addClass(viewmode);
	$.ajax({
		type: "POST",
		url: "/cfc/remote/CustomerRemote.cfc",
		data: "method=UpdatePreference&ResultsView=" + viewmode,
		success: function(message) { }
	});
}

function saveSearch(savedSearchId, customerId, searchId, searchName, searchFreq) {
	$.ajax({
		type: "POST",
		url: "/cfc/remote/CustomerSavedSearchRemote.cfc",
		data: {
			method: "save",
			guiCustomerSavedSearchID: savedSearchId,
			guiCustomerId: customerId,
			guiSearchID: searchId,
			szSearchName: searchName,
			bytEmailFrequency: searchFreq
		},
		success: function(message) {
			$("#SaveSearch").hide();
			$("#SavedSearch").show();
		}
	});
}

function CompareProperties(url)
{
	$("#frmCompare").attr("action", url).submit();
}

function AddCompare(propID, searchID)
{
	$.ajax({
		type: "POST",
		url: "/cfc/remote/SearchResultsRemote.cfc",
		data: "method=AddCompareProperty&guiPropertySummaryID=" + propID +  "&guiSearchID=" + searchID,
		success: function(message) { }
	});
}

function RemoveCompare(propID, searchID)
{
	$.ajax({
		type: "POST",
		url: "/cfc/remote/SearchResultsRemote.cfc",
		data: "method=SubtractCompareProperty&guiPropertySummaryID=" + propID +  "&guiSearchID=" + searchID,
		success: function(message) { }
	});
}

function ClearCompare(searchID)
{
	$("input.compare").attr("checked", false);
	$.ajax({
		type: "POST",
		url: "/cfc/remote/SearchResultsRemote.cfc",
		data: "method=ClearCompareList&guiSearchID=" + searchID,
		success: function(message) { }
	});
}

$(document).ready(function(){
	// Functions for handling compare/driving directions clicks
	$("a.compare-link").bind("click", function(e) {
		e.preventDefault();
		CompareProperties($(this).attr("href"));
	});
	$("input.compare").bind("click", function(e) {
		if ($(this).attr("checked")) {
			AddCompare($(this).val(), $("#guiSearchID").val());
		} else {
			RemoveCompare($(this).val(), $("#guiSearchID").val());
		}
	});
	
	$("#SaveSearchDialog").dialog({
		autoOpen: false,
		draggable: false,
		resizable: false,
		width: 350,
		height: 200
	});
	$("#SaveSearchDialog").css("display", "block");
	
	$(".save-search").bind("click", function(e) {
		e.preventDefault();
		if ($("#isLoggedIn").val() == "true") {
			$("#ssSearchID").val($("#SaveSearchId").val());
			$("#SaveSearchDialog").dialog("open");
		} else {
			$("#jqRedir").val("true");
			$("#jqCustSaveSearch").val($("#SaveSearchId").val());
			$("#jqRegistrationLink").attr("href", "/homefinder/registration.cfm?custSaveSearch=" + $("#SaveSearchId").val());
			$("#jqLoginDialog").dialog("open");
		}
	});
	
	$("#ssCancel").bind("click", function(e) {
		e.preventDefault();
		$("#SaveSearchDialog").dialog("close");
	});
	
	$("#ssSave").bind("click", function(e) {
		e.preventDefault();
		
		//Do the saving code
		saveSearch($("#ssSavedSearchId").val(), $("#ssCustomerId").val(), $("#ssSearchID").val(), $("#ssName").val(), $("#ssFrequency").val());
		
		$("#SaveSearchDialog").dialog("close"); //Hide the dialog box
	});
	
	$("input.mark-as-favorite").bind("click", function(e) {
		e.preventDefault();
		var propId = $(this).next(".property-id-store").val();
		if ($("#isLoggedIn").val() == "true") {
			$(this).prev(".marked-as-favorite").css("display","inline");
			$(this).css("display","none");			
			addFavorite(propId);
		} else {
			$("#jqRedir").val("true");
			$("#jqCustSaveProperty").val(propId);
			$("#jqRegistrationLink").attr("href", "/homefinder/registration.cfm?saveProperty=" + propId);
			$("#jqLoginDialog").dialog("open");
		}
	});
	$("input.translated-image").click(function(e) {
		e.preventDefault();
		document.location.href = $(this).attr("href");
	});
});