


$(document).ready(function(){
	
	$(document).pngFix();
	
	
	// header animations
	$("div.anim").each(function(){	
		var indx = $(this).index();
		var speed_rdm = Math.floor(Math.random()*401);
		var delay_rdm = Math.floor(Math.random()*3001);
		var timeout_rdm = Math.floor(Math.random()*4001);
		$(this).fadeIn(speed_rdm);
		$(this).delay( 2000+delay_rdm ).cycle({
			speed: speed_rdm+200,
			timeout:2000+timeout_rdm
		});
	});

	// gallery slideshow rollovers
	//$("a.gallery-thumb").fadeTo('slow',0.5);
	$("a.gallery-thumb").hover(function(){
		$(this).fadeTo('slow',0.3);
	},function(){
		$(this).fadeTo('slow',1);
	});
	
	// gallery fancybox
	if($("a.gallery-thumb").length) {
	$("a.gallery-thumb").fancybox({
		'titleShow'		: false,
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic'
	});
	}

	
	// scrollable floorplans
	$("#floorplan-items").scrollable({ circular: true, mousewheel: true }).navigator({
		// this ul object now functions as our navigator
		navi:'ul#floorplan-list',
		// select A tags inside the navigator to work as items (not direct children)
		naviItem: 'a',
		// assign "current" class name for the active A tag inside navigator
		activeClass: 'current',
		history:true
	});
	
	$("#footer-petfriendly strong").hover(function(){
		$("#petfriendly-details").toggle('fast');
	});
	
	// load XML for availability/sitemap data
    $.ajax({
        type: "GET",
        url: "/data/rebuilddata.php",  // for the rebuilt style
        dataType: "xml",
        success: xmlParserSitemap
    });
    
    // scrollable sitemap items
	$("#sitemap-images").scrollable({ 
		mousewheel: true,
		// hide any unit detail items when changing slides
		onBeforeSeek: function(e,s){
		   	$(".unit-data").hide("fast");
		   	return true;
		}
	}).navigator({
		// this ul object now functions as our navigator
		navi:'ul#sitemap-level-list',
		// select A tags inside the navigator to work as items (not direct children)
		naviItem: 'a',
		// assign "current" class name for the active A tag inside navigator
		activeClass: 'current',
		// enable history for clicks
		history:true
	});;

	
});


	// set maphilight defaults (no-show for empty items)
    hilightDefaults = "{ "+
		"fillColor: '000000',"+
		"fillOpacity: 0,"+
		"strokeColor: 'ff6666',"+
		"strokeOpacity: 0,"+
		"strokeWidth: 3,"+
		"neverOn: true,"+
		"}";
		
	highlightAvailabileYes = "{"+
		"strokeColor:'33ff66',"+
		"strokeOpacity: 1,"+
		"alwaysOn:true,"+
		"fillColor:'ffffff',"+
		"fillOpacity:0.0"+
		"}";
	highlightAvailabileNo = "{"+
		"strokeColor:'ff3333',"+
		"strokeOpacity: 1,"+
		"alwaysOn:true,"+
		"fillColor:'ff3333',"+
		"fillOpacity:0.2"+
		"}";


