var docXmlFAQ;
var simpleXHR = null;
var currentFAQPagesNumber = 0;
var titleBarFAQPrc = 5;
var firstFAQChild = null;

var faq_page = function()
{
	var xmlNode;
	var borderPrc;
	var bigDiv;
	var childPage = null;
	var titleBar = null;
	
	this.make = function( node )
	{
		xmlNode = node;
		if( xmlNode != null )
		{
			childPage = null;
			borderPrc = currentFAQPagesNumber * titleBarFAQPrc + currentFAQPagesNumber;
			
			bigDiv = document.createElement("div");
			bigDiv.style.position = "absolute";
			bigDiv.style.top = borderPrc + "%";
			bigDiv.style.width = "100%";
			bigDiv.style.height = (100 - borderPrc) + "%";
			bigDiv.style.background = "#E3DB95";
			
			titleBar = document.createElement("div");
			titleBar.style.position = "absolute";
			titleBar.style.width = "99%";
			titleBar.style.height = titleBarFAQPrc + "%";
			titleBar.style.borderColor = "#C8910E";
			titleBar.style.background = "#E0AD24";
			titleBar.style.textAlign = "center";
			titleBar.style.borderStyle = "solid";
			titleBar.style.cursor = "pointer";
			addEvent( titleBar, 'click', this.returnToMyMenu, false );
			
			var title_txt = document.createTextNode( "--  " + xmlNode.getAttribute( "value" ) + "  --" );
			
			var answer_div = document.createElement("div");
			answer_div.style.position = "absolute";
			answer_div.style.top = titleBarFAQPrc + 2 + "%";
			answer_div.style.left = "2%";
			answer_div.style.width = "97%";
			answer_div.style.height = (99 - borderPrc - titleBarFAQPrc) + "%";
			
			if( xmlNode.childNodes.length > 0 )
			{
				var questionUl = null;
				for( var i = 0; i < xmlNode.childNodes.length; i++ )
				{
					var currentNode = xmlNode.childNodes[i];
					if( currentNode.nodeType != 3 )
					{
						if( currentNode.nodeName == "answer" && currentNode.childNodes.length > 0 && questionUl == null )
						{
							var answer_txt = document.createTextNode( currentNode.childNodes[0].nodeValue );
							answer_div.appendChild( answer_txt );
							break;
						}
						else if( currentNode.nodeName == "question" )
						{
							if( questionUl == null )
							{
								questionUl = document.createElement("ul");
								questionUl.style.listStyleType = "disc";
								questionUl.style.marginTop = "0";
								questionUl.style.marginLeft = "15px";
								questionUl.style.padding = "1%";
							}
							
							var new_li = null;
							var new_txt = null;
							for( var i = 0; i < xmlNode.childNodes.length; i++ )
							{
								if( xmlNode.childNodes[i].nodeType != 3 )
								{
									new_li = document.createElement( "li" );
									new_li.style.cursor = "pointer";
									new_li.style.borderStyle = "double";
									new_li.style.borderColor = "#4B9643";
									new_li.style.background = "#BDDCD4";
									new_li.style.textIndent = "10px";
									new_li.style.color = "#3F045A";
									new_li.style.marginBottom = "2px";
									new_li.setAttribute("id", xmlNode.childNodes[i].getAttribute( "id" ) );
									
									new_li.onmouseover = function()
									{
										this.style.background = "#ACCBC3";
									}
									
									new_li.onmouseout = function()
									{
										this.style.background = "#BDDCD4";
									}
									new_li.onclick = function()
									{
										childPage = new faq_page();
										childPage.make( searchQuestionById( this.getAttribute( "id" ) ) );
									}
									
									new_txt = document.createTextNode( xmlNode.childNodes[i].getAttribute( "value" ) );
									new_li.appendChild( new_txt );
									
									questionUl.appendChild( new_li );
								}
							}
							
							answer_div.appendChild( questionUl );
						}
					}
				}
			}
			
			titleBar.appendChild( title_txt );
			bigDiv.appendChild( titleBar );
			bigDiv.appendChild( answer_div );
			document.getElementById('common_popup_faq_contained').appendChild( bigDiv );
			
			currentFAQPagesNumber++;
		}
	}
	
	this.closeAll = function()
	{
		currentFAQPagesNumber--;
		if( childPage != null )
			childPage.closeAll();
		
		removeEvent( titleBar, 'click', this.returnToMyMenu, false );
		
		removeAllNodes( bigDiv, true );
		delete( childPage );
		childPage = null;
	}
	
	this.returnToMyMenu = function()
	{
		if( childPage != null )
			childPage.closeAll();
		
		delete( childPage );
		childPage = null;
	}
}

