// JavaScript Document
$(document).ready(function() 
{
	
/************ COMMON ELEMENTS ********************/	
	
	$("#topnav li").prepend("<span></span>"); //Throws an empty span tag right before the a tag
                
    $("#topnav li").each(function() 
	{ //For each list item...
    	var linkText = $(this).find("a").html(); //Find the text inside of the a tag
        $(this).find("span").show().html(linkText); //Add the text in the span tag
    }); 
                
    $("#topnav li").hover(function() 
	{	//On hover...
    	$(this).find("span").stop().animate(
		{ 
        	marginTop: "-40" //Find the span tag and move it up 40 pixels
        }, 250);
    }, 
	function() 
	{ //On hover out...
    	$(this).find("span").stop().animate(
		{
			marginTop: "0" //Move the span back to its original state (0px)
        }, 
		250);
	});
    
	//Date picker code for availablilty search
	
	$( "#datepicker-a" ).datepicker(
	{
		dateFormat: 'dd-mm-yy',
		changeMonth: true,
		changeYear: true,
		minDate: 0, 
					
		onSelect: function(dateText, inst)
		{
        	var the_date = new Date(dateText);
			var dtNextDay = $.datepicker.parseDate('dd-mm-yy', dateText);
			dtNextDay.setDate(dtNextDay.getDate()+1);
			$("#datepicker-d").datepicker('option', 'minDate', dtNextDay);
		}
	});
				
	$( "#datepicker-d" ).datepicker(
	{
		dateFormat: 'dd-mm-yy',
		changeMonth: true,
		changeYear: true
	});
				
	//AJAX FOR SEARCH BOX
	
	$('#search-form label').hide();	   
	
	// Get the text from the label element
	var labelTxt = $('#search-form label').text();
	
	// Put the text from the label element into the search field's value	
	$('#search-input').attr('value',labelTxt);
	
	// Focus & blur effects
	$('#search-input').focus(function()
	{
		if ((this.value == '') || (this.value == labelTxt)) 
		{
			$(this).val('');
		}
	});
	
	$('#search-input').blur(function()
	{
		if (this.value == '') 
		{
			$(this).val(labelTxt);
		}
	});
				
	// On submit change the value while server side code is run
	$("#submit").click(function()
	{
		$('#search-input').attr('value','Searching...');		
	});
				
	//AJAX FOR AVAILABILITY GUESTS/ROOMS SELECT BOX
				
	$('#wait_1').hide();
	$('#guests').change(function()
	{
		$('#temp_room').hide();
		$('#wait_1').show();
		$('#result_1').hide();
		$.get("/drops.php", 
		{
        	func: "guests",
        	drop_var: $('#guests').val()
		}, 
		function(response)
		{
        	$('#result_1').fadeOut();
			setTimeout("finishAjax('result_1', '"+escape(response)+"')", 400);
		});
       	return false;
   	});
			
	$.validator.setDefaults(
	{
    	errorPlacement: function(error, element) 
		{  
         	$(element).attr(
			{
				"title": error.append()
			});
		},
    	highlight: function(element)
		{
    		$(element).removeClass("textinput");
    	    $(element).addClass("errorHighlight");
    	},
    	unhighlight: function(element)
		{
    		$(element).removeClass("errorHighlight");
    	    $(element).addClass("textinput");
    	}
    });
	
	jQuery.validator.messages.required = "";
	$("#avail").validate(
	{
		invalidHandler: function(e, validator) 
		{
			var errors = validator.numberOfInvalids();
			if (errors) 
			{
				var message = errors == 1
				? 'You missed 1 field. Please check below.'
				: 'You missed ' + errors + ' fields.  Please check below.';
				$("div.error span").html(message);
				$("div.error").show();
			} 
			else 
			{
				$("div.error").hide();
			}
		},
		onkeyup: false,
		submitHandler: function(form) 
		{
			$("div.error").hide();
			
			$.post("/dialogtest.php", $("#avail").serialize(), function(data){
				$('#formResponse').html(data);
    			$('#formResponse').dialog('open'); 
  			});
			//alert("submit! use link below to go to the other step");
			//('#avail').submit();
			//$("#avail").ajaxSubmit(
			//{
            	//target: '#client-script-return-data',
            	//success: function() 
				//{ 
					//$('#formResponse').dialog('open'); 
					//successEvents('#client-script-return-msg');
				//}
         	//});
		},
		debug:false
	});	
	
	


/****************** PAGE SPECIFIC ELEMENTS *********************************/

	$('#closedialog').click(function() {             
		$('#formResponse').dialog('close');
		return false;
	});

	$("#formResponse").dialog(
	{
    	autoOpen: false,
    	resizable: false,
    	position: 'center',
    	stack: true,
		title: 'Quick Availability Check',
    	height: 'auto',
    	width: 'auto',
    	modal: true,
		buttons: { "Close": function() { $(this).dialog('close'); } },
        close: function(ev, ui) { $(this).close(); }

	});
	
/*	$('#avail').submit( function() 
	{
		$.ajax(
		{
			dataType: 'html',
			url: this.attr('action'),
			method: 'POST',
			async: false,
			data: $("#avail").serialize(),
			beforeSend:  $("#formResponse").dialog('open'),
			success: function (response, status, xml) 
			{
				alert("submit! use link below to go to the other step");
			}
		});
	
		return false; // prevent normal form submission.
	});

	*/
	
	$('#headerimg').cycle(
	{
    	delay: 6000,
        speed: 500
        // before: onBefore
    });

	$('#search-form label').hide();	 
	
	// Get the text from the label element
	var labelTxt = $('#search-form label').text();
	
	// Put the text from the label element into the search field's value	
	$('#search-input').attr('value',labelTxt);
	
	// Focus & blur effects
	$('#search-input').focus(function()
	{
		if ((this.value == '') || (this.value == labelTxt)) 
		{
			$(this).val('');
		}
	});
	
	$('#search-input').blur(function()
	{
		if (this.value == '') 
		{
			$(this).val(labelTxt);
		}
	});
	
	// On submit change the value while server side code is run
	$("#submit").click(function()
	{
		$('#search-input').attr('value','Searching...');		
	});	
				
	$("#review").validate();		

	
});
