﻿// JScript File
// requires jquery.js, jquery.modal.js, jquery.cookie.js, popUp.js, IDXSService.js

$(document).ready(function () {
	popUp.saveItemCssClass = "results_button2";
	popUp.removeItemCssClass = "results_button2_saved";
	if ($("#featuredListings").length && $.cookie('Superlative.Atlas.Public.IDXMini.FeaturedProperties') == "0")
		idxMini.toggleFeaturedProperties();
});

var idxMini = {

	ynSignUpCompare: false,
	ynSignUpViewOnMap: false,
	ynSignUpSlideshowMedium: false,
	ynSignUpVirtualTourMedium: false,
    ynSignUpEmailToFriend: false,

    doPage: function (pageNumber) {
		$("#page").val(pageNumber);
		this.postBack();
	},

    postBack: function () {
		postBackUrl = $("#postBackParams").val();
        postBackUrl = postBackUrl.replace("__page__", $("#page").val()).replace("__psize__", $("#pagesize").val())
			.replace("__sorting__", $("#lcSort").length ? $("#lcSort").val() : "").replace("__view__", $("#view").val());
		location.href = postBackUrl;
	},

    toggleFeaturedProperties: function () {
		if (!$("#featuredListings").length) return;
		$("#featuredListings").toggle();
		if ($("#featurePropertiesButton").attr("class") == "featured_title_button_minus") {
			$("#featurePropertiesButton").attr("class", "featured_title_button_plus");
			$("#featuredContainer").attr("class", "featured_container_contracted");
			$("#featuredListings").attr("class", "featuredListings_hide");
			$("#featuredListings").attr("style", "display:none;");
			$.cookie('Superlative.Atlas.Public.IDXMini.FeaturedProperties', '0', { path: '/', expires: 10 });
		}
		else {
			$("#featurePropertiesButton").attr("class", "featured_title_button_minus");
			$("#featuredContainer").attr("class", "featured_container_expanded");
			$("#featuredListings").attr("class", "featuredListings");
			$.cookie('Superlative.Atlas.Public.IDXMini.FeaturedProperties', '1', { path: '/', expires: 10 });
		}
	},

    checkAll: function () {
		if ($("#allcheck").attr("checked"))
			$("input[type='checkbox'][name='mlsnums']").attr("checked", "checked");
		else
			$("input[type='checkbox'][name='mlsnums']").removeAttr("checked");
	},

    changeView: function (view) {
		if (view == $("#view").val()) return;
		$("#view").val(view);
        for (i = 0; i < 3; i++) {
			var src =	$("#imgView" + i).attr("src");
			if (parseInt(view) == i && src.indexOf("_selected.gif") == -1)
				$("#imgView" + i).attr("src", src.replace(".gif", "_selected.gif"));
			else if (src.indexOf("_selected.gif") != -1)
				$("#imgView" + i).attr("src", src.replace("_selected.gif", ".gif"));
		}
		//this.postBack();
	},

    saveSearchCriteria: function () {
        idxsSvc.setCriteria($("#leadid").val(), s_arrayutil.serializeForm('searchForm') + "&" + $("#serializedMoreFields").val(),
			"idxMini.saveSearchCriteriaCB");
	},

    saveSearchCriteriaCB: function (args) {
		if (args.result)
			this.saveSearch($("#segmentid").val(), $("#mlstableid").val());
	},

    saveSearch: function (segmentID, mlsTableID) {
		if ($("#leadid").val() == "0") {
			popUp.signUp("&nextop=savesearch&segmentid=" + segmentID + "&mlstableid=" + mlsTableID);
			return;
		}
		popUp.show("/Dialogs/SaveSearch.aspx?leadid=" + $("#leadid").val() + "&segmentid=" + segmentID + "&mlstableid=" + mlsTableID);
	},

    saveNewSearch: function (searchName, segmentID, mlsTableID, refresh) {
		if (refresh)
			idxsSvc.saveNewSearch($("#leadid").val(), searchName, segmentID, mlsTableID, "idxMini.saveNewSearchCBRefresh");
		else
			idxsSvc.saveNewSearch($("#leadid").val(), searchName, segmentID, mlsTableID, "idxMini.saveNewSearchCB");
	},

    saveNewSearchCB: function (args) {
		if (args.result) {
            $("#savedSearchesCount").html(parseInt($("#savedSearchesCount").html()) + 1);
            if (typeof getLead == 'function') {
				getLead();
			}
      popUp.steps += ',savesearch';
			popUp.doMsg("Your new search is saved.", "");
		}
		else {
			popUp.doMsg("The system failed to save your search. Please try again.", "");
		}
	},

    saveNewSearchCBRefresh: function (args) {
		if (args.result) {
            $("#savedSearchesCount").html(parseInt($("#savedSearchesCount").html()) + 1);
      popUp.steps += ',savesearch';
			popUp.doMsg("Your new search is saved.", "window.location.reload()");
		}
		else {
			popUp.doMsg("The system failed to save your search. Please try again.", "window.location.reload()");
		}
	},

    bulkAction: function (act, countChecked, mlsNum) {
		switch (act) {
			case "save":
				if (countChecked < 1) {
					alert("Please select one or more properties.");
					return;
				}
				popUp.saveItem(mlsNum, false);
				break;
			case "remove":
				if (!popUp.isLoggedIn()) {
					popUp.signUp("");
					return;
				}
				if (countChecked < 1) {
					alert("Please select one or more properties.");
					return;
				}
				popUp.removeItem(mlsNum, false);
				break;
			case "cma":
				if (this.ynSignUpCompare && !popUp.isLoggedIn()) {
					popUp.signUp("");
					return;
				}
				else if (countChecked < 2 || countChecked > 4) {
					alert("Please select two to four properties");
					return;
				}
                if ($("#postBackParams").length)
				popUp.show('/Dialogs/CMA.aspx?p=' + mlsNum + "&" + $("#postBackParams").val());
                else
                    popUp.show('/Dialogs/CMA.aspx?p=' + mlsNum + "&uid=" + $("#uid").val() + "&segmentid=" + $("#segmentid").val());
				break;
			case "map":
				if (!GBrowserIsCompatible()) {
					alert("Your Browser is not compatible with Google Map");
					return;
				}
				if (this.ynSignUpViewOnMap && !popUp.isLoggedIn()) {
					popUp.signUp("");
					return;
				}
				if (countChecked < 1) {
					alert("Please select one or more properties");
					return;
				}
				popUp.show('/Dialogs/ViewOnMap.aspx?p=' + mlsNum + "&" + $("#postBackParams").val());
				break;
			case "emailagent":
				if (!popUp.isLoggedIn()) {
					popUp.signUp("");
					return;
				}
				if (countChecked < 1) {
					alert("Please select one or more properties.");
					return;
				}
				popUp.emailListings("agent", mlsNum);
				break;
			case "emailfriend":
                if (this.ynSignUpEmailToFriend && !popUp.isLoggedIn()) {
                    popUp.signUp("");
                    return;
                }
				if (countChecked < 1) {
					alert("Please select one or more properties.");
					return;
				}
				popUp.emailListings("friend", mlsNum);
				break;
		}
	},

    doAction: function (ddlField, cbField) {
        var act = $("#" + ddlField).val();
        var countChecked = this.countChecked(cbField);
        var mlsNum = this.getChecked(cbField);
        $("#" + ddlField).val("-");
        this.bulkAction(act, countChecked, mlsNum);
    },

    getChecked: function (fieldName) {
		var ret = "";
        $("input[name='" + fieldName + "']:checked").each(function () {
			ret += $(this).val() + ",";
		});
        if (ret.length > 0) ret = ret.substring(0, ret.length - 1);
		return ret;
	},

    countChecked: function (fieldName) {
		return $("input[name='" + fieldName + "']:checked").length;
	}
}