function searchQuestionById( id )
{
	var questionsFAQ = null;
	
	if( docXmlFAQ != null )
		questionsFAQ = docXmlFAQ.getElementsByTagName('question');
	
	if( questionsFAQ != null )
	{
		for( var i = 0; i < questionsFAQ.length; i++ )
		{
			if( questionsFAQ[i].getAttribute( "id" ) == id )
				return questionsFAQ[i];
		}
	}
	return null;
}

function returnToFAQMenu()
{
	if( firstFAQChild != null )
	{
		firstFAQChild.closeAll();
		delete( firstFAQChild );
		firstFAQChild = null;
	}
}

function openFAQFromHelp()
{
	document.getElementById('common_popup_help_contained').style.display = "none";
	document.getElementById('common_popup_faq_contained').style.display = "block";
	document.getElementById('common_popup_txt').firstChild.nodeValue = "FAQ : Menu principal";	// Change popup title
	addEvent( document.getElementById('common_popup_bar1_2'), 'click', returnToFAQMenu, false );
	document.getElementById('common_popup_bar1_2').style.cursor = "pointer";
	
	var rootFAQ = null;
	if( docXmlFAQ != null )
		rootFAQ = docXmlFAQ.getElementsByTagName('page')[0];
		
	if( rootFAQ == null )
	{
		document.getElementById('common_popup_faq_contained').style.textAlign = "center";
		document.getElementById('common_popup_faq_contained').style.paddingTop = "30px";
		document.getElementById('common_popup_faq_contained').style.color = "#BB3333";
		document.getElementById('common_popup_faq_contained').firstChild.nodeValue = "La FAQ n'a pas pu être chargé...Le problème sera rêglé au plus vite. Merci de votre compréhension.";
	}
	else
	{
		if( rootFAQ.childNodes.length > 0 )
		{
			var new_ul = document.createElement("ul");
			new_ul.style.listStyleType = "disc";
			new_ul.style.marginTop = "0";
			new_ul.style.marginLeft = "15px";
			new_ul.style.padding = "1%";
			
			var new_li = null;
			var new_txt = null;
			for( var i = 0; i < rootFAQ.childNodes.length; i++ )
			{
				if( rootFAQ.childNodes[i].nodeType != 3 )
				{
					new_li = document.createElement( "li" );
					new_li.style.cursor = "pointer";
					new_li.style.borderStyle = "double";
					new_li.style.borderColor = "#4B9643";
					new_li.style.background = "#BDDCD4";
					new_li.style.textIndent = "10px";
					new_li.style.color = "#3F045A";
					new_li.style.marginBottom = "2px";
					new_li.setAttribute("id", rootFAQ.childNodes[i].getAttribute( "id" ) );
					
					new_li.onmouseover = function()
					{
						this.style.background = "#ACCBC3";
					}
					
					new_li.onmouseout = function()
					{
						this.style.background = "#BDDCD4";
					}
					new_li.onclick = function()
					{
						firstFAQChild = new faq_page();
						firstFAQChild.make( searchQuestionById( this.getAttribute( "id" ) ) );
					}
					
					new_txt = document.createTextNode( rootFAQ.childNodes[i].getAttribute( "value" ) );
					new_li.appendChild( new_txt );
					
					new_ul.appendChild( new_li );
				}
			}
			
			document.getElementById('common_popup_faq_contained').appendChild( new_ul );
		}
	}
}

function closeFAQFromHelp()
{
	removeAllNodes( document.getElementById('common_popup_faq_contained'), false );
	currentFAQPagesNumber = 0;
	document.getElementById('common_popup_bar1_2').style.cursor = "";
	removeEvent( document.getElementById('common_popup_bar1_2'), 'click', returnToFAQMenu, false );
	document.getElementById('common_popup_faq_contained').style.display = "none";
}

function initFAQ()
{
	simpleXHR = initXHR();
	loadSimpleXML( "./data/xml/faq.xml", simpleXHR, loadingCallBack, "Erreur lors du chargement de la FAQ !|Un email d'erreur vien d'être envoyé au Webmaster.|Merci de votre compréhension." );
}

function loadingCallBack( docXml )
{
	docXmlFAQ = docXml;
	delete( simpleXHR );
}