var properties = new Array();
var loaded = false;

function Property () {
	this._id 		= null;
	this._name 		= null;
	this._town 		= null;
	this._href 		= null;
	this._alt 		= null;
	this._imgsrc 	= null;
	this._desc  	= null;
	this._cat	  	= null;
	
	this.getId = function () {
		return this._id;
	}
	
	this.getName = function () {
		return this._name;
	}

	this.getTown = function () {
		return this._town;
	}

	this.getHref = function () {
		return this._href;
	}
	
	this.getAlt = function () {
		return this._alt;
	}
	
	this.getImgsrc = function () {
		return this._imgsrc;
	}

	this.getDesc = function () {
		return this._desc;
	}
	
	this.getCat = function () {
		return this._cat;
	}
	
	this.loadData = function (d) {
		this._id 		= d.id;
		this._name 		= d.name;
		this._town 		= d.town;
		this._href 		= d.href;
		this._imgsrc 	= d.imgsrc;
		this._desc	 	= d.desc;
		this._cat	 	= d.cat;
	}
}

function doAnimation(data, display) { 
	if (display) {
		$(".homepage-property-image").animate({opacity: 0.06},1000 , function () {
			$(".homepage-property-title").find('.id').text(data.getId());
			$(".homepage-property-title").find('.name').text(data.getName());
			$(".homepage-property-title").find('.town').text(data.getTown());
			$(".homepage-property-details").find('.cat').text(data.getCat());
			$(".homepage-property-details").find('.property-summary').replaceWith('<div class="property-summary">'+data.getDesc()+'</div>');
			$(".homepage-property-image").animate({opacity: 1.0}, 2000);
			$(".homepage-property-image").find('a').attr("href", data.getHref()).attr("title", data.getName());
			$(".summary").find('a').attr("href", data.getHref()).attr("title", data.getName());
			$(".homepage-property-image").find('.property_image').attr("alt", data.getTown()).attr("src", data.getImgsrc());
		});
		return;
	}
	$(".homepage-property-image").animate({opacity: 0.06},1000 , function () {
		$(".homepage-property-title").find('.id').text(data.id);
		$(".homepage-property-title").find('.name').text(data.name);
		$(".homepage-property-title").find('.town').text(data.town);
		$(".homepage-property-details").find('.cat').text(data.cat);
		$(".homepage-property-details").find('.property-summary').replaceWith('<div class="property-summary">'+data.desc+'</div>');
		$(".homepage-property-image").animate({opacity: 1.0}, 2000);
		$(".homepage-property-image").find('a').attr("href", data.href).attr("title", data.name);
		$(".summary").find('a').attr("href", data.href).attr("title", data.name);
		$(".homepage-property-image").find('.property_image').attr("alt", data.town).attr("src", data.imgsrc);
	});
};

$(document).ready(function() {

	var loadProperty = function(propertyID) {
		if (loaded == false) {
			
			$.getJSON("load_property.php", {id: propertyID}, function(data){

				p = new Property();
				p.loadData(data);
				propertyID = p.getId();
				
				for (i=0; i<properties.length; i++) {
					if (properties[i].getId() == propertyID) {
						loaded = true;
					}
				}
				if (loaded == false) {
					properties.push(p);
				}
				delete p;
				
				if (loaded == false) {
					loadProperty(propertyID);
				}
			});
		}
	}

	var prevPropertyID;
	var changeProperty = function() { 
		propertyID = $("table#home-table td.homepage-property-title div.id").html();

		var idx = 0;
		for (i=0; i<properties.length; i++) {
			if (properties[i].getId() == propertyID) {
				idx = i + 1;
			}
		}
		if (idx > properties.length-1) idx = 0;
		doAnimation(properties[idx],true);
	};
	
	var propertyID = $("table#home-table td.homepage-property-title div.id").html();
	loadProperty(propertyID);
	window.setInterval(changeProperty, 7000);
});
