/*
 * Note, this code is provided as a starting point only.
 * You should understand the code and see how you can
 * integrate it into your own.
 *
 * No guarantees given.
 * 
 */

(nolib = window.nolib || {}).velocitor = function(config) {  
    var nav = config.elem,
        vector = [],
        lvector = [],
        velocity = config.velocity || 0,
        t,
        interval = config.interval || 25,
        threshold = config.threshold || 0.5,
        fire = function() {
            config.show();
        },
        load = function() {
            vector = [];
            lvector = [];
            config.hide();
            return function() {
                fire();
                trigger = function() {};
            };
        },
        trigger = load(),
        sample = function() {
            velocity = vector[2]/interval;
            if (lvector.length == 3 && (lvector[2] === vector[2] || velocity < threshold)) {
                trigger();
            }
            lvector = [].concat(vector);
        };
        
        
    nav.onmouseover = function(e) {
        document.onmousemove = function(e) {
            vector[0] = (e || window.event).clientX - (lvector[0] || 0);
            vector[1] = (e || window.event).clientY - (lvector[1] || 0);
            vector[2] = Math.sqrt(Math.pow(vector[0], 2) + Math.pow(vector[1], 2));
        };
        t = window.setTimeout(function() {
            sample();
            t = window.setTimeout(arguments.callee, interval);
        }, interval);
    };
    nav.onmouseout = function() {
        document.onmousemove = null;
        clearTimeout(t);
        trigger = load();
    };
};

/* sample usage
  
window.onload = function() {
    var dd = document.getElementById("dd");
    nolib.velocitor({
        elem: document.getElementById("nav"),
        show: function() {
             dd.style.display = "block";
        },
        hide: function() {
            dd.style.display = "none";
        }  
    });
};

*/