$(function() {
    var animation_points = [
        [93,90],
        [162,140],
        [240,105],
        [315,90],
        [495,48],
        [695,52]
    ];

    var fly = $("#fly"),
    last_point = animation_points[0];
    fly.css({marginLeft: last_point[0], marginTop: last_point[1]}).show(1).sprite({
        fps: 20,
        no_of_frames: 10
    }).spState(1).active();

    function move_fly() {
        var random_point = Math.floor(Math.random()*animation_points.length),
        next_point = animation_points[random_point],
        distance = 0;

        if (next_point[0] > last_point[0]) {
            fly.spState(1);
            distance = next_point[0] - last_point[0];
        } else if (next_point[0] < last_point[0]) {
            fly.spState(2);
            distance = last_point[0] - next_point[0];
        }

        var anim_speed = 2000 + distance*3;
        fly.fps(20).animate({marginLeft: next_point[0], marginTop: next_point[1]}, anim_speed, function(event) {
            last_point = next_point;
            fly.fps(12 + Math.floor(Math.random()*4));
        });
    }
    setInterval(move_fly, 6000);
});

