


var StickyCursor = {
    load: function() 
    {
        this.on = true;

        var optD = 10;
        var optMotionStopInterval = 50;

        // var img = $('<img/>').attr('src','css/images/cursor-blog.png');
        var cur = $('<div/>').addClass('custom-cursor');
        cur.css({
            zIndex:100,
            position: 'absolute',
            top: 0,
            left: 0
        }).hide();
        $(document.body).append( cur );

        cur.fadeIn('slow');


        function iconMotion( cur , tx , ty ) {
            var cx = cur.offset().left;
            var cy = cur.offset().top;
            var dx = - (( cx - tx ) * 1.0) / optD;
            var dy = - (( cy - ty ) * 1.0) / optD;
            var mx = parseInt(cx + dx) + 3;
            var my = parseInt(cy + dy) + 3;
            cur.css({ left: mx, top: my });
        }

        var animated;


        var onMove = function(e) {
            var tx = e.pageX;
            var ty = e.pageY;
            iconMotion( cur , tx, ty);
            if( animated ) {
                clearInterval( animated );
                animated = null;
            }
            animated = window.setInterval( function() { 
                iconMotion( cur , tx, ty );

                var offset = cur.offset();
                if( offset.left == tx && offset.top == ty ) {
                    clearInterval( animated );
                    animated = null;
                }
            }, optMotionStopInterval );
        };
        this.onMove = onMove;
        $(document.body).bind('mousemove', this.onMove);
    },

    unload: function() 
    {
        this.on = false;
        $('.custom-cursor').fadeOut('slow',function() {
                $(this).remove();
        });
        $(document.body).unbind( 'mousemove', this.onMove );
    },
    toggle: function() 
    {
        ( this.on ) ? this.unload() : this.load();
    }
};

$(document.body).ready( function() {
    StickyCursor.load();
});


