/**
 * @copyright  Copyright(c) 2005, IC Zones
 * @author     Michael Jolin
 * @since      2005,08,01
 * @package		JS
 *
 * @update		Michael Jolin -> 2006,02,27
**/

var nameCookie		= '';
var tooltip			= '';
var tooltip_list  = '';
//var resizeElement = '';
/**
 * Select good action to make on menu
 *
 * @author		Michael Jolin
 * @since		2005,08,01
 *
 * @update		Michael Jolin -> 2006,06,21
**/
function menuAction( menuId, action, type , forceClose ) {
	if ( document.getElementById(type + '_' + menuId) ) {
		var menu = document.getElementById(type + '_' + menuId);
		if( menu && menu != 'undefined' ){
			var list;
			if( document.getElementById(menuId) ) {
				list = document.getElementById(menuId).style;
			}

			if( menu.className == 'open' || action == 'close' || forceClose == 1 ) {
				if( list ) {
					list.position 		= 'absolute';
					list.visibility 	= 'hidden';
					list.display		= 'none';
					resizeToScreenheight();
				}
				menu.className 	= 'close';
				if ( action && action != 'close' && type == 'menu' ) {
					addToCloseMenu( menuId );
				}
			} else {
				var value = readCookie( nameCookie );
				if( !( action == 'open' && value.indexOf(menuId) >= 0 ) ) {
					if( list ) {
						list.position 		= '';
						list.visibility 	= 'visible';
						list.display		= 'block';
						resizeToScreenheight();
					}
					menu.className 	= 'open';
					if( action && action != 'open' && type == 'menu' ) {
						delToCloseMenu( menuId );
					}
				}
			}
		}
	}
}

/**
 * Close menu
 *
 * @author		Guillaume Lacroix
 * @since		2006,05,25
**/
function closeMenu( menuIds ){
	var ids = menuIds.split(',');
	for ( var i=0; i<ids.length; i++ ) {
		menuAction( ids[i], false, 'menu', 1 );
	}
}

function openMenu( menuIds ){
	var ids = menuIds.split(',');
	for ( var i=0; i<ids.length; i++ ) {
		if( ids[i] != '' ) {
			menuAction( ids[i], true, 'menu' );
		}
	}
}

/**
 * Close menu in cookie
 *
 * @author		Michael Jolin
 * @since		2005,08,01
 *
 * @update		Michael Jolin -> 2006,04,12
 * @update		Guillaume Lacroix -> 2006,05,17
**/
function closeMenuCookie( userName, zoneName , roleMenu, forceMenu ) {
	if( ! byPassCook ) {
		nameCookie = userName + '_' + zoneName;
		var menu = readCookie( nameCookie );
	
		if( forceMenu != null ) {
			menu = forceMenu;
		} else if( menu == '' ) {
			if( roleMenu != null ) {
				menu = roleMenu + ',';
			} else {
				menu = '590,697,591,5,521,';
			}
		}
	
		writeCookie( nameCookie + '=' + menu );
		var ids = menu.split(',');
		for( var i=0; i<ids.length-1; i++ ) {
			menuAction( ids[i], false, 'menu' );
		}
	}
}

/**
 * Keep out menu from cookie
 *
 * @author		Michael Jolin
 * @since		2005,08,01
 * @update		Guillaume Lacroix -> 2006,05,18
**/
function delToCloseMenu( menuId ) {
	if( ! byPassCook ) {
		var value 	= readCookie( nameCookie );
		if( value ) {
			var pos 		= value.indexOf(  ',' + menuId + ',' ) +1;
			if( pos < 0 ) {
				pos = value.indexOf( menuId + ',' );
			}
			var menu		= nameCookie + '=' + value.substr( 0, pos ) + value.substr( pos+menuId.length+1, value.length );
			writeCookie( menu );
			findOtherUL( menuId, 'open' );
		}
	}
}

/**
 * Add menu to cookie
 *
 * @author		Michael Jolin
 * @since		2005,08,01
 * @update		Guillaume Lacroix -> 2006,05,18
**/
function addToCloseMenu( menuId ) {
	if( ! byPassCook ) {
		var value 	= readCookie( nameCookie );
		if( value.indexOf( ( ',' + menuId + ',' ) ) < 0 ) {
			if( value.substr( 0 , menuId.lenght ) != menuId ){
				var menu 	= nameCookie + '=' + value + menuId + ",";
				writeCookie( menu );
			}
		}
		findOtherUL( menuId, 'close' );
	}
}

/**
 * Find all UL under select menu
 *
 * @author		Michael Jolin
 * @since		2005,08,01
**/
function findOtherUL( menuId, action ) {
	if( document.getElementById(menuId) ) {
		var item = document.getElementById(menuId);
		for( var i=0; i<item.childNodes.length; i++ ) {
			if( item.childNodes[i].nodeName == 'LI' && item.childNodes[i].id != "" ) {
				var subItem = item.childNodes[i].id;
				var id = subItem.substr(5, subItem.length );
				menuAction( id, action, 'menu' );			
			}
		}
	}
}

