// JScript File
// requires jquery.js

var popUp = {

    proxy: "",
    rootUrl: "",

    show: function(url) {
        var dialogUrl = (this.rootUrl != "") ? (this.rootUrl + url) : url;
        dialogUrl = (this.proxy != "") ? (this.proxy + this.urlEncode(dialogUrl)) : dialogUrl;
        $('#dialogContainer').html("<div id='dialogContent' style='display: inline; z-index: 3000; position: absolute'></div>" +
					"<div id='dialogVeil' style='width: 100%; height: 100%; display: block; background-color: #333333; position: fixed; top: 0px; left: 0px; z-index: 2999; opacity: .50; filter: alpha(opacity=50)'></div>");
        $('#dialogContent').load(dialogUrl, null, function() {
					$('#dialogContainer').show();
					popUp.center($("#dialogContent"));
					popUp.posVeil($("#dialogVeil"));
        });
    },
    
    hide: function() {
        $('#dialogContainer').html("");
        $('#dialogContainer').hide();
    },

    isShow: function() {
      return $("#dialogContainer").css("display") == "block";
    },
    
    center: function(elmt) {
			elmt.css("position", "absolute");
			var topPx = ($(window).height() - elmt.height()) / 2 + $(window).scrollTop();
			if (topPx < 0) topPx = 0;
			elmt.css("top", topPx + "px");
			elmt.css("left", ($(window).width() - elmt.width()) / 2 + $(window).scrollLeft() + "px");
    },
    
    posVeil: function(elmt) {
			var height, width;
			if ($.browser.msie) {
					height = Math.max(document.body.scrollHeight, document.body.clientHeight, document.body.offsetHeight);
					width = document.body.scrollWidth;
			}
			else {
					height = document.body.parentNode.scrollHeight;
					width = document.body.parentNode.scrollWidth;
			}
			elmt.css("height", height + "px");
			elmt.css("width", width + "px");
    },
    
    msg: "",
    nextStep: "",
    steps: "",

    doMsg: function(msg, nextStep) {
        this.msg = msg;
        this.nextStep = nextStep;
        this.show('/Dialogs/Message.html');
    },

    addMsg: function(msg, nextStep) {
        this.msg += msg;
        this.nextStep = nextStep;
        this.show('/Dialogs/Message.html');
    },

    login: function(params) {
        this.show('/Dialogs/Login.aspx?uid=' + this.getUid() + params);
    },

    signUp: function(params) {
        this.show('/Dialogs/SignUp.aspx?uid=' + this.getUid() + "&rid=" + $("#rid").val() + "&mlsid=" + $("#mlsid").val() +
			params);
    },
    
    forgotPassword: function(closable) {
				//alert("closable: " + closable);
				this.show("/Dialogs/ForgotPassword.aspx?nc=" + (closable ? "0" : "1"));
    },

    loginIframe: function(uid, email, password) {
        if ($.browser.msie) {
            if (document.frames['publicIFrame'])
                document.frames['publicIFrame'].location.href = this.rootUrl + "/LoginIframe.aspx?uid=" + uid +
					"&email=" + email + "&passwd=" + password;
        } else {
        }
    },

    logoutIframe: function(uid) {
        if ($.browser.msie) {
            if (document.frames['publicIFrame'])
                document.frames['publicIFrame'].location.href = this.rootUrl + "/LogoutIframe.aspx?uid=" + uid;
        } else {
        }
    },

    saveItem: function(mlsNums, refresh) {
        if ($("#leadid").val() == "0") {
            this.signUp("&nextop=saveitem&mlsnums=" + mlsNums);
            return;
        }
        if (refresh)
            idxsSvc.saveItems($("#leadid").val(), mlsNums, "popUp.saveItemCBRefresh");
        else
            idxsSvc.saveItems($("#leadid").val(), mlsNums, "popUp.saveItemCB");
    },

    saveItemCssClass: "",
    saveItemLabel: "Save this Property",
    removeItemCssClass: "",
    removeItemLabel: "Remove Saved Property",

    saveItemCB: function(args) {
        var listings = args.result.listings;
        if (listings.length > 0) {
            $("#savedItemsCount").html(parseInt($("#savedItemsCount").html()) + listings.length);
            if (typeof getLead == 'function') {
                getLead();
            }
            for (i = 0; i < listings.length; i++) {
                $("#saveItem_" + listings[i].mlsTableID + "___" + listings[i].mlsNum).text(this.removeItemLabel);
                $("#saveItem_" + listings[i].mlsTableID + "___" + listings[i].mlsNum).attr("class", this.removeItemCssClass);
                $("#saveItem_" + listings[i].mlsTableID + "___" + listings[i].mlsNum).attr("href", "javascript:popUp.removeItem('" + listings[i].mlsTableID + "___" + listings[i].mlsNum + "')");
            }
        }
        this.steps += ',saveitem';
        this.doMsg(args.result.msg, "");
    },

    saveItemCBRefresh: function(args) {
        this.steps += ',saveitem';
        this.addMsg(args.result.msg, "window.location.reload()");
    },

    removeItem: function(mlsNums, refresh) {
        if ($("#leadid").val() == "0") {
            this.signUp("&nextop=removeitem&mlsnums=" + mlsNums);
            return;
        }
        if (refresh)
            idxsSvc.removeItems($("#leadid").val(), mlsNums, "popUp.removeItemCBRefresh");
        else
            idxsSvc.removeItems($("#leadid").val(), mlsNums, "popUp.removeItemCB");
    },

    removeItemCB: function(args) {
        var listings = args.result.listings;
        if (listings.length > 0) {
            $("#savedItemsCount").html(parseInt($("#savedItemsCount").html()) - listings.length);
            if (typeof getLead == 'function') {
                getLead();
            }
            for (i = 0; i < listings.length; i++) {
                $("#saveItem_" + listings[i].mlsTableID + "___" + listings[i].mlsNum).text(this.saveItemLabel);
                $("#saveItem_" + listings[i].mlsTableID + "___" + listings[i].mlsNum).attr("class", this.saveItemCssClass);
                $("#saveItem_" + listings[i].mlsTableID + "___" + listings[i].mlsNum).attr("href", "javascript:popUp.saveItem('" + listings[i].mlsTableID + "___" + listings[i].mlsNum + "')");
            }
        }
        this.steps += ',removeitem';
        this.doMsg(listings.length + " property(s) removed.", "");
    },

    removeItemCBRefresh: function(args) {
        var listings = args.result.listings;
        this.steps += ',removeitem';
        this.addMsg(listings.length + " property(s) removed.", "window.location.reload()");
    },
    
    emailListings: function(op, mlsNums) {
			this.show("/Dialogs/EmailListings.aspx?op=" + op + "&sid=" + this.getUid() + "&rid=" + $("#rid").val() + 
				"&leadid=" + $("#leadid").val() + "&segmentid=" + $("#segmentid").val() + "&mlsnums=" + mlsNums);
    },
    
    urlEncode: function(str) {
        return escape(str).replace("/", "%2F").replace("+", "%2B");
    },

    getUid: function() {
      if ($("#sid").length) return $("#sid").val();
      else return $("#uid").val();
    },
    
    isLoggedIn: function() {
			return $("#leadid").val() != "0";
    }

}