var menuLeftListTop = 0;
var menuButtonTop = 0;
var menuMaxScroll = 0;
var menuIsInScroll = false;
var menuContainHeight;
var currentBarHeight = 200;
var previousMouseY = 0;
var menuGroupSelection = null;
var currentPageLink = "";
var menuCurrentSelectionObj = null;
var menuDelIsOpen = false;

var menuPersoXHR = null;

var myMenu = "";

var menuBaseItems = new Array();

function clearMenu()
{
	var i = 0;
	var max = menuBaseItems.length;
	while( i < max )
	{
		var temp = menuBaseItems.pop();
		temp.remove();
		delete(temp);
		temp = null;
		i++;
	}
}

function initMenuPerso()
{
	menuPersoXHR = initXHR();
}

function menuMakeTextForSave()
{
	var result = "";
	for( var i in menuBaseItems )
	{
		result += menuBaseItems[i].makeForSave();
	}
	return result;
}

function menuDelElement()
{
	if( menuCurrentSelectionObj != null )
		menuCurrentSelectionObj.remove();
	delete( menuCurrentSelectionObj );
	menuCurrentSelectionObj = null;
	
	// Save
	ajaxRequest( "./php/save_menu.php", menuPersoXHR, null, "value=" + menuMakeTextForSave() );
	
	menuDelPopupClose();
}

function menuAddLink()
{
	var toSet = "page sans nom";
	if( document.getElementById("menu_popup_add_value").value != "" )
	{
		toSet = document.getElementById("menu_popup_add_value").value;
		toSet = toSet.replace( "|", "_" );
		toSet = toSet.replace( "[", "_" );
		toSet = toSet.replace( "]", "_" );
		toSet = toSet.replace( "\"", "_" );
		toSet = toSet.replace( "'", "_" );
	}
	
	var GroupSelect = document.getElementById('menu_add_group_select');
	menuGroupSelection = GroupSelect.options[GroupSelect.selectedIndex].value.split(';');
	
	var newLink = new menuLinkItem();
	
	if( menuGroupSelection[0] == 0 )
	{
		newLink.start( null, toSet, currentPageLink, 1 )
		menuBaseItems.push( newLink );
	}
	else
	{
		var correctGroup = null;
		
		if( menuGroupSelection.length == 1 )
			correctGroup = menuBaseItems[menuGroupSelection[0] - 1];
		else
			correctGroup = menuBaseItems[menuGroupSelection[0] - 1].getCorrectGroup( 1 );
			
		newLink.start( correctGroup, toSet, currentPageLink, correctGroup.getMargin() + 1 );
		correctGroup.addChild( newLink );
	}
	
	// Save
	ajaxRequest( "./php/save_menu.php", menuPersoXHR, null, "value=" + menuMakeTextForSave() );
	
	menuAddPopupClose();
}

function menuAddGroup()
{
	var toSet = "groupe sans nom";
	if( document.getElementById("menu_popup_add_grp_value").value != "" )
	{
		toSet = document.getElementById("menu_popup_add_grp_value").value;
		toSet = toSet.replace( "|", "_" );
		toSet = toSet.replace( "[", "_" );
		toSet = toSet.replace( "]", "_" );
		toSet = toSet.replace( "\"", "_" );
		toSet = toSet.replace( "'", "_" );
	}
	
	var GroupSelect = document.getElementById('menu_add_grp_group_select');
	menuGroupSelection = GroupSelect.options[GroupSelect.selectedIndex].value.split(';');
	
	var newDir = new menuDirectoryItem();
	
	if( menuGroupSelection[0] == 0 )
	{
		newDir.start( null, toSet, 1 )
		menuBaseItems.push( newDir );
	}
	else
	{
		var correctGroup = null;
		
		if( menuGroupSelection.length == 1 )
			correctGroup = menuBaseItems[menuGroupSelection[0] - 1];
		else
			correctGroup = menuBaseItems[menuGroupSelection[0] - 1].getCorrectGroup( 1 );
		
		newDir.start( correctGroup, toSet, correctGroup.getMargin() + 1 );
		correctGroup.addChild( newDir );
	}
	
	// Save
	ajaxRequest( "./php/save_menu.php", menuPersoXHR, null, "value=" + menuMakeTextForSave() );
	
	menuAddGrpPopupClose();
}

