
$(function() {
    var active = null;

    $('a.pick').each(function() {
        var a = $(this);
        var parent = a.parent();

        parent.click(function() {
            $(document).unbind('click');
            if(active) {
                active.fadeOut();
            }
            var div = $(this).data('analysis');
            if(!div) {
                var href = a.attr('href') + '?request';
                var div = $('<div></div>');
                div.addClass('analysis');
                $(this).data('analysis', div)
                div.load(href, '', function() {div.prepend('<img src="/style/img/close.gif" width="30" heigt="30" alt="" class="close" />');});
                div.prepend('<img src="/style/img/close.gif" width="30" heigt="30" alt="" class="close" /><img src="/style/img/ring_animation.gif" width="140" heigt="140" alt="" class="ring" />');
                var o = parent.offset();
                var left = o.left;
                var top = o.top;
                if((left < 0) || (top < 0)) {
                    left = top = 0;
                    parent.parents().each(function(i) {
                        try {
                            if(i < 5) {
                                var o = $(this).offset();
                                left = left + o.left;
                                top = top + o.top;
                            }
                        } catch(error) {}
                    });
                }
                div.css({'left': left+'px', 'top': top+'px'});
                $('body').append(div);
            }
            active = div;
            div.fadeIn();

            setTimeout( function() {
                $(document).click( function() {
                    $(document).unbind('click');
                    div.fadeOut();
                    return false;
                });
            }, 0);

            return false;
        });
    });
});
