var html = "";
var tabColorTheme = new Array();
var tab = new Array();

var C_BACK_OFFICE = false;
var C_CURRENT_PID = "-1";
var C_CURRENT_DOMAIN = "none";

var C_DECAL_L2 = "5px";
var C_DECAL_L3 = "20px";
var C_SURBRILLANCE = true;
var C_SURBRILLANCE_MODE = "1";
var stdColorThemeBck = "#E7E7E7";
var stdColorThemeText = "#426180";
var stdColorThemeTextOver = "#FFFFFF";

var C_BULLET_MODE = "";
var C_SITE = "";

var C_PATH_IMG = "image/zres/";
var menuTab = new Array();


/* Fonctions pour la construction du menu */	

function menu( id, parent, directparentid, superparentid, name, url, template, level, nbEnfants , uid , inMenu)
{
	this.id = id;
	this.parent = parent;
	this.directparentid = directparentid;
	this.superparentid = superparentid;
	this.name = name;
	this.url = url;
	this.template = template;
	this.level = level;
	this.nbEnfants = nbEnfants;
	if ( uid == 'undefined' || uid.length == 0 )
	{
		uid = "rubrique" + C_CURRENT_DOMAIN.toLowerCase() + "_" + id;
	}
	this.uid = uid;
	this.inMenu = inMenu;
	this.add()
}

menu.prototype.initMenu = initMenu;
menu.prototype.add = add;
menu.prototype.get = get;
menu.prototype.drawMenu = drawMenu;

function add( newone )
{
	try
	{
		menuTab[menuTab.length] = { id:this.id}
	}catch(e){
		DeclencherException( C_NOM_PROC, e );
	}
}

function get( idxMenu )
{
	for( var i = 0 ; i < menuTab.length; i++ )
	{
		if ( menuTab[i].id == idxMenu )
		{
			return i;
		}
	}
	return -1;
}

function echoItem(m)
{
	var str = "";
	var sep = "\n";
	str += "id = " + m.id;
	str += sep;
	str += "parent = " + m.parent;
	str += sep;
	str += "directparentid = " + m.directparentid;
	str += sep;
	str += "superparentid = " + m.superparentid;
	str += sep;
	str += "name = " + m.name;
	str += sep;
	str += "url = " + m.url;
	str += sep;
	str += "template = " + m.template;
	str += sep;
	str += "level = " + m.level;
	str += sep;
	str += "nbenfants = " + m.nbEnfants;
	str += sep;
	str += "uid = " + m.uid;
	str += sep;
	alert(str);
}

function drawMenu(puce)
{
	var C_NOM_PROC = "drawMenu";
	try
	{
		// Ici on définit notre DIV Menu
		html += "<div class='sidebar' id='linkbar'>";	
		for( var i = 0; i < menuTab.length; i++ )
		{
			var t = menuTab[i].id;
			var s = eval( 'menu' + t);
			//echoItem(s);
			if ( s.level == 1 && s.inMenu == 1 )
			{
				html += writeLevel1( s,puce );
				//html += "<br>";
			}
		}
		html += "</div>";
		//prompt('',html);
		document.write( html );
		initMenu();
		
	}catch( e ){
		DeclencherException( C_NOM_PROC, e );
	}
}

