/**
 * HomeAway namespace
 */
 
 
 
var $j = jQuery.noConflict();



var ha = {
strings: {},
	settings: {},
    
    util: {
    	isInt: function(c){ return((c>="0")&&(c<="9")) },
    	initToggles: function(){
			$j(".expand .content").addClass("hidden");
			$j(".expand .show").removeClass("hidden");
			$j(".expand .action").bind("click", function(){
				var el = $j(this).parent("div.container").children("div.content");
				if(el.hasClass("hidden")){
					el.removeClass("hidden");
					$j(this).addClass("open");
				}
				else{
					el.addClass("hidden");
					$j(this).removeClass("open");
				}
			});
		},
		initPopupUrls: function(){
			$j(".popup-url").bind("click", function(){
				window.open(this.href);
				return false;
			});
		},	
		initSearchSwap: function(){
			if($j("#searchKeywords").val() == ""){ 
				$j("#searchKeywords").addClass("default");
				$j("#searchKeywords").val($j("#searchKeywords").attr("rel"));
			}
			else if($j("#searchKeywords").val() != $j("#searchKeywords").attr("rel")){
				$j("#searchKeywords").val($j("#searchKeywords").attr("rel"));
			}
			$j("#searchKeywords").focus(function(){
				var val = $j(this).val();
				$j(this).removeClass("default");	
				if($j(this).attr("rel") == $j(this).val()){
					$j(this).val("");
				}
			});
			$j("#searchKeywords").blur(function(){
				var val = $j(this).val();
				$j(this).removeClass("default");
				if($j(this).val() == ""){
					$j(this).addClass("default");
					$j(this).val($j(this).attr("rel"));
				}
			});
			

			if($j("#searchKeywordsWidget").val() == ""){ 
					$j("#searchKeywordsWidget").addClass("default");
					$j("#searchKeywordsWidget").val($j("#searchKeywordsWidget").attr("rel"));
				}
				else if($j("#searchKeywordsWidget").val() != $j("#searchKeywordsWidget").attr("rel")){
					$j("#searchKeywordsWidget").val($j("#searchKeywordsWidget").attr("rel"));
					$j("#searchKeywordsWidget").attr("rel");
				}
				$j("#searchKeywordsWidget").focus(function(){
					var val = $j("#searchKeywordsWidget").val();
					$j("#searchKeywordsWidget").removeClass("default");	
					if($j("#searchKeywordsWidget").attr("rel") == $j("#searchKeywordsWidget").val()){
						$j("#searchKeywordsWidget").val("");
					}
				});
				$j("#searchKeywordsWidget").blur(function(){
					var val = $j("#searchKeywordsWidget").val();
					$j("#searchKeywordsWidget").removeClass("default");
					if($j("#searchKeywordsWidget").val() == ""){
						$j("#searchKeywordsWidget").addClass("default");
						$j("#searchKeywordsWidget").val($j("#searchKeywordsWidget").attr("rel"));
					}
				});
				
		},
		
		    initSubmit: function(){
				$j('.input-keyword').keyup(function(e){
		        		e = e || window.event;
		        		var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
		        		if (keyCode == 13) {
		        			ha.util.submit(e);
		        		}
		       	 });
				// this function attaches the submit function to the submit button of search froms
				$j('form[id=keywordSearchForm],form[id=widget-search-form]').each(function(){
					$j(this).submit(function(){ return false; });
		
					$j('.search-submit-button', $j(this)).click(function(e){
						ha.util.submit(e);
					});
				});
		    },
		
		    submit: function(e){
				// this function checks the search form keyword value against the defualt value to make sure the default value isnt passed when the user hasnt entered a keyword
		
				var formObj = $j(e.target).parents('form'); //gets the parent form of the submit button clicked
				var keywords = $j("input[name=keywords]", formObj).val();    //gets the value of the forms keyword input	
				var defaultMessage = $j("input[rel]", formObj).attr("rel");	 //gets the default value of the keyword input
		
				var arrival = $j("input[name=startDateInput]", formObj).val();
				var departure = $j("input[name=endDateInput]", formObj).val();
		
				if ($j.trim(keywords) != "" && keywords != defaultMessage) {
					keywords = "/keywords:" + encodeURIComponent(keywords);
				} else {
					keywords = "";
				}
		
				var url = brand.siteDomain + '/search';
				      url += keywords;
				      url += ha.util.dateParam($j("input[name=startDateInput]", formObj) ,'arrival');
				      url += ha.util.dateParam($j("input[name=endDateInput]", formObj), 'departure');
		
				window.location = url;
		    },
		
		    // return a formatted url param for a given search date string
		    dateParam: function(o,s){
				if ((typeof $j(o).val() == "undefined") || (ha.util.delim($j(o).val()) == "")){
		    			return "";
		    		} else if ($j(o).val() != ha.strings.dateInputHelpText) {
		    			if (ha.strings.dateInputHelpText == 'mm/dd/yyyy') {
		    				var parts = $j(o).val().split("/")
		    				return '/' + s + ':' + parts[2] + '-' + parts[0] + '-' + parts[1];
		    			}
		    			return '/' + s + ':' + $j(o).val().split("/").reverse().join("-");
		    		}
		    		return "";
		    },
		    
		    // set the date string delimiters
		    delim: function(s){
				if (s.indexOf("/") == -1) {
					if(s.indexOf("-") > -1) s = s.replace(/-/g, "/");
					else if(s.indexOf(".") > -1) s = s.replace(/\./g, "/");
					else s = "";
				}
				return s;
		    }
		}
    
    
};

$j(document).ready(function(){
	ha.util.initToggles();
	ha.util.initSearchSwap();
	ha.util.initSubmit();
});