// =======================================================================
// = ALERT! =============================================================
// = This plugin has been modified from its original source (line 18). =
// ====================================================================

/*--------------------------------------------------------------------
 * jQuery plugin: customInput()
 * by Maggie Wachs and Scott Jehl, http://www.filamentgroup.com
 * Copyright (c) 2009 Filament Group
 * Dual licensed under the MIT (filamentgroup.com/examples/mit-license.txt) and GPL (filamentgroup.com/examples/gpl-license.txt) licenses.
 * Article: http://www.filamentgroup.com/lab/accessible_custom_designed_checkbox_radio_button_inputs_styled_css_jquery/
 * Usage example below (see comment "Run the script...").
--------------------------------------------------------------------*/
(function($){

$.fn.customInput = function() {
  $(this).each(function(i) {
    if ( $(this).is('[type=checkbox],[type=radio]') && $(this).parent( '.custom-' + $(this).attr('type') ).length == 0 ) {
      
      var input = $(this);

      // get the associated label using the input's id
      var label = $('label[for='+input.attr('id')+']');
      
      // Don't make items disappear because a label is missing
      if (!label.length) {
        return;
      }

      // get type, for classname suffix
      var inputType = ( input.is('[type=checkbox]') ) ? 'checkbox' : 'radio';

      // wrap the input + label in a div
      $('<div class="custom-'+ inputType +'"></div>').insertBefore(input).append(input, label);

      // find all inputs in this set using the shared name attribute
      var allInputs = $('input[name='+input.attr('name')+']');
      
      var $as = label.find('a[target=_blank]');
      
      $as.each( function() {
      
        var href= this.href;
        
        $(this).bind('click', function(e) {
          e.stopPropagation(); 
          e.preventDefault();
          window.open( href, '_blank' );
          return false;
          
        });
        
        this.href = '#';
        this.removeAttribute('target');
        
      });

      // necessary for browsers that don't support the :hover pseudo class on labels
      label.hover(
        function() {
          $(this).addClass('hover')
          
          if (inputType == 'checkbox') {
            input.trigger('customOver');
          }
          else {
            $('[name=' + input.attr('name') + ']').each(function() {
              $(this).trigger('customOverName');
            });
          }
          
          if ( inputType == 'checkbox' && input.is(':checked') ) {
            $(this).addClass('checkedHover');
          }
        },
        function() {
          $(this).removeClass('hover checkedHover')
          if (inputType == 'checkbox') {
            input.trigger('customOut');
          }
          else {
            $('[name=' + input.attr('name') + ']').each(function() {
              $(this).trigger('customOutName');
            });
          }
        }
      );

      // bind custom event;, trigger it; bind click, focus, blur events
      input.bind('updateState', function() {
        if ( input.is(':checked') ) {
          if ( input.is(':radio') ) {
            allInputs.each(function() {
              $('label[for='+$(this).attr('id')+']').removeClass('checked');
            });
          }
          label.addClass('checked');
        }
        else {
          label.removeClass('checked checkedHover checkedFocus');
        }
      })
      .bind('updateState disable enable', function() {
        if ( input.is(':disabled') ) {
          label.addClass('disabled');
          if ( input.is(':checked') ) {
            label.addClass('checkedDisabled');
          }
        }
        else {
          label.removeClass('disabled checkedDisabled');
        }
      })
      .trigger('updateState')
      .click(function(){
        $(this).trigger('updateState');
      })
      .focus(function(){
        label.addClass('focus');
        if ( inputType == 'checkbox' && input.is(':checked') ) {
          label.addClass('checkedFocus');
        }
      })
      .blur(function() {
        label.removeClass('focus checkedFocus');
      });

    }
  });
  return this;
};

})(jQuery);

