Square = function($obj){
    
    this.$ = $obj;
    var thiss = this;
    this.$.mouseenter(function(){
        thiss.$.stop();
        thiss.paused = true;
    }).mouseout(function(){
        thiss.paused = false;
        thiss.move();
    });
    
    this.height = this.$.outerHeight();
    this.width = this.$.outerWidth();
    
    this.dx = Math.round(Math.random()*this.moveRatio-(this.moveRatio/2));
    this.dy = Math.round(Math.random()*this.moveRatio-(this.moveRatio/2));
    this.$.css({
        position: "absolute",
        left: Math.round(Math.random()*800),
        top: Math.round(Math.random()*500)
    });
    thiss.move();
}

Square.prototype.moveRatio = 200;
Square.prototype.animTime = 500;

Square.prototype.winX = $(window).width()
Square.prototype.winY = $(window).height()

Square.prototype.move = function(){
    var thiss = this;
    if(this.paused){
        return;
    }
    var left = this.newX || this.$.position().left;
    var top =  this.newY || this.$.position().top;
    var maxLeft = this.winX-this.width;
    var maxTop =  this.winY-this.height;
            
    this.newX = left+this.dx;
    this.newY = top+this.dy;
            

    if(this.newX<0 || this.newX>maxLeft){
        this.dx = -this.dx;
    }
    if(this.newY<0 || this.newY>maxTop){
        this.dy = -this.dy;
    } 

    this.$.animate({
        left: this.newX,
        top: this.newY
    },this.animTime,"linear", function(){
        thiss.move()
    });
}

$(document).ready(function(){
    var SquaresList = [];
    $("a").each(function(){
        SquaresList.push( new Square( $(this) ) );
    }); 
})