/**
 * Read cookie for menu
 *
 * @author		Michael Jolin
 * @since		2005,08,01
**/
function readCookie( name ) {
	if( !byPassCook ) {
		var nameEQ = name + '=';
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while( c.charAt(0) == ' ' ) {
				c = c.substring( 1, c.length );
			}
			if (c.indexOf(nameEQ) == 0) {
				return c.substring(nameEQ.length,c.length);
			}
		}
		return '';
	}
}

/**
 * Write cookie for menu
 *
 * @author		Michael Jolin
 * @since		2005,08,01
**/
function writeCookie( value ) {
	if( ! byPassCook ) {
		var today	= new Date();
		var year 	= today.getFullYear();
		var month 	= today.getMonth() + 1;
		var day	 	= today.getDay();
		var date 	= new Date( year, month, day );
		date	 		= date.toGMTString();
		var the_cookie 	= value + ";path=/;expires=" + date;
		document.cookie 	= the_cookie;
	}
}

/**
 * Function for bypass bug
 *
 * @author		Michael Jolin
 * @since		2005,08,01
**/
function functionNull() {
	readCookie( nameCookie );
}

/**
 * Block menu from the list
 *
 * @author		Michael Jolin
 * @since		2005,08,01
**/
function blockMenu( menuId ) {
	var menu 	= document.getElementById("elem_"+menuId);
	var style 	= menu.style;

	if( checkIt( 'msie 7' ) ) {
		menu.onclick = function() { return true; };
		style.color 				= 'black';
		style.backgroundColor 	= 'gray';
	} else {
		menu.onclick = functionNull;
		style.color 				= '#000000';
		style.backgroundColor 	= '#CCCCCC';
	}
	style.cursor				= 'default';
	style.fontWeight			= 'bolder';
}

/**
 * resize element to the screen height
 *
 * @author		Guillaume Lacroix
 * @since		2006,09,4
**/
function resizeToScreenheight( inId , addHeight ) {
	if( !addHeight ) {
		addHeight = 0;
	}

	var element = document.getElementById( inId );
	if( !element ) {
		 try { element = document.getElementById( resizeElement );} catch(e) { }
	}
	if( element) {
		if( checkIt( 'firefox' ) ) {
	///		var heighTESt = document.getElementById( 'mainDiv').clientHeight;
			element.style.minHeight = ( document.body.clientHeight + parseInt( addHeight, 10 ) )+ 'px';
		} else {
			element.style.height = ( document.body.clientHeight + parseInt( addHeight, 10 ) ) + 'px';
		}
	}
}

/**
 * Show the tooltip menu
 *
 * @author		Michael Jolin
 * @since		2006,02,27
 *
 * @update		Guillaume Lacroix -> 2006,03,30
**/
function showToolTip( id ) {
	if( tooltip != '' ) {
		hideToolTip( tooltip );
		/*	hideToolTip( id );*/
	}
	tooltip	= id;
	var menu	= document.getElementById( id );
	var text = menu.innerHTML;
	width = ( text.length * 7 );
	menu.style.width = width + 'px';
	menu.className = 'showTooltip';
	tooltip_list = id;
}

/**
 * Hide the tooltip menu
 *
 * @author		Michael Jolin
 * @since		2006,02,27
**/
function hideToolTip( id ) {
	if( !id ) {
		id = tooltip;
	}

	tooltip	= '';
	var menu	= document.getElementById( id );
	if( menu ) {
		menu.className = 'hideTooltip';
	}
}

/**
 * resize the background of the menu
 *
 * @author		Guillaume Lacroix
 * @since		2006,05,23
**/
function setMenuHeight() {
	if( !byPassMenuHeight ) {
		var browserName = navigator.appName;
		var bodyHeight = document.getElementById( 'main_content' );
		if( ! bodyHeight ) {
			bodyHeight = document.getElementById( 'mainContent' );
		}

		var leftBar = document.getElementById( 'LeftBar' );
		var rightBar = document.getElementById( 'RightBar' );

		if( browserName == 'Netscape' || navigator.appVersion.indexOf( 'msie 7' ) ) {
			var wid=screen.width;
			if( bodyHeight ) {
				if( bodyHeight.offsetHeight < 800 ) {
					bodyHeight = 650;
				} else {
					bodyHeight = bodyHeight.offsetHeight;
				}
			}
			if( leftBar ) {
				leftBar.style.minHeight = ( bodyHeight + 350 )+ 'px';
			}
			if( rightBar )	{
				rightBar.style.minHeight = ( bodyHeight + 350 )+ 'px';
			}
		}
	}
}

/**
 * select the right menu
 *
 * @author		Guillaume Lacroix
 * @since		2006,09,7
**/
function menuSelect( inMenuID ){
	var selectItem = document.getElementById( 'elem_' + inMenuID );
	if( selectItem ) {
		var _className = selectItem.className;
		if( _className.indexOf( 'Center' ) > -1 ) {
			_className = 'selected' + _className.substr( _className.indexOf( 'Center' ) );
		} else {
			_className = 'selected';
			selectItem.parentNode.className = _className;
		}
		selectItem.className = _className;
	}
}

