jQuery(document).ready(function() {
  
    jQuery(".search_dd dt a").click(function() {
        jQuery(".search_dd dd ul").toggle();
    });
                
    jQuery(".search_dd dd ul li a").click(function() {
        var text = jQuery(this).html();
        jQuery(".search_dd dt a span").html(text);
        jQuery(".search_dd dd ul").hide();
        jQuery("#result").html("Selected value is: " + getSelectedValue("search_dd"));
		jQuery("#search_search_type").val(getSelectedValue("search_dd"));
		// only show search field "near" if searching iraves
		if(jQuery(this).attr('rel') == 'near') {
			jQuery("#form_input_city").show();
			search_type = 'iraves';
		} else {
			jQuery("#form_input_city").hide();
			search_type = '';
		}
    });
                
    function getSelectedValue(id) {
        return jQuery("#" + id).find("dt a span.value").html();
    }

    jQuery(document).bind('click', function(e) {
        var $clicked = jQuery(e.target);
        if (! $clicked.parents().hasClass("search_dd"))
            jQuery(".search_dd dd ul").hide();
    });


});

// on search form submit check if the search type is iraves and if the near field was filled out
// if so changed the <form action=""> to city search 
function search_onsubmit(form) {
	var val = jQuery("#search_search_city").val();
	if(search_type == "iraves" && val != "") {
		try {
		// search iraves city
		form.action = '/search/city';
		//alert(form.action)
		form.submit();
		} catch(e){alert(e)}
	} else{
		form.submit();
	}
}
