var Listings=function(){var el;var init=function(){el=$('#listing-container');el.html('&nbsp;');el.append('<div class="loader"></div>');$('.loader').css('width','618px');getListingData(render);}
var getListingData=function(callback){$.ajax({type:"GET",url:"http://"+location.host+"/listing-service.php",dataType:"xml",success:function(xml){callback(xml);},error:function(){$('.loader').hide();el.html('<p>Listings are currently not available</p>');}});}
var render=function(xml){var listings=mapListings(xml);var id=getUrlListingId();$('.loader').hide();el.html('');el.css('display','none');if(id==-1){$('#title').html('Active Listings');$('#listingsTemplate').tmpl(listings).appendTo(el);}
else{var listing=listings[id];if(listing!=null){$('#title').html(listing.address1);$('#listingTemplate').tmpl(listing).appendTo(el);setTimeout(function(){$('#thumbs').galleriffic({imageContainerSel:'#slideshow',controlsContainerSel:'#controls',numThumbs:6,syncTransitions:true,defaultTransitionDuration:900,playLinkText:'Play Slideshow',delay:2000});},100);setTimeout(function(){var latlng=new google.maps.LatLng(listing.lat,listing.lon);var options={zoom:15,center:latlng,mapTypeId:google.maps.MapTypeId.ROADMAP};var map=new google.maps.Map(document.getElementById("map_canvas"),options);var marker=new google.maps.Marker({position:latlng,map:map,title:listing.address1});},200);}
else{el.html('&nbsp;');el.html('<p>This property is no longer available. <a href="/rental-listings/">View available listings</a> </p>');}}
el.slideDown('fast');}
var mapListings=function(xml){var propertyData=$(xml).find('Property');var listings=[];$(propertyData).each(function(){var listing=mapListing($(this)[0]);listings[listing.id]=listing;});return listings;}
var getUrlListingId=function()
{var vars=[],hash;var hashes=window.location.href.split('/');var value=hashes[hashes.length-1];if(value==""){value=hashes[hashes.length-2];}
var id=parseInt(value);if(isNaN(id)){id=-1;}
return id;}
var mapListing=function(xml){var property={};property.id=$(xml).find('ID').text();property.type=$(xml).find('PropertyType').text();property.sqfeet=$(xml).find('SquareFeet').text();property.bd=$(xml).find('Bedrooms').text();property.ba=parseFloat($(xml).find('Bathrooms').text()).toFixed(1);var tempDesc=$(xml).find('LongDescription').text();var sslen=97;property.desc=tempDesc.substring(sslen,tempDesc.length);property.pid=tempDesc.substring(sslen-38,sslen-1);property.address1=$(xml).find('Address1').text();property.address2=$(xml).find('Address2').text();property.zip=$(xml).find('PostalCode').text();property.state=$(xml).find('State').text();property.city=$(xml).find('City').text();property.lat=$(xml).find('Latitude').text();property.lon=$(xml).find('Longitude').text();property.price=$(xml).find('Price').text();property.deposit=$(xml).find('Deposit').text();property.headline=$(xml).find('Headline').text();property.videoUrl=$(xml).find('VirtualTourURL').text();property.showMap=$(xml).find('ShowMap').text();var tempAvail=$(xml).find('DateAvailable').text();if(tempAvail=="")
{tempAvail="Not Available";}else{var d1=newDate(tempAvail.substring(0,10));var now=new Date();if(d1<now){tempAvail="Available Now!";}
else{var month=d1.getMonth()+1;tempAvail="Available "+month+"/"+d1.getDate()+"/"+d1.getFullYear();}}
property.available=tempAvail;property.photos=[];$(xml).find('Photo').each(function(i,val){var url=$(val).find('URL').text();property.photos[i]=url;});return property;}
return{init:init}}();function trimText(value,len){if(value.length>len)
{value=value.substring(0,len);value+="...";}
return value;}
function newDate(str)
{str=str.split('-');var date=new Date();date.setFullYear(str[0],str[1]-1,str[2]);date.setHours(0,0,0,0);return date;}
