﻿var menu_hover_current = null;
var menu_hover_current_hack = null;
var menu_hover_timeout = null;

function menu_hover_change(dest)
{
  if (menu_hover_timeout) {
    clearTimeout(menu_hover_timeout);
    menu_hover_timeout = null;
  }
  if (menu_hover_current == dest) return;
  if (menu_hover_current) {
    menu_hover_current.className = menu_hover_current.className.replace(/ hover(_icon)?/g, "");
  }
  if (dest) {
    dest.className += " hover";
    if (/icon/.test(dest.className)) dest.className += " hover_icon";
  }
  menu_hover_hack(dest);
  menu_hover_current = dest;
}

function menu_hover_hack(dest)
{
  // hack for IE6
  if (navigator.appName != "Microsoft Internet Explorer") return;
  if (!/MSIE 6/.test(navigator.userAgent)) return;
  if (menu_hover_current_hack && menu_hover_current_hack.parentNode) {
    menu_hover_current_hack.parentNode.removeChild(menu_hover_current_hack);
  }
  if (!menu_new || !dest) return;
  if (!menu_hover_current_hack) {
    var blank = 'about:blank';
    // about:blank is not secure enough to be mixed with https content -- work around
    if (window.location.protocol === 'https:') blank = '/blank.html';
    menu_hover_current_hack = document.createElement('IFRAME');
    menu_hover_current_hack.src = blank;
    menu_hover_current_hack.style.zIndex = -1;
    menu_hover_current_hack.style.position = 'absolute';
  }
  var submenu = dest.getElementsByTagName('UL')[0];
  submenu.parentNode.appendChild(menu_hover_current_hack);
  menu_hover_current_hack.style.left = submenu.offsetLeft + 'px';
  menu_hover_current_hack.style.top = submenu.offsetTop + 'px';
  menu_hover_current_hack.style.width = submenu.offsetWidth + 'px';
  menu_hover_current_hack.style.height = submenu.offsetHeight + 'px';
}

function menu_hover_add_events(node)
{
  var old_onmouseover = node.onmouseover ? node.onmouseover : function() {};
  node.onmouseover = function() {
    old_onmouseover();
    menu_hover_change(this);
  }

  var old_onmouseout = node.onmouseout ? node.onmouseout : function() {};
  node.onmouseout = function() {
    old_onmouseout();
    if (menu_hover_timeout) clearTimeout(menu_hover_timeout);
    menu_hover_timeout = setTimeout('menu_hover_change(null);', menu_new ? 350 : 1000);
  }
}

function menu_hover_init()
{
  if (!document.getElementById) return;
  
  var menu = document.getElementById("page_menu");
  var children = menu.childNodes;
  for (var i = 0; i < children.length; i++) {
    var node = children[i];
    if (node.className && node.className.indexOf("tab") != -1) {
      menu_hover_add_events(node);
    }
  }
}

var menu_new = false;
var menu_header_element = null;
var menu_toggle_element = null;

function menu_style_init()
{
  if (!document.getElementById) return;

  menu_header_element = document.getElementById('page_header');
  if (!menu_header_element) return;

  menu_header_element.className += " menu_old_toggle menu_toggle"
  menu_new = GetCookie('m') !== 'o';
  menu_style_update();
}

function menu_toggle_init()
{
  menu_toggle_element = document.createElement('BUTTON');
  menu_toggle_element.id = 'page_menu_toggle';
  menu_toggle_element.onclick = function() {
    menu_new = !menu_new;
    menu_style_update();
  };
  menu_header_element.appendChild(menu_toggle_element);

  menu_style_update();
}

function menu_style_update()
{
  var toggle_text;
  if (menu_new) {
    toggle_text = 'klasyczne menu';
    menu_header_element.className = menu_header_element.className.replace(/menu_old/g, 'menu_new');
  } else {
    toggle_text = 'nowe menu';
    menu_header_element.className = menu_header_element.className.replace(/menu_new/g, 'menu_old');
  }
  if (menu_toggle_element) menu_toggle_element.innerHTML = '<span class="raquo">&raquo;</span><span> ' + toggle_text + '</span>';
  menu_hover_hack(menu_hover_current);
  SetCookie('m', menu_new ? 'n' : 'o');
}
