jQuery(function( $ ){

	populateResorts();
	populateHotels();
	populateRating();
	populateBoard();

	/* START LM FILTER EVENTS/FUNCTIONS */

	$("a[rel=more-options]").click(function() {
		$("div#more").slideToggle(600, function() {
			if($(this).is(':visible')){
				$("#more-arrow").attr("src", "/images/more-expanded.gif");
			}else{
				$("#more-arrow").attr("src", "/images/more-collapsed.gif");
			}
		});
		this.blur();
		this.hideFocus = true;
		this.style.outline = 'none';
		return false;
	});

	function populateResorts(){
		$.getJSON('/scripts/query-resorts.php', {destName:$('#destName :selected').text()}, function(data) {

			var select = $('#destResorts');
			var options = select.attr('options');

			$('option', select).remove();

			options[options.length] = new Option('Any', '', true, true);
			$.each(data, function(index, array) {
				options[options.length] = new Option(array['resort']);
			});

		});
	}

	function populateRating(){
		$.getJSON('/scripts/query-hotel-rating.php', {destName:$('#destName :selected').text(),destResorts:$('#destResorts').val(),destHotel:$('#hotel').val()}, function(data) {
			var select = $('#hotel-rating');
			var options = select.attr('options');

			$('option', select).remove();

			options[options.length] = new Option('Any', '', true, true);
			$.each(data, function(index, array) {
				options[options.length] = new Option(array['rtext'],array['rating']);
			});

		});
	}

	function populateBoard(){
		$.getJSON('/scripts/query-hotel-board.php', {destName:$('#destName :selected').text(),destResorts:$('#destResorts').val(),hRating:$('#hotel-rating').val(),destHotel:$('#hotel').val()}, function(data) {
			var select = $('#hotel-board');
			var options = select.attr('options');

			$('option', select).remove();

			options[options.length] = new Option('Any', '', true, true);
			$.each(data, function(index, array) {
				options[options.length] = new Option(array['btext'],array['board']);
			});

		});
	}

	function populateHotels(){
		$.getJSON('/scripts/query-hotels.php', {destName:$('#destName :selected').text(),destResorts:$('#destResorts').val(),hRating:$('#hotel-rating').val(),hBoard:$('#hotel-board').val()}, function(data) {
			var select = $('#hotel');
			var options = select.attr('options');

			$('option', select).remove();

			options[options.length] = new Option('Any', '', true, true);
			$.each(data, function(index, array) {
				options[options.length] = new Option(array['hname'],array['hcode']);
			});

		});
	}

	function populateRooms(){
		$.getJSON('/scripts/query-hotel-rooms.php', {destName:$('#destName :selected').text(),destResorts:$('#destResorts').val(),hRating:$('#hotel-rating').val(),hBoard:$('#hotel-board').val(),destHotel:$('#hotel').val()}, function(data) {
			var select = $('#hotel-room');
			var options = select.attr('options');

			$('option', select).remove();

			options[options.length] = new Option('Any', '', true, true);
			$.each(data, function(index, array) {
				options[options.length] = new Option(array['rname'],array['rcode']);
			});

		});
	}

	$('#destName').change(function() {
		populateResorts();
		populateHotels();
		populateRating();
		populateBoard();
	});
	$('#destResorts').change(function() {
		populateHotels();
		populateRating();
		populateBoard();
	});
	$('#hotel-rating').change(function() {
		populateHotels();
		populateBoard();
	});
	$('#hotel-board').change(function() {
		if($('#hotel').val()==''){populateHotels();}
	});
	$('#hotel').change(function() {
		populateBoard();
	});

	function updateResults(){
		var getdc = $('#destName').val();
		var getdn = $('#destName :selected').text();
		var getres = $('#destResorts').val();
		var gethc = $('#hotel').val();
		var gethn = $('#hotel :selected').text();
		var gethrc = $('#hotel-rating').val();
		var gethrt = $('#hotel-rating :selected').text();
		var getbb = $('#hotel-board').val();
		$.ajax({
			url: '/scripts/query-last-minute.php',
			data: {dc: ''+getdc+'', dn: ''+getdn+'', res: ''+getres+'', hc: ''+gethc+'', hn: ''+gethn+'', hrc: ''+gethrc+'', hrt: ''+gethrt+'', bb: ''+getbb+''},
			cache: false,
			success: function(data) {
				$('#lmResults').html(data);
				$('#lmResults').delay(10000).unblock();
				$.scrollTo($('div#lm-filter-panel'), 500, {axis:'y'});
			}
		});
	}

	$('.btn-lm-update').click(function() {
		if($('#destName').val()!=''){
			$('#lmResults').block({ message: '<img src="/images/large-loader.gif"><br><h2 class="coloured">Please wait a few moments whilst we update results...</h2>' });
			updateResults();
		}else{alert('Please select a destination to continue.');}
		this.blur();
		this.hideFocus = true;
		this.style.outline = 'none';
		return false;
	});

	/* END LM FILTER EVENTS/FUNCTIONS */

	/* START LM GRID EVENTS */

	//$('.hr-col-1').corner("tl keep cc:#fff 20px");
	//$('.hr-col-3').corner("tr keep cc:#fff 20px");

	$('div.pane').scrollTo( 0 );
	$.scrollTo(0);

	$('a[rel=search]').live('click', function() {
		$.blockUI({ message: $('#pkload') });
		document.getElementById('pkloader').innerHTML = '<img src=\'/images/loader.gif\' border=\'0\' />';
		this.blur();
		this.hideFocus = true;
		this.style.outline = 'none';
	});

	$('#rel-position-left').live('click', function() {
		$('#pane-target').stop().scrollTo( '-=490', 800 );
		this.blur();
		this.hideFocus = true;
		this.style.outline = 'none';
		return false;
	});

	$('#rel-position-right').live('click', function() {
		$('#pane-target').stop().scrollTo( '+=490', 800 );
		this.blur();
		this.hideFocus = true;
		this.style.outline = 'none';
		return false;
	});

	/* END LM GRID EVENTS*/

});
