function addEvent(obj, type, fn) {
    if (obj.addEventListener) {
        obj.addEventListener(type, fn, false);
    } else if (obj.attachEvent) {
        var real_func = "e" + type + fn;
        var func = type + fn;
        
        obj[real_func] = fn;
        obj[func] = function() { obj[real_func](window.event); }
        obj.attachEvent("on" + type, obj[func]);
    }
}


function createElement(element) {
    if (document.createElementNS) {
        return document.createElementNS('http://www.w3.org/1999/xhtml', element);
	}
    
    if (document.createElement) {
        return document.createElement(element);
    }
    
	return false;
}


function stylizeBoxes() {
    var divs = document.getElementsByTagName('div');
    var boxes = [];
    
    var div;
    for (var i = 0; div = divs[i]; i++) {
        if ($(div).hasClassName('box')) {
            boxes.push(div);
        }
    }
    
    var box;
    for (var i = 0; box = boxes[i]; i++) {
      var new_box = createElement('div');
      new_box.className = box.className;
      new_box.id = box.id;
      box.id = '';
      box.parentNode.replaceChild(new_box, box);
          
      var box_top_left = createElement('div');
      box_top_left.className = 'box-top-left';
      new_box.appendChild(box_top_left);
          
      var box_top_right = createElement('div');
      box_top_right.className = 'box-top-right';
      box_top_left.appendChild(box_top_right);
          
      var box_bottom_left = createElement('div');
      box_bottom_left.className = 'box-bottom-left';
      box_top_right.appendChild(box_bottom_left);
          
      var box_bottom_right = createElement('div');
      box_bottom_right.className = 'box-bottom-right';
      box_bottom_left.appendChild(box_bottom_right);
          
      box.className = 'box-content';
      box_bottom_right.appendChild(box);
    }
    
    // IE iframe bug
    if (navigator.userAgent.indexOf('MSIE') != -1) {
      var iframes = document.getElementsByTagName('iframe');
      for (var i = 0; iframe = iframes[i]; i++) {
        iframe.style.display = 'none';
        iframe.style.display = 'block';
      }
    }
}

var current_submenu = null;
var show_submenu_timeout_var = null;
var show_submenu_timeout = 150;

var button_to_menu = [];
button_to_menu[0] = { image : 'chessdom_button_tournaments', menu : 'div_chessdom_title_tournaments_mini_buttons' };
button_to_menu[1] = { image : 'chessdom_button_players', menu : 'div_chessdom_title_players_mini_buttons' };
button_to_menu[2] = { image : 'chessdom_button_games', menu : 'div_chessdom_title_games_mini_buttons' };
button_to_menu[3] = { image : 'chessdom_button_multimedia', menu : 'div_chessdom_title_multimedia_mini_buttons' };
button_to_menu[4] = { image : 'chessdom_button_theory', menu : 'div_chessdom_title_theory_mini_buttons' };
button_to_menu[5] = { image : 'chessdom_button_fun', menu : 'div_chessdom_title_fun_mini_buttons' };
button_to_menu[6] = { image : 'chessdom_button_fans', menu : 'div_chessdom_title_fans_mini_buttons' };
button_to_menu[7] = { image : 'chessdom_button_history', menu : 'div_chessdom_title_history_mini_buttons' };


function getMenuId(image_id) {
  var x;
  
  button_to_menu.each(function(obj) {
    if (obj.image == image_id) {
      x = obj.menu;
    }
  });
  
  return x;
}


function menuImageOver(event) {
  if (current_submenu != null) {
    $(current_submenu).hide();
  }
  
  var image_id = Event.element(event).id;
  var menu_id = getMenuId(image_id);
  $(menu_id).style.display = 'block';
  current_submenu = menu_id;
}


function menuMove(event) {
  Event.stop(event);
}


function initSubmenus() {
  button_to_menu.each(function(obj) {
    var image = $(obj.image);
    image.observe('mouseover', menuImageOver);
    image.observe('mousemove', menuMove);
    var menu = $(obj.menu);
    menu.observe('mousemove', menuMove);
  });
  
  document.body.onmousemove = function() { if (current_submenu != null) $(current_submenu).hide(); }
}


function initialize() {
  stylizeBoxes();
  initSubmenus();
}


function deinitialize() {
  button_to_menu.each(function(obj) {
    var image = $(obj.image);
    image.stopObserving('mouseover', menuImageOver);
    image.stopObserving('mousemove', menuMove);
    var menu = $(obj.menu);
    menu.stopObserving('mousemove', menuMove);
  });
  
  document.body.onmousemove = null;
}


window.onload = initialize;
window.onunload = deinitialize;
