/*
 * 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 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 mega_nav()
{
  $('ul#mega div.meganav').parent().hoverIntent({
    over:function(){
      $(this).addClass('activetab');
    },
    timeout: 500,
    out:function(){
      $(this).removeClass('activetab');
    }
  });
}

function ddPngFix()
{
  DD_belatedPNG.fix('.pngbg, .activetab a, .activetab span');
}

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

  if (typeof(DD_belatedPNG) != 'undefined') {
    ddPngFix();
  }
  form_auto_submit();
  mega_nav();
//  drop_nav();

  formMagic();
});

