jQuery.fn.dataset = function(name) {
  if (this.length == 0) {
    return null;                        // no nodes, return
  }
  if (this.length == 1) {               // for a single node, return value
    if (this[0].dataset != undefined) {
      return this[0].dataset[name];
    }
    return this.attr("data-"+name);     // fallback if dataset is not a member of the node
  }
  var values = {};                      // multiple nodes, return dictionary from object id to value
  this.each(function(o) {
    values[$(o).attr("id")] = $(o).dataset(name);
  });
  return values;
};

/*
 * jQuery autoResize (textarea auto-resizer)
 * @copyright James Padolsey http://james.padolsey.com
 * @version 1.04
 */
/**/

//$('#eBoxText').autoResize({extraSpace: 0, limit: 300});

(function($) {
  $.fn.autoResize = function(options) {
    var settings = $.extend({
      onResize: function() {},
      animate: true,
      animateDuration: 150,
      animateCallback: function() {},
      extraSpace: 20,
      limit: 1000
    }, options);
    this.filter('textarea').each(function() {
      var textarea = $(this).css({resize:'none','overflow-y':'hidden'}),
      origHeight = textarea.height(),
      clone = (function() {
        var props = ['height','width','lineHeight','textDecoration','letterSpacing'],
        propOb = {};
        $.each(props, function(i, prop) {
          propOb[prop] = textarea.css(prop);
        });
        return textarea.clone().removeAttr('id').removeAttr('name').css({
          position: 'absolute',
          top: 0,
          left: -9999
        }).css(propOb).attr('tabIndex','-1').insertBefore(textarea);
      })(),
      lastScrollTop = null,
      updateSize = function() {
        clone.height(0).val($(this).val()).scrollTop(10000);
        var scrollTop = Math.max(clone.scrollTop(), origHeight) + settings.extraSpace,
        toChange = $(this).add(clone);
        if (lastScrollTop === scrollTop) {
          return;
        }
        lastScrollTop = scrollTop;
        if ( scrollTop >= settings.limit ) {
          $(this).css('overflow-y','');
          return;
        }
        settings.onResize.call(this);
        settings.animate && textarea.css('display') === 'block' ?
        toChange.stop().animate({height:scrollTop}, 
        settings.animateDuration, 
        settings.animateCallback)
        : toChange.height(scrollTop);
      };
      textarea.unbind('.dynSiz')
              .bind('keyup.dynSiz', updateSize)
              .bind('keydown.dynSiz', updateSize)
              .bind('change.dynSiz', updateSize);
    });
    return this;
  };
})(jQuery);

jQuery.fn.outerHtml = function(include_scripts) {
  if (include_scripts === undefined){ include_scripts = false; }
  var clone = this.clone();
  var items = jQuery.map(clone, function(element){
    if (jQuery.nodeName(element, "script")){
      if (include_scripts){
        var attributes;
        if (element.attributes){
          attributes = jQuery.map(element.attributes, function(attribute){
            return attribute.name + '="' + attribute.value + '" ';
          });
        }
        return '<' + element.nodeName + ' ' + attributes.join(' ') + ">" + jQuery(element).html() + "</" + element.nodeName +'>';
      } else {
        return '';
      }
    } else {
      return jQuery('<div>').append(element).remove().html();
    }
  });
  return items.join('');
}

//and use it like this:
//$('#div1').swap('#div2');
jQuery.fn.swap = function(b){ 
    b = jQuery(b)[0]; 
    var a = this[0]; 
    var t = a.parentNode.insertBefore(document.createTextNode(''), a); 
    b.parentNode.insertBefore(a, b); 
    t.parentNode.insertBefore(b, t); 
    t.parentNode.removeChild(t); 
    return this; 
};
  
  
