if(typeof isRemote == "undefined") var isRemote = 0;

jQuery(document).ready(function() {
	jQuery("[name='wishlist'][data-propertyid][data-wishlist]:not([data-activated])")
		.unbind("mouseover")
		.unbind("mouseout")
		.unbind("click")
		.attr("disabled","")
		.mouseover(function() { jQuery("tr[data-disableonclick]").attr("data-disableonclick","true"); })
		.mouseout(function() { jQuery("tr[data-disableonclick]").attr("data-disableonclick","false"); })
		.click(function() {
			var source = jQuery(this);

			var flag       = parseInt(source.attr("data-wishlist"));
			var propertyid = source.attr("data-propertyid");
			var mode       = (flag == 1) ? "delete" : "add";
			var sourceall  = jQuery("[name='wishlist'][data-propertyid='" + propertyid + "'][data-wishlist]");

			if(isRemote == 1) {
				var link   = "http://" + mySnipApps.snipapps_domain + "/templates/realtor/_remote/wishlist.cfm/mode/" + mode;
				var params = "json=1&accountid=" + request_account_id + "&callerdomain=" + mySnipApps.caller_domain + "&" + mySnipApps.session_key + "&format=json&jsoncallback=?";
			} else {
				var link   = "http://" + CGI["http_host"] + "/templates/realtor/_remote/wishlist.cfm/mode/" + mode;
				var params = "json=1";
			}

			params = params + "&propertyid=" + propertyid + "&format=json&jsoncallback=?";

			jQuery(sourceall).attr("disabled","disabled");

			jQuery.getJSON(link, params, function(json) {
				var DoCommit = json.data.docommit[0];
	
				if(DoCommit == 1) {
					var msg = (flag == 0) ? "data-removemessage" : "data-addmessage";
					var cls = (flag == 0) ? "ai-realtor-box_listing_wishlist_remove" : "ai-realtor-box_listing_wishlist_add";
					var wsh = (flag == 0) ? 1 : 0;

					// Globally modify all wishlist buttons assigned to that property.
					jQuery(sourceall).each(function() {
						jQuery(this).attr("data-wishlist",wsh)
							.removeClass("ai-realtor-box_listing_wishlist_remove")
							.removeClass("ai-realtor-box_listing_wishlist_add")
							.addClass(cls);

						if(jQuery(this).attr(msg) != "undefined" && jQuery(this).attr(msg) != "") {
							jQuery(this).text(jQuery(this).attr(msg));
						}
					});

					if(isRemote == 1) {
						// If remote, reload all wishlist sumary boxes via mySnipApps.
						mySnipApps.restoreTo("realtor","box_wishlist");
					} else {
						// otherwise, reload via ajax
						jQuery("div[name='realtor_box_wishlist']").html("<DIV ALIGN='center' STYLE='padding-top:55px'><IMG SRC='/common/images/loader_grey.gif' BORDER='0'></DIV>").load("http://" + CGI["http_host"] + "/templates/realtor/_remote/wishlist.cfm/mode/summary");
					}
				} else {
					alert("There was an error completing this operation. If this occurs frequently, please contact the site administrator.");
				}

				jQuery(sourceall).attr("disabled","");
			},"json");
		})
		.attr("data-activated",1);
});
