(function($){
  '$:nomunge';

  var cache = {},

    doTimeout = 'doTimeout',

    aps = Array.prototype.slice;

  $[doTimeout] = function() {
    return p_doTimeout.apply( window, [ 0 ].concat( aps.call( arguments ) ) );
  };

  $.fn[doTimeout] = function() {
    var args = aps.call( arguments ),
      result = p_doTimeout.apply( this, [ doTimeout + args[0] ].concat( args ) );

    return typeof args[0] === 'number' || typeof args[1] === 'number'
      ? this
      : result;
  };

  function p_doTimeout( jquery_data_key ) {
    var that = this,
      elem,
      data = {},

      args = arguments,
      slice_args = 4,
      
      id        = args[1],
      delay     = args[2],
      callback  = args[3];
    
    if ( typeof id !== 'string' ) {
      slice_args--;
      
      id        = jquery_data_key = 0;
      delay     = args[1];
      callback  = args[2];
    }

    if ( jquery_data_key ) {

      elem = that.eq(0);
      elem.data( jquery_data_key, data = elem.data( jquery_data_key ) || {} );
      
    } else if ( id ) {
      data = cache[ id ] || ( cache[ id ] = {} );
    }

    data.id && clearTimeout( data.id );
    delete data.id;

    function cleanup() {
      if ( jquery_data_key ) {
        elem.removeData( jquery_data_key );
      } else if ( id ) {
        delete cache[ id ];
      }
    };

    function actually_setTimeout() {
      data.id = setTimeout( function(){ data.fn(); }, delay );
    };
    
    if ( callback ) {

      data.fn = function( no_polling_loop ) {
        callback.apply( that, aps.call( args, slice_args ) ) && !no_polling_loop

          ? actually_setTimeout()

          : cleanup();
      };

      actually_setTimeout();
      
    } else if ( data.fn ) {

      delay === undefined ? cleanup() : data.fn( delay === false );
      return true;
      
    } else {
 
      cleanup();
    }
    
  };
  
})(jQuery);
