var enquiries = new Array();
var Enquiry = Class.create();

Enquiry.prototype =
{
	initialize: function (element, type)
	{
		this.type = type;
		this.span = element;
		this.anchor = this.span.parentNode;
		this.href = this.anchor.href;
		var parts = this.href.split('/');
		this.uuid = parts[parts.length - 1];
		enquiries[enquiries.length] = this;
		var rel = this.anchor.rel;
		this.action = rel;
		this.wording = 
		{
			add : 'Add this '+type+' to my enquiry',
			remove : 'Remove this '+type+' from my enquiry'
		}
		this.urls =
		{
			add : '/add-to-enquiry/'+this.uuid,
			remove : '/remove-from-enquiry/'+this.uuid			
		}
		this.anchor.href = 'javascript: void(0)';
		Event.observe(this.anchor, 'click', this.execute.bind(this));
		Event.observe(this.anchor, 'click', function () { this.anchor.blur(); }.bind(this));
		Event.observe(window, 'unload', this.onUnload.bind(this));
	},
	execute: function (event)
	{
		Event.stop(event);
		if (this.action == 'busy')
		{
			return;
		}
		this.span.className = 'add-'+this.type+'-to-enquiry-busy';
		this.span.innerHTML = 'Processing...';
		this.nextAction = this.action == 'remove' ? 'add' : 'remove';		
		for (i = 0; i < enquiries.length; i ++)
		{
			if (enquiries[i] == this || enquiries[i].uuid != this.uuid)
			{
				continue;
			}
			enquiries[i].action = 'busy';
			enquiries[i].span.className = 'add-'+this.type+'-to-enquiry-busy';
			enquiries[i].span.innerHTML = 'Processing...';
		}
		var url = eval('this.urls.'+this.action);			
		var ajax = new Ajax.Request(
			url,
			{
				onSuccess: this.afterExecute.bind(this)
			}
		);			
		this.action = 'busy';
	},
	afterExecute: function (resp)
	{
		this.action = this.nextAction;		
		this.span.className = 'add-'+this.type+'-to-enquiry'+(this.action == 'add' ? '' : '-remove');
		this.span.innerHTML = eval('this.wording.'+this.action);
		for (i = 0; i < enquiries.length; i ++)
		{
			if (enquiries[i] == this || enquiries[i].uuid != this.uuid)
			{
				continue;
			}
			enquiries[i].action = this.action;
			enquiries[i].span.className = 'add-'+this.type+'-to-enquiry'+(this.action == 'add' ? '' : '-remove');
			enquiries[i].span.innerHTML = eval('this.wording.'+this.action);			
		}
	},
	onUnload: function (event)
	{
		if (this.action == 'busy')
		{
			this.action = 'add';
			this.span.className = 'add-'+this.type+'-to-enquiry';
			this.span.innerHTML = eval('this.wording.'+this.action);
		}
	}
}

Event.observe(
	window,
	'load',
	function ()
	{		
		// do villas
		
		var spans = $$('span.add-villa-to-enquiry');		
		spans.each(
			function (element)
			{
				new Enquiry(element, 'villa');
			}
		);	
		
		// do locations
		
		spans = $$('span.add-location-to-enquiry');
		spans.each(
			function (element)
			{
				new Enquiry(element, 'location');
			}
		);		

		// do villas
		
		var spans = $$('span.add-villa-to-enquiry-remove');		
		spans.each(
			function (element)
			{
				new Enquiry(element, 'villa');
			}
		);	
		
		// do locations
		
		spans = $$('span.add-location-to-enquiry-remove');
		spans.each(
			function (element)
			{
				new Enquiry(element, 'location');
			}
		);		
	}
)