(function(){
	Event.onDOMReady(function(){
		// collect filters and add click events
		var filters = $$('#destination-selection li a');
		filters.each(function(item){
			Event.observe(item, 'click', function(event){
				// do not follow the link we will ajax update
				Event.stop(event);
				
				// deselect previous filter then select the current one
				$$('#destination-selection li a.selected').each(function(item){
					item.removeClassName('selected');
				});
				Event.element(event).addClassName('selected');
				// remove focus border when clicking
				Event.element(event).blur();
				
				// build url for ajax request
				var params = Event.element(event).href.split('?')[1];
				
				// make ajax request
				new Ajax.Request('/hyatt/resorts/winter/i_dropdown.jsp',{
					method: 'get',
					parameters: params,
					onComplete: function(transport){
						Element.replace('destination', transport.responseText);
						newDestinations();
					}
				});
			});
		});
		newDestinations();
		
		// add popup events to terms and conditions links
		$$('a.terms-conditions').each(function(item){
			Event.observe(item, 'click', function(event){
				Event.stop(event);
				winOpen(Event.element(event).href,'terms',625,250,50,50,'resizable=1,scrollbars=1');
			});
		});
	});
	
	function newDestinations(){
		Event.observe('destination', 'change', function(event){
			showProperty(Event.element(event));
		});
	}

	function showProperty(selectElement){
		var selectedOption = selectElement.options[selectElement.selectedIndex];
		var url = selectedOption.value;
		// only redirect if url is defined
		if(url != null || url != '')
			window.location = url;
	}
})();
