/**
 * Helper funkce a promenne pro hl. menu
 */
var menu_timeout    = 500;
var closetimer = null;
var submenuSource = null;
var submenuHolder = null;

function menu_open()
{  menu_canceltimer();
    menu_close();
    if ($(this).attr('rel')) {
        submenuSource = $('#'+$(this).attr('rel'));
        submenuHolder = $('#submenu-data');
        submenuHolder.html(submenuSource.html());
        submenuHolder.addClass(submenuSource.attr('rel'));
        submenuHolder.show();
    }
}

function menu_close()
{
    if(submenuHolder) {
        submenuHolder.html(''); 
        submenuHolder.removeClass(submenuSource.attr('rel'));
        submenuHolder.hide();
    }
}

function menu_timer()
{  closetimer = window.setTimeout(menu_close, menu_timeout);}

function menu_still()
{  menu_canceltimer(); }

function menu_canceltimer()
{  if(closetimer)
{  window.clearTimeout(closetimer);
    closetimer = null;}}

/**
 * Inicializace hl. menu na webu
 */
function initMainMenu()
{
    $('#navigation').delegate('a.subtitle', 'click', function(event) {
        var self = $(this);
    
        self.qtip({
            overwrite: true, // Make sure we only render one tooltip
            content: {
                text: self.next('ul').clone() // Use the submenu as the qTip content
            },
            position: {
                my: 'top left',
                at: 'bottom left',
                
                // Append the nav tooltips to the #navigation element (see show.solo below)
                container: $('#navigation'),
                viewport: $(window),
                adjust: { 
                    method: 'shift flip' 
                }
            },
            show: {
                event: event.type, // Make sure to sue the same event as above
                ready: true, // Make sure it shows on first mouseover
                solo: true
            },
            hide: {
                delay: 200,
                event: 'click mouseleave',
                fixed: true // Make sure we can interact with the qTip by setting it as fixed
            },
            style: {
                classes: 'ui-tooltip-nav', // Basic styles
                tip: false // We don't want a tip... it's a menu duh!
            },
            events: {
                /*
                * If you'd like to hide ALL parent menus when we mouse out of this menu
                * simply uncomment the code below!
                *
                *   hide: function(event, api) {
                *      var oEvent = event.originalEvent || event,
                *         parentMenu = api.elements.target.parents(qtip),
                *         ontoMenu = $(oEvent.relatedTarget || oEvent.target).closest(qtip).length;
                *
                *    if(!ontoMenu) { parentMenu.qtip('hide', oEvent); }
                * },
                */
    
                // Toggle an active class on each menus activator
                //toggle: function(event, api) {
                //    api.elements.target.toggleClass('active', event.type === 'tooltipshow');
                //}
            }
        });
        return false;
    });
}

