
/*
 * Reference to the element currently following the cursor, or null
 */
cursorCurrentShownElement = null;
cursorOffsetX = 10;
cursorOffsetY = 10;


/*
 * Update position of cursorCurrentShownElement
 */
function cursorUpdatePosition(evt) {
    var mouseX = function(evt) {
        if (!evt)
            evt = window.event;
        if (evt.pageX)
            return evt.pageX;
        else if (evt.clientX)
            return evt.clientX + (document.documentElement.scrollLeft ?  document.documentElement.scrollLeft : document.body.scrollLeft);
        else
            return 0;
    }
    
    var mouseY = function(evt) {
        if (!evt)
            evt = window.event;
        if (evt.pageY)
            return evt.pageY;
        else if (evt.clientY)
            return evt.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
        else
            return 0;
    }
    
    try {
        if(cursorCurrentShownElement) {
            cursorCurrentShownElement.style.left = (parseInt(mouseX(evt)) + cursorOffsetX) + 'px';
            cursorCurrentShownElement.style.top = (parseInt(mouseY(evt)) + cursorOffsetY) + 'px';
        }
    } catch(err) {
        // ingore
    }
}


/*
 * Start having an object following the cursor.
 * Arg 'element' is the object to follow the cursor.
 */
function cursorStartFollow(element) {
    cursorCurrentShownElement = element;
    document.onmousemove = cursorUpdatePosition;
    cursorUpdatePosition();
}


/*
 * Stop having an object following the cursor.
 * Arg 'callback' is a methid which can do anything, but
 * should remove (hide or delete) the current object
 * being showed. The callback method recieves a single
 * argument, which is the current object following the cursor.
 *
 * Argument is optional.
 */
function cursorStopFollow(callback) {
    if(typeof(callback) == 'function') {
        callback(cursorCurrentShownElement);
    }
    document.onmousemove = '';
    cursorCurrentShownElement = null;
}

