$.resetErrorPositions = function() {

  $('.formError').each( function() {

    var $error      = $(this),
        field_class = '',
        error_html  = $error.html();

    $error.removeClass('formError');

    field_class = $error.attr('class');

    $error.remove();

    $error = $.validationEngine.buildPrompt( $('#'+field_class), '');

    $error.html( error_html );

  });

}; // resetErrorPositions

$.fn.hideButtons = function(txt) {

  txt = txt || 'Saving...';

  var html = '';

  $.each(this.find('.form-button'), function(i, n) {
    $(n).hide();
  });

  this.addClass('button-arrow-none');

  $msg = this.find('.form-submit-processing');

  if ( $msg.length ) {
    $msg.html(txt);
  }
  else {

    // html += '<div class="left">';
    // html += '<object height="20" width="20">';
    // html += '<embed wmode="transparent" src="'+Config.ASSETSURL+'swf/preloading_animation_blue.swf" width="20" height="20"></embed>';
    // html += '</object>';
    // html += '</div>';
    html += '<div class="left" style="left:7px; top:3px; position:relative;">'+txt+'</div>';

    $('<div>').addClass('form-submit-processing cfix relative').html(html).appendTo(this);
  }

  return this;

};

$.fn.showButtons = function() {

  $.each(this.find('.form-button'), function(i, n) {
    $(n).show();
  });

  this.removeClass('button-arrow-none');

  this.find('.form-submit-processing').remove();

  return this;

};

$.fn.showMessages = function( messages, options ) {

  if ( messages ) {

    var $this      = this,
        $container = $this.find('.messages'),
        msgs       = [],
        fade       = true;

    options = options || {};

    fade = (typeof(options.fade) != 'undefined') ? options.fade : true;

    if ( !$container.length ) {

      $container = $('<div class="messages"></div>').hide().addClass( options.classes || '' );

      if ( options.floating && options.floating === true ) {
        $container.addClass('messages-floating');
      }

      if ( options.prepend && options.prepend === true ) {
        $container.prependTo( $this );
      }
      else {
        $container.appendTo( $this );
      }

    }

    $.each( messages, function(i, msg) {

      this.type = this.type || 'message';

      switch ( this.type ) {

        case 'error' :
        case 'message' :
        case 'success' :
          break;

        default :
          throw ('"' + this.type +'" is not a valid message type');

      } // switch type

      msgs.push('<div class="' + this.type + '"><span class="icon-status-' + this.type + ' icon sprite-site-elements"></span>' + this.message + '</div>');

    });

    $container.html( msgs.join('') ).show();

    if (fade) {
      $container.delay(5000).fadeOut(1000);
    }

  }

  return this;

}; // showMessages

// Normalizes an object so does not return object, since it's a reference anywho
$.legacyNormalize = function( params ) {

  // loop through params
  $.each( params, function( key, value ) {

    // replace dashes out of key
    var replacement = key.replace( /-/g, '_' );

    // if key is now different, set new key and delete old
    if ( replacement !== key ) {

      params[ replacement ] = value;
      delete( params[key] );

    }

  });

  return true;

}; // legacyNormalize

$.fn.defaultValue = function(defaultVal) {

  $.each(this, function() {

    var self = $(this);

    var getValue = function() {
      return self.val();
    };

    var setValue = function(val) {
      self.val(val);
    };

    var onFocus = function() {
      if (getValue() == defaultVal) {
        setValue('');
      }
    };

    var onBlur = function() {
      if (getValue() == '') {
        setValue(defaultVal);
      }
    };

    $.fn.defaultValue.destroy = function() {
      setValue('');
      self.unbind('focus.defaultValue', onFocus);
      self.unbind('blur.defaultValue', onBlur);
    };

    self.bind('focus.defaultValue', onFocus);
    self.bind('blur.defaultValue', onBlur);
    self.data('defaultVal', defaultVal);

    self.trigger('blur');

  });

  return this;

};

$.fn.clonePosition = function(src_obj, passed_params) {

  var params   = $.extend({
                   setWidth      : true,
                   setHeight     : true,
                   setLeft       : true,
                   setTop        : true,
                   offsetLeft    : 0,
                   offsetTop     : 0,
                   func          : 'offset',
                   factor_scroll : false
                 }, passed_params),
      offset   = src_obj[params.func](),
      cssRules = {};

  // using absolute position so make sure scrollbars are thought of
  if ( params.factor_scroll === true ) {
    offset.top  += Config.$document.scrollTop();
    offset.left += Config.$document.scrollLeft();
  }

  if (params.setWidth) {
    cssRules.width = src_obj.outerWidth() + 'px';
  }

  if (params.setHeight) {
    cssRules.height = src_obj.outerHeight() + 'px';
  }

  if (params.setLeft) {
    cssRules.left =  (offset.left + parseInt( params.offsetLeft, 10 )) + 'px';
  }

  if (params.setTop) {
    cssRules.top =  (offset.top + parseInt( params.offsetTop, 10 )) + 'px';
  }

  return this.css(cssRules);

};

$.popup = function( params ) {

  return $('<div />')
          .popup({input: $('#popup-template')
              .template(params)
            });

}; // popup

// determine proper scroll element for all browsers
$.scrollElement = function() {

  return ( $.browser.opera ) ? $('html:not(:animated)') : $('html:not(:animated), body:not(:animated)');

}; // scrollElement

$.fn.attrToInt = function( attribute ) {

  var ret = parseInt( $(this).attr(attribute), 10 );

  if ( !$.isValidNumber( ret ) ) {
    return 0;
  }

  return ret;

};

$.isValidNumber = function( x ) {

  if ( typeof x !== 'number' || isNaN( x ) || x === Infinity ) {
    return false;
  }

  return true;

}; // isValidNumber
