Date.format = 'mm/dd/yy';

$(document).ready(
	function()
	{
	    
        var cal;
    	var $this;
    
    	var checkForMouseout = function(event)
    	{
    		var el = event.target;
    		
    		while (true){
    			if (el == cal) {
    				return true;
    			} else if (el == document) {
    				$this.dpClose();
    				return false;
    			} else {
    				el = $(el).parent()[0];
    			}
    		}
    	};
    	
    	
    	$('.date-pick')
    	   .datePicker({clickInput:true, createButton:true})
    	   .bind(
    			'dpDisplayed',
    			function(event, datePickerDiv)
    			{
    				cal = datePickerDiv;
    				$this = $(this);
    				$(document).bind(
    					'mouseover',
    					checkForMouseout
    				);
    			}
    		).bind(
    			'dpClosed',
    			function(event, selected)
    			{
    				$(document).unbind(
    					'mouseover',
    					checkForMouseout
    				);
    			}
    		);
        $('#Arrive').bind(
    		'dpClosed',
    		function(e, selectedDates)
    		{
    			var d = selectedDates[0];
    			if (d) {
    				d = new Date(d);
    				$('#Depart').dpSetStartDate(d.addDays(1).asString());
    				$('#Depart').dpSetSelected(d.addDays(1).asString());
					$('#Depart').val(d.asString());
    			}
    		}
    	);
    	$('#Depart').bind(
    		'dpClosed',
    		function(e, selectedDates)
    		{
    			var d = selectedDates[0];
    			if (d) {
    				d = new Date(d);
    			}
    		}
    	);
    	$('#Arrive').trigger('change');
	}
)