function menuPersoAddPagePop()
{
	menuDelIsOpen = false;
	document.getElementById('menu_popup_del_contained').style.display = "none";
	document.getElementById('menu_popup_add_grp_contained').style.display = "none";
	
	document.getElementById('menu_popup_add_contained').style.display = "block";
	document.getElementById('menu_popup').style.display = "block";
	document.getElementById('menu_popup_txt').firstChild.nodeValue = "Ajouter cette page";	// Change popup title
	document.getElementById("menu_popup_add_value").value = "";
	document.getElementById('menu_popup_bar1_2').style.display = "block";
	document.getElementById('menu_popup_close').onclick = menuAddPopupClose;
	
	if( connected )
	{
		document.getElementById('menu_add_disconnected').style.display = "none";
		document.getElementById('menu_add_connected').style.display = "block";
		
		// Clear selection
		removeAllNodes( document.getElementById('menu_add_group_select'), false );
		
		var n_option;
		var n_option_text;
		
		var GroupSelect = document.getElementById('menu_add_group_select');
		
		// Root option
		n_option = document.createElement("option");
		n_option.value = "0";
		n_option.style.textAlign = "center";
		n_option.style.fontWeight = "bold";

		n_option_text = document.createTextNode( "menu" );
		n_option.appendChild(n_option_text);

		GroupSelect.appendChild(n_option);
		
		for(var i = 0; i < menuBaseItems.length; i++)
		{
			if( menuBaseItems[i].getType() == 1 )
			{
				n_option = document.createElement("option");
				n_option.value = i + 1;
				n_option.style.textAlign = "left";
				n_option.style.fontWeight = "normal";

				n_option_text = document.createTextNode( menuBaseItems[i] );
				n_option.appendChild(n_option_text);

				GroupSelect.appendChild(n_option);
				
				menuBaseItems[i].makeOptions( GroupSelect, i + 1 );
			}
		}
	}
	else
	{
		document.getElementById('menu_add_connected').style.display = "none";
		document.getElementById('menu_add_disconnected').style.display = "block";
	}
}

function menuPersoAddGroupPop()
{
	menuDelIsOpen = false;
	document.getElementById('menu_popup_del_contained').style.display = "none";
	document.getElementById('menu_popup_add_contained').style.display = "none";
	
	document.getElementById('menu_popup_add_grp_contained').style.display = "block";
	document.getElementById('menu_popup').style.display = "block";
	document.getElementById('menu_popup_txt').firstChild.nodeValue = "Ajouter un groupe de pages";	// Change popup title
	document.getElementById("menu_popup_add_grp_value").value = "";
	document.getElementById('menu_popup_bar1_2').style.display = "block";
	document.getElementById('menu_popup_close').onclick = menuAddGrpPopupClose;
	
	if( connected )
	{
		document.getElementById('menu_add_grp_disconnected').style.display = "none";
		document.getElementById('menu_add_grp_connected').style.display = "block";
		
		// Clear selection
		removeAllNodes( document.getElementById('menu_add_grp_group_select'), false );
		
		var n_option;
		var n_option_text;
		
		var GroupSelect = document.getElementById('menu_add_grp_group_select');
		
		// Root option
		n_option = document.createElement("option");
		n_option.value = "0";
		n_option.style.textAlign = "center";
		n_option.style.fontWeight = "bold";

		n_option_text = document.createTextNode( "menu" );
		n_option.appendChild(n_option_text);

		GroupSelect.appendChild(n_option);
		
		for(var i = 0; i < menuBaseItems.length; i++)
		{
			if( menuBaseItems[i].getType() == 1 )
			{
				n_option = document.createElement("option");
				n_option.value = i + 1;
				n_option.style.textAlign = "left";
				n_option.style.fontWeight = "normal";

				n_option_text = document.createTextNode( menuBaseItems[i] );
				n_option.appendChild(n_option_text);

				GroupSelect.appendChild(n_option);
				
				menuBaseItems[i].makeOptions( GroupSelect, i + 1 );
			}
		}
	}
	else
	{
		document.getElementById('menu_add_grp_connected').style.display = "none";
		document.getElementById('menu_add_grp_disconnected').style.display = "block";
	}
}

