/**
 * Luxury Hotel
 * @author Kevin Dew <kev@dewsolutions.co.uk> 2007
 * Dependencies: Mootools v1.11+ 
 * Destination Select
 */ 
 
/**
 * Change hotels to match destinations
 */ 
function changeDest()
{
	var destVal = $('destination').getValue();
	var selectedHotel = $('hotel').getValue();
	//clean up existing hotel selects
	var hotelChildren = $('hotel').getChildren();
	if(hotelChildren)
	{
		for(i = 0; i < hotelChildren.length; i++)
		{
			if(hotelChildren[i].getProperty('value') != 0)
				hotelChildren[i].remove();
		}
	}
	
	if(typeof destinations[destVal] != 'undefined')
	{
		//get specific hotels for this destination
		for(var i = 0; i < destinations[destVal].length; i++)
		{
			var val = destinations[destVal][i];
			//find hotel
			for(var j = 0; j < hotelIds.length; j++)
			{
				if(hotelIds[j] == val)
				{
					if(hotelIds[j] == selectedHotel)
						new Element('option', {'value' : val, 'selected' : 'selected'}).appendText(hotelNames[j]).injectInside('hotel');
					else
						new Element('option', {'value' : val}).appendText(hotelNames[j]).injectInside('hotel');
				}
			}
		}
		
	}
	else
	{
		//show all hotels
		for(var i = 0; i < hotelIds.length; i++)
			if(hotelIds[i] == selectedHotel)
				new Element('option', {'value' : hotelIds[i], 'selected' : 'selected'}).appendText(hotelNames[i]).injectInside('hotel');
			else
				new Element('option', {'value' : hotelIds[i]}).appendText(hotelNames[i]).injectInside('hotel');
	}	
}

//onload
window.addEvent('domready', function()
{
	if($('destination') && $('hotel') && $('dest_select'))
	{
		//run this when script loads old data could still be present
		changeDest();
		$('destination').addEvent('change', changeDest);
		
		$('hotel').addEvent('change', function(e)
		{
			if($('hotel').getValue() != 0)
			{
				//append the get string onto the form so we keep a consistent hotel style (good for SEO)
				var action = $('dest_select').getProperty('action');
				action = action.split('?')[0];
				window.location = action+'?hotel='+$('hotel').getValue();
			}
			
		});
	}
});