$(document).ready(function(){
    
    var elements = new Array;
    $(".joke a, .joke span").css({
        position: 'absolute'
    });
    
    var oldX, oldY, maxX, maxY, newX, newY;
    
    maxX = $("body").width();
    maxY = $("body").height();
    
    
    $(".joke a, .joke span").live('mouseover', function(){
        
        oldX = parseInt($(this).css('marginLeft'));
        oldY = parseInt($(this).css('marginTop'));
 
        if (!$(this).hasClass("fake"))
        {
                   
            $(this).css({
                marginTop: oldX + 'px',
                marginLeft: oldY + 'px'
            }, 'fast');


            $(this).attr("rel", oldX + ":" + oldY);
        }
        
        $(this).addClass("fake");
        
        newX = Math.floor(Math.random()*(maxX - $(this).width()));
        newY = Math.floor(Math.random()*(maxY - $(this).height()));

        $(this).animate({
            marginTop: newY + 'px',
            marginLeft: newX + 'px'
        }, 'fast');
        
    });
    
    var reset = function(){
        var rel;
        $.each($(".fake"), function(k, element){
            rel = $(element).attr("rel");
            rel = rel.split(":");
            
            $(element).animate({
                marginTop: rel[1] + 'px',
                marginLeft: rel[0] + 'px'
            }, 100)
            .removeClass("fake");
        });
        
    };
    
    var x,y;
    $("body").mousemove(function(e){
        x = e.clientX;
        y = e.clientY;
        
        if (x > maxX - 100 || y > maxY - 100)
        {
            reset();
        }
    });
});
