var livre_form = function()
{
	var id, left, top, width, height, back;
	var isPlaying;
	var contained;
	var formXHR;
	
	var nbTable;
	var coms = new Array();
	var formContained;
	var me = null;
	var leftDiv, rightDiv;
	var previousSelected = null;
	var msgDone = null;
	
	var isLoading = false;
	
	var currentPage = 1;
	var currentPageOnTable = 0;
	var currentTable = 0;
	
	var tablesDiv = null;
	
	this.start = function( node )
	{
		isPlaying = false;
		
		formXHR = initXHR();
		me = this;
		
		id = node.getAttribute('id');
		left = parseInt(node.getAttribute('left'));
		top = parseInt(node.getAttribute('top'));
		width = parseInt(node.getAttribute('width'));
		height = parseInt(node.getAttribute('height'));
		back = node.getAttribute('back');
		
		isLoading = true;
		ajaxRequest( "././php/getLivres.php", formXHR, this.loadingDone, "" );
	}
	
	this.addDone = function( result )
	{
		isLoading = false;
		if( trackSqlError( result ) )
		{
			msgDone.style.display = "block";
		}
	}
	
	this.loadingDone = function( result )
	{
		isLoading = false;
		if( trackSqlError( result ) )
		{
			if( result != "0" )
			{
				var lines = result.split( '&' );
				
				var nbT = parseInt(lines[0]);
				nbTable = parseInt( nbT / 3 );
				if( nbT > nbTable * 3 )
					nbTable++;
				
				for( var i = 1; i < lines.length; i++ )
				{
					var values = lines[i].split('|');
					if( values.length == 3 )
					{
						var newLine = new Array(3);
						newLine[0] = values[0];
						newLine[1] = values[1];
						newLine[2] = values[2];
						coms.push( newLine );
					}
				}
			}
		}
		
		// First time
		if( !isPlaying )
		{
			if( back != "" )
			{
				var preload = new Image();
				preload.onload = makeNextObject;
				preload.src = back;
			}
			else makeNextObject();
		}
		else
			me.fillTable();	// Refresh
	}
	
	this.fillTable = function()
	{
		if( rightDiv != null )
		{
			removeAllNodes( rightDiv, false );
		}
		
		var widthRef = width - 60;
		
		var i = 0;
		while( i < coms.length )
		{
			var curDiv = document.createElement("div");
			curDiv.style.textAlign = "left";
			curDiv.style.left = "0px";
			curDiv.style.top = i * 110 + "px";
			curDiv.style.width = widthRef / 2 + "px";
			curDiv.style.position = "absolute";
			curDiv.style.fontSize = "14px";
			curDiv.style.fontFamily = "times";
			var curDivTitle = document.createElement("u");
			curDivTitle.style.fontSize = "16px";
			curDivTitle.style.margin = "30px";
			var curDivTitleTxt = document.createTextNode("De " + coms[i][0] + " le " + DateTimeToFrStr(coms[i][1]));
			var curBr = document.createElement("br");
			var curDivTxt = document.createTextNode(coms[i][2]);
			
			curDivTitle.appendChild(curDivTitleTxt);
			curDiv.appendChild(curDivTitle);
			curDiv.appendChild(curBr);
			curDiv.appendChild(curDivTxt);
			rightDiv.appendChild(curDiv);
			i++;
		}
	}

	this.play = function( parent )
	{	
		if( isPlaying == false )
		{
			var widthRef = width - 80;
			
			contained = document.createElement("div");

			contained.setAttribute("id", id);

			contained.style.top = top + "px";
			contained.style.left = left + "px";
			contained.style.width = width + "px";
			contained.style.height = height + "px";
			contained.style.position = "absolute";
			contained.style.background = "url('" + back + "')";
			contained.style.backgroundRepeat = "no-repeat";
			
			// Form contained
			formContained = document.createElement("div");
			formContained.style.left = "30px";
			formContained.style.color = "#CFA102";
			formContained.style.position = "absolute";
			formContained.style.fontWeight = "bold";
			formContained.style.fontFamily = "times";
			
			leftDiv = document.createElement("div");
			leftDiv.style.textAlign = "center";
			leftDiv.style.top = "20px";
			leftDiv.style.left = "0px";
			leftDiv.style.width = widthRef / 2 + "px";
			leftDiv.style.position = "absolute";
			leftDiv.style.fontSize = "24px";
			formContained.appendChild(leftDiv);
			
			rightDiv = document.createElement("div");
			rightDiv.style.textAlign = "center";
			rightDiv.style.top = "20px";
			rightDiv.style.left = widthRef / 2 + 26 + "px";
			rightDiv.style.width = widthRef / 2 + "px";
			rightDiv.style.position = "absolute";
			rightDiv.style.fontSize = "24px";
			formContained.appendChild(rightDiv);
			
			if( coms.length > 0 )
			{
				this.fillTable();
					
				if( nbTable > 1 )
				{
					currentPageOnTable = 1;
					currentTable = 0;
					me.makeNumTable(3, 4);
				}
			}
			else
			{
				var divNoCom = document.createElement("div");
				divNoCom.style.position = "absolute";
				divNoCom.style.textAlign = "center";
				divNoCom.style.top = "140px";
				divNoCom.style.left = "0px";
				divNoCom.style.width = widthRef / 2 + "px";
				divNoCom.style.fontSize = "22px";
				var txtNoCom = document.createTextNode("Il n'y a aucun message pour le moment...");
				divNoCom.appendChild(txtNoCom);
				rightDiv.appendChild(divNoCom);
			}
				
			if( connected )
			{
				var postTitlteDiv = document.createElement("div");
				postTitlteDiv.style.position = "absolute";
				postTitlteDiv.style.textAlign = "center";
				postTitlteDiv.style.top = "20px";
				postTitlteDiv.style.left = "0px";
				postTitlteDiv.style.width = widthRef / 2 + "px";
				postTitlteDiv.style.fontSize = "20px";
				var txtTitleTxt = document.createTextNode("Poster un nouveau message :");
				var postInput = document.createElement("textarea");
				postInput.style.position = "absolute";
				postInput.style.left = "10px";
				postInput.style.top = "50px";
				postInput.style.width = widthRef / 2 - 26 + "px";
				postInput.style.height = "100px";
				var restDiv = document.createElement("div");
				restDiv.style.position = "absolute";
				restDiv.style.textAlign = "center";
				restDiv.style.top = "160px";
				restDiv.style.left = "0px";
				restDiv.style.width = widthRef / 2 + "px";
				restDiv.style.fontSize = "14px";
				var restDivTxt = document.createTextNode("Il vous reste 150 caractères.");
				msgDone = document.createElement("div");
				postInput.onkeyup = function()
				{
					if( postInput.value.length >= 150 )
						postInput.value = postInput.value.substr(0, 150);
					restDivTxt.nodeValue = "Il vous reste " + (150 - postInput.value.length) + " caractères.";
					msgDone.style.display = "none";
				}
				msgDone.style.position = "absolute";
				msgDone.style.textAlign = "center";
				msgDone.style.top = "230px";
				msgDone.style.left = "0px";
				msgDone.style.width = widthRef / 2 + "px";
				msgDone.style.fontSize = "14px";
				msgDone.style.display = "none";
				var msgDoneTxt = document.createTextNode("Votre message a bien été pri en compte et sera validé au plus tôt.");
				var sendButton = document.createElement("input");
				sendButton.type = "button";
				sendButton.style.position = "absolute";
				sendButton.style.top = "190px";
				sendButton.style.left = "80px";
				sendButton.style.width = "80px";
				sendButton.style.border = "double";
				sendButton.value = "Envoyer";
				sendButton.onclick = function()
				{
					if( !isLoading )
					{
						isLoading = true;
						ajaxRequest( "././php/addLivre.php", formXHR, me.addDone, "msg=" + postInput.value );
						postInput.value = "";
					}
				}
				
				postTitlteDiv.appendChild(txtTitleTxt);
				leftDiv.appendChild(postTitlteDiv);
				leftDiv.appendChild(postInput);
				restDiv.appendChild(restDivTxt);
				leftDiv.appendChild(restDiv);
				leftDiv.appendChild(sendButton);
				msgDone.appendChild(msgDoneTxt);
				leftDiv.appendChild(msgDone);
			}
			else
			{
				var txtDiv = document.createElement("div");
				txtDiv.style.position = "absolute";
				txtDiv.style.textAlign = "center";
				txtDiv.style.top = "130px";
				txtDiv.style.left = "0px";
				txtDiv.style.width = widthRef / 2 + "px";
				txtDiv.style.fontSize = "24px";
				var txtC = document.createTextNode("En vous inscrivant,");
				var brC = document.createElement("br");
				var txtC1 = document.createTextNode("vous pourrez vous aussi");
				var brC1 = document.createElement("br");
				var txtC2 = document.createTextNode("laisser vos messages ici !");
				
				txtDiv.appendChild(txtC);
				txtDiv.appendChild(brC);
				txtDiv.appendChild(txtC1);
				txtDiv.appendChild(brC1);
				txtDiv.appendChild(txtC2);
				leftDiv.appendChild(txtDiv);
			}
			
			contained.appendChild(formContained);
			parent.appendChild(contained);
			
			if( parent == document.getElementById('map_contained') )redimMapContained( left, top, width, height );

			isPlaying = true;
		}
	}
	
	this.makeNumTable = function(rowPerPage, nbPages)
	{
		var widthRef = width - 80;
		
		if( tablesDiv != null )
		{
			removeAllNodes( tablesDiv, true );
			tablesDiv = null;
		}
		tablesDiv = document.createElement("div");
		tablesDiv.style.position = "absolute";
		tablesDiv.style.left = widthRef / 2 + 26 + "px";
		tablesDiv.style.top = height - 48 + "px";
		tablesDiv.style.textAlign = "center";
		tablesDiv.style.width = widthRef / 2 + "px";
		
		var iT = currentTable * nbPages;
		
		if( currentTable > 0 )
		{
			var curButDiv = document.createElement("b");
			curButDiv.style.cursor = "pointer";
			curButDiv.style.color = "#226622";
			curButDiv.style.padding = "2px";
			curButDiv.style.borderStyle = "solid";
			curButDiv.style.borderWidth = "1px";
			curButDiv.onclick = function()
			{
				currentPageOnTable = nbPages;
				currentTable--;
				var popI = coms.length;
				while( popI > 0 )
				{
					coms.pop();
					popI--;
				}
				if( !isLoading )
				{
					isLoading = true;
					currentPage = currentTable * nbPages + currentPageOnTable;
					var correctOffset = (currentPage - 1) * rowPerPage;
					ajaxRequest( "././php/getLivres.php", formXHR, me.loadingDone, "offset=" + correctOffset );
				}
				me.makeNumTable(rowPerPage, nbPages);
			}
			var curText = document.createTextNode("< ");
			
			curButDiv.appendChild(curText);
			tablesDiv.appendChild(curButDiv);
		}
		
		currentPage = currentTable * nbPages + currentPageOnTable;
		
		var maxIT = nbPages;
		while( iT < nbTable && maxIT > 0 )
		{
			if( iT > 0 )
			{
				var curSep = document.createTextNode(" - ");
				tablesDiv.appendChild(curSep);
			}
			var curButDiv = document.createElement("b");
			if( iT == currentPage - 1 )
			{
				previousSelected = curButDiv;
				curButDiv.style.background = "#8888AA";
			}
			curButDiv.style.cursor = "pointer";
			curButDiv.style.color = "#226622";
			curButDiv.style.padding = "2px";
			curButDiv.style.borderStyle = "solid";
			curButDiv.style.borderWidth = "1px";
			curButDiv.onclick = function()
			{
				previousSelected.style.background = "";
				this.style.background = "#8888AA";
				previousSelected = this;
				var popI = coms.length;
				while( popI > 0 )
				{
					coms.pop();
					popI--;
				}
				
				var butOffset = parseInt(this.firstChild.nodeValue);
				var correctOffset = (butOffset - 1) * rowPerPage;
				if( !isLoading )
				{
					isLoading = true;
					currentPage = butOffset;
					currentPageOnTable = parseInt(currentPage / nbPages);
					ajaxRequest( "././php/getLivres.php", formXHR, me.loadingDone, "offset=" + correctOffset );
				}
			}
			var curText = document.createTextNode(iT + 1);
			
			curButDiv.appendChild(curText);
			tablesDiv.appendChild(curButDiv);
			iT++;
			maxIT--;
		}
		
		if( nbTable > currentTable * nbPages + nbPages )
		{
			var curSep = document.createTextNode(" - ");
			tablesDiv.appendChild(curSep);
			
			var curButDiv = document.createElement("b");
			curButDiv.style.cursor = "pointer";
			curButDiv.style.color = "#226622";
			curButDiv.style.padding = "2px";
			curButDiv.style.borderStyle = "solid";
			curButDiv.style.borderWidth = "1px";
			curButDiv.onclick = function()
			{
				currentPageOnTable = 1;
				currentTable++;
				var popI = coms.length;
				while( popI > 0 )
				{
					coms.pop();
					popI--;
				}
				if( !isLoading )
				{
					isLoading = true;
					currentPage = currentTable * nbPages + currentPageOnTable;
					var correctOffset = (currentPage - 1) * rowPerPage;
					ajaxRequest( "././php/getLivres.php", formXHR, me.loadingDone, "offset=" + correctOffset );
				}
				me.makeNumTable(rowPerPage, nbPages);
			}
			var curText = document.createTextNode(">");
			
			curButDiv.appendChild(curText);
			tablesDiv.appendChild(curButDiv);
		}
		
		formContained.appendChild(tablesDiv);
	}
	
	this.stop = function()
	{
		if( isPlaying == true )
		{
			removeAllNodes( contained, true );
			isPlaying = false;
		}
	}
}