/*

   ZloiB's PopUp Menu, version 1.4
   
   features Exxbrain

   You can freely use this menu on your own pages.

   mailto: zloib@mail.ru

*/

var
DOM = document.getElementById ? 1:0;
IE = document.all && document.all.item ? 1:0;
IE4 = IE && !DOM;

// Settings:
HIDE_DELAY = 300;
DOT_SRC = "i/dot.gif"; // Spacer image (name and path)

function GetElem( id ) { return DOM? document.getElementById(id) : document.all[id]; }
function GetStyle( id ) { return GetElem(id).style; }
function GetX( elem ) {
 var par = elem.offsetParent, x = 0;
 if ( par == null ) return elem.offsetLeft;
 else x = elem.offsetLeft + GetX(par);
 return x;
}
function GetY( elem ) {
 var par = elem.offsetParent, y = 0;
 if ( par == null ) return elem.offsetTop;
 else y = elem.offsetTop + GetY(par);
 return y;
}

function MNU_mouseOver( obj, menuNum, itemNum )
{
 if ( !obj.activated ) return;
 var menu = obj.menu;
 clearTimeout( obj.timer );
 clearTimeout( obj.rootTimer );
 MNU_hideTree( obj, menuNum );
 obj.litNow = MNU_getTree( obj, menuNum, itemNum );
 MNU_switchColor( obj, true );
 targetNum = menu[menuNum][itemNum].target;
 if ( targetNum > 0 ) {
    thisX = parseInt( menu[menuNum][0].st.left ) + parseInt( menu[menuNum][itemNum].st.left );
    thisY = parseInt( menu[menuNum][0].st.top ) + parseInt( menu[menuNum][itemNum].st.top );
    with( menu[targetNum][0].st ) {
       left = parseInt( thisX + menu[targetNum][0].x ) + "px";
       top  = parseInt( thisY + menu[targetNum][0].y ) + "px";
       display = "block";
    }
 }
 window.status = menu[menuNum][itemNum].href;
}

function MNU_mouseOut( obj, menuNum, itemNum )
{
 if ( !obj.activated ) return;
 if ( !menuNum && !obj.menu[menuNum][itemNum].target )
    MNU_hideTree( obj, 0 );
 else
    obj.timer = setTimeout( "MNU_hideTree( "+obj.objname+", 0 )", HIDE_DELAY );
 obj.rootTimer = setTimeout( "MNU_hideRootProc( "+obj.objname+", 0 )", HIDE_DELAY );
 window.status = "";
}

function MNU_onClick( obj, menuNum, itemNum )
{
 if ( !obj.activated ) return;
 if ( obj.menu[menuNum][itemNum].href != "" ) {
    MNU_hideTree( obj, 0 );
    document.location.href = obj.menu[menuNum][itemNum].href;
 }
}

function MNU_getTree( obj, menuNum, itemNum )
{
 itemArray = new Array( obj.menu.length );
 while(1) {
    itemArray[menuNum] = itemNum;
    if ( !menuNum ) return itemArray;
    itemNum = obj.menu[menuNum][0].parentItem;
    menuNum = obj.menu[menuNum][0].parentMenu;
 }
}

function MNU_switchColor( obj, isOver )
{
 for( m = 0; m < obj.litNow.length; m++ ) {
    if ( obj.litNow[m] ) {
       newClass = isOver? obj.menu[m][0].overCl : obj.menu[m][0].outCl;
       obj.menu[m][ obj.litNow[m] ].ref.className = newClass;
    }
 }
}

function MNU_hideTree( obj, menuNum )
{
 var keepMenus = MNU_getTree( obj, menuNum, 1 );
 for( i = 0; i < obj.menu.length; i++ )
    if ( !keepMenus[i] ) obj.menu[i][0].st.display = 'none';
 MNU_switchColor( obj, false );
}

function MNU_showRoot( bindObj, objName )
{
 eval( 'var obj = '+objName+';' );
 if ( !obj.activated ) return;

 var bx = GetX(bindObj), by = GetY(bindObj);

 clearTimeout( obj.rootTimer );
 MNU_mouseOver( obj, 0, 0 );
 with( obj.menu[0][0] ) {
    st.left = x+bx+"px";
    st.top = y+by+"px";
    st.display = 'block';
 }
}

