animatedcollapse.addDiv('updateinfo', 'fade=1,');
animatedcollapse.addDiv('changemyagent', 'fade=1,');
animatedcollapse.init();

function addFavorite(propertyID) {
	$.ajax({
		type: "POST",
		url: "/cfc/remote/CustomerRemote.cfc",
		data: "method=AddFavorite&guiPropertySummaryID=" + propertyID,
		success: function(message) { }
	});
}

function clickFavorite(el) {
	alert($(el));
}

$(document).ready(function() {
	var bindMarkAsFavorite = function() {
		$("input.mark-as-favorite").bind("click", function(e) {
			e.preventDefault();
			var propId = $(this).next(".property-id-store").val();
			$(this).prev(".marked-as-favorite").css("display","inline");
			$(this).css("display","none");			
			addFavorite(propId);
		});
	};
	
	var bindTranslatedImage = function() {		
		$("input.translated-image").click(function(e) {
			e.preventDefault();
			document.location.href = $(this).attr("href");
		});
	};
	
	if ($("#saved-search-results").length > 0) {
		ColdFusion.navigate("/homefinder/saved-search-results.cfm?guiSearchId=" + $("input[name='guiSearchId']:first").val(), "saved-search-results", function() {
			bindMarkAsFavorite();
			bindTranslatedImage();
		});	    	
	}
	
	$(".toggle-info").click(function(e) {
    	animatedcollapse.toggle('updateinfo');
    	e.preventDefault();
    });
    $(".toggle-agent").click(function(e) {
    	animatedcollapse.toggle('changemyagent');
    	e.preventDefault();
    });
    
    $(".favorite-edit").hide();
    $(".comments-edit").click(function(e) {
    	var parentEl = $(this).parent();
    	parentEl.hide().siblings(".favorite-view").hide("fast", function() {
    		parentEl.siblings(".favorite-edit").show("slow");
    		parentEl.siblings("form").children("textarea").focus();		 
    	});   	
    	e.preventDefault();
    });
    
    $(".comments-cancel").click(function(e) {
    	hideForm($(this).parent());
    	e.preventDefault();
    });
    
    var hideForm = function(el) {
    	el.siblings(".favorite-edit").hide("slow", function() {
    		el.hide();
    		el.siblings(".favorite-view").show();
    	});
    };
    
    $(".comments-save").click(function(e) {
    	var parentEl = $(this).parent();
    	var formData = {};
    	formData.txtNotes = parentEl.siblings("form").children("textarea").val();
    	formData.guiCustomerFavoritePropertyID = parentEl.siblings("form").children("input[name=guiCustomerFavoritePropertyID]").val();
    	$.post("/cfc/remote/CustomerFavoritePropertyRemote.cfc?method=updateNotes", formData, function(data) {
    		parentEl.siblings(".favorite-notes").html(data);
    		hideForm(parentEl);
    	}, "json");
    	e.preventDefault();
    });
    
    $(".search-link").click(function(e) {
    	var el = $(this).parent().siblings(".hf-yellowbox-arrow-image");
    	if (el.css("display") != "inline") {
    		el.css("display", "inline");
	    	el = el.parent().removeClass("hf-display-saved-search").addClass("inside10");
	    	el = el.parent().addClass("jq-rounded-inneryellow");
	    	el = el.parent().addClass("jq-rounded").parent().addClass("hf-compare-arrow");
	    	
	    	turnOffSearch(el.siblings(".hf-compare-arrow"));
	    	
	    	$(".jq-rounded-inner,.jq-rounded-inneryellow", el).corner("round 3px").parent().css('padding', '1px').corner("round 5px");
	    	$('<div class="tr"></div><div class="tl"></div><div class="br"></div><div class="bl"></div>').appendTo(".rounded", el);
	    	
	    	var guiSearchId = $(this).parent().siblings("input[name='guiSearchId']").attr("value");
	    	ColdFusion.navigate("/homefinder/saved-search-results.cfm?&guiSearchId=" + guiSearchId, "saved-search-results", function() {
	    		bindMarkAsFavorite();
	    		bindTranslatedImage();
	    	});
    	}
    	e.preventDefault();
    });
    
    var turnOffSearch = function(el) {
    	el = $(el).removeClass("hf-compare-arrow").children(".jq-rounded").removeClass("jq-rounded");
    	$(".jquery-corner", el).remove();
    	el = el.children(".jq-rounded-inneryellow").removeClass("jq-rounded-inneryellow");
    	el = el.children(".inside10").removeClass("inside10").addClass("hf-display-saved-search");
    	el = el.children(".hf-yellowbox-arrow-image").css("display", "none");
    };
    var sorting = {};
    sorting.dateSort = 0;
    sorting.priceSort = 11;
    sorting.citySort = 12;
    sorting.streetSort = 13;
    sorting.ratingSort = 4;
    
    var currentSort = 0;
    
    $(".sort").click(function(e) {
    	var that = this; 
    	var clickedSort = sorting[$(this).attr("id")];
    	if (clickedSort == currentSort) {
    		currentSort = (currentSort + 10) % 20;
    	}
    	else {
    		currentSort = clickedSort;
    	}
    	$(".sort").removeClass("sort-arrow-down").removeClass("sort-arrow-up");
    	if (currentSort < 10) {
			$(this).addClass("sort-arrow-up")
		}
		else {
			$(this).addClass("sort-arrow-down");
		}
    	$.get("/cfc/remote/CustomerFavoritePropertyRemote.cfc", {
    		method:"getSortOrder",
    		guiCustomerId:guiCustomerId,
    		lOrderBy:currentSort
    	},
			function (sortedArray) {
				var resultContainer = $("#favorites");
				for (var i = 0; i < sortedArray.length; i++) {
					$("#" + sortedArray[i]).appendTo(resultContainer);
				}
			}
		, "json");
    	
    	e.preventDefault();
    	
    });
    var currentSearchEl = {};
    $(".save-search").bind("click", function(e) {
		e.preventDefault();
		currentSearchEl = this;
		$("#ssName").val($(this).parent().siblings("input[name='szSearchName']").val());
		var frequency = $(this).parent().siblings("input[name='bytEmailFrequency']").val();
		$("#ssFrequency").children("option").each(function() {
			this.selected = $(this).val() == frequency;
		});
		$("#ssSearchID").val($(this).parent().siblings("input[name='guiSearchId']").val());
		$("#ssSavedSearchId").val($(this).parent().siblings("input[name='guiCustomerSavedSearchId']").val());
    });
    
    $("#ssSave").bind("click", function(e) {
		e.preventDefault();
		var parent = $(currentSearchEl).parent();
		var newName = $("#ssName").val();
		var newFrequency = "Never";
		parent.siblings("input[name='szSearchName']").val(newName);
		parent.siblings("input[name='guiSearchId']").val($("#ssSearchId").val());
		parent.siblings("input[name='guiCustomerSavedSearchId']").val($("#ssSavedSearchId").val());
		$("#ssFrequency").children("option").each(function() {
			if (this.selected) {
				var currentVal = $(this).val();
				parent.siblings("input[name='bytEmailFrequency']").val(currentVal);
				if (currentVal == 0) {
					newFrequency = "Never";
				}
				else if (currentVal == 1) {
					newFrequency = "Daily";
				}
				else if (currentVal == 2) {
					newFrequency = "Weekly";
				}
				return;
			}
		});
		parent.siblings(".search-link-container").children(".search-link").html(newName);
		parent.siblings(".saved-search-info").children(".email-frequency").html(newFrequency);
    });
    
    $("a.DeleteSearch").click(function (e) {
    	if (!confirm("Are you sure you want to delete this saved search?")) {
    		e.preventDefault();
    	}
    });
    $("a.compare-link").bind("click", function(e) {
		e.preventDefault();
		var propIds = "";
		var url = $(this).attr("href");
		$("input[name='propID']").each(function() {
			if (this.checked) {
				if (propIds == "") {
					propIds = $(this).val();
				}
				else {
					propIds += "," + $(this).val();
				}
			}
		});
		$("input[name='propIDs']").val(propIds);
		$("#frmCompare").attr("action", url).submit();
	});
	$("a.remove-favorite").click(function(e) {
		if (!confirm("Are you sure you want to remove this favorite property?")) {
			e.preventDefault();
		}
	});
	$(".auto-submit-star").rating({
	  callback: function(value, link){
		$.ajax({
			type: "POST",
			url: "/cfc/remote/CustomerFavoritePropertyRemote.cfc",
			data: {
				method: 'updateRank',
				guiCustomerFavoritePropertyID: $(this).prev("input[name=guiCustomerFavoritePropertyID]").val(),
				lRank: value
			},
			success: function(message) {
			
			}
		});
	  }
	});
	
	$("#ChangePasswordDialog").dialog({
		autoOpen: false,
		draggable: false,
		resizable: false,
		width: 380,
		height: 300
	});
	$("#ChangePasswordDialog").css("display", "block");
	
	$("#changePasswordLink").click(function(e) {
		e.preventDefault();
		$("#ChangePasswordDialog").dialog("open");
	});
	
	$("#cpCancel").click(function(e) {
		e.preventDefault();
		$("#ChangePasswordDialog").dialog("close");
	});
	$("#cpSave").click(function(e) {
		e.preventDefault();
		$.post(
			"/cfc/remote/CustomerRemote.cfc",
			{
				method: "changePassword",
				guiCustomerId: $("#cpGuiCustomerId").val(),
				oldPassword: $("#cpOldPassword").val(),
				newPassword: $("#cpNewPassword").val(),
				newPasswordConfirm: $("#cpNewPasswordConfirm").val()
			},
			function(errorMessages) {
				if (errorMessages.length > 0) {
					var errorContainer = $("#cpErrors  ul.form-error-container").html("");
					for (var i = 0; i < errorMessages.length; i++) {
						errorContainer.append("<li>" + errorMessages[i] + "</li>");
					}
					$("#cpErrors").css("display", "block");
				}
				else {
					$("#cpOldPassword").val("");
					$("#cpNewPassword").val("");
					$("#cpNewPasswordConfirm").val("");
					$("#ChangePasswordDialog").dialog("close");
				}
			},
			"json"
		);
	});
	(function () {
		var agentCol = document.getElementById("agent-col");
		$("#bitWorkingWithCompanyAgentNo").click(function(e) {
			agentCol.style.display = "none";
		});
		
		$("#bitWorkingWithCompanyAgentYes").click(function(e) {
			agentCol.style.display = "inline";
		});
	})();
});