//Displays the detailed search results in the main search pages - communities

var commObjects = [];

function loadCommData() {
	var numComms = 0;
	var unitNumber = 0;
	commObjects = [];
	if (typeof(commsArray) == "undefined") return 0;
	for (commID in commsArray) {
		commObjects[unitNumber++] = new AseComm(commID, commsArray[commID]);
		numComms++;
	}

	var s = (numComms == 1) ? '' : 'ES';
	$("#num_search_results").text(numComms + ' MATCH' + s + ' TOTAL');
	return numComms;
}

function AseComm(id, data) {
	this.id = id;
	this.thumbWidth = 75;
	this.rent = data['rent'];
	this.renthigh = data['renthigh'];
	this.name = data['name'];
	this.beds = data['beds'];
	this.sqft = data['sqft'];
	this.sqfthigh = data['sqfthigh'];
	this.baths = data['baths'];
	this.bathshigh = data['bathshigh'];
	this.distance = parseFloat(data['d']);
	this.photoCount = data['commassets']['photo_count'];
	this.promo = data['promo'];
	this.frontJpeg = data['front_jpeg'];
	this.num_avail = data['num_avail'];
}

AseComm.prototype.display = function(template) {
	// make a list of bedroom types
	var numBeds = 0;
	var bedsStr = '';
	if (typeof(this.beds) == 'object') {
		var counter = 0;
		var hasNumber = false;
		for (var i in this.beds) numBeds++;
		
		for (var i in this.beds) {
			bedsStr += this.beds[i];
			if (typeof(this.beds[i]) == "number") hasNumber = true;
			if (counter < numBeds-1 && numBeds > 2) bedsStr += ', ';
			if (counter == numBeds - 2) bedsStr += ' and ';
			counter++;
		}
		if (hasNumber) bedsStr += ' Bedroom';
	} else {
		bedsStr = this.beds;
		if (typeof(this.beds) == "number" || this.beds.match(/[0-9]/) == this.beds) {
			bedsStr += ' Bedroom';
			if (this.beds > 1) bedsStr += 's';
		}
	}
	if (!onSeoPage) bedsStr += ' w/ ';
	
	var s = ((this.baths != this.bathshigh) || this.baths != '1') ? 's' : '';
	var bathsStr = (onSeoPage) ? '' : (this.baths == this.bathshigh ? this.baths : this.baths + '-' + this.bathshigh) + ' Bathroom' + s + '.';
	var distanceS = (this.distance == 1) ? '' : 's';
	
	var html = template;
	
	if(this.num_avail == 0) {
		var available_text = '';
	} else {
		if(homesMode) {
				var available_text = 'available for rent';
		} else {
				var available_text = this.num_avail + ' available for rent';
		}
	}
	html = html.replace('{available_text}', available_text);
	
	numericRent = (this.rent == 0 || this.rent == null);
	if (!numericRent) {
		html = html.replace('{per_month}', '<span class="rent-period">per month</span>');
	}
	else {
		html = html.replace('{per_month}', '');
	}
	
	html = html.replace('{front_jpeg}', '/files' + this.frontJpeg);
	
	if(homesMode)
	{
		html = html.replace('{display_text}', 'Home for Rent');
		html = html.replace('{apthome}', 'home');
	}
	else
	{
		html = html.replace('{display_text}', 'Apartment Community');
		html = html.replace('{apthome}', 'apartment');
	}
	
	var promo = (this.promo) ? '<div class="coupon-specials do-stuff-red"><a href="#" id="promo_' + this.id + '">View Coupon Special</a></div>' : '';
	
	html = html.replace('{promo}', promo);
	
	template = $(html).get(0);
	
	var assetBuffer = '<ul>';
	var assetsArray = commsArray[this.id]['commassets'];
	var assetTrans = {'rtavail':'Real Time Availability','reddot':'Red Dot Site Map','community_photos':'Professional Photos','site_maps':'Community Map', 'youtube':'Community Videos', 'fp3d':'3D Floor Plans','fp2dc':'Color Floor Plans','fpintdec':'Interior Decorator'
	};
	
	//loop through all the possible asset types (codename : class(now matches codename):longname)
	var visClass;
	for (var assetCode in assetTrans) {
		visClass = (assetsArray[assetCode] == 1) ? '' : ' empty';
		assetBuffer += '<li class="flare-' + assetCode + visClass + '"><a href="#">' + assetTrans[assetCode] + '</a></li>';
	}
	assetBuffer += '</ul>';
	
	if(homesMode)
	{
		linkto = '/homes/rentals/';
	}
	else
	{
		linkto = '/apartments/community/'
	}
	
	var data = {
		name_field : this.name,
		amount_field : (numericRent) ? 'Call for Details' : '<span class="dollar">$</span>' + (this.rent == this.renthigh ? this.rent : this.rent + '-<span class="dollar">$</span>' + this.renthigh),
		distance_field: Math.round(this.distance*10)/10 + ' mile' + distanceS,
		sqft_field : this.sqft == null ? '' : ( this.sqft == this.sqfthigh ? this.sqft : this.sqft + '-' + this.sqfthigh) + ' <span class="sqft">sq. ft.</span>',
		beds_field : bedsStr,
		baths_field: bathsStr,
		link_to_comm : linkto + this.id + '.html',
		jpeg_id : 'jpeg_' + this.id,
		'unit-result-flare' : assetBuffer,
		'photo-thumb-number' : this.photoCount
	}
	var html = pure.autoRender(template, data);
	html = html.replace('{name}', this.name);
	return html;
}