$(document).ready(function() {
	
	// If you change this string, also change the constant
	// SearchController.DEFAULT_HEADER_SEARCH_TEXT
	var defaultSearch = "search for properties";
	
	var searchBox = $("#searchbox");
	
	searchBox.addClass("gray-txt");
	searchBox.val(defaultSearch);
	
	searchBox.focus(
			function (e) {
				if (searchBox.val() === defaultSearch) {
					searchBox.val("");
					searchBox.removeClass("gray-txt").addClass("black-txt");
				}
			}
		);
	searchBox.blur( 
			function (e) {
				if (searchBox.val() === "") {
					searchBox.val(defaultSearch);
					searchBox.removeClass("black-txt").addClass("gray-txt");
				}
			}
		);
	
	searchBox.autocomplete("/search/autocomplete.htm", {
		delay: 100,
		minChars: 3, 
		max: 9,
		highlight: false,
		cacheLength: 10,
		matchSubset: false,
		width: 272,
		extraParams: { displayCount: 'true'}
	});
		
	dateFields('#checkindate', '#checkoutdate', {isUpdateCookie: false});
	
	$("#searchBar-form").submit(searchOnSubmit);
});

function searchOnSubmit() {
	
	var chkIn = $('#checkindate').val();
	var chkOut = $('#checkoutdate').val();
	
	if(checkdates(chkIn,chkOut)){
	
		chkIn = (chkIn == 'check-in') ? "" : chkIn;
		chkOut = (chkOut == 'check-out') ? "" : chkOut;
		
		
		$.cookie("filterCheckin", chkIn, { expires: 0,path:'/' }); 
		$.cookie("filterCheckout", chkOut, { expires: 0,path:'/' });
		
		setSendOmnitureDate();
		
		// This cookie is used to tell that this is a fresh search. 
	    // Changes done to resolve issue: search austin and then again search for austin.
	    // For this case search params are going for first search but for second search
	    // page is loaded from cache causing search params not being sent.
	    $.cookie("VR_OMNITURE_MULTISEARCH", "NewSearch", {
			path : '/vacation-rentals/'
		});
		
	}
	else
		return false;
	
}