function menuPersoDelPop()
{
	document.getElementById('menu_popup_add_contained').style.display = "none";
	document.getElementById('menu_popup_add_grp_contained').style.display = "none";
	
	document.getElementById('menu_popup_del_contained').style.display = "block";
	document.getElementById('menu_popup').style.display = "block";
	document.getElementById('menu_popup_txt').firstChild.nodeValue = "Confirmer la suppression ?";	// Change popup title
	document.getElementById('menu_popup_bar1_2').style.display = "block";
	document.getElementById('menu_popup_close').onclick = menuDelPopupClose;
	
	if( connected )
	{
		document.getElementById('menu_del_disconnected').style.display = "none";
		document.getElementById('menu_del_connected').style.display = "block";
		
		if( menuCurrentSelectionObj == null )
		{
			document.getElementById('menu_popup_del_but1').style.display = "none";
			document.getElementById('menu_popup_del_but2').style.display = "none";
			document.getElementById('menu_popup_del_txt').firstChild.nodeValue = "Veuillez selectionner un élèment du menu...";	// Change popup text
		}
		else
		{
			document.getElementById('menu_popup_del_but1').style.display = "block";
			document.getElementById('menu_popup_del_but2').style.display = "block";
			
			if( menuCurrentSelectionObj.getType() == 1 )
				document.getElementById('menu_popup_del_txt').firstChild.nodeValue = "Etes vous sûr de vouloir supprimer le groupe \"" + menuCurrentSelectionObj + "\" et les élèments qu'il contient ?";	// Change popup text
			else
				document.getElementById('menu_popup_del_txt').firstChild.nodeValue = "Etes vous sûr de vouloir supprimer le lien \"" + menuCurrentSelectionObj + "\" ?";	// Change popup text
		}
	}
	else
	{
		document.getElementById('menu_del_connected').style.display = "none";
		document.getElementById('menu_del_disconnected').style.display = "block";
	}
	menuDelIsOpen = true;
}

function menuAddPopupClose()
{
	document.getElementById('menu_popup_add_contained').style.display = "none";
	document.getElementById('menu_popup').style.display = "none";
}

function menuAddGrpPopupClose()
{
	document.getElementById('menu_popup_add_grp_contained').style.display = "none";
	document.getElementById('menu_popup').style.display = "none";
}

function menuDelPopupClose()
{
	menuDelIsOpen = false;
	document.getElementById('menu_popup_del_contained').style.display = "none";
	document.getElementById('menu_popup').style.display = "none";
}

function updateMenuScrollButton()
{
	var currentContainHeight = document.getElementById('favory_list_contained').scrollHeight;
	currentBarHeight = menuMaxScroll;
	
	if( currentBarHeight < 8 )
		currentBarHeight = 8;
		
	if( currentContainHeight > menuContainHeight )
	{
		var menuPercent = menuContainHeight / currentContainHeight;
		currentBarHeight *= menuPercent;
		var topVariation = -menuLeftListTop / ( currentContainHeight - menuContainHeight );
		menuButtonTop = topVariation * (menuMaxScroll - currentBarHeight );
		
		var maxScrollTop = menuMaxScroll - currentBarHeight;
	
		if( menuButtonTop < 0 )
			menuButtonTop = 0;
		else if( menuButtonTop > maxScrollTop )
			menuButtonTop = maxScrollTop;
		
		document.getElementById('scroll_bar_button').style.top = menuButtonTop + 14 + "px";
		document.getElementById('scroll_bar_button_center').style.height = currentBarHeight - 8 + "px";
		document.getElementById('scroll_bar_button_bottom').style.top = currentBarHeight - 5 + "px";
	}
	else
	{
		menuButtonTop = 0;
		menuLeftListTop = 0;
		document.getElementById('scroll_bar_button').style.top = "14px";
		document.getElementById('scroll_bar_button_center').style.height = currentBarHeight - 8 + "px";
		document.getElementById('scroll_bar_button_bottom').style.top = currentBarHeight - 5 + "px";
		document.getElementById('favory_list').style.top = "0px";
	}
}

