/*
 * for inline thickbox implementation
 */
function change_tb_href()
{
  var qString = $("a.thickbox").attr('rel');
  $("a.thickbox").attr('href', '#TB_inline?' + qString);
}

/*
 * auto submit form that has class of "auto" when any select is changed
 */
function form_auto_submit()
{
  $('form.auto select').change(function(){
    // added this to null second select if first is changed
    if (this.id == 'f1') {
      $('select#f2').attr('selectedIndex',0);
    }
    var $form = $(this).parents('form');
    $form.submit();
  });
}

function drop_nav()
{
  $('#nav ul ul').attr('style','display:none');
  $('#nav ul ul').parent().hoverIntent({
    over:function(){
      $(this).children('ul').slideToggle(100);
    },
    timeout: 500,
    out:function(){
      $(this).children('ul').slideToggle(100);
    }
  });
}

function toggleContent()
{
  $('.hidden').hide();
  $('a[class~=toggle]').click(function(){
    var toggleId = $(this).attr('href').replace('#','');
    $("#" + toggleId).toggle();
    var $html = $("#" + toggleId).is(":visible") ? 'hide' : 'more';

    $(this).text($html);
    return false;
  });
}

function formMagic()
{
  // remove "required" class on labels
  $('form label.required').removeClass('required');
  // addptional class to input and textarea with value of "optional"
  $("form label.optional ~ input[type=text][value='optional']").addClass('optional');
  $("form label.optional ~ textarea[value='optional']").addClass('optional');
  // look for optional labels that have an empty input field or textarea as sibling
  // and add optional as value to field. remove value of optional on focus
  $("form label.optional ~ input[type=text][value='']").val('optional').addClass('optional').click(function(){
    if (this.value == 'optional') {
      $(this).val('').removeClass('optional');
    }
  }).blur(function(){
    if ($.trim(this.value) == '') {
      $(this).val('optional').addClass('optional');
    }
  });
  // ... and for textarea
  $("form label.optional ~ textarea[value='']").val('optional').addClass('optional').click(function(){
    if (this.value == 'optional') {
      $(this).val('').removeClass('optional');
    }
  }).blur(function(){
    if ($.trim(this.value) == '') {
      $(this).val('optional').addClass('optional');
    }
  });

  // remove field value of optional on submit
  $("form").submit(function(){
    $("form label.optional ~ input[type=text][value='optional']").val('').removeClass('optional');
    $("form label.optional ~ textarea[value='optional']").val('').removeClass('optional');
    return true;
  });

}

function ddPngFix()
{
  DD_belatedPNG.fix('#page, #nav a span, #nav ul ul, a.link-cape-trib, a.book-now, a.booking-small, a.flag');
}

function popLink()
{
  $("a.pop").attr('target','_blank');
}


/* onDocumentReady Call */
$(function(){
  if ($('a.thickbox').hasClass('inline')) {
      change_tb_href();
  }

  if (typeof(DD_belatedPNG) != 'undefined') {
    ddPngFix();
  }

  drop_nav();
  toggleContent();
  popLink();
  formMagic();

});

