/*

Author:
  Ben Nadel / Kinky Solutions
  
  Link:
  http://www.bennadel.com/index.cfm?event=blog.view&id=1851

*/

// I select the first ancestor that matches the given
    // selector for each element in the collection.    
    jQuery.fn.closestParents = function( selector ){
      var result = $ ( [] );
 
      // Check to see if there is a selector. If not, then
      // we're just gonna return the parent() call.
      if (!selector){
 
        // Since there is no selector, the user simply
        // wants to return the first immediate parent
        // of each element.
        return( this.parent() );
 
      }
 
      // Loop over each element in this collection.
      this.each(
        function( index, node ){
          // For each node, we are going to get all the
          // parents that match the given selector; but
          // then, we're only going to add the first
          // one to the ongoing collection.
          result = result.add(
            $ ( node ).parents( selector ).first()
          );
        }
      );
 
      // Return the new collection, pushing it onto the
      // stack (such that end() can be used to return to
      // the original collection).
      return(
        this.pushStack(
          result,
          "closestParents",
          selector
        )
      );
    };
