$(function()
{
  enable_main_menu_dropdowns();
});

function clone(element)
{
  var copy = element.clone().detach();
  copy.removeAttr('id');

  return copy;
}

function reload_frame()
{ document.location.href = document.location.href; }

function get_target(event)
{ return typeof event.target != 'undefined' ? event.target : event.srcElement; }

function reload()
{ document.location = document.location; }

function redirect(url)
{ document.location = url; }

function is_checked(checkbox)
{ return checkbox.is(':checked'); }

function uncheck(checkbox)
{ checkbox.removeAttr('checked'); }

function enable_main_menu_dropdowns()
{
  var popup = null;
  var menuitems = [];
  var timer = null;
  
  $('.dropdown').each(function()
  {
    var a = $(this);
    var li = a.closest('li');

    var items = [];
    
    li.find(':hidden').each(function()
    {
      var hidden = $(this);
      var obj = $("<div>" + hidden.val() + "</div>");
      
      var url = obj.find('.url').text();
      var caption = obj.find('.caption').text();
      
      var highlighted = hidden.hasClass('highlighted_menuitem') ? 1 : 0;
      
      items.push({url: url, caption: caption, highlighted: highlighted});
    });
    
    menuitems.push({element: li, items: items}); 
  });

  $('body').mouseover(function(event)
  {
    var target = get_target(event);

    var menuitem = match_menuitem(target);

    if (menuitem != null)
    {
      ensure_no_timer();
      if (has_popup())
        destroy_popup();
  
      var menudiv = menuitem.element.closest('ul.menu');
      create_popup(menuitem, menuitem.element.offset().left, menudiv.offset().top + menudiv.height());
    }
    else if (is_popup(target))
      ensure_no_timer();
    else
    {
      if (!is_popup(target) && has_popup() && target.closest('ul.menu').length == 0)
        destroy_popup_soon();
    }
  });
  
  function match_menuitem(item)
  {
    for (var i = 0; i < menuitems.length; i++)
      if (is_child_of(item, menuitems[i].element))
        return menuitems[i];

    return null;
  }
  
  function create_popup(menuitem, x, y)
  {    
    if (has_timer())
      clear_timer();

    popup = $("<div class='menu_popup' />");
    popup.css('left', x + 'px').css('top', y + 'px');

    var items = menuitem.items;
        
    for (var i = 0; i < items.length; i++)
    {
      var item = items[i];
      
      var div = $('<div/>');

      div.text(item.caption);
      
      if (item.highlighted == 1)
        div.addClass('highlighted_menuitem');
      
      set_div_click(div, item.url);
      
      popup.append(div);
    }
    
    $('body').append(popup);
  }

  function has_timer()
  { return timer != null; }
  
  function set_div_click(div, url)
  {
    div.click(function()
    {
      redirect(url);
    });
  }
  
  function is_child_of(a, b)
  {
    if (is_same_item(a, b))
      return true;
    
    var a_parent = a.parent();
    
    return a_parent != null && a_parent.length == 1 && !is_same_item(a, a_parent)
      ? is_child_of(a_parent, b) : false;
  }
  
  function is_same_item(a, b)
  { return a.get(0) == b.get(0); }

  function has_popup()
  { return popup != null; }
  
  function destroy_popup()
  {
    popup.remove();
    popup = null;
  }

  function clear_timer()
  {
    clearTimeout(timer);
    timer = null;
  }
  
  function destroy_popup_soon()
  { timer = setTimeout(destroy_popup, 500); }
  
  function is_popup(item)
  { return popup != null && is_child_of(item, popup); }
  
  function get_target(event)
  { return $(typeof event.target != 'undefined' ? event.target : event.srcElement); }
  
  function ensure_no_timer()
  {
    if (has_timer())
      clear_timer();
  }
}

function create_option(val, text)
{
  var option = $('<option/>');

  option.val(val);
  option.text(text);
  
  return option;
}