var menuLinkItem = function()
{
	var m_parentObj;
	var m_text;
	var m_margin;
	var m_link;
	var m_myLi = null;
	var m_newLi = null;
	var m_me = null;
	
	this.makeForSave = function()
	{
		return m_text + ":" + m_link + "|";
	}
	
	this.getType = function()
	{
		return 0;
	}
	
	this.toString = function()
	{
		return m_text;
	}
	
	this.unSelect = function()
	{
		m_myLi.style.background = "";
	}
	
	this.goToPage = function()
	{
		if( menuCurrentSelectionObj != null )
			menuCurrentSelectionObj.unSelect();
		menuCurrentSelectionObj = m_me;
		m_myLi.style.background = "#AFD2D2";
		if( menuDelIsOpen )
			menuPersoDelPop();
		
		changeCurrentPage(m_link);
	}
	
	this.start = function( parentObj, text, link, margin )
	{
		m_parentObj = parentObj;
		m_text = text;
		m_margin = margin;
		m_link = link;
		m_me = this;
		
		m_newLi = document.createElement("li");
		
		m_myLi = document.createElement("li");
		m_myLi.style.width = 220 - margin * 8 + "px";
		m_myLi.style.textAlign = "left";
		m_myLi.style.fontFamily = "times";
		m_myLi.style.fontWeight = "bold";
		m_myLi.style.fontSize = "14px";
		m_myLi.title = text;
		m_myLi.style.cursor = "pointer";
		
		var img = document.createElement("img");
		img.src = "./data/images/button/page.gif";
		img.style.marginLeft = "4px";
		img.style.marginRight = "8px";
		img.style.top = "3px";
		img.style.position = "relative";
		
		addEvent( m_myLi, 'click', this.goToPage, false );
		
		var goodTxt = text;
		if( text.length > 28 )
			goodTxt = text.substr(0, 28 - margin * 2 ) + "...";
		var liTxt = document.createTextNode( goodTxt );
		
		m_myLi.appendChild( img );
		m_myLi.appendChild( liTxt );
		m_newLi.appendChild( m_myLi );
		
		if( parentObj == null )
			document.getElementById('favory_list_contained').appendChild( m_newLi );
		else
			parentObj.getMyDOM().appendChild( m_newLi );
	}
	
	this.remove = function()
	{
		removeAllNodes( m_newLi, true );
		
		if( m_parentObj == null )
			menuBaseItems = delFromArray( menuBaseItems, this );
		else
			m_parentObj.deleteChild( this );
	}
}

var menuDirectoryItem = function()
{
	var m_parentObj;
	var m_myDOM = null;
	var m_text;
	var m_margin;
	var m_me = null;
	var m_childs = new Array();
	var childsOpened = false;
	var m_myLi = null;
	var m_newLi = null;
	
	this.makeForSave = function()
	{
		var result = m_text + "[";
		for( var i in m_childs )
		{
			result += m_childs[i].makeForSave();
		}
		result += "]";
		return result;
	}
	
	this.getType = function()
	{
		return 1;
	}
	
	this.getMargin = function()
	{
		return m_margin;
	}
	
	this.getCorrectGroup = function( id )
	{
		if( id < menuGroupSelection.length - 1 )
			return m_childs[ menuGroupSelection[id] ].getCorrectGroup( id + 1 );
		else
			return m_childs[ menuGroupSelection[id] ];
	}
	
	this.makeOptions = function( parent, previousVal )
	{
		var n_option;
		var n_option_text;
		
		for(var i in m_childs)
		{
			if( m_childs[i].getType() == 1 )
			{
				var value = previousVal + ";" + i;
				var text = m_childs[i].toString();
				var prefix = "";
			
				n_option = document.createElement("option");
				n_option.value = value;
				n_option.style.marginLeft = m_margin * 10 + "px";
				// Patch ie
				if( ie )
				{
					var j = 0;
					while( j < m_margin )
					{
						prefix += "    ";
						j++;
					}
				}
				n_option.style.textAlign = "left";
				n_option.style.fontWeight = "normal";

				n_option_text = document.createTextNode( prefix + text );
				n_option.appendChild(n_option_text);

				parent.appendChild(n_option);
				
				m_childs[i].makeOptions( parent, value );
			}
		}
	}
	
	this.toString = function()
	{
		return m_text;
	}
	
	this.unSelect = function()
	{
		m_myLi.style.background = "";
	}
	
	this.switchVisibility = function()
	{
		if( menuCurrentSelectionObj != null )
			menuCurrentSelectionObj.unSelect();
		menuCurrentSelectionObj = m_me;
		m_myLi.style.background = "#AFD2D2";
		if( menuDelIsOpen )
			menuPersoDelPop();
		
		if( childsOpened )
		{
			childsOpened = false;
			m_myDOM.style.display = "none";
		}
		else
		{
			childsOpened = true;
			m_myDOM.style.display = "block";
		}
		updateMenuScrollButton();
	}
	
	this.start = function( parentObj, text, margin )
	{
		m_parentObj = parentObj;
		m_text = text;
		m_margin = margin;
		m_me = this;
		
		m_newLi = document.createElement("li");
		
		m_myLi = document.createElement("li");
		m_myLi.style.borderLeft = "double";
		m_myLi.style.borderBottom = "solid";
		m_myLi.style.width = 220 - margin * 8 + "px";
		m_myLi.style.textAlign = "left";
		m_myLi.style.fontFamily = "times";
		m_myLi.style.fontWeight = "bold";
		m_myLi.style.fontSize = "14px";
		m_myLi.title = text;
		m_myLi.style.cursor = "pointer";
		
		var img = document.createElement("img");
		img.src = "./data/images/button/dossier.gif";
		img.style.marginLeft = "4px";
		img.style.marginRight = "8px";
		img.style.top = "3px";
		img.style.position = "relative";
		
		addEvent( m_myLi, 'click', this.switchVisibility, false );
		
		var goodTxt = text;
		if( text.length > 28 )
			goodTxt = text.substr(0, 28 - margin * 2 ) + "...";
		var liTxt = document.createTextNode( goodTxt );
		
		m_myDOM = document.createElement("ul");
		m_myDOM.style.marginTop = "0px";
		m_myDOM.style.marginLeft = "8px";
		m_myDOM.style.padding = "0px";
		m_myDOM.style.listStyleType = "none";
		m_myDOM.style.display = "none";
		
		m_myLi.appendChild( img );
		m_myLi.appendChild( liTxt );
		m_newLi.appendChild( m_myLi );
		m_newLi.appendChild( m_myDOM );
		if( parentObj == null )
			document.getElementById('favory_list_contained').appendChild( m_newLi );
		else
			parentObj.getMyDOM().appendChild( m_newLi );
	}
	
	this.remove = function()
	{
		for( var i = 0; i < m_childs.length; i++ )
		{
			var curChild = m_childs.pop();
			curChild.remove();
		}
		
		removeAllNodes( m_newLi, true );
		
		if( m_parentObj == null )
			menuBaseItems = delFromArray( menuBaseItems, this );
		else
			m_parentObj.deleteChild( this );
	}
	
	this.deleteChild = function( obj )
	{
		m_childs = delFromArray( m_childs, obj );
	}
	
	this.getMyDOM = function()
	{
		return m_myDOM;
	}
	
	this.addChild = function( obj )
	{
		m_childs.push( obj );
	}
}

