var menuItemImage = new Image();
menuItemImage.src = '../images/taMenuItem.gif'

var itemSelected = false;

function get_element_by_id(elem_id)
{
   if (document.getElementById)
      return document.getElementById(elem_id);
   if (document.all)
      return document.all[elem_id];
   return null;
}

function insert_ctrl(parent_node_id, ctrl_node_id)
{
   ctrl = get_element_by_id(ctrl_node_id);
   part = get_element_by_id(parent_node_id);
   part.appendChild(ctrl);
}

function create_ctrl(mymass, ctrl_name, ctrl_width, ctrl_height, ctrl_img, ctrl_text, ctrl_url)
{
   myctrl = document.createElement('div');
   myctrl.id=ctrl_name;
   myctrl.className= 'selector';
   myctrl.style.width=ctrl_width+'px';
   myctrl.style.height=ctrl_height+'px';

   if (ctrl_img)
   {
      myview = document.createElement('img');
      myview.src = ctrl_img;
      myview.width=ctrl_width;
      myview.height=ctrl_height;
      myview.width=ctrl_width;
      //myview.alt=ctrl_text;
   }
   else
   {
      myview = document.createTextNode(ctrl_text);
   }

   if (ctrl_url)
   {
      myhref = document.createElement('a');
      myhref.href = ctrl_url;
      myhref.appendChild(myview);
      myctrl.appendChild(myhref);
   }
   else
   {
      myctrl.appendChild(myview);
   }
   return (myctrl);
}

function create_menu(parent_ctrl, menu_dx, menu_dy, submenu)
{

   var mymenu;
   mymenu = document.createElement('div');

   main[parent_ctrl.id] = new Array();
   main[parent_ctrl.id]["menu"] = new Array();
   main[parent_ctrl.id]["item"] = new Array();
   main[parent_ctrl.id]['allhidden'] = true;
   main[parent_ctrl.id]['gasit'] = true;

   parent_ctrl.onmouseout = new Function('onMouseOutCtrl("' + parent_ctrl.id + '", '+submenu+')');
   parent_ctrl.onmouseover = new Function('onMouseOverCtrl("' + parent_ctrl.id + '", '+submenu+')');

   main[parent_ctrl.id]["menu"].push(mymenu);
   menuid = main[parent_ctrl.id]["menu"].length;
   mymenu.id='menu_'+parent_ctrl.id+'_'+menuid;

   if (submenu)
   {
      myimg = document.createElement('img');
      myimg.src = menuItemImage.src;
      myimg.width = menuItemImage.width;
      myimg.height = menuItemImage.height;
      myimg.style.display = 'inline';
      myimg.hspace = 2;

      parent_ctrl.childNodes[0].childNodes[0].childNodes[0].childNodes[1].appendChild(myimg);
   }
   mymenu.className='menu';
   mymenu.style.left=menu_dx+'px';
   mymenu.style.top=menu_dy+'px';
   parent_ctrl.appendChild(mymenu);
   return (menuid);
}

function create_item(parent_menu_id, mymass, item_text, item_width, item_height, item_dx, item_dy, item_url)
{
   mymenu = mymass["menu"][parent_menu_id-1];
   myitem = document.createElement('div');
   mytab = document.createElement('table');
   mytab.width = item_width;
   myrow = mytab.insertRow(0);
   mytd1 = myrow.insertCell(0);
   mytd1.className = 'menuText';
   mytd2 = myrow.insertCell(1);
   mytd2.style.textAlign='right';
   mytd2.vAlign='middle';
   mymass['item'].push(myitem);

   myitem.id = mymenu.id+'_item_'+mymass['item'].length;
   mytd2.id = myitem.id + '_expand';

   myitem.className='menuItem';
   myitem.style.left = item_dx+'px';
   // Absolute   
   myitem.style.top = ((mymass['item'].length - 1) * item_height) + 'px'
   // Relative   
   myitem.style.top = item_dy+'px';
   myitem.style.width = item_width+'px';
   myitem.style.height = item_height+'px';
   myitem.style.background = mycolor["bgr"]["nrm"];
   myitem.style.color = mycolor["txt"]["nrm"];

   myitem.onclick = new Function('select_item("'+item_url+'")');
   myitem.onmouseout = new Function('menu_item_off("'+myitem.id+'")');
   myitem.onmouseover = new Function('menu_item_on("'+myitem.id+'")');

   mytd1.appendChild(document.createTextNode(item_text));

   myitem.appendChild(mytab);

   mymenu.appendChild(myitem);
   return (myitem);
}

function onMouseOutCtrl(ctrl_name, submenu)
{
   main[ctrl_name]['gasit']=true;
   myfun = new Function ('davai_gasit("'+ctrl_name+'")');

   if (submenu)
      menu_item_off(ctrl_name);
   setTimeout(myfun, 50);
}
function onMouseOverCtrl(ctrl_name, submenu)
{
   var mylist, tempitem, selitem;
   main[ctrl_name]['gasit'] = false;
   mylist = main[ctrl_name]['menu'][0];
   hide_all_menu(ctrl_name);
   mylist.style.visibility = "visible";
   main[ctrl_name]['allhidden'] = false;
   if (submenu)
      menu_item_on(ctrl_name);
}
function select_item(myitem_url)
{
   if (myitem_url && !itemSelected)
   {
      itemSelected = true;
      location.href = myitem_url;
   }
}
function menu_item_off(myitem_name)
{
   myitem = get_element_by_id(myitem_name);
   myitem.style.background = mycolor["bgr"]["nrm"];
   myitem.style.color = mycolor["txt"]["nrm"];
}
function menu_item_on(myitem_name)
{
   myitem = get_element_by_id(myitem_name);
   myitem.style.background = mycolor["bgr"]["hov"];
   myitem.style.color = mycolor["txt"]["hov"];
}
function hide_all_menu(ctrl_name)
{
   if (main[ctrl_name]['allhidden']) return;
   mymenu = main[ctrl_name]["menu"];
   for(var i=0;i<mymenu.length;i++)
   {
      mymenu[i].style.visibility = 'hidden';
   }
   main[ctrl_name]['allhidden']=true;
}
function davai_gasit(ctrl_name)
{
   if (main[ctrl_name]['gasit']) hide_all_menu(ctrl_name);
   main[ctrl_name]['gasit'] = false;
}