function writeLevel1( s,puce )
{
	var C_NOM_PROC = "writeLevel1";
	var str = "";
	try
	{
		var parent = GetCookie( "m_chosen" );
		var currentid = GetCookie( "thisid" );
		var level = GetCookie( "level" );
		//var display="inline";
		var display="none";
		var color="";
		tab[tab.length] = {id:s.id,parent:s.directparentid ,level:s.level};
		//str += currentid + " / " + s.id + " / " + level;
		if (currentid == s.id && level == 1){
			display="inline";
		}
		if ( s.nbEnfants > 0 )
		{
			str += "<div style='padding-bottom: 3'><a class='somm' href='javascript:__wcmnavto(\"" + s.url + "\"," + s.template + ", "+ s.superparentid + ", " + s.directparentid + ", " + s.id + ", " + s.level + ", \"" + s.uid + "\");'>" + puce + "&nbsp;<span class='rubriqueMere' id='rm_" + s.id + "'>" + s.name + "</span></a></div>";
			str += "<div id='r" + s.id + "' name='r" + s.id + "' style='display:"+display+"'>";
			str += "<div style='background-image:url(\"image/bibliosite/pointille04.gif\"); background-repeat: repeat-x; height: 3; padding-bottom: 3'><img src='image/bibliosite/pointille04.gif' height='3'></div>";
			str += "<div style='background-color:#F6F6F6'>"+writeLevel2( s.id ) + "</div></div>";
			str += "<div style='background-image:url(\"image/bibliosite/pointille04.gif\"); background-repeat: repeat-x; height: 3; padding-bottom: 3'><img src='image/bibliosite/pointille04.gif' height='3'></div>";
		}else{
			str += "<div style='padding-bottom: 3'><a class='somm' id='rubriqueMere' href='javascript:__wcmnavto(\"" + s.url + "\"," + s.template + ", "+ s.superparentid + ", " + s.directparentid + ", " + s.id + ", " + s.level + ", \"" + s.uid + "\");'>";
			str += "" + puce + "&nbsp;<span class='rubriqueMere' id='rm_" + s.id + "'>" + s.name + "</span>";
			str += "</a></div>";
			str += "<div style='background-image:url(\"image/bibliosite/pointille04.gif\"); background-repeat: repeat-x; height: 3; padding-bottom: 3'><img src='image/bibliosite/pointille04.gif' height='3'></div>";	
		}
	}catch( e ){
		DeclencherException( C_NOM_PROC, e );
	}
	return str;
}

function writeLevel2(directparentid )
{
	var C_NOM_PROC = "writeLevel2";
	var str = "";
	try
	{
		var currentid = GetCookie( "thisid" );	
		var tabLocal = new Array();
		for ( var i = 0 ; i < menuTab.length; i++ )
		{
			var t = menuTab[i].id;
			var s = eval( 'menu' + t);
			if ( s.directparentid == directparentid )
			{
				tabLocal[tabLocal.length] = s.id;
			}
		}
		
		for ( var i = 0; i < tabLocal.length; i++)
		{
			var s = eval( 'menu' + tabLocal[i] );
			if ( s.nbEnfants > 0 )
			{
				tab[tab.length] = {id:s.id,parent:s.directparentid, level:s.level};
				if ( C_BACK_OFFICE )
				{
					str += "<a class='soussomm' href='javascript:_switch(" + s.id + " );' title=''>";	
				}else{
					str += "<a class='soussomm' href='javascript:__wcmnavto(\"" + s.url + "\"," + s.template + ", " + s.superparentid + ", " + s.directparentid + ", " + s.id + ", " + s.level + ", \"" + s.uid + "\");' title=''>";
				}				
			}else{
				str += "<a class='soussomm' href='javascript:__wcmnavto(\"" + s.url + "\"," + s.template + ", " + s.superparentid + ", " + s.directparentid + ", " + s.id + ", " + s.level + ", \"" + s.uid + "\");' title=''>";
			}
			str += "<span id='squarelink_" + s.id + "'>" + getSquareLink( s.superparentid ) + "</span>";
			str += "<span id='link_" + s.id + "'>" + s.name + "</span></a><br>";	
			
		
			if ( s.nbEnfants > 0 )
			{
				var display = "none";
				if (currentid == s.id)
				{
					//display = "block";
				}
				str += "<div id='r" + s.id + "' name='r" + s.id + "' style='display:" + display + "'>";
				str += writeLevel3( s.id );
				str += "</div>";
			}
			
		}
	}catch( e ){
		DeclencherException( C_NOM_PROC, e );
	}
	return str;
}

function writeLevel3( directparentid )
{
	var C_NOM_PROC = "writeLevel3";
	var str = "";
	try
	{
		var tabLocal = new Array();
		for ( var i = 0 ; i < menuTab.length; i++ )
		{
			var t = menuTab[i].id;
			var s = eval( 'menu' + t);
			
			if ( s.directparentid == directparentid )
			{
				tabLocal[tabLocal.length] = s.id;
			}
		}
		
		for ( var i = 0; i < tabLocal.length; i++)
		{
			var s = eval( 'menu' + tabLocal[i] );
			
			tab[tab.length] = { id:s.id , parent : s.directparentid, level:s.level};
			
			str += "&nbsp;&nbsp;<a href='javascript:__wcmnavto(\"" + s.url + "\"," + s.template + ", " + s.superparentid + ", " + s.directparentid + ", " + s.id + ", " + s.level + ", \"" + s.uid + "\");' title=''>";
			str += "<span id='squarelink_" + s.id + "'>" + getSquareLink( s.superparentid) + "</span>";
			str += "<span id='link_" + s.id + "'>" + s.name + "</span></a>";
			str += "<br>";
		}
	}catch( e ){
		DeclencherException( C_NOM_PROC, e );
	}
	return str;
}

