/*
	WAYA Rentals JavaScript
*/

/*
	Display object for all hiding/showing/toggling of elements
*/
var Display = 
{
	/*
		Sets up hiding and toggling of "Read more..." links for Property page overviews
	*/
	toggleFullOverview: function(event)
	{
		//	Change link text
		if(!$('more').visible()) 	$('read_more').update('Read less&hellip;');
		else 						$('read_more').update('Read more&hellip;');
		
		//	Toggle & transition
		Effect.toggle('more','blind',{ duration: 0.25 });
		
		//	Stop default event behavior
		Event.stop(event);
	}
}
Event.observe(document,'dom:loaded',function()
{
	if($('more')) 				Element.hide('more');
	if($('read_more')) 			Element.observe('read_more','click',Display.toggleFullOverview);
});

var Forms = 
{
	/*
		Disables all submit buttons onclick to prevent double-submits
	*/
	safeSubmit: function(event)
	{
		//	Grab all submit buttons
		var submitButtons = $$('input[type=submit]');
		
		//	Loop through and disable all submit buttons
		submitButtons.each(function(element)
		{
			element.disabled = true;
		});
	}
}
Event.observe(document,'dom:loaded',function()
{
	//	Grab all froms
	var forms = $$('form');
	
	//	Loop through and listen for all submit clicks
	forms.each(function(element)
	{
		Element.observe(element,'submit',Forms.safeSubmit);
	});
});

/*
	Provide smooth scrolling for anchored links
*/
var SmoothAnchors = 
{
	load: function()
	{
		//	Grab all anchored links except those with only # and #more links (used to toggle extended Property overview)
		//	Loop through, listen for event, scrolling effect, then stop default event behavior
		$$('a[href^=#]:not([href=#]):not([href=#more])').each(function(element)
		{
			element.observe('click',function(event)
			{
				new Effect.ScrollTo(this.hash.substr(1),{ duration: 0.375 });
				Event.stop(event);
			}.bindAsEventListener(element))
		})
	}
}
Event.observe(document,'dom:loaded',SmoothAnchors.load);

var Tables = 
{
	/*
		Provide alternating row striping for tables with "striped" CSS class
	*/
	stripeRows: function()
	{
		//	Grab every other table row
		var rows = $$('table.striped tr:nth-child(even)');
		
		//	Add "alt" CSS class
		rows.each(function(element)
		{
			Element.addClassName(element,'alt');
		});
	}
}
Event.observe(document,'dom:loaded',Tables.stripeRows);
