﻿
var divOfferLightBox;
function InitPage() {
    //getStates();

    if ($("#"+metroDDLID).val()=="@")
    {
        $("div.displayCount").hide();
        $("div.displayButton").hide();
    }
    if (isSearch) {
        $("div.displayCount").show();
        $("div.displayButton").show();
    }
    //events handler
    
    /*
    $("select:first").change(function(index) {
        var state = $(this);
        if (state.val() != "@") {
            $("#planCount").empty();
            getMetros(state.val(),"@");
        }
    });
    */

    //Initialize offer lightbox
    divOfferLightBox = new YAHOO.widget.Overlay("divOfferLightBox", { visible: false, width: "675px", height: "580px" });
    divOfferLightBox.render();

    $("a.lnkOffer").click(function() {
        var offerKey = $(this).attr("rel");
        OpenOfferOverlay(offerKey);
    });
    
    $("select").change(function(index) {
        var metro = $("#"+metroDDLID).val();
        var price = $("#" + ddPrice).val();
        var beds = $("#" + ddBeds).val();
        var baths = $("#" + ddBaths).val();
        var sqft = $("#" + ddSqFt).val();

        if (metro != "@") {
            $("tr.trCriteria input").val(metro);
            getPlansCount(metro, beds, baths, sqft, price);

        }
        else {
            $("div.displayCount").hide();
            $("div.displayButton").hide();
            $("div.displayRefine").show();
        }
    });
}

//functions for lightbox
function OpenOfferOverlay(offerKey) {
    Reposition_Curtain();

    var divCurtain = YAHOO.util.Dom.get("divCurtain");
    YAHOO.util.Dom.setStyle(divCurtain, "display", "block");

    divOfferLightBox.cfg.setProperty("height", "580px");

    divOfferLightBox.show();

    $("#ifrOffer").attr("src", lfSource + offerKey);

    Reposition_OfferOverlay();
}

var Reposition_OfferOverlay = function() {
    divOfferLightBox.center();
}

function CloseOfferOverlay() {
    var divCurtain = YAHOO.util.Dom.get("divCurtain");
    YAHOO.util.Dom.setStyle(divCurtain, "display", "none");

    divOfferLightBox.cfg.setProperty("height", "0px");

    divOfferLightBox.hide();
}

var Reposition_Curtain = function() {
    var divCurtain = YAHOO.util.Dom.get("divCurtain");

    var docHeight = YAHOO.util.Dom.getDocumentHeight();
    var docWidth = YAHOO.util.Dom.getDocumentWidth();

    divCurtain.style.width = docWidth + "px";
    divCurtain.style.height = docHeight + "px";
}

function getMetros(stateID,defaultValue) {
    var serviceUrl = "SearchHandler.ashx/";
    var proxy = new ServiceProxy(serviceUrl);

    //invoke service with processing callbacks
    proxy.invoke("getMetros",
                    { stateKey: stateID },
    //callback
                    function(metros) {
                        //loop through status list
                        var metroOption = null;
                        var defaultOption = $("select:eq(1) OPTION:first");
                        var metroList = $("select:eq(1)");
                        metroList.empty().append(defaultOption);

                        $.each(metros, function(index) {

                            //get current status type
                            var metro = this;

                            metroOption = new Option(metro.Name, metro.MetroAreaKey);

                            if ($.browser.msie) {
                                metroList[0].add(metroOption);
                            }
                            else {
                                metroList[0].add(metroOption, null);
                            }
                        }); //state loop

                        metroList.val(defaultValue);

                    },
                    onPageError, "POST", true
                    );
}

function getPlansCount(metroID, minBeds, minBaths, minSqFt, maxPrice) {
    if (minBeds == "@") minBeds = 0;
    if (minBaths == "@") minBaths = 0;
    if (minSqFt == "@") minSqFt = 0;
    if (maxPrice == "@") maxPrice = 99999999;
    var serviceUrl = "SearchHandler.ashx/";
    var proxy = new ServiceProxy(serviceUrl);

    //invoke service with processing callbacks
    proxy.invoke("getPlansCount",
                    { metroID: metroID,
                        minBeds: minBeds,
                        minBaths: minBaths,
                        minSqFt: minSqFt,
                        maxPrice: maxPrice
                    },
    //callback
                    function(homes) {
                    $("#"+planCount).text(homes.count);
                    $("div.displayCount").show();
                    if (homes.count == "0") {
                        $("div.displayButton").hide();
                        $("div.displayRefine").show();
                    }
                    else {
                        $("div.displayButton").show();
                        $("div.displayRefine").hide();
                    }
                    },
                    onPageError, "POST", true
                    );
}

//page error handler
function onPageError(error) {

    //ensure all loading panels are hidden
    //$hideallst();

    //show error message
    alert("Error occurred: " + error.Message);
}