// needed for direct link to online leasing app
var siteID = "2330887";
var todaysDate=new Date();
dateNeeded = todaysDate.toLocaleDateString();

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

	var units = "";
	var unitsOnThisFloor = "";
	
	var unitLinks = "";
	
    
    $(xml).find("Floorplan").each(function () {
    
    	floorplanName = $(this).attr("name");
    	floorplanID = $(this).attr("id");
    	
    	// 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!";
    	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++;
							dateNeeded = todaysDate.toLocaleDateString();
							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="+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>";
    	
    	// add list to each unit
    	$("#floorplan-"+floorplanName).append(units);
    	
//    	$("#floorplan-C1").append(unitLinks);
    	
    	//alert(floorplanName);
    	
    });
    
}


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

    $('#loading-sitemap-data').hide("fast");
    
    $(xml).find("UnitGroup").each(function () {
    
    	suiteNum = $(this).attr("suite");
    	
    	// set availability styles of imagemap areas
		if ($(this).children("Unit[availability=yes]").length) {
			availability = "yes";
			$("area[rel="+suiteNum+"]").addClass(highlightAvailabileYes);
		} else {
			availability = "no";
			$("area[rel="+suiteNum+"]").addClass(highlightAvailabileNo);
		}
    	
    	title = $(xml).find("Floorplan[id="+$(this).attr("floorplanID")+"]").attr("name");
    	desc = "<p>"+ $(xml).find("Floorplan[id="+$(this).attr("floorplanID")+"]").attr("bedrooms") +" Bed";
    	desc += ", "+ $(xml).find("Floorplan[id="+$(this).attr("floorplanID")+"]").attr("bathrooms") +" Bath";
    	desc += ", "+ $(xml).find("Floorplan[id="+$(this).attr("floorplanID")+"]").attr("sqft") +" Sq. Ft.</p>";
    	
    	// determine availability of each individual unit and report
    	$(this).find("Unit").each(function() {
    		desc += "<p>";
    		if($(this).siblings().length>0) {
				desc += "Room "+ $(this).attr("room") +": ";
    		}
    		if($(this).attr("availability")=="no") {
    			if($(this).attr("userOption")=="applied") {
	    			desc += "Application on File.";
	    		} else {
	    			desc += "Occupied";
					if($(this).attr("availableDate")!="") {
		    			desc += ", Available on: "+ $(this).attr("availableDate");
					}
	    			desc += ".";
	    		}
    		} else {
    			desc += "<strong class='available'>Available Today!</strong>";
//	 			desc += " <a href='http://property.onesite.realpage.com/ol/default.aspx?siteID="+siteID+"&unitId="+$(this).attr("unitID")+"&dateneeded="+encodeURI(dateNeeded)+"' target='_blank'>Lease Online</a>";
    		}
    		desc += "</p>";
    	});
    	
    	// add individual <div> node to DOM
        $("#sitemap-data").append('<div class="unit-data available-'+availability+'" id="unit-'+suiteNum+'"><h4>' + suiteNum + ' <em>(' + title + ')</em></h4><div class="description">' + desc + '</div><a href="#" class="close">Close</a></div>');
    	
    });
    
    // get the map's offset, so we can show the details on the map
    
    // define actions for imagemap areas
    // note that the "rel" attribute contains the suite number for that area
    $('area').each(function(i){
        $(this).click(function (e) {
		    mapOffset = $("#sitemap-data").offset();
        	newY = e.pageY-(mapOffset.top)+15;
        	newX = e.pageX-(mapOffset.left)+15;
	    	$(".unit-data").hide("fast");
	    	$("#unit-"+$(this).attr("rel")).show("fast").css({top:(newY)+"px",left:(newX)+"px"});
	    	return false;
        });
    });
    
    // activate close button for each unit-data box
	$(".unit-data a.close").click(function(){
		$(this).parent().hide("fast");
		return false;
	});
    
    // trigger map hilights for each level's sitemap
    $(".sitemap-image").maphilight(hilightDefaults);

}

/****
 *
 *	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;
}
 

function hideUnitData(e){
	alert('x');
   	$(".unit-data").hide("fast");
}

function zeroPad(num, places) {
  var zero = places - num.toString().length + 1;
  return Array(+(zero > 0 && zero)).join("0") + num;
}

function getCounterNumbers(){
	$("#counterNumbers li").html("<img src='http://villasonguadalupe.com/wp-content/themes/blankSlate3.0/img/counter_nbr_blr.png'>");
	$.ajax({
		url:"/counter/getCount.php?s=fd",
		dataType: "json",
		success:function(data){
			var theCountPadded = zeroPad(data.count, 3);
			$("#counterNumbers li:nth-child(1)").html(theCountPadded.substring(0,1));
			$("#counterNumbers li:nth-child(2)").html(theCountPadded.substring(1,2));
			$("#counterNumbers li:nth-child(3)").html(theCountPadded.substring(2,3));
			$("#counterDate").html("(As of " + data.date + ")");
		}
	});
}

