
var zmIDCount           = 0;
var zmIDPrefix          = 'zmChild';
var zmMenuList          = new Array();
var zmTimeOut           = null;
var zmCurItem           = null;
var zmMainElm;

ZM_PROPERTIES_CNT   = 7;
ZM_CAPTION_ID       = 0;
ZM_URL_ID           = 1;
ZM_TARGET_ID        = 2;
ZM_IMG_ID           = 3;
ZM_DESCRIPTION_ID   = 4;
ZM_WIDTH_ID         = 5;
ZM_HEIGHT_ID        = 6;


function zmDraw( id, data ){
  	var elm     = zmGetElement(id);     //Récupére l'element principale "Menu"
  	var strMain = '';
  	var strSub  = '';
  	var i, index, item, idSub, hasChild;
  	
    zmMainElm = elm;

    var left = data[0][ZM_WIDTH_ID];
    var top = 10;

    strMain += '<table class="MainMenu" width="100%" border="0" style="border-collapse: collapse" cellspacing="0" cellpadding="0">';
    strMain += '<tr>';
    strMain += '<td class="adminMenuTopLeft" width="10" height="10"></td>';
    strMain += '<td class="adminMenuIcon" width="40"></td>';
    strMain += '<td class="adminMenuTopMiddle" width="7"></td>';
    strMain += '<td class="adminMenuLabel"></td>';
    strMain += '<td class="adminMenuLabel" width="8"></td>';
    strMain += '<td class="adminMenuTopRight" width="16" height="10"></td>';
    strMain += '</tr>';
      
  	for(i=0;i<data.length;i++){
		  item = data[i];
    	if (!item)	continue;
    	
    	hasChild   = ( item.length > ZM_PROPERTIES_CNT );
      idSub      = hasChild ? zmGetNewID () : null;
      idSubL     = (!idSub) ? 'null' : ('\'' + idSub + '\'');

      //Ajout de l'item dans une liste en ligne
      index = zmMenuList.length;
      zmMenuList[index] = item;
      
    	strMain += '<tr height="' + item[ZM_HEIGHT_ID] + '"';
      strMain += ' onmouseover="zmItemMouseOver( this, 1, ' + idSubL + ', ' + index + '  );"';
      strMain += ' onmouseout="zmItemMouseOut( this );"';
      strMain += '>';
      
      strMain += '<td class="adminMenuIcon"></td>';
      if ( item[ZM_IMG_ID] != '' )
        strMain += '<td class="adminMenuIcon"><img src="'+ item[ZM_IMG_ID] + '" border="0"></td>';
      else
        strMain += '<td class="adminMenuIcon">&nbsp;</td>';
      strMain += '<td class="adminMenuMiddle"></td>';
      
      target='';
      if ( item[ZM_TARGET_ID] == '_blank' )
          target=' target="_blank"';
          
      if ( item[ZM_URL_ID] != '' )
        strMain += '<td class="adminMenuLabel"><a href="' + item[ZM_URL_ID] + '"' + target + ' class="adminMenuLabel">' + item[ZM_CAPTION_ID] + '</a></td>';
      else
        strMain += '<td class="adminMenuLabel">' + item[ZM_CAPTION_ID] + '</td>';

      if (hasChild)
        strMain += '<td class="adminMenuHasChild"></td>';
      else
        strMain += '<td class="adminMenuLabel"></td>';
      strMain += '<td class="adminMenuRight"></td>';
      strMain += '</tr>';
      
     if (hasChild){
			 strSub += zmDrawSub(item, idSub, left, top );
     }
     top += item[ZM_HEIGHT_ID];

   }
   
   strMain += '<tr height="18">';
   strMain += '<td class="adminMenuBottomLeft"></td>';
   strMain += '<td class="adminMenuBottomMiddleL" height="10"></td>';
   strMain += '<td class="adminMenuBottomMiddle" width="5"></td>';
   strMain += '<td class="adminMenuBottomMiddleR" height="10" colspan="2"></td>';
   strMain += '<td class="adminMenuBottomRight" width="10" height="10"></td>';
   strMain += '</tr>';
   strMain += '</table>' + strSub;
   
   //alert( strMain );
   elm.innerHTML = strMain;
}