function recFillMenuPerso( value, parent, margin )
{
	var currentTxt = "";
	var currentLink = "";
	var currentChar = '';
	var typeFound = false;
	var isDir = false;
	var nbDir = 0;
	for(var i = 0; i < value.length; i++ )
	{
		currentChar = value.charAt(i);
		if( isDir )
		{
			if( currentChar == '[' )
				nbDir++;
			else if( currentChar == ']' )
			{
				nbDir--;
				if( nbDir == 0 )
				{
					var newDir = new menuDirectoryItem();
					newDir.start( parent, currentTxt, margin );
					if( parent != null )
						parent.addChild( newDir );
					else
						menuBaseItems.push( newDir );
					recFillMenuPerso( currentLink, newDir, margin + 1 );
					currentTxt = "";
					currentLink = "";
					isDir = false;
					typeFound = false;
					continue;
				}
			}
			currentLink += currentChar;
		}
		else if( currentChar == '|' )
		{
			var newLink = new menuLinkItem();
			newLink.start( parent, currentTxt, currentLink, margin )
			if( parent != null )
				parent.addChild( newLink );
			else
				menuBaseItems.push( newLink );
			currentTxt = "";
			currentLink = "";
			isDir = false;
			typeFound = false;
		}
		else if( currentChar == '[' && !typeFound )
		{
			nbDir++;
			isDir = true;
			typeFound = true;
		}
		else if( currentChar == ':' && !typeFound )
		{
			typeFound = true;
		}
		else
		{
			if( typeFound )
				currentLink += currentChar;
			else
				currentTxt += currentChar;
		}
	}
}

function startScrollMenu()
{
	menuIsInScroll = true;
	previousMouseY = getMouseY();
}

function stopScrollMenu()
{
	menuIsInScroll = false;
}

function scrollMenu()
{
	menuButtonTop += ( getMouseY() - previousMouseY );
	previousMouseY = getMouseY();
	
	var maxScrollTop = menuMaxScroll - currentBarHeight;
	
	if( menuButtonTop < 0 )
		menuButtonTop = 0;
	else if( menuButtonTop > maxScrollTop )
		menuButtonTop = maxScrollTop;
	
	document.getElementById('scroll_bar_button').style.top = menuButtonTop + 14 + "px";
	
	var scrollVariation = menuButtonTop / maxScrollTop;
	menuLeftListTop = scrollVariation * ( menuContainHeight - document.getElementById('favory_list_contained').scrollHeight );
	document.getElementById('favory_list').style.top = menuLeftListTop + "px";
}