/*
Author   : bieler batiste
Company  : doSimple : http://www.dosimple.ch
send me a mail for more informations : faden@PASDEPOURRIELaltern.org - remove ( PASDEPOURRIEL )

Short javascript function to create and handle a CSS navigation menu

Copyright (C) 2004  Bieler Batiste

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

// the timeout for the menu
var timeout = 2000;

// not very clean but simple
// the function can be run in the HTML for faster display
// window.onload=initMenuDeroulant;

// creat timeout variables for list item
// it's for avoid some warning with IE
for( var i = 0; i < 100; i++ )
{
    eval("var timeoutli" + i + " = false;");
}

// this fonction apply the CSS style and the event
function initDynamicActions(idMenu)
{
    // a test to avoid some browser like IE4, Opera 6, and IE Mac
    if ( browser.isDOM1 
    && !( browser.isMac && browser.isIE ) 
    && !( browser.isOpera && browser.versionMajor < 7 )
    && !( browser.isIE && browser.versionMajor < 5 ) )
    {
        // get some element
        var div = document.getElementById(idMenu); // the root element
        var lisU = div.getElementsByTagName('ul'); // all the li
        
        // change the class name of the menu, 
        // it's usefull for compatibility with old browser
        div.className=idMenu;
        
//        alert('lisU.length >' + lisU.length);
        
        if ( lisU.length > 0 )
        {        
        	applyMyDynamicActions(lisU, idMenu);
        }
    }
}

function applyMyDynamicActions(lisU, idMenu)
{
    for (var i=0; i<lisU.length; i++ )
    {
	  	if (isMyDynamicActionUl(lisU.item(i)))
	    {
			// improve IE key navigation
			if ( browser.isIE )
			{
				myAddAnEvent(lisU.item(i).parentNode.getElementsByTagName('a')[0],'keyup',myShow);
			}
	            
			// link events to list item
			myAddAnEvent(lisU.item(i).parentNode.getElementsByTagName('a')[0],'click',myShow);
			myAddAnEvent(lisU.item(i).parentNode.getElementsByTagName('a')[0],'mouseout',myTimeoutHide);
			myAddAnEvent(lisU.item(i).parentNode.getElementsByTagName('a')[0],'blur',myTimeoutHide);
			myAddAnEvent(lisU.item(i).parentNode.getElementsByTagName('a')[0],'focus',myShow);
			lisU.item(i).parentNode.getElementsByTagName('a')[0].setAttribute('idMenu', idMenu);
	
			// add an id to list item
			lisU.item(i).parentNode.getElementsByTagName('a')[0].setAttribute( 'id', idMenu + "a" + i );
	            
			lisU.item(i).style['visibility'] = 'hidden';
		}
	}
}

// Cette fonction regarde si le li possede un lu 'actions'
function isMyDynamicActionUl(lisU)
{
//	alert('lisU.className' + lisU.className);
//	alert('lisU.className == dynamic-actions' + (lisU.className == 'dynamic-actions'));
	return lisU.className == 'dynamic-actions';
}

function myAddAnEvent( target, eventName, functionName )
{
    // apply the method to IE
    if ( browser.isIE )
    {
        //attachEvent dont work properly with this
        eval('target.on'+eventName+'=functionName');
    }
    // apply the method to DOM compliant browsers
    else
    {
        target.addEventListener( eventName , functionName , true ); // true is important for Opera7
    }
}
    
// hide the first ul element of the current element
function myTimeoutHide()
{
    // start the timeout
    eval( "timeout" + replaceAll(this.id, '-', '') + " = window.setTimeout('myHideUlUnderA( \"" + this.id + "\" )', " + timeout + " );");
}

// Hide all the ul elements found in the menu
// Show the first ul element found in the parent element (div) of this element (a)
function myShow()
{
     // myShow the sub menu
    myHideAllUlUnderNode(document.getElementById(this.getAttribute('idMenu')));
    this.parentNode.getElementsByTagName('ul')[0].style['visibility'] = 'visible';
}

// hide all the ul wich are under the element (div)
function myHideAllUlUnderNode( node )
{
	var lisU = node.getElementsByTagName('ul');
	    
    for ( var i=0; i<lisU.length; i++ )
    {
		if (lisU.item(i).className == 'dynamic-actions')
		{
			lisU.item(i).style['visibility'] = 'hidden';
		}
    }
} 

// hide all the ul found in the parent element (div) of this element (a)
function myHideUlUnderA( idA )
{
	document.getElementById(idA).parentNode.getElementsByTagName('ul')[0].style['visibility'] = 'hidden';
} 

// remplace all the occurences of the string
function replaceAll(stringIn, fromIn, toIn)
{
	var i = stringIn.indexOf(fromIn);
	var c = stringIn;

	while (i > -1)
	{
		stringIn = stringIn.replace(fromIn, toIn);
		i = stringIn.indexOf(fromIn);
	}

	return stringIn;
}
