$(document).ready(function() {

  $('body').removeClass('no_js');

  jQuery.fn.exists = function(){return jQuery(this).length>0;}

  // Forms Functions
  if ($('.form, #directory-search form').exists()) {

    // Shows and Hides the "other" Fields
    $.fn.otherField = function() {

      // Dropdown Selections - Initital Visibility
      $('.has_more option:selected').each(function() {
        if ($(this).hasClass('do_more')) {
          $(this).closest('.field').nextAll('.more:first').show();
        } else {
          $(this).closest('.field').nextAll('.more:first').hide();
        }
      });

      if ($.browser.msie && $.browser.version.substring(0,1) === '6') {
        $('.has_more input[type=radio].do_more').each(function() {
          if ($(this).is(':checked')) {
            $(this).closest('.field').nextAll('.more:first').show();
          } else {
            $(this).closest('.field').nextAll('.more:first').hide();
          }
        });
      } else {
        $('.has_more input.do_more').each(function() {
          if ($(this).is(':checked')) {
            $(this).closest('.field').nextAll('.more:first').show();
          } else {
            $(this).closest('.field').nextAll('.more:first').hide();
          }
        });
      }

      // Dropdown Selection - Interactive Show/Hide
      $('.has_more select').change(function() {
        if ($(this).find('option:selected').hasClass('do_more')) {
          $(this).closest('.field').nextAll('.more:first').show();
        } else {
          $(this).closest('.field').nextAll('.more:first').hide();
        }
      });

      // Radio Buttons - Interactive Show/Hide
      $('.has_more input[type=radio]').change(function() {
        if ($(this).hasClass('do_more')) {
          $(this).closest('.field').nextAll('.more:first').show();
        } else {
          $(this).closest('.field').nextAll('.more:first').hide();
        }
      });

      if ($.browser.msie && $.browser.version.substring(0,1) === '6') {
        // Do Nothing
      } else {
        $('.has_more input[type=checkbox].do_more').click(function() {
          if ($(this).is(':checked')) {
            $(this).closest('.field').nextAll('.more:first').show();
          } else {
            $(this).closest('.field').nextAll('.more:first').hide();
          }
        });
      }

    };

    if ($.browser.msie && $.browser.version.substring(0,1) === '6') {
      // Do Nothing
    } else {
      // Show Message Box when focus inputs
      $.fn.FocusMessage = function() {

        $('.form .l_col input[type=text]').focus(function(){
          $(this).parent().siblings('.messages').show();
          $(this).closest('.field').addClass('is_focused');
        });

        $('.form .l_col input[type=text]').blur(function(){
          $(this).parent().siblings('.messages').hide();
          $(this).closest('.field').removeClass('is_focused');
        });

      };
    }

    // Message Hover Function
    $.fn.messageHover = function() {

      $('.field').hover(function() {
        $(this).children('.messages').show();
      }, function() {
        if ($(this).hasClass('is_focused')) {
          $(this).stop().children('.messages').show();
        } else {
          $(this).stop().children('.messages').hide();
        }
      });

    };

    $('.form').otherField();
    $('.form').messageHover();
    if ($.browser.msie && $.browser.version.substring(0,1) === '6') {
      // Do Nothing
    } else {
      $('.form').FocusMessage();
    }

  }

  if ($('a#fancybox').exists()) {
    $('a#fancybox').fancybox({'titleShow' : false, 'width' : 600, 'height' : 630});
  }

  // New Event Calendar Dropdown Menu
  if ($('#event-nav').exists()) {
    $('.new_hover_test ul').hide();
    $('.new_hover_test legend').hover(function(){
      $(this).siblings('ul').show();
    },function(){
      $(this).siblings('ul').hide();
    });
    $('.new_hover_test ul').hover(function(){
      $(this).show();
    },function(){
      $(this).hide();
    });
  }

  jQuery.fn.initLabel = function() {
    $(this).each(function(){
      if ( $(this).val() == '' ){
          $(this).parent().siblings('label').show();
      } else {
          $(this).parent().siblings('label').hide();
      }
    });
  };

  jQuery.fn.showHideLabel = function() {
    $(this).focus(function(){
      $(this).parent().siblings('label').hide();
    });
    $(this).blur(function(){
      if ( $(this).val() == '' ){
          $(this).parent().siblings('label').show();
      }
    });
  };

  if ($.browser.msie && $.browser.version.substring(0,1) === '6') {
    // Do Nothing
  } else {
    $('.inst_grp .title input, .inst_grp .grade input, .qual_grp .title input, .qual_grp .body input').initLabel();
    $('.inst_grp .title input, .inst_grp .grade input, .qual_grp .title input, .qual_grp .body input').showHideLabel();
  }

  if ($('#payment').exists()) {

    $('#payment').addClass('js');
    $('#worldpay').show();
    $('#cheque').hide();
    $('#transfer').hide();

    $('#summary').after('<div id="radio"><label>Please Select from the following payment methods</label><div class="options"><label for="payOnline"><input type="radio" name="test" id="payOnline" value="Pay Online" checked="checked" class="current">Pay Online</label><label for="payByCheque"><input type="radio" name="test" id="payByCheque" value="Pay by Cheque">Pay by Cheque</label><label for="payByBankTransfer"><input type="radio" name="test" id="payByBankTransfer" value="Pay by Bank Transfer">Pay by Bank Transfer</label></div></div>');

    $('#radio input[type=radio]').click(function() {

      // Add/Remove Class on Change
      $(this).parent().parent().find('input').removeClass('current');
      $(this).addClass('current');

      if ($('#payOnline').hasClass('current')) {
        $('#worldpay').show();
      } else {
        $('#worldpay').hide();
      }

      if ($('#payByCheque').hasClass('current')) {
        $('#cheque').show();
      } else {
        $('#cheque').hide();
      }

      if ($('#payByBankTransfer').hasClass('current')) {
        $('#transfer').show();
      } else {
        $('#transfer').hide();
      }

    });

  }

  /*if ($.browser.msie && $.browser.version.substring(0,1) === '6') {
    DD_belatedPNG.fix('#tick_type p span, #form_nav li .left, #form_nav li .right, .form .line, .form .submit, input.back, input.save');
  }*/

  $('.form tbody tr:first-child').addClass('cards_row');

  $('.form .delegates .col_1 label').hide();
  $('.form .delegates .col_2 label').addClass('label_pos');
  $('.form .delegates .col_3 label').addClass('label_pos');
  $('.form .delegates .col_4 label').addClass('label_pos');

});
