var news_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 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/getNews.php", formXHR, this.loadingDone, "" );
	}
	
	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( formContained != null )
		{
			removeAllNodes( formContained, false );
		}
		
		var widthRef = width - 160;
		
		var i = 0;
		while( i < coms.length )
		{
			var curDiv = document.createElement("div");
			curDiv.style.textAlign = "left";
			curDiv.style.top = i * 120 + "px";
			curDiv.style.width = widthRef + "px";
			curDiv.style.position = "absolute";
			curDiv.style.fontSize = "14px";
			curDiv.style.fontFamily = "times";
			curDiv.style.color = "#222222";
			var curDivTitle = document.createElement("u");
			curDivTitle.style.fontSize = "16px";
			curDivTitle.style.margin = "30px";
			curDivTitle.style.color = "#0A5AD3";
			var curDivTitleTxt = document.createTextNode("Posté le " + DateTimeToFrStr(coms[i][0]) + " : " + 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);
			formContained.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 = "50px";
			formContained.style.top = "30px";
			formContained.style.color = "#0A5AD3";
			formContained.style.position = "absolute";
			formContained.style.fontWeight = "bold";
			formContained.style.fontFamily = "times";
			
			if( coms.length > 0 )
			{
				this.fillTable();
					
				if( nbTable > 1 )
				{
					currentPageOnTable = 1;
					currentTable = 0;
					me.makeNumTable(3, 5);
				}
			}
			else
			{
				var divNoCom = document.createElement("div");
				divNoCom.style.position = "absolute";
				divNoCom.style.textAlign = "center";
				divNoCom.style.top = "160px";
				divNoCom.style.width = widthRef + "px";
				divNoCom.style.fontSize = "24px";
				var txtNoCom = document.createTextNode("Il n'y a aucune nouveauté pour le moment...");
				divNoCom.appendChild(txtNoCom);
				formContained.appendChild(divNoCom);
			}
			
			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 = "20px";
		tablesDiv.style.top = height - 60 + "px";
		tablesDiv.style.textAlign = "center";
		tablesDiv.style.width = widthRef + "px";
		
		var iT = currentTable * nbPages;
		
		if( currentTable > 0 )
		{
			var curButDiv = document.createElement("b");
			curButDiv.style.cursor = "pointer";
			curButDiv.style.color = "#0A4FB6";
			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/getNews.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 = "#C7DCFC";
			}
			curButDiv.style.cursor = "pointer";
			curButDiv.style.color = "#0A4FB6";
			curButDiv.style.padding = "2px";
			curButDiv.style.borderStyle = "solid";
			curButDiv.style.borderWidth = "1px";
			curButDiv.onclick = function()
			{
				previousSelected.style.background = "";
				this.style.background = "#C7DCFC";
				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/getNews.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 = "#0A4FB6";
			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/getNews.php", formXHR, me.loadingDone, "offset=" + correctOffset );
				}
				me.makeNumTable(rowPerPage, nbPages);
			}
			var curText = document.createTextNode(">");
			
			curButDiv.appendChild(curText);
			tablesDiv.appendChild(curButDiv);
		}
		
		contained.appendChild(tablesDiv);
	}
	
	this.stop = function()
	{
		if( isPlaying == true )
		{
			removeAllNodes( contained, true );
			isPlaying = false;
		}
	}
}