/* Javascript is generally available and needed on all pages */

var texts = {q:"what do you want?", location:"where"};
$(function() {
   
   var container = $('#search');
   var inp = $('input[name="q"]', container);
   inp.bind('focus', function() {
      if (this.value==texts[$(this).attr('name')]) {
	 $(this).val('');
      }
   }).bind('blur', function() {
      if ($.trim(this.value)=='') {
	 $(this).val(texts[$(this).attr('name')]);
      }
   });
   if (!inp.val()) {
      inp.val(texts[inp.attr('name')]);
      inp.addClass('unfocused');
   }
   
   var inp = $('input[name="location"]', container);
   inp.bind('focus', function() {
      if (this.value==texts[$(this).attr('name')]) {
	 $(this).val('');
      }
   }).bind('blur', function() {
      if ($.trim(this.value)=='') {
	 $(this).val(texts[$(this).attr('name')]);
      }
   });
   if (!inp.val()) {
      inp.val(texts[inp.attr('name')]);
   }  
   
   $('form', container).bind('submit', function() {
      // when the search form is submitted, remove any default texts that
      // were entered there by the javascript
      if ($('input[name="location"]', this).val()==texts['location'])
        $('input[name="location"]', this).val('');
      
      if ($('input[name="q"]', this).val()==texts['q'])
        $('input[name="q"]', this).val('');
      
      //if ($('input[name="q"]', this).val().length || $('input[name="location"]', this).val().length)
      if ($('input[name="location"]', this).val())
        return true;
      else if ($('input[name="q"]', this).val()) {
         /*
         var pre_text = $('#location').text();
         $('#location').text('Please enter a location').addClass('error');
         setTimeout(function() {
            $('#location').text(pre_text).removeClass('error');
         }, 5*1000);
         return false;
          */
         return true;
      } else {
         
         // put the default text back in 
         $('input[name="location"]', this).val(texts['location']);
         $('input[name="q"]', this).val(texts['q']);
         return false;
      }
   });
   
});