/**
 * This file based on the datetweaks module's js file, which is currently not ready for d7
 */

// $Id:$

/**
 * @file
 * Alters case study and solution teasers to create the hover effect on images.
 */
(function ($) {
	
  // Attach our behavior to the drupal namespace
  Drupal.behaviors.ials_textfield_helptext = {
    attach: function(context, settings) {
      // Call our function
      ials_textfield_helptext.init();
    }
  };

  var ials_textfield_helptext = function(){
    var pub = {};
    
    var requiredMsg = 'This field is required';

    /**
     * Init function
     */
    pub.init = function() {
      pub.debug('called ials_textfield_helptext init');
      pub.defaultTextInFields();
    };

    /**
     * Adds default text to a textfield
     */
    pub.defaultTextInFields = function() {
      // Create a reference to all instances of inputs that have this class
      $defaultTextFields = $("input.ials-textfield-helptext");
      // Create a focus event listener for these fields
      $defaultTextFields.focus(function(srcc) {
        // If the value is the same as the title, then we want to remove the default text and class
        if ($(this).val() == $(this)[0].title) {
          $(this).removeClass("help-text-active");
    	  $(this).val("");
        }
      });
      // Create a blur event listener for these fields
      $defaultTextFields.blur(function() {
        // If the field is blank (or set to the title) when they leave it, add the class and default message based on the title
        if ($(this).val() == "" || $(this).val() == $(this)[0].title) {
          $(this).addClass("help-text-active");
          $(this).val($(this)[0].title);
        }
      });
      // Add a listener on the parent form to empty the textfield of any help text when submitted
      $parentForms = $defaultTextFields.closest('form');
      $parentForms.submit(pub.clearHelpOnSubmit);
      // Fire a blur event on our fields to create the initial default text and class
      $defaultTextFields.blur();
    }

    /**
     * Clears help text on submit
     */
    pub.clearHelpOnSubmit = function(event) {
      var allowSubmit = true; 
      // Get any text fields in this form that have help text
      $textField = $("input.ials-textfield-helptext", $(this));
      $textField.each(function(index, domElem){
        var thisTitle = $(domElem)[0].title;
        // If they still have the help text in there, remove it
        if ($(domElem).val() == thisTitle) {
          isValid = false;
          $(domElem).val('');
          $(domElem).removeClass("help-text-active");
          // Keep from submitting if required
          if ($(domElem).hasClass('required')) {
            allowSubmit = false;
            $(domElem).css('background', '#FEF5F1');
            $(domElem).addClass('error');
            $(domElem).val(requiredMsg);
            // Add a one-time click listener to remove the error state
            $(domElem).one('click', function() {
              $(this).val('');
              $(this).removeClass('error');
              $(domElem).css('background', '#ffffff');
            });
          }
        }
        // If 
        else if ($(domElem).val() == requiredMsg) {
          allowSubmit = false;
        }
      });
      return allowSubmit;
    }
    	
    /**
     * Debug wrapper
     */
    pub.debug = function(sMessage, oObject) {
      if (typeof console !== 'undefined' && 'log' in console) {
        console.log(sMessage, oObject);
      }
    };
        
    // Return our namespaced objects and functions
    return pub;
  }();

})(jQuery);
;
(function ($) {

/**
 * Open Mollom privacy policy link in a new window.
 *
 * Required for valid XHTML Strict markup.
 */
Drupal.behaviors.mollomPrivacy = {
  attach: function (context) {
    $('.mollom-privacy a', context).click(function () {
      this.target = '_blank';
    });
  }
};

/**
 * Attach click event handlers for CAPTCHA links.
 */
Drupal.behaviors.mollomCaptcha = {
  attach: function (context, settings) {
    // @todo Pass the local settings we get from Drupal.attachBehaviors(), or
    //   inline the click event handlers, or turn them into methods of this
    //   behavior object.
    $('a.mollom-switch-captcha', context).click(getMollomCaptcha);
  }
};

/**
 * Fetch a Mollom CAPTCHA and output the image or audio into the form.
 */
function getMollomCaptcha() {
  // Get the current requested CAPTCHA type from the clicked link.
  var newCaptchaType = $(this).hasClass('mollom-audio-captcha') ? 'audio' : 'image';

  var context = $(this).parents('form');

  // Extract the Mollom session id and form build id from the form.
  var mollomSessionId = $('input.mollom-session-id', context).val();
  var formBuildId = $('input[name="form_build_id"]', context).val();

  // Retrieve a CAPTCHA:
  $.getJSON(Drupal.settings.basePath + 'mollom/captcha/' + newCaptchaType + '/' + formBuildId + '/' + mollomSessionId,
    function (data) {
      if (!(data && data.content)) {
        return;
      }
      // Inject new CAPTCHA.
      $('.mollom-captcha-content', context).parent().html(data.content);
      // Update session id.
      $('input.mollom-session-id', context).val(data.session_id);
      // Add an onclick-event handler for the new link.
      Drupal.attachBehaviors(context);
      // Focus on the CATPCHA input.
      $('input[name="mollom[captcha]"]', context).focus();
    }
  );
  return false;
}

})(jQuery);
;

