(function($){
    
    /* ----
       superLink jQuery plugin
       (c) James Padolsey
       ----
       Contributors:
            Brian Fisher
    */
    
    $.fn.superLink = function(link) {
        link = link || 'a:first';
		
        return this.each(function(){
            
            var $container = $(this),
                $targetLink = $(link, $container).clone(true);
            
            /* Take all current mouseout handlers of $container and
               transfer them to the mouseout handler of $targetLink */
            var mouseouts = $(this).data('events') && $(this).data('events').mouseout;
            if (mouseouts) {
                $.each(mouseouts, function(i, fn){
                    $targetLink.mouseout(function(e){
                        fn.call($container, e);
                    });
                });
                delete $(this).data('events').mouseout;
            }
            
            /* Take all current mouseover handlers of $container and
               transfer them to the mouseover handler of $targetLink */
            var mouseovers = $(this).data('events') && $(this).data('events').mouseover;
            if (mouseovers) {
                $.each(mouseovers, function(i, fn){
                    $targetLink.mouseover(function(e){
                        fn.call($container, e);
                    });
                });
                delete $(this).data('events').mouseover;
            }
			
			var targetToWrap = $container.find('td:first');
			if (targetToWrap.contents().length) {
				targetToWrap.contents().wrapAll('<div class="linkWrapper" />');
			} else {
				targetToWrap.html('<div class="linkWrapper">&nbsp;</div>'); 
			}
			var linkWrapper = $container.find('div.linkWrapper');
			linkWrapper.css('position', 'relative');
			
			var offsetTop = targetToWrap.position().top - linkWrapper.position().top;
			
            $targetLink
                .click(function(){
                    $targetLink.blur();
                })
                .css({
                    position: 'absolute',
                    top: offsetTop,
                    left: -($container.find('td:first').css('padding-left').replace("px", "")),
                    /* IE requires background to be set */
                    backgroundColor: '#FFF',
					display: 'block',
					opacity: 0,
                    width: $container.outerWidth(),
                    height: $container.outerHeight(),
                    padding: 0
                })
                .appendTo(linkWrapper);
                
        });
    };
    
})(jQuery);
