window.onload = init;

function init() {
    if (document.getElementById && document.getElementsByTagName) {
        var myMenu = document.getElementById("menu").getElementsByTagName("A");
        if (!myMenu) { return; }
        else {
            for (var i=0;i<myMenu.length;i++) {
                myMenu[i].onmouseover = navHoverStyle;
                myMenu[i].onfocus = navHoverStyle;
            }
            document.getElementById("menu").style.visibility = "visible";
        }
    }
}

// Stores the currently open UL objects
var openMenus = new Array();

// Stores the timer for closing the menu
var navTimer;

function navHoverStyle(e) {
    if (!e) var e = window.event;
    if (e.target) var tg = e.target;
    else if (e.srcElement) var tg = e.srcElement;

    var linkElm = tg;
    while (linkElm.nodeName != 'A')
        linkElm.parentNode;

    while (tg.nodeName != 'LI')
        tg = tg.parentNode;

    // Determine if and if so, which submenu items to close
    var tgParent = tg.parentNode;
    while (tgParent.nodeName != 'UL')
        tgParent = tgParent.parentNode;

    if (tgParent.id == 'menu') {
        closeAll(0);
    }
    else {
        var j=0;
        while (openMenus[j] != tgParent) {
            j++;
        }
        closeAll(j+1);
    }

    // Determine if the current item has a submenu and if so, open it
    for ( var i=0;i<tg.childNodes.length;i++) {
        if ( tg.childNodes[i].nodeName == 'UL') {
            var subMenuElm = tg.childNodes[i];
        }
    }

    if (subMenuElm) {
        linkElm.className = 'unfolded';
        subMenuElm.style.display = 'block';
        openMenus.push(subMenuElm);
    }

    // Set the timer
    checkNavTimer();

    return false;
}

function checkNavTimer() {
    if (navTimer) clearTimeout(navTimer);
    navTimer = setTimeout('closeAll(0)',5000);
}

function closeAll(lvl) {
    var oMl = openMenus.length-1;
    for ( var i=oMl;i>=lvl;i--) {
        var linkElm = openMenus[i].previousSibling;
        while (linkElm.nodeName != 'A')
            linkElm = linkElm.previousSibling;

        linkElm.className = '';
        openMenus[i].style.display = 'none';
        openMenus.pop();
    }
}


function parseId(mixedId,key)
{
    var arrId = mixedId.split('_');
    if(arrId.length<(key+1)) {
        return 0;
    }
    return parseInt(arrId[key],10);
}

$(function() {
   $('#facebook').hover(
      function () {
         t=setTimeout(function(){
             if ($('#facebook').css('right')=='-296px')
             {
                 $('#facebook').animate({ right: '+=296' }, 500, function() {});
             }
         }, 500);
      },
      function () {
        clearTimeout(t);
        if ($('#facebook').css('right')=='0px')
        {
            $('#facebook').animate({right: '-=296' }, 500, function() {});
        }
      }
   );
   
   $('#kontakt').hover(
      function () {
         t=setTimeout(function(){
             if ($('#kontakt').css('right')=='-296px')
             {
                 $('#kontakt').animate({right: '+=296' }, 500, function() {});
             }
         }, 500);
      },
      function () {
        clearTimeout(t);
        if ($('#kontakt').css('right')=='0px')
        {
            $('#kontakt').animate({right: '-=296'}, 500, function() {});
        }
      }
   );

   $('#voxr').hover(
      function () {
         t=setTimeout(function(){
             if ($('#voxr').css('right')=='-296px')
             {
                 $('#voxr').animate({right: '+=296' }, 500, function() {});
             }
         }, 500);
      },
      function () {
        clearTimeout(t);
        if ($('#voxr').css('right')=='0px')
        {
            $('#voxr').animate({right: '-=296'}, 500, function() {});
        }
      }
   );
   
   $('#domdecor').hover(
      function () {
         t=setTimeout(function(){
             if ($('#domdecor').css('right')=='-296px')
             {
                 $('#domdecor').animate({right: '+=296' }, 500, function() {});
             }
         }, 500);
      },
      function () {
        clearTimeout(t);
        if ($('#domdecor').css('right')=='0px')
        {
            $('#domdecor').animate({right: '-=296'}, 500, function() {});
        }
      }
   );
   
   if (parseInt(jQuery.browser.version)==6 && jQuery.browser.msie)
   {
       $('#facebook').css('display','none');
       $('#kontakt').css('display','none');
       $('#voxr').css('display','none');
       $('#domdecor').css('display','none');
   } else {
       $('#facebook').css('visibility','visible');
       $('#kontakt').css('visibility','visible');
       $('#voxr').css('visibility','visible');
       $('#domdecor').css('visibility','visible');
   }
});