function zmDrawSub(subItems, id, left, top  ){
	var strMain = '';
	var strSub = '';
	var i, item, idSub, hasChild;
	strMain += '<div id="' + id + '" class="adminSubMenu"';
  strMain += ' style="position: absolute; visibility: hidden; z-index: 100; left: ' + left + 'px; top: ' + top + 'px; width: ' + subItems[ZM_WIDTH_ID] + 'px">';
  strMain += '<table width="100%" border="0"  style="border-collapse: collapse"  cellspacing="0" cellpadding="0">';
  strMain += '<tr>';
  strMain += '<td class="adminMenuTopLeft" width="10" height="10"></td>';
  strMain += '<td class="adminMenuIcon" width="20"></td>';
  strMain += '<td class="adminMenuTopMiddle" width="7"></td>';
  strMain += '<td class="adminMenuLabel"></td>';
  strMain += '<td class="adminMenuLabel" width="8"></td>';
  strMain += '<td class="adminMenuTopRight" width="16" height="10"></td>';
  strMain += '</tr>';
  
  left += subItems[ZM_WIDTH_ID];
  for (i=ZM_PROPERTIES_CNT;i<subItems.length;i++){
    item = subItems[i];
		if (!item)
			continue;
    strMain +=  '<tr height=\'' + item[ZM_HEIGHT_ID] + '\'';
		hasChild = (item.length > ZM_PROPERTIES_CNT);
		idSub = hasChild ? zmGetNewID () : null;
		idSubL     = (!idSub) ? 'null' : ('\'' + idSub + '\'');
		
    //Ajout de l'item dans une liste en ligne
    index = zmMenuList.length;
    zmMenuList[index] = item;

    strMain += ' onmouseover="zmItemMouseOver( this, 0, ' + idSubL + ', ' + index + ' );"';
    strMain += ' onmouseout="zmItemMouseOut( this );"';
    strMain += '>';

    strMain += '<td class="adminMenuIcon"></td>';
    if ( item[ZM_IMG_ID] != '' )
      strMain += '<td class="adminMenuIcon"><img src="'+ item[ZM_IMG_ID] + '" border="0"></td>';
    else
      strMain += '<td class="adminMenuIcon">&nbsp;</td>';
    strMain += '<td class="adminMenuMiddle"></td>';

    target='';
    if ( item[ZM_TARGET_ID] == '_blank' )
       target=' target="_blank"';

    if ( item[ZM_URL_ID] != '' )
      strMain += '<td class="adminMenuLabel"><a href="' + item[ZM_URL_ID] + '"' + target + ' class="adminMenuLabel">' + item[ZM_CAPTION_ID] + '</a></td>';
    else
      strMain += '<td class="adminMenuLabel">' + item[ZM_CAPTION_ID] + '</td>';

    if (hasChild)
      strMain += '<td class="adminMenuHasChild"></td>';
    else
      strMain += '<td class="adminMenuLabel"></td>';
    strMain += '<td class="adminMenuRight"></td>';
    strMain += '</tr>';

 		if (hasChild)
			strSub += zmDrawSub(item, idSub, left, top );
	  top += item[ZM_HEIGHT_ID];

			
	}
	
	strMain += '<tr height="18">';
  strMain += '<td class="adminMenuBottomLeft"></td>';
  strMain += '<td class="adminMenuBottomMiddleL" height="10"></td>';
  strMain += '<td class="adminMenuBottomMiddle" width="5"></td>';
  strMain += '<td class="adminMenuBottomMiddleR" height="10" colspan="2"></td>';
  strMain += '<td class="adminMenuBottomRight" width="10" height="10"></td>';
  strMain += '</tr>';

	strMain += '</table></div>' + strSub;
	return strMain;
}

function zmShow(){

}

//------------------------------------------------------------------------------
//                                   EVENT
//------------------------------------------------------------------------------

function zmItemMouseOver( elm, isMain, idSub, index ){
  var i, item;
  clearTimeout (zmTimeOut);
  if ( !elm.zmIsMain )
    elm.zmIsMain = isMain;
	var Menu = zmGetMenu (elm);


	if (zmCurItem){

		if (zmCurItem == elm || zmCurItem == Menu){
			item = zmMenuList[index];
			return;
		}

    
		var curMenu = zmGetMenu (zmCurItem );

    //alert ('CurMenu: ' + curMenu.id + ' Parent: ' + Menu.zmParentMenu + ' IdSub: ' + idSub);

		if (curMenu != Menu.zmParentMenu){
			if (curMenu.id != idSub){
				zmHideMenu (curMenu, Menu );
      }
		}
	}

	zmCurItem = elm;

	if (idSub){
		var subMenu = zmGetElement (idSub);
		zmShowSubMenu (elm, subMenu );
	}
	
}

function zmShowSubMenu (elm, subMenu ){

	if (!subMenu.zmParentMenu){
		var Menu = zmGetMenu (elm);
		subMenu.zmParentMenu = Menu;
		if (!Menu.zmSubMenu)
			Menu.zmSubMenu = new Array ();

		Menu.zmSubMenu[Menu.zmSubMenu.length] = subMenu;

 }

	subMenu.style.visibility = 'visible';


}

function zmItemMouseOut (elm){
	zmTimeOut = window.setTimeout ('zmHideMenuTime()', 1000);
}

function zmHideMenuTime (){
	if (zmCurItem){
		zmHideMenu(zmGetMenu(zmCurItem), null );
		zmCurItem = null;
	}
}

function zmHideMenu (Menu, curMenu){
  var i;
  
	if (Menu.zmSubMenu){
		for (i = 0; i < Menu.zmSubMenu.length; i++){
			zmHideSubMenu( Menu.zmSubMenu[i] );
		}
	}

	while (Menu && Menu != curMenu){
		if (Menu.className == 'adminSubMenu') {
			Menu.style.visibility = 'hidden';
	  }else if (Menu.className == 'adminMenu') {
      Menu.style.visibility = 'hidden';
  	}else{
			break;
    }
		Menu = zmGetMenu (Menu.zmParentMenu);
	}
	
}

function zmShow(){
   zmMainElm.style.visibility = 'visible';
}


function zmHideSubMenu (Menu ){
  var i;

	if (Menu.style.visibility == 'hidden')
		return;
	if (Menu.zmSubMenu){
		for (i = 0; i < Menu.zmSubMenu.length; i++){
			zmHideSubMenu (Menu.zmSubMenu[i] );
		}
	}
	Menu.style.visibility = 'hidden';
}


//------------------------------------------------------------------------------
//                                ABSTRACT / TOOLS
//------------------------------------------------------------------------------

function zmStat( msg ){
   var elm     = zmGetElement('stat');
   elm.innerHTML = msg;
}

//Récupération d'un  objet sur tous les navigateurs
function zmGetElement(id){
	if (document.all) return document.all[id];
	return document.getElementById (id);
}

function zmGetMenu (elm){
	while (elm){
		if (elm.className == 'adminMenu' || elm.className == 'adminSubMenu' || elm.className == 'adminMainBtn' )
			return elm;
		elm = elm.parentNode;
	}
	return null;
}

//Récupére un id unique
function zmGetNewID(){
  return zmIDPrefix + '_' + (zmIDCount++);

}


