$(document).ready(function() {

  /* Input fields with a title - the title gets put into the field automatically */
  $('input[title]').each(function() {
    // on focus - make text empty if it is set to emptytext
    $(this).focus(function() {
      if ($(this).attr('value') == $(this).attr('title')) {
        $(this).attr('value', '');
      }
    });
    
    // on blur - make text emptytext if it is empty
    $(this).blur(function() {
      if ($(this).attr('value') == '') {
        $(this).attr('value', $(this).attr('title'));
      }
    });
    
    // run the blur event
    $(this).blur();

  });
  
  
  /* Booking box state selector */
  $('#booking-box-state').change(function() {
    // Ajax request to get parks
    var state_id = parseInt($(this).val(), 10);
    if (isNaN(state_id)) state_id = 0;
    
    $.getJSON('ajax_get_parks.php', {state: state_id}, function(data) {

      var html = '<option value="" style="font-style: italic;">-- Destination Park --</option>';
      
      for (idx in data) {
        if (typeof(data[idx]) != 'object') continue;
        var row = data[idx];
        
        if (state_id == 0) row.name += ', ' + row.state;
        html += '<option value="' + row.id + '">' + row.name + '</option>';
      }
      
      var curr_value = $('#booking-box-park').val();
      $('#booking-box-park').html(html).val(curr_value);
    });
    
  });
  
  $('#booking-box-state').change();
  
  
  // Bookmarker for IE only - firefox users can use the star in the awesomebar
  if ($.browser.msie) {
    $('.bookmark-page').click(function() {
      window.external.AddFavorite(window.location.href, document.title);
    });
    $('.bookmark-page').css('cursor', 'pointer');
    $('.bookmark-page').show();
  }
  
});