function MNU_hideRootProc( obj ) { obj.menu[0][0].st.display = 'none'; }

function MNU_hideRoot( objName )
{
 eval( 'var obj = '+objName+';' );
 if ( !obj.activated ) return;
 obj.rootTimer = setTimeout( "MNU_hideRootProc( "+obj.objname+", 0 )", HIDE_DELAY );
}

function CObjMenu( objName, menuArr )
{
 this.objname = objName;
 this.menu = menuArr;
 this.litNow = new Array();
 this.timer = 0;
 this.rootTimer = 0;
 this.activated = 0;
}

function MNU_launch( objName, menuArr )
{
 eval( objName+' = new CObjMenu("'+objName+'",menuArr); var obj = '+objName+';' );
 var menu = obj.menu;
 for( m = 0; m < menu.length; m++ ) {
    with( menu[m][0] ) {
       var str = '', itemX = 0, itemY = 0;
       for( currItem = 1; currItem < menu[m].length; currItem++ ) {
          with( menu[m][currItem] ) {
             var itemID = 'obj'+obj.objname+'menu'+m+'item'+currItem;
             var w = vert? width : length;
             var h = vert? length : width;
             var dx = IE4? -1:1;
             var dy = IE4? 0:1;

             if ( href != '' ) cursor='cursor: hand; cursor: pointer';
             else cursor='cursor:default;';
             
             str += '<div id="'+itemID+'" ';
             str += 'style="position: absolute; left: '+itemX+'px; top: '+itemY+'px; width: '+w+'px; height='+h+'px; display: block; '+cursor+'"';
             str += 'class="'+outCl+'" ';
             str += 'onMouseOver="MNU_mouseOver('+obj.objname+','+m+','+currItem+')" ';
             str += 'onMouseOut="MNU_mouseOut('+obj.objname+','+m+','+currItem+')" ';
             str += 'onClick="MNU_onClick('+obj.objname+','+m+','+currItem+')">';

             str += '<div><img width="4" height="'+h+'" src="'+DOT_SRC+'"></div>';

             str += '<div style="position: absolute; top: 0; left: 0; padding: 2px 5px 0 5px;">'+text+'</div>';

             if ( target > 0 ) {
                if ( vert ) str += '<div style="position: absolute; top: 1px; left: '+(w-11)+'px; color: #d0dfc5;"><b>&gt;</b></div>';
                menu[target][0].parentMenu = m;
                menu[target][0].parentItem = currItem;
             }
             str += '</div>';

             if ( vert ) itemY += length + spacing + dy;
             else itemX += length + spacing + dx;
          }
       }

       document.write( '<div id="obj'+obj.objname+'menu'+m+'div"'+'style="position: absolute; display: none;">'+str+'</div>' );
       st = GetStyle( 'obj'+obj.objname+'menu'+m+'div' );

       for( currItem = 1; currItem < menu[m].length; currItem++ ) {
          itemName = 'obj'+obj.objname+'menu'+m+'item'+currItem;
          menu[m][currItem].ref = GetElem( itemName );
          menu[m][currItem].st  = GetStyle( itemName );
       }
    }
 }
 obj.activated = 1;
}


////////////////////////////////////////////////////////////////////////
//
//  INTERFACE FUNCTIONS
//
////////////////////////////////////////////////////////////////////////

function CMenu( bVertical, iOffsetX, iOffsetY, iWidth, szOverStyleClass, szOutStyleClass )
{
 this.vert = bVertical;
 this.x = iOffsetX;
 this.y = iOffsetY;
 this.width = iWidth;
 this.overCl = szOverStyleClass;
 this.outCl = szOutStyleClass;
 this.parentMenu = null;
 this.parentItem = null;
 this.ref = null;
 this.st = null;
}

function CItem( iTarget, iLength, iSpacing, szText, szHref )
{
 this.target = iTarget;
 this.length = iLength;
 this.spacing = iSpacing;
 this.text = szText;
 this.href = szHref;
 this.ref = null;
 this.st = null;
}

function CreateMenu( szMenuID, menuArray )
{
 if ( DOM || IE ) return MNU_launch( szMenuID, menuArray );
}

function ShowMenu( bindObject, szMenuID )
{
 MNU_showRoot( bindObject, szMenuID );
}

function HideMenu( szMenuID)
{
 MNU_hideRoot( szMenuID );
}

