/**
* Copyright(c) Plink Limited 2010. All rights reserved.
*/

var ie = document.all ? true : false
var max_menu = 0;
var trail = new Array();
var rows;
var cols;
var last_menu;

function menu_clicked(row, col)
{
   var cols2go = col - trail[0];
   var i = 0;
   var cur_arr = new Array();
   var count = 0;
   for (j in sections)
       if (++count == trail[0])
           cur_arr = sections[j]["submenus"];

      while (cols2go)
       {
          cols2go--;
          i++;
          row = row - trail[i] + 1;
          count = 0;
          for (j in cur_arr)
              if (++count == trail[i])
                  cur_arr = cur_arr[j]["submenus"];
       }

   count = 0;
   for (j in cur_arr)
       if (++count == row)
           eval(cur_arr[j]["url"]);
}

function cell_over(row, col)
{
   if (trail.length)                                        // If no menus are displayed do nothing
   {
      var id = row + '_' + col;

      // If the current row has nothing in it then clear the menus
      if (document.getElementById('d' + id).innerHTML == '')
      {
         clear_menus();
         document.body.style.cursor = "auto";
      }
      else
      {
         if (last_menu == id)
             return;
         last_menu = id;
         var atr = (ie ? 'className' : 'class');
         var cur_menu = sections;
         for (t in trail)
         {
            var count = 0;
            for (var i in cur_menu)
                if (++count == trail[t])
                    cur_menu = cur_menu[i]["submenus"];
         }

         if (col < parseInt(trail[0]) + trail.length - 1)   // If the user has gone left of the rightmost menu
         {
            var upper = parseInt(trail[0]) + trail.length - 1;
            for (var j = parseInt(col) + 1;j <= upper;j++)
            {
               for (var i = 1;i <= rows;i++)
               {
                  var id2 = i + '_' + j;
                  document.getElementById('d' + id2).innerHTML = '';
                  document.getElementById('a' + id2).innerHTML = '';
                  document.getElementById('dt' + id2).style.visibility = 'hidden';
                  document.getElementById('s' + id2).setAttribute(atr,'submenu');
                  document.getElementById('b' + id2).setAttribute(atr,'submenu');
               }
               trail.pop();
               last_menu = 0;
            }
         }

         // See if this menu has a submenu
             count = 0;
         var offset = 0;
         for (var i = 1;i < trail.length;i++)               // Set the menu offset
             offset = offset + trail[i] - 1;

         for (var i in cur_menu)
             if (row == (++count + offset))
             {
                // Highlight and set the cursor

                document.getElementById('s' + id).setAttribute(atr,'sub_highlight');
                document.getElementById('b' + id).setAttribute(atr,'sub_highlight');
                document.body.style.cursor = "pointer";

                if (cur_menu[i]["submenus"])
                {
                   trail.push(row - offset);                // Remember it
                       var count2 = 0;
                   for (var j in cur_menu[i]["submenus"])
                   {
                      var id2 = parseInt(row - 1 + ++count2) + '_' + (1 + parseInt(col));
                      document.getElementById('d' + id2).innerHTML =  '&nbsp;' + j;
                      document.getElementById('dt' + id2).style.visibility = 'visible';

                      if (cur_menu[i]["submenus"][j]["submenus"])
                          document.getElementById('a' + id2).innerHTML = '>';
                   }
                }
             }
            else
            {
               document.getElementById('s' + (count + offset) + '_' + col).setAttribute(atr,'submenu');
               document.getElementById('b' + (count + offset) + '_' + col).setAttribute(atr,'submenu');
            }
      }
   }
   else
   {
      clear_menus();
      document.body.style.cursor = "auto";
   }
}

function clear_menus()
{
   document.getElementById('menu_div').style.zIndex = -1;
   var count = 0;
   var atr = (ie ? 'className' : 'class');

   for (i in sections)
   {
      count++;
      document.getElementById('m' + count).setAttribute(atr,'menu');
      document.body.style.cursor = "auto";
   }

   for (var r = 1;r <= rows;r++)
       for (var c = 1;c <= cols;c++)
       {
          var id = r + '_' + c;
          document.getElementById('a' + id).innerHTML = '';
          document.getElementById('d' + id).innerHTML = '';
          document.getElementById('dt' + id).style.visibility = 'hidden';
          document.getElementById('s' + id).setAttribute(atr,'submenu');
          document.getElementById('b' + id).setAttribute(atr,'submenu');
       }

   trail = new Array();
   last_menu = 0;
}

function menu_over(menu)
{
   clear_menus();
   document.getElementById('menu_div').style.zIndex = 20;

   if (ie)
      document.getElementById('m' + menu).setAttribute('className', 'highlight');
   else
      document.getElementById('m' + menu).setAttribute('class', 'highlight');

   var count = 0;
   for (i in sections)
   {
      count++;
      if (count == menu && sections[i]["submenus"])
      {
         var count2 = 0;
         for (j in sections[i]["submenus"])
         {
            count2++;
            document.getElementById('d' + count2 + '_' + menu).innerHTML =  '&nbsp;' + j;
            document.getElementById('dt' + count2 + '_' + menu).style.visibility = 'visible';
            if (sections[i]["submenus"][j]["submenus"])
                document.getElementById('a' + count2 + '_' + menu).innerHTML = '>';
         }
         trail.push(menu);
      }
   }

   if (document.getElementById('s' + menu))
      document.getElementById('s' + menu).style.visibility = 'visible';
   document.body.style.cursor = "pointer";
}

