﻿$(function () {

    var menuAutoHideDelay = 1000; // ms

    // init level 0 items
    $('li.nav0').each(function () {

        var id = $(this).attr('id').substr(5);
        var li = $(this);

        $('a.navigation', this).mouseenter(function () {

            closeAllMenusExceptId(id);

            var nav1bar = getLevel1MenuById(id);

            // clear the auto hide timeout
            clearTimeout(nav1bar.data('hideTo'));

            // set nav1bars x coords
            nav1bar.css('left', li.position().left + 'px');
            //nav1bar.css('left', '200px');

            nav1bar.css('top', '0px');


            // make fully visible
            nav1bar.show();

        });

        $(this).mouseleave(function () {

            var nav1bar = getLevel1MenuById(id);

            // set the auto hide timeout
            var to = setTimeout(function () {
                // hide self
                nav1bar.hide();
            }, menuAutoHideDelay);
            nav1bar.data('hideTo', to);



        });

    });

    // init level 1 items
    $('.nav1-bar').each(function () {


        var id = $(this).attr('id').substr(5);
        var li = $('.nav0#nav0-' + id);

        var lp = li.position();

        //$(this).css('position', 'absolute')
        //       .css('top', lp.top + 'px')
        //               .css('left', lp.left + 'px')
        //;

        //console.info(li);

        $(this).mouseenter(function () {

            var li = $('.nav0#nav0-' + id);
            var lp = li.position();

            //$(this).css('left', lp.left + 'px');

            // clear the auto hide timeout
            clearTimeout($(this).data('hideTo'));

            // make fully visible
            this.show();

        });

        $(this).mouseleave(function () {
            var self = $(this);

            // set the auto hide timeout
            var to = setTimeout(function () {
                // hide self
                self.hide();
            }, menuAutoHideDelay);
            $(this).data('hideTo', to);

        });


    });

});



function microtime2(get_as_float) {
    // Returns either a string or a float containing the current time in seconds and microseconds  
    // 
    // version: 1102.614
    // discuss at: http://phpjs.org/functions/microtime
    // +   original by: Paulo Freitas
    // *     example 1: timeStamp = microtime(true);
    // *     results 1: timeStamp > 1000000000 && timeStamp < 2000000000
    var now = new Date().getTime() / 1000;
    var s = parseInt(now, 10);

    return (get_as_float) ? now : (Math.round((now - s) * 1000) / 1000) + ' ' + s;

}

function microtime() {
    return microtime2(true) * 1000;
}

function getLevel1MenuById(id) {
    return $('div#nav1-' + id).first();
}

function closeAllMenusExceptId(except_id) {
    $('.nav1-bar').each(function () {
        var id = $(this).attr('id').substr(5);

        if (except_id != id)
            $(this).hide();
    });
}











