/**
 * stub dataAccessor object that knows how to get the various types of data
 * needed for FCE Apartment browse and search functionality. Each method
 * returns a subset of data organized as arrays and/or custom Objects.
 *
 *
 * @todo replace all static JSON with in-line JSON objects or XMLHttpRequests
 * as needed to acces live data.
 *
 * @author sbeam
 * @date Wed Nov  7 14:19:50 EST 2007
 * Altered by Real Page
 */
var FCEAptDataRetriever = {
    aptCategoryLists: null,
    FloorplanDetails: null,
    FCE_Data: null,
    /**
     * get all apartment listings, indexed by bedrooms. This is called from
     * apartment_search.html when the user clicks on the '1 bed', '2 bed'
     * buttons, etc. The data will be consumed in FCEAptSearch object which
     * will dynamically create a DOM table to display the results as needed.
     *
     * @param beds str, ie "1" "2" or "penthouse"
     * @param avail_only bool limit results to available apartments only.
     * @return Object arrayListObject - Array of simple generic Objects, each with the following properties:
     *           { "aptkey":string, // apartment ID
     *             "plan":string,   // plan name
     *             "bath":string,   // #baths
     *             "bed":string,    // #beds
     *             "sqft":string,   // square footage
     *             "floor":string,  // floor#
     *             "rent":string,   // rent
     *             "avail":boolean  // if true, apt is available for rent.
     *           }
     *
     * @todo replace with whatever is needed to access live data.
     */
    setup: function(data) {
        this.FCE_DATA = data.ApartmentDetailsByUnitNumber;
        this.aptCategoryLists = data.ApartmentsByCategory;
        this.FloorplanDetails = data.FloorplanDetailsByFloorplanID;
    },
    fetchAptListings: function(beds, avail_only) {
        var aptList;
        var BedsS = beds.toUpperCase();
        var x = this.aptCategoryLists["k" + BedsS];
        if (x != null) {
            aptList = this.aptCategoryLists["k" + BedsS].Apartments;
        } else {
            aptList = new Array();
        }
        var clonelist = new Array();
        var clonelistIndex = 0;
        for (var i = aptList.length - 1; i >= 0; i--) {
            if (avail_only && !aptList[i].avail) continue;
            clonelist[clonelistIndex++] = aptList[i];
        }
        return clonelist;
    },



    /** 
    * content for aptInfoDialogs in apt-browse - this is the small
    * pointer/popup that shows when user hovers over the apartment on the
    * floor plate. We pass the current floor# and the current apartment# as
    * arguments.
    *
    * @param floor str floor to get info for, ie "11"
    * @param apt str apt# on that floor, ie "24"
    * @return false if not found or Object: { aptkey:101, 
    *                                         floor:3, 
    *                                         aptnum:27, 
    *                                         hdr:"327 Debussy", 
    *                                         lines:["1 Bedroom", "1 Bath", "$1400/month"] }
    *
    * @todo replace with whatever is needed to access live data.
    */
    fetchAptInfoByFloor: function(floor, aptnum) {
        var isLeased = false;
        for (var aptkey in this.FCE_DATA) {
            var apt = this.FCE_DATA[aptkey];
            if (apt.floor == floor && apt.aptnum == aptnum) {
                return { "aptkey": aptkey,
                    "aptnum": this.FCE_DATA[aptkey].aptnum,
                    "floor": this.FCE_DATA[aptkey].floor,
                    "hdr": this.FCE_DATA[aptkey].shortHeader,
                    "lines": this.FCE_DATA[aptkey].infoLines,
                    "avail": this.FCE_DATA[aptkey].isLeased
                };
            }
        }
    },


    /**
    * get info object corresponding to the identified apartment, using only
    * the "aptkey" - since this is the same as floor+aptnum, then it should
    * return the same as the above function, given the same floor/apt#. For
    * instance, this may be passed just "1124".
    *
    * @param aptkey string, ie "1124"
    * @return false if not found or Object: { aptkey:101, 
    *                                         floor:3, 
    *                                         aptnum:27, 
    *                                         hdr:"327 Debussy", 
    *                                         lines:["1 Bedroom", "1 Bath", "$1400/month"] }
    *
    * @todo replace with whatever is needed to access live data.
    */
    fetchAptInfo: function(aptkey) {
        if (this.FCE_DATA[aptkey])
            return { "aptkey": aptkey,
                "aptnum": this.FCE_DATA[aptkey].aptnum,
                "floor": this.FCE_DATA[aptkey].floor,
                "hdr": this.FCE_DATA[aptkey].shortHeader,
                "lines": this.FCE_DATA[aptkey].infoLines
            };
    },

    fetchFloorplanDetails: function(fpidkey) {
        if (this.FloorplanDetails[fpidkey]) return this.FloorplanDetails[fpidkey];
    },
    /** 
    * description and listing, by aptkey (id). This should return the full
    * details on the apartment identified by aptkey
    *
    * @param apt str apartment id (aptkey)
    * @return object {
    *                  "aptnum":int,        // the apartment number (without the floor), ie "27"
    *                  "floor":int,         // the floor this apt is on, ie "11"
    *                  "shortHeader":string // the 'header' for the display, ie "1127 Debussy - 3 bedroom"
    *                  "infoLines":Array    // list of strings, for the popup/pointer widget, ie ["1 bed", "2 bath", "$2300"]
    *                  "description":string // the long description
    *                  "detailedList":Array // list of strings, for the apt detail display, ie ["1 bed", "2 bath", "1155 SQft.", "$2300/month", "Available"]
    *                }
    *
    * @todo replace with whatever is needed to access live data.
    */
    fetchAptDetails: function(apt) {
        if (this.FCE_DATA[apt]) {
            var theAptData = this.FCE_DATA[apt]
            var floorplanData = this.fetchFloorplanDetails(theAptData.FloorplanIDKey);
            return {
                "aptnum": theAptData.aptnum,
                "floor": theAptData.floor,
                "shortHeader": theAptData.shortHeader,
                "infoLines": theAptData.infoLines,
                "description": floorplanData.description,
                "detailedList": theAptData.detailedList,
                "normalFPImage": floorplanData.normalFPImage,
                "largeFPImage": floorplanData.largeFPImage
            };
        }
    },

    /**
    * get the number of the first available aparment on the identified floor.
    * This is so that when the search/browse page first loads, we can begin
    * by showing the details of some apartment
    *
    * @param floor int the floor #, ie "11"
    * @return int the number of the first apartment we have on it, ie, "27"
    *
    * @todo replace with whatever is needed to access live data.
    */
    fetchFirstAptNum: function(floor) {
        for (var i in this.FCE_DATA) {
            if (this.FCE_DATA[i].floor == floor) return this.FCE_DATA[i].aptnum;
        }
    },


    /**
    * get the name and path to the apartment Plan image for the apartment
    * identified by aptkey
    *
    * @param aptkey str the apartment key/number, ie "1127"
    * @param large bool if true, get the zoom version
    * @return string url/path to image
    *
    * @todo replace with whatever is needed to access live data.
    */
    fetchAptPlanImg: function(aptkey, large) {
        var theAptData = this.FCE_DATA[aptkey]
        var floorplanData = this.fetchFloorplanDetails(theAptData.FloorplanIDKey);
        return (large) ? floorplanData.largeFPImage : floorplanData.normalFPImage;
        
    }



};

