/**
 * 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 Thu Nov  8 08:13:51 EST 2007
 */
var FCEAptDataRetriever = {

    /**
     * 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.
     */
    fetchAptListings: function(beds, avail_only) {

      window.eval("FCEAptDataRetriever.aptList = " + GetAptList()); //get all data

        if (this.aptList) 
        {
            // filter out unavailable apts using Array.splice()
                for (var i=this.aptList.length-1; i>=0; i--)
                 {
					if (beds == 1 )
						{
						if ((!this.aptList[i].avail && avail_only) ||
							(this.aptList[i].bed != beds && this.aptList[i].bed != 0 && beds != 'penthouse') ||
							(this.aptList[i]['floor'] != '17' && beds == 'penthouse'))
						{
							this.aptList.splice(i, 1);
						}
						}
					else
					{
						if ((!this.aptList[i].avail && avail_only) ||
							(this.aptList[i].bed != beds && beds != 'penthouse') ||
							(this.aptList[i]['floor'] != '17' && beds == 'penthouse'))
						{
							this.aptList.splice(i, 1);
						}
					}
                }
				return this.aptList;
        }
                
    },



    /**
     * 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) {
        for (var aptkey in FCE_DATA_DUMMY) {
            var apt = FCE_DATA_DUMMY[aptkey];
            if (apt.floor == floor && apt.aptnum == aptnum) {
                return { "aptkey":aptkey,
                         "aptnum":FCE_DATA_DUMMY[aptkey].aptnum,
                         "floor":FCE_DATA_DUMMY[aptkey].floor,
                         "hdr":FCE_DATA_DUMMY[aptkey].shortHeader,
                         "lines":FCE_DATA_DUMMY[aptkey].infoLines
                       };
            }
        }
    },


    /**
     * 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 (FCE_DATA_DUMMY[aptkey])
            return { "aptkey":aptkey,
                     "aptnum":FCE_DATA_DUMMY[aptkey].aptnum,
                     "floor":FCE_DATA_DUMMY[aptkey].floor,
                     "hdr":FCE_DATA_DUMMY[aptkey].shortHeader,
                     "lines":FCE_DATA_DUMMY[aptkey].infoLines
                   };
    },


   /**
    * 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 (FCE_DATA_DUMMY[apt]) return FCE_DATA_DUMMY[apt];
    },

    /**
     * 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 FCE_DATA_DUMMY) {
            if (FCE_DATA_DUMMY[i].floor == floor) return FCE_DATA_DUMMY[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) {
		eval('var imgs = ' + GetImageList(FCE_DATA_DUMMY[aptkey].fpid));
		if (large) {
            return imgs.largePic;
        }
        else {
            return imgs.smallPic;
        }
    }



};
FCEAptDataRetriever.aptList = new Object();




/**
 * We're using the developers FCE_DATA_DUMMY var since his functions are wired up to it
 */
window.eval("var FCE_DATA_DUMMY = " + GetMITSFromCache());

function createXMLHttpRequest() {
    if (typeof XMLHttpRequest != "undefined") {
        return new XMLHttpRequest();
    } else if (typeof ActiveXObject != "undefined") {
        return new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        throw new Error("XMLHttpRequest not supported");
    }
}

function GetWFolder()
{
	var w = location.search;
	var re = /w\=([^&]*)/;
	if (re.test(w))
	{
		var res = w.match(re);
		if (typeof(res.length) != 'undefined' && res.length > 1)
			return res[1];
	}
	return 'creekside';
}

function GetMITSFromCache()
{
    var o_MITSxmlhttp,s_MITSxmlhttpresponsetext,MITSurl, o_MITSxmldom

    MITSurl = "carljson.asp?w=" + GetWFolder();

    o_MITSxmlhttp=createXMLHttpRequest();

        with(o_MITSxmlhttp)
        {
          open("POST",MITSurl,false);
          send("");
        }
        s_MITSxmlhttpresponsetext = o_MITSxmlhttp.responseText;

    return s_MITSxmlhttpresponsetext;
 }

function GetAptList()
{
    var o_MITSxmlhttp,s_MITSxmlhttpresponsetext,MITSurl, o_MITSxmldom

    MITSurl = "carljson.asp?w=" + GetWFolder() + "&list=1"

    o_MITSxmlhttp=createXMLHttpRequest();

        with(o_MITSxmlhttp)
        {
          open("POST",MITSurl,false);
          send("");
        }
        s_MITSxmlhttpresponsetext = o_MITSxmlhttp.responseText;

    return s_MITSxmlhttpresponsetext;
 }

function GetImageList(fpid)
{
    var o_MITSxmlhttp,s_MITSxmlhttpresponsetext,MITSurl, o_MITSxmldom

    MITSurl = "heatherjson.asp?w=" + GetWFolder() + "&fpid=" + fpid;

    o_MITSxmlhttp=createXMLHttpRequest();

        with(o_MITSxmlhttp)
        {
          open("POST",MITSurl,false);
          send("");
        }
        s_MITSxmlhttpresponsetext = o_MITSxmlhttp.responseText;

    return s_MITSxmlhttpresponsetext;
 }