function write_menus(align, top, left)
{
   /*
   Objects from parents down to children:
       m1, m2...:       top menus (td)
       dt1_1, dt1_2...: sub menu  (div)
       s1_1, s1_2...:   sub menu  (td)
       d1_1, d1_2...:   sub menu  (div)
       b1_1, b1_2...:   arrow     (td)
       a1_1, a1_2...:   arrow     (div)
   */
   var offset = 0;
   var cell_height = 20;

   if (!align)
      align = 'center';

   document.write("<div style='position:absolute; ");
   if (left)
       document.write("left:" + left + "px; ");
   document.write("top:" + top + "px; z-index:10;'>");
   document.write("<table width='100%' cellspacing='0' cellpadding='0'><tr><td width='100%' align='" + align + "'><table cellspacing='0' cellpadding='0' border='0'><tr>");

   rows = _get_rows(sections);
   cols = _get_cols(sections);
   var extras = 0;

   var count = 0;
   for (i in sections)
       count++;

   if (align == 'center')
   {
      extras = cols - count;
      cols = cols + extras;
   }

   for (var r = 0;r <= rows;r++)
   {
      document.write("<tr>");
      if (align != 'left')
         document.write("<td class='menu_ph' onMouseOver=\"clear_menus();\"></td>");
      for (var c = 1;c <= cols;c++)
      {
         var col = (c <= extras ? 0 : c - extras);
         var id = r + '_' + col;
         if (col == 0)                                      // Padding to even up a lopsided centred menu
            document.write("<td class='menu_ph' onMouseOver=\"clear_menus();\"></td>");
         else if (r == 0 && col <= count)                   // Top menu row
         {
            var count2 = 0;
            for (i in sections)
            {
               if (++count2 == col)
               {
                  var onclick = (sections[i]['url'] ? 'onClick="' + sections[i]['url'] + '"' : '');
                  document.write("<td class='menu'><table class='menu_bg'><tr><td class='menu' id='m" + col + "' onMouseOver=\"menu_over('" + col + "');\"" + onclick + "><b>" + i + "</b></td></tr></table></td>");
               }
            }
         }
         else                                               // The rest
         {
            document.write("<td class='menu_ph' onMouseOver=\"cell_over('" + r + "', '" + col + "');\"><div style='visibility:hidden' id='dt" + id + "'><table width='100%' cellspacing='0'><tr><td nowrap='1' class='submenu' id='s" + id + "' onClick='menu_clicked(" + r + "," + col + ");'><div id='d" + id + "'></div></td><td id='b" + id + "' align='right' class='submenu'><div id='a" + id + "'></div></td></tr></table></div></td>");
         }
      }

      document.write("<td class='menu_ph' onMouseOver=\"clear_menus();\"></td></tr>");
      if (!r)
      {
         document.write("</table></td></tr></table></div><div id='menu_div' style='visibility:visible;position:absolute; ");
         if (left)
             document.write("left:" + left + "px; ");
         document.write("top:" + (parseInt(top) + 24) + "px; z-index:-1;'>");
         document.write("<table width='100%' cellspacing='0' cellpadding='0'><tr><td width='100%' align='" + align + "'><table cellspacing='0' cellpadding='0' border='0'><tr>");
      }
   }

   cols = cols - extras;
   document.write("<tr><td class='menu_ph' onMouseOver=\"clear_menus();\" colspan='100' height='20'></td></tr></table></td></tr></table></div>");
   max_menu = count;
   return ((offset + 1) * -10);
}

function _get_rows(menu_arr)
{
   var inrows = 0;
   for (i in menu_arr)
   {
      if (menu_arr[i]["submenus"])
      {
         var sub_rows = _get_sub_rows(menu_arr[i]["submenus"]);
         if (sub_rows > inrows)
            inrows = sub_rows;
      }
   }

   return inrows;
}

function _get_sub_rows(menu_arr)
{
   var inrows = 0;
   var count = 0;
   for (i in menu_arr)
   {
      count++;
      if (count > inrows)
         inrows = count;
      if (menu_arr[i]["submenus"])
      {
         var sub_rows = _get_sub_rows(menu_arr[i]["submenus"]) - 1;
         if (sub_rows + count > inrows)
            inrows = sub_rows + count;
      }
   }
   return inrows;
}

function _get_cols(menu_arr)
{
   var count = 0;
   var columns = 0;
   for (i in menu_arr)
   {
      count++;
      if (count > columns)
          columns = count;
      if (menu_arr[i]["submenus"])
      {
         var sub_cols = _get_sub_cols(menu_arr[i]["submenus"]);
         if (sub_cols + count - 1 > columns)
            columns = sub_cols + count - 1;
      }
   }
   return columns;
}

function _get_sub_cols(menu_arr)
{
   var columns = 1;
   for (i in menu_arr)
      if (menu_arr[i]["submenus"])
      {
         var sub_cols = _get_sub_cols(menu_arr[i]["submenus"]);
         if (sub_cols + 1 > columns)
            columns = sub_cols + 1;
      }
   return columns;
}