/**
 * Unselect a menu
 *
 * @author		Michael Jolin
 * @since		2006,10,03
 *
 * @update		Guillaume Lacroix -> 2006,11,20
**/
function menuUnSelect( inMenuID ) {
	var selectItem = document.getElementById( 'elem_' + inMenuID );
	if( selectItem ) {
		var _className = selectItem.className;
		if( _className.indexOf( 'Center' ) > -1 ){
			_className = 'file' + _className.substr( _className.indexOf( 'Center' ) );
		} else {
			_className = 'file';
			selectItem.parentNode.className = _className;
		}
		selectItem.className = _className;
	}
}

/**
 * Reset the option for menu
 *
 * @author		Michael Jolin
 * @since		2006,10,04
**/
function resetMenu( title, old, id ) {
	title = convertEntity( title );
	document.title = title;
	menuUnSelect( old );
	menuSelect( id );
}


/**
 * Menu Over LI tag
 *
 * @author		Guillaume Lacroix
 * @since		2006,12,13
**/
//var ulOverOpen = new Array();   
//ulOverOpen[0] = 'treeviewV';   
var timeoutMenuOut;
var lastMenuAction = '';

function menuOverLI( id , inEvent ) {
	menuDisplayEls( 'none' );
	//html.displayTagName( 'input', 'none' );
	html.displayTagName( 'select', 'none' );

	clearTimeout( timeoutMenuOut );
	elemTarget = inEvent.target;
	if( !elemTarget ) {
		elemTarget = inEvent.srcElement;
	}
	
	switch (elemTarget.nodeName) {
		case 'A':
		case 'LABEL':
			elemTarget = elemTarget.parentNode;
		break;
	}

	if( elemTarget.nodeName == 'LI' && lastMenuAction != elemTarget.getAttribute( 'onmouseover' ) ) {
		lastMenuAction = elemTarget.getAttribute( 'onmouseover' );

		for( var j=0; j<ulOverOpen.length; j++) {
			if( elemTarget.parentNode.getAttribute('id') == ulOverOpen[j] ) { //check if it's the right event
				var tmpArray = [];

				for( var k = 0 ; k < ulOverOpen.length; k++ ){	//update ulOverOpen array and hide other sub UL
					if( k < (j+1) ) {
						tmpArray[k] = ulOverOpen[k];
					} else {
						document.getElementById( ulOverOpen[k] ).style.display = 'none';
					}
				}
				ulOverOpen = tmpArray;
				for( var i=0; i < elemTarget.childNodes.length; i++ ) { //seek for UL child
					if( elemTarget.childNodes[i].nodeName == 'UL' ) {
						var ulChild = elemTarget.childNodes[i];
						var ulChildId = ulChild.getAttribute('id');
						ulOverOpen[j+1] =  ulChildId;
						
						ulChild.style.display = 'block';
						i = elemTarget.childNodes.length;
					}
				}
				j = ulOverOpen.length;
			}
		}
	}
}


function menuClickLI( inEvent ) {
	elemTarget = inEvent.target;
	if( !elemTarget ) {
		elemTarget = inEvent.srcElement;
	}
	//alert( elemTarget );
}


/**
 * menu mouseout Trigger
 *
 * @author		Guillaume Lacroix
 * @since		2006,11,17
**/
function mouseOutUlMenu() {
	timeoutMenuOut = setTimeout( hideSubUL, 300 );
}

/**
 * hide UL of submenu
 *
 * @author		Guillaume Lacroix
 * @since		2006,11,17
**/
function hideSubUL() {
	menuDisplayEls( '' );
	html.displayTagName( 'input', '' );
	html.displayTagName( 'select', '' );

	for( var k = 1 ; k < ulOverOpen.length; k++ ) {
		if( ulOverOpen[k] ){
			document.getElementById( ulOverOpen[k] ).style.display = 'none';
		}
	}

	ulOverOpen = [];
	ulOverOpen[0] = ulOverOpenINIT;
	lastMenuAction = '';
}

/**
 * Modify the image source when click on a LI group
 *
 * @author		Guillaume Lacroix
 * @since		2006,11,17
**/
function changeImgLi( imgID , srcUP , srcDOWN ) {
	var imgLI = document.getElementById( imgID );
	if( imgLI ) {
		if( imgLI.src.indexOf( srcUP ) > -1 ) {
			imgLI.src = srcDOWN;
		} else {
			imgLI.src = srcUP;
		}
	}
}

/**
 * hide or show some els
 *
 * @author		Guillaume Lacroix
 * @since		2007,09,27
**/
function menuDisplayEls( displ ) {
	if( html.$( 'flashPlayerBox' ) ) {
		html.$( 'flashPlayerBox' ).style.display = displ;
	}
	if( checkIt( 'msie' ) ){
		if( html.$( 'Navigator-rzwrap' ) ) {
			html.$( 'Navigator-rzwrap' ).style.display = displ;
		}
	}
}

ulOverOpen = new Array();
ulOverOpen[0] = 'menu_964';
ulOverOpenINIT = ulOverOpen[0];
