var defis_class_form = function()
{
	var id, left, top, width, height, type, size;
	var isPlaying;
	var contained;
	var formXHR;
	
	var nbTable;
	var coms = new Array();
	var formContained;
	var table1 = null;
	var me = null;
	
	var nbj, kdoType, kdo, players;
	
	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'));
		type = parseInt(node.getAttribute('type'));
		size = parseInt(node.getAttribute('size'));
		
		if( connected )
		{
			isLoading = true;
			ajaxRequest( "././php/getDefisResult.php", formXHR, this.loadingDone, "game=" + paramsURL[1][1] + "&type=" + paramsURL[0][1] );
		}
		else
			makeNextObject();
	}
	
	this.loadingDone = function( result )
	{
		isLoading = false;
		if( trackSqlError( result ) )
		{
			if( result != "0" )
			{
				var lines = result.split( '&' );
				
				var firstValues = lines[0].split('|');
				nbj = parseInt(firstValues[0]);
				kdotType = parseInt(firstValues[1]);
				kdo = parseInt(firstValues[2]);
				players = parseInt(firstValues[3]);
				
				nbTable = parseInt( nbj / size );
				if( nbj > nbTable * size )
					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 );
					}
				}
			}
		}
		
		if( !isPlaying )
			makeNextObject();
		else
			me.fillTable();	// Refresh
	}
	
	this.fillTable = function()
	{
		if( table1 != null )
		{
			removeAllNodes( table1, true );
			table1 = null;
		}
		
		table1 = document.createElement("table");
		table1.style.position = "absolute";
		table1.style.top = "54px";
		table1.style.borderColor = "#0D5F05";
		table1.style.borderStyle = "solid";
		table1.style.borderWidth = "2px";
		table1.width = width - 30;
		
		var tbody = document.createElement('tbody');
		table1.appendChild(tbody);
			
		var row = document.createElement("tr");
		var cell1 = document.createElement("td");
		cell1.style.width = "30px";
		cell1.style.borderStyle = "solid";
		cell1.style.borderWidth = "2px";
		var celltxt1 = document.createTextNode("Position");
		var cell2 = document.createElement("td");
		cell2.style.width = "50px";
		cell2.style.borderStyle = "solid";
		cell2.style.borderWidth = "2px";
		var celltxt2 = document.createTextNode("Pseudo");
		var cell3 = document.createElement("td");
		cell3.style.width = "50px";
		cell3.style.borderStyle = "solid";
		cell3.style.borderWidth = "2px";
		var celltxt3 = document.createTextNode("Score");
		var cell4 = document.createElement("td");
		cell4.style.width = "100px";
		cell4.style.borderStyle = "solid";
		cell4.style.borderWidth = "2px";
		var celltxt4 = null;
		if( paramsURL[0][1] == 2 )
			celltxt4 = document.createTextNode("Cadeau gagné");
		else
			celltxt4 = document.createTextNode("Gain potentiel");
		
		cell1.appendChild(celltxt1);
		row.appendChild(cell1);
		cell2.appendChild(celltxt2);
		row.appendChild(cell2);
		cell3.appendChild(celltxt3);
		row.appendChild(cell3);
		cell4.appendChild(celltxt4);
		row.appendChild(cell4);
		tbody.appendChild(row);
		
		var font = "normal";
		var color = "#000000";
		var i = 0;
		while( i + (currentPage - 1) * size < nbj && i < size )
		{
			if( i < coms.length && myId == coms[i][0] )
			{
				color = "#0000CC";
				font = "bolder";
			}
			else
			{
				color = "#000000";
				font = "normal";
			}
			var curRow = document.createElement("tr");
			var curCell1 = document.createElement("td");
			curCell1.style.borderStyle = "solid";
			curCell1.style.borderWidth = "1px";
			curCell1.style.fontWeight = font;
			curCell1.style.color = color;
			var curCell1txt1 = document.createTextNode(i + ((currentPage - 1) * size + 1));
			var curCell2 = document.createElement("td");
			curCell2.style.borderStyle = "solid";
			curCell2.style.borderWidth = "1px";
			curCell2.style.fontWeight = font;
			curCell2.style.overFlow = "hidden";
			curCell2.style.color = color;
			var curCelltxt2 = null;
			if( i < coms.length )
				curCelltxt2 = document.createTextNode(coms[i][1]);
			else
				curCelltxt2 = document.createTextNode("Non joué");
			var curCell3 = document.createElement("td");
			curCell3.style.borderStyle = "solid";
			curCell3.style.borderWidth = "1px";
			curCell3.style.fontWeight = font;
			curCell3.style.overFlow = "hidden";
			curCell3.style.color = color;
			var curCelltxt3 = null;
			if( i < coms.length )
				curCelltxt3 = document.createTextNode(coms[i][2]);
			else
				curCelltxt3 = document.createTextNode("Non joué");
			var curCell4 = document.createElement("td");
			curCell4.style.borderStyle = "solid";
			curCell4.style.borderWidth = "1px";
			curCell4.style.fontWeight = font;
			curCell4.style.color = color;
			var curCelltxt4 = null;
			if( i == 0 && currentPage == 1 )
			{
				if( kdoType == 0 )
					curCelltxt4 = document.createTextNode(kdo + " Kdor");
				else if( kdoType == 0 )
					curCelltxt4 = document.createTextNode(kdo + " Hooky");
				else
					curCelltxt4 = document.createTextNode(kdo);
			}
			else
				curCelltxt4 = document.createTextNode("Perdant");

			curCell1.appendChild(curCell1txt1);
			curRow.appendChild(curCell1);
			curCell2.appendChild(curCelltxt2);
			curRow.appendChild(curCell2);
			curCell3.appendChild(curCelltxt3);
			curRow.appendChild(curCell3);
			curCell4.appendChild(curCelltxt4);
			curRow.appendChild(curCell4);
			tbody.appendChild(curRow);
			
			i++;
		}
			
		formContained.appendChild(table1);
	}

	this.play = function( parent )
	{	
		if( isPlaying == false )
		{
			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";
			
			// Form contained
			formContained = document.createElement("div");
			formContained.style.left = "24px";
			formContained.style.color = "#0B0B64";
			formContained.style.position = "absolute";
			formContained.style.fontWeight = "bold";
			formContained.style.fontFamily = "times";
			
			if( connected )
			{
				var title1 = document.createElement("u");
				title1.style.left = "0px";
				title1.style.top = "10px";
				title1.style.width = width + "px";
				title1.style.textAlign = "center";
				title1.style.position = "absolute";
				var title1Txt = document.createTextNode("Classement du tournois");
				title1.appendChild(title1Txt);
				formContained.appendChild(title1);
				
				this.fillTable();
				
				if( nbTable > 1 )
				{
					currentPageOnTable = 1;
					currentTable = 0;
					me.makeNumTable(size, 10);
				}
			}
			else if( type == 0 )
			{
				formContained.style.top = "120px";
				formContained.style.width = width + "px";
				formContained.style.textAlign = "center";
				formContained.style.fontSize = "24px";
				var txtC = document.createTextNode("En vous inscrivant,");
				var brC = document.createElement("br");
				var txtC1 = document.createTextNode("vous pourrez défier d'autres joueurs...");
				var brC1 = document.createElement("br");
				var txtC2 = document.createTextNode("Et gagner encore plus !");
				
				formContained.appendChild(txtC);
				formContained.appendChild(brC);
				formContained.appendChild(txtC1);
				formContained.appendChild(brC1);
				formContained.appendChild(txtC2);
			}
			
			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;
		
		if( tablesDiv != null )
		{
			removeAllNodes( tablesDiv, true );
			tablesDiv = null;
		}
		tablesDiv = document.createElement("div");
		tablesDiv.style.position = "absolute";
		tablesDiv.style.top = height - 20 + "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 = "#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/getDefisResult.php", formXHR, me.loadingDone, "game=" + paramsURL[1][1] + "&type=" + paramsURL[0][1] + "&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/getDefisResult.php", formXHR, me.loadingDone, "game=" + paramsURL[1][1] + "&type=" + paramsURL[0][1] + "&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/getDefisResult.php", formXHR, me.loadingDone, "game=" + paramsURL[1][1] + "&type=" + paramsURL[0][1] + "&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;
		}
	}
}