function getBlank( width )
{
	return "<img src='" + C_PATH_IMG + "pixtrans.gif' border='0' width='" + width + "'>";
}

function getThemeColor( theme ) 
{
	for ( var i = 0 ; i < tabColorTheme.length; i++ )
	{
		if ( tabColorTheme[i].id == theme )
		{
			return tabColorTheme[i].color;
		}
	}
}

function getLetter( themeid )
{
	var C_NOM_PROC = "getLetter";
	var str = "";
	try 
	{
		for ( var i = 0 ; i < tabColorTheme.length; i++ )
		{
			if ( tabColorTheme[i].id == themeid )
			{
				return tabColorTheme[i].letter;
			}
		}
	}catch( e ){
		DeclencherException( C_NOM_PROC, e );
	}
	return str;

}

function getSquareLink( superparentid )
{
	var C_NOM_PROC = "getSquareLink";
	var str=".:&nbsp;";
	/*
	try
	{
		if ( C_BULLET_MODE == 'IMG' )
		{
			str += "<img src='" + C_PATH_IMG + "bullet_item_off_" + getLetter( superparentid ) + ".gif' border='0'>";
		}else if ( C_BULLET_MODE == 'CHEVRONS' ){
			str += "&#187;";	
		}
		str += "&nbsp;";
	}catch( e ){
		DeclencherException( C_NOM_PROC, e );
	}
	*/
	return str;
}



/* Fonctions utiles à la navigation */

function __wcmnavto( url , target, sp , dp, id , level , uid )
{
	SetCookie( "superparent", sp );
	SetCookie( "thisid", id );
	SetCookie( "level" , level );
	if ( C_BACK_OFFICE )
	{
		_navto( url + "&id=" + id, target, dp);
	}else{
		if ( url.length > 0 )
		{
			if ( url.indexOf( "http://" ) > -1 )
			{
				var w = window.open( url );
			}else{
				
				url = "ebn.ebn?pid=" + C_CURRENT_PID + "&domain=" + C_CURRENT_DOMAIN + "&site=" + C_SITE + "&uid=" + url + "&rub=" + id;
			}			
		}else{
				url = "ebn.ebn?pid=" + C_CURRENT_PID + "&domain=" + C_CURRENT_DOMAIN + "&site=" + C_SITE + "&uid=" + uid + "&rub=" + id;
		}
		_navto( url, 1 , dp);
	}
}

function __switch( id )
{
	try
	{
		for ( var i = 0 ; i < tabColorTheme.length ; i++ )
		{
			document.getElementById('h_' + tabColorTheme[i].id ).style.background = stdColorThemeBck;
			document.getElementById('rm_' + tabColorTheme[i].id).style.color = stdColorThemeText;
		}
		document.getElementById('h_' + id).style.background = tabColorTheme[getIndexInTab(tabColorTheme, id)].color;
		document.getElementById('rm_' + id).style.color = stdColorThemeTextOver;
	}catch(ex){
		//alert( "changeColor " + ex.message );
	}		
	_switch( id );
}
	
function getIndexInTab( tab, id )
{
	for ( var i = 0; i < tab.length; i++)
	{
		if ( tab[i].id == id ) return i;
	}
	return -1;
}

function initMenu()
{
	var C_NOM_PROC = "initMenu";
	try
	{
		var sp = GetCookie( "superparent" );
		var dp = GetCookie( "m_chosen" );
		var id = GetCookie( "thisid" );
		if ( id != null && id != "" && id != "0" )
		{
			var level = GetCookie( "level" );
			
			var s = eval( 'menu' + id );
			if (level == null) level = '0';
			if ( id != null )
			{
				switch ( level )
				{
					case '1':
						_switch( sp );	
						activateTheme( id );
						break;
					case '2':
						if ( s.nbEnfants > 0 )
						{
							_switch( id );	
						}else{
							_switch( dp );	
						}
						activateTheme( dp );
						activateRub( id , dp);
						break;
					case '3':
						_switch( id );	
						activateTheme( sp );
						activateRub( dp , sp);
						activateSRub( id , sp);
						break;
					default:
						DeclencherException( C_NOM_PROC, null )
						break;	
				}
			}
		}
	}catch( e ){
		//DeclencherException( C_NOM_PROC , e );
	}
}

function activateRub( id, dp )
{
	var C_NOM_PROC = "activateRub";
	try
	{
		document.getElementById('link_' + id).style.color = getThemeColor(dp);
		
		if ( C_BULLET_MODE == 'IMG' )
		{
			document.getElementById('squarelink_' + id).innerHTML = "<img src='" + C_PATH_IMG + "bullet_item_on_" + getLetter( dp ) + ".gif' border='0'>&nbsp;";
		}

		
	}catch(e){
		//DeclencherException( C_NOM_PROC, e );
	}
}

function activateSRub( id, sp )
{
	var C_NOM_PROC = "activateSRub";
	try
	{
		if ( C_SURBRILLANCE )
		{
			if ( C_SURBRILLANCE_MODE == '1' )
			{
				document.getElementById('link_' + id).style.background = getThemeColor( sp );
				document.getElementById('link_' + id).style.color = stdColorThemeTextOver;
			}else{
				document.getElementById('link_' + id).style.color = getThemeColor( sp );
			}
		}
		if ( C_BULLET_MODE == 'IMG' )
		{
			document.getElementById('squarelink_' + id).innerHTML = "<img src='" + C_PATH_IMG + "bullet_item_on_" + getLetter( sp ) + ".gif' border='0'>&nbsp;";
		}
	
	}catch(e){
		//DeclencherException( C_NOM_PROC, e );
	}
}
function activateTheme( id )
{
	var C_NOM_PROC = "activateTheme";
	try
	{
		document.getElementById('h_' + id).style.background = tabColorTheme[getIndexInTab(tabColorTheme, id)].color;
		document.getElementById('rm_' + id).style.color = stdColorThemeTextOver;
	}catch(e){
		//DeclencherException( C_NOM_PROC, e );
	}
}

function DeclencherException( proc, e )
{
	var msg = "";
	if ( e != null )
	{
		msg = "Une erreur est survenue dans la méthode [" + proc + "]\nDescription : " + e.message;
	}else{
		msg = "Une erreur est survenue dans la méthode [" + proc + "]\nDescription impossible";
	}
	alert( msg );
}



function closeAll(eparent)
{
	for ( var i = 0; i < tab.length ; i++ )
	{
		if ( tab[i].id != eparent)
		{
			try
			{
				document.getElementById( 'r' + tab[i].id ).style.display = 'none';
			}catch(ex){
				//alert( "[closeAll] = " + ex.message );
			}
		}
	}
}
function openDiv( did )
{
	if (did != null )
	{
		try
		{
			document.getElementById('r' + did).style.display = "block";
		}catch(ex){
		//	alert("[openDiv] = " + ex.message);
		}
	}
}
function getIndexTab( id )
{
	for ( var i = 0; i < tab.length; i++)
	{
		if ( tab[i].id == id ) {
			return i;
		}
	}
	return -1;
}
function _switch( eid )
{
	var idx = getIndexTab( eid );
	if (idx==-1)
		return;
	
	var eparent = tab[idx].parent;
	var i = idx;
	closeAll( eparent );
	
	openDiv( eid );
	while( eparent != 0 )
	{
		openDiv( eparent );
		eid = getIndexTab( eparent );
		if (eid==-1)
			return;
		eparent = tab[eid].parent;
	}
	
}
function _navto( url, cible, elidparent )
{
	SetCookie('m_chosen', elidparent);
	// cible = 1 => dans la meme fenetre
	// cible = 2 => dans une nouvelle fenetre 
	if (typeof(cible) == 'undefined' || cible.length == 0) cible = 1;
	if ( cible == 2 )
	{
		window.open( url );
	}else{
		self.location.href=url;
	}
}

/* Fonctions utiles à la navigation */

var SEPARATEUR_CHEMIN_FER = "&gt;";

function getLinkCheminFer(pid, domain, cmenu , site, noLink )
{
	if ( typeof( noLink ) == 'undefined' ){
		var noLink = false;
	}

	var str = "";
	var uid = cmenu.uid;
	if (cmenu.url.length > 0) uid = cmenu.url;
	if ( noLink )
	{
		//str += "<span>";
		//str += cmenu.name;
		//str += "</span>"		
		str += "<a href='ebn.ebn?pid=" + pid + "&domain=" + domain + "&site=" + site + "&uid=" + uid + "&rub=" + cmenu.id + "' class='partenaires'>";
		str += cmenu.name;
		str += "</a>";

	}else{
		str += "<a href='ebn.ebn?pid=" + pid + "&domain=" + domain + "&site=" + site + "&uid=" + uid + "&rub=" + cmenu.id + "' class='partenaires'>";
		str += cmenu.name;
		str += "</a>";

	}
	return str;
}
function addExternalLink( href, name , separator )
{
	var str = "";
	if ( typeof( separator ) == 'undefined' ) var separator = false;

	if ( separator ) str += SEPARATEUR_CHEMIN_FER;
	str += "<a href='" + href + "' class='partenaires'>";
	str += name;
	str += "</a>";
	document.write( str );
}

function getCheminFer( pid, domain, site, obj , id )
{
	var str = "";
	var rubriques = new Array();
	if ( typeof(id) == 'undefined') var id = GetCookie( "thisid" );
	try{
		var cmenu = eval( "menu" + id );
		if ( cmenu != null )
		{
			while( cmenu.directparentid != 0 )
			{
				rubriques[rubriques.length] = cmenu.id;
				cmenu = eval( "menu" + cmenu.directparentid );
			}
		
		
			for( var i = 0; i < rubriques.length ; i++)
			{
				cmenu = eval( "menu" + rubriques[rubriques.length - 1 - i] );
				if ( cmenu != null )
				{
					str += SEPARATEUR_CHEMIN_FER;
					if ( i < rubriques.length - 1 )
					{
						str += getLinkCheminFer(pid, domain, cmenu, site , false );
					}else{
						str += getLinkCheminFer(pid, domain, cmenu, site, true);	
					}
				}
				
			}
		}
	}catch( e ){
		// si cmenu n'est pas defini on continu
		//alert(e.message);
	}
	return str;	
}

function getCheminFerRecherche( pid, domain, site, rubrique )
{
	var tab = rubrique.split( "," );
	for ( var i = 0 ; i < tab.length; i++ )
	{
		if ( Trim(tab[i]).length > 0 )
		{
			addExternalLink( "ebn.ebn?pid=" + pid + "&domain=" + domain + "&site=" + site, getRes('ctx_accueil') , false );
			document.write( getCheminFer(pid,domain,site,"rubrique", tab[i]) );
			document.write( "<br/>" );
		}
	}

}
function getContexte (rub) {
	var contexte = "accueil > ";
	alert("contexte = " + contexte);
	var cmenu = eval( "menu" + rub);
	alert("cmenu = " + cmenu);
	contexte += cmenu.name;
	return contexte;
}

/*
function getMotsClefs( pid, site, obj , id )
{
	var str = "";
	var rubriques = new Array();
	if ( typeof(id) == 'undefined') var id = GetCookie( "thisid" );
	var cmenu = eval( "menu" + id );
	if ( cmenu != null )
	{
		while( cmenu.directparentid != 0 )
		{
			rubriques[rubriques.length] = cmenu.id;
			cmenu = eval( "menu" + cmenu.directparentid );
		}
	}
	
	for( var i = 0; i < rubriques.length ; i++)
	{
		cmenu = eval( "menu" + rubriques[rubriques.length - 1 - i] );
		if ( cmenu != null )
		{
			str += SEPARATEUR_CHEMIN_FER;
			if ( i < rubriques.length - 1 )
			{
				str += getLinkCheminFer(pid, cmenu, site , false );
			}else{
				str += getLinkCheminFer(pid, cmenu, site, true);	
			}
		}
		
	}
	
	return str;	
}
*/