/**
 * 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 JSONOBJECT1 = null;  //temp JSON object.
var JSONOBJECT2 = null;  //temp JSON object.
var JSONOBJECT3 = null;

function convertJSONstr2Obj(json){
	var f = window.eval ("JSONOBJECT1 = " + json );
	return JSONOBJECT1;
}

var FCEAptDataRetriever = {
	aptList : null,
	FloorplanDetails : new Object(),	
	FCE_DATA : null,
	FloorplanPictureAssociations : new Object(),
	getFCE_Data : function() {
		/**
 		 * We're using the developers FCE_DATA_DUMMY var since his functions are wired up to it
		 */
		if(this.FCE_DATA == null) {
			//window.eval("var JSONOBJECT1 = " +GetMITSFromCache());  //get all data
			//this.FCE_DATA = JSONOBJECT1;
			this.FCE_DATA = convertJSONstr2Obj(GetMITSFromCache());
		}
		return this.FCE_DATA;
	},
    /**
     * 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,kind) {
	if(this.aptList == null) {
		//window.eval("var JSONOBJECT2 = " +GetAptList()); //get all data
		//this.aptList = JSONOBJECT2;
		this.aptList =  convertJSONstr2Obj(GetAptList());;
	}
	if (this.aptList) 
	{
		var clonelist = new Array();
		var clonelistIndex=0;
		// add to clonelist
		for (var i=this.aptList.length-1; i>=0; i--)
		{
	        if (avail_only && !this.aptList[i].avail) continue;
			if (!(beds == "ALL" || (beds!= "ALL" && this.aptList[i].bed == beds))) continue; 
            if (!(kind == "ALL" || (kind!= "ALL" && this.aptList[i].kind == kind))) continue;
            //made it past the filters, so we add.
			clonelist[clonelistIndex++] = this.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 FCE_DATA_DUMMY = this.getFCE_Data();			 
        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) {
	var FCE_DATA_DUMMY = this.getFCE_Data();		   
        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) {
	var FCE_DATA_DUMMY = this.getFCE_Data();
        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) {
	var FCE_DATA_DUMMY = this.getFCE_Data();
        for (var i in FCE_DATA_DUMMY) {
            if (FCE_DATA_DUMMY[i].floor == floor) return FCE_DATA_DUMMY[i].aptnum;
        }
    },

    fetchImgs : function(floorplanid) {
	var imgs;
	if(this.FloorplanPictureAssociations[floorplanid] == null) {
		imgs =  convertJSONstr2Obj(GetImageList(floorplanid));
		this.FloorplanPictureAssociations[floorplanid] = imgs;
	}
	else{
		imgs = this.FloorplanPictureAssociations[floorplanid];
	}
	return imgs;

    },

    /**
     * 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 FCE_DATA_DUMMY = this.getFCE_Data();
	var imgs = this.fetchImgs(FCE_DATA_DUMMY[aptkey].fpid);
	if (large) {
            return imgs.largePic;
        }
        else {
            return imgs.smallPic;
        }
    },
	
	/**
     * get the name and path to the apartment Plan Zoom image for the apartment
     * see fetchAptPlanImg
     *
     */
	fetchAptPlanZoomImg : function(aptkey, large) {
		var FCE_DATA_DUMMY = this.getFCE_Data();
		var imgs = this.fetchImgs(FCE_DATA_DUMMY[aptkey].fpid);
		return imgs.zoomPic;
	},
	
	/**
	 *
	 *
	 */
	fetchFloorplanDetails: function(floorplanid) {
		var responsetext;
		if(this.FloorplanDetails[floorplanid] == null) {
			var FloorplanDetailsURL = "floorplanDetails.asp?w=" + GetWFolder() + "&flpid=" + floorplanid;
			responsetext = SimpleGetData(FloorplanDetailsURL);
			
			this.FloorplanDetails[floorplanid] = responsetext;
		}
		else {
			responsetext = this.FloorplanDetails[floorplanid];
		}		
		return responsetext;
    },
    fetchFloorplanPointerInfo : function() {
	var responseobj =  convertJSONstr2Obj(GetFloorplanPointerInfo());
	return responseobj;
    }
			       

};
//FCEAptDataRetriever.aptList = new Object();
//FCEAptDataRetriever.FloorplanDetails = new Object();





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 SimpleGetData(url){
    //synchronous ajax call (hence why we call it simple) 
    var o_MITSxmlhttp,s_MITSxmlhttpresponsetext,MITSurl, o_MITSxmldom

    MITSurl =url;

    o_MITSxmlhttp=createXMLHttpRequest();

        with(o_MITSxmlhttp)
        {
          open("POST",MITSurl,false);
          send("");
        }
        s_MITSxmlhttpresponsetext = o_MITSxmlhttp.responseText;

    return s_MITSxmlhttpresponsetext;
}

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 url;
    url = "carljson.asp?w=" + GetWFolder();
    return SimpleGetData(url);
 }

function GetAptList()
{
    var url;
    url  = "carljson.asp?w=" + GetWFolder() + "&list=1";
    return SimpleGetData(url);
 }

function GetImageList(fpid)
{
    var url;
    url  = "heatherjson.asp?w=" + GetWFolder() + "&fpid=" + fpid;
    return SimpleGetData(url);
}
function GetFloorplanPointerInfo() {
    var url;
    url = "FloorplanPointerJSON.asp";
    return SimpleGetData(url);
} 

