
// wrapper for jQuery compatibility
(function ($) {


$(document).ready(function(){
	
	// load XML for availability/sitemap data
	if($(".shadow-plan").length>0){
	//alert("xml");
    $.ajax({
        type: "GET",
        url: "/data/rebuilddata.php",  // for the rebuilt style
        dataType: "xml",
        success: xmlParserFloorplans
    });
    }
    
});



// needed for direct link to online leasing app
var siteID = "1093651";
var todaysDate=new Date();
//dateNeeded = todaysDate.toLocaleDateString();
dateNeeded = todaysDate.getMonth()+1+"/"+todaysDate.getDate()+"/"+todaysDate.getFullYear();

/*******************************
*
*	function xmlParserFloorplans(xml)
*		receives an XML object and creates objects in the DOM
*
*******************************/
function xmlParserFloorplans(xml) {

	var units = "";
	var unitsOnThisFloor = "";
	
	var unitLinks = "";
	
	// set date (may be different according to js library, or something...)
//	var dateNeeded = Date.today().toString("MM/dd/yyyy");
//	var dateNeeded = todaysDate.toLocaleDateString();
	var dateNeeded = todaysDate.getMonth()+1+"/"+todaysDate.getDate()+"/"+todaysDate.getFullYear();
    
    $(xml).find("Floorplan").each(function () {
    
    	floorplanName = $(this).attr("name").toLowerCase();
    	floorplanID = $(this).attr("id");
		unitCountTotal = 0;
    	
    	// show list of available units
    	units = "<form action='http://property.onesite.realpage.com/ol/default.aspx' target='_blank' method='GET'>";
    	units += "<input type='hidden' name='siteid' value='"+siteID+"' />";
  //  	units += "<input type='hidden' name='dateneeded' value='"+dateNeeded+"' />";
    	units += "<p>Select an available unit from the menu to lease it today!<br />";
    	units += "<select name='unitid'>";
  			for(f=1;f<10;f++) {
	  			unitCount = 0;
  				unitsOnThisFloor = "<optgroup label='"+getOrdinal(f)+" Floor'>";
				$(xml).find("UnitGroup[floorplanID="+floorplanID+"] Unit").each(function () {
				
					// check to see if we're looking at the proper floor level
					if($(this).attr("floorLevel")==f) {
				
						thisUnitAvailable = false;
						
						// determine unit availability
						if($(this).attr("availability")=="no") {
							if($(this).attr("userOption")!="applied") {
								if($(this).attr("availableDate")!="") {
									thisUnitAvailable = true;
									availableDate = $(this).attr("availableDate");
								}
							}
						} else {
							thisUnitAvailable = true;
							availableDate = "Today";
						}
						
						// if it's available, figure what will be shown
						if(thisUnitAvailable) { 
						
							unitCount++;
							unitCountTotal++;
							// figure date needed (may change depending on js libary or something)
//							dateNeeded = Date.today().toString("MM/dd/yyyy");
//							dateNeeded = todaysDate.toLocaleDateString();
							dateNeeded = todaysDate.getMonth()+1+"/"+todaysDate.getDate()+"/"+todaysDate.getFullYear();
							thisUnit = "" + getOrdinal($(this).attr("floorLevel"))+" Floor, Apt #"+$(this).attr("suite")+" &bull; $"+$(this).attr("rentPrice") + "/mo.";
							if ($(this).attr("availableDate")) {
								thisUnit += " (Available "+$(this).attr("availableDate")+")";
							}
							unitLinks += "<br /><a href='http://property.onesite.realpage.com/ol/default.aspx?siteID="+siteID+"&unitId="+$(this).attr("unitID")+"&dateneeded="+encodeURI(dateNeeded)+"'>"+thisUnit+"</a>";
							thisUnit = "<option value='"+$(this).attr("unitID")+"'>"+thisUnit+"</option>";
						
							unitsOnThisFloor += thisUnit + "\n";
						}
					
					}
				});
    			unitsOnThisFloor += "</optgroup>";
    			if(unitCount>0) {
    				units += unitsOnThisFloor;
    			}
    		}
    	units += "</select>";
    	
	   	units += "</p>";
	   	units += "Desired Move-in Date <input type='text' value='"+dateNeeded+"' name='dateneeded' style='width:100px;' /> ";
	   	units += "<input type='submit' value='Lease Today' />";
	   	units += "</form>";
	   	
	   	if(unitCountTotal==0) {
	   		units = "<p><em>Sorry, no apartments available right now with this floor plan.</em></p>";
	   	}
    	
    	// add list to each unit
    	$("#"+floorplanName+" .option-title").append(units);
    	
    	
    	//alert(floorplanName);
    	
    });
    
}



/****
 *
 *	function getOrdinal(num)
 *
 ****/
function getOrdinal(num) {
	var temp = "";
	switch(num){
		case 1 :
		case '1' : temp = "1st";
				break;
		case 2 :
		case '2' : temp = "2nd";
				break;
		case 3 :
		case '3' : temp = "3rd";
				break;
		default : temp = num+"th";
				break;
	}
	return temp;
}

// end wrapper
})(jQuery);
 
