var conquete_ile = function()
{
	var id, back, but_prev, but_next, game_page;
	var isPlaying;
	var contained;
	var formXHR;
	
	var case1 = null;
	var case2 = null;
	
	var prec_but = null;
	var suiv_but = null;
	
	var coms = new Array();
	var me = null;
	
	var curId = 0;
	
	var isLoading = false;
	
	this.start = function( node )
	{
		isPlaying = false;
		
		formXHR = initXHR();
		me = this;
		
		id = node.getAttribute('id');
		game_page = node.getAttribute('game_page');
		back = node.getAttribute('back');
		but_prev = node.getAttribute('but_prev');
		but_next = node.getAttribute('but_next');
		
		isLoading = true;
		ajaxRequest( "././php/getConquetes.php", formXHR, this.loadingDone, "" );
	}
	
	this.loadingDone = function( result )
	{
		isLoading = false;
		if( trackSqlError( result ) )
		{
			if( result != "0" )
			{
				var lines = result.split( '&' );
				
				for( var i = 0; i < lines.length; i++ )
				{
					var values = lines[i].split('|');
					if( values.length == 10 )
					{
						var newLine = new Array(9);
						newLine[0] = values[0];
						newLine[1] = values[1];
						newLine[2] = values[2];
						newLine[3] = values[3];
						newLine[4] = values[4];
						newLine[5] = values[5];
						newLine[6] = values[6];
						newLine[7] = values[7];
						newLine[8] = values[8];
						newLine[9] = values[9];
						coms.push( newLine );
					}
				}
			}
		}
		
		if( back != "" )
		{
			var preload = new Image();
			preload.onload = makeNextObject;
			preload.src = back;
		}
		else makeNextObject();
	}
	
	this.makeConq = function(id, casing)
	{
		var title = document.createElement("div");
		title.style.fontWeight = "bold";
		title.style.fontSize = "16px";
		title.style.top = "0px";
		title.style.left = "0px";
		title.style.width = "160px";
		title.style.position = "absolute";
		title.style.textAlign = "center";
		var titleTxt = document.createTextNode(coms[id][1]);
		title.appendChild(titleTxt);
		casing.appendChild(title);
		
		var img = document.createElement("img");
		img.src = coms[id][2];
		img.style.top = "20px";
		img.style.left = "30px";
		img.style.width = "100px";
		img.style.position = "absolute";
		img.style.textAlign = "center";
		img.width = 100;
		img.height = 100;
		img.id = id;
		casing.appendChild(img);
		
		img.onmouseover = function()
		{
			var ind = parseInt(this.id);
			var titleVal = coms[ind][1];
			var imgVal = coms[ind][2];
			var descVal = coms[ind][4];
			if( descVal.length > 150 )
				descVal = descVal.substr(0, 150) + "...";
			descVal += "\n\nPrix du territoire : " + coms[ind][6] + " Kdor";
			descVal += "\nPrix du pirate : " + coms[ind][7] + " Hooky";
			if( coms[ind][8] > 0 )
				descVal += " ou " + coms[ind][8] + " Kdor";
			descVal += "\nPrix de la bombe atomique : " + coms[ind][9] + " Kdor";
			var sendVal = coms[ind][5];
			var stockVal = "Conquête";
			var destVal = "0";
			isOnLink = true;
			shopDialogPlay( this, titleVal, imgVal, descVal, sendVal, stockVal, destVal );
		}
		img.onmouseout = function()
		{
			isOnLink = false;
			shopDialogStop();
		}
		
		var endDate = document.createElement("div");
		endDate.style.fontWeight = "bold";
		endDate.style.fontSize = "16px";
		endDate.style.top = "122px";
		endDate.style.left = "0px";
		endDate.style.width = "160px";
		endDate.style.position = "absolute";
		endDate.style.textAlign = "center";
		var endDateTxt = document.createTextNode("Fin de la partie le " + DateTimeToFrStr(coms[id][0]) + " à 22 H !");
		endDate.appendChild(endDateTxt);
		casing.appendChild(endDate);
	}
	
	this.showGame = function(id)
	{
		if( case1 != null )
			removeAllNodes( case1, false );
		
		if( case2 != null )
			removeAllNodes( case2, false );
		
		case1.name = coms[id][3];
		this.makeConq(id, case1);
		
		if( id + 1 < coms.length )
		{
			case2.style.display = "block";
			case2.name = coms[id + 1][3];
			this.makeConq(id + 1, case2);
		}
		else
			case2.style.display = "none";
		
		if( id + 2 < coms.length )
			suiv_but.style.display = "block";
		else
			suiv_but.style.display = "none";
		if( id > 0 )
			prev_but.style.display = "block";
		else
			prev_but.style.display = "none";
	}

	this.play = function( parent )
	{	
		if( isPlaying == false )
		{
			contained = document.createElement("div");

			contained.setAttribute("id", id);

			contained.style.top = "0px";
			contained.style.left = "0px";
			contained.style.width = "613px";
			contained.style.height ="460px";
			contained.style.position = "absolute";
			contained.style.background = "url('" + back + "')";
			contained.style.backgroundRepeat = "no-repeat";
			
			case1 = document.createElement("div");
			case1.style.top = "164px";
			case1.style.left = "86px";
			case1.style.width = "160px";
			case1.style.height ="160px";
			case1.style.position = "absolute";
			case1.style.cursor = "pointer";
			
			case1.onclick = function()
			{
				changeCurrentPage(game_page + "?idg=" + this.name);
			}
			
			case2 = document.createElement("div");
			case2.style.top = "164px";
			case2.style.left = "378px";
			case2.style.width = "160px";
			case2.style.height ="160px";
			case2.style.position = "absolute";
			case2.style.cursor = "pointer";
			
			case2.onclick = function()
			{
				changeCurrentPage(game_page + "?idg=" + this.name);
			}
			
			prev_but = document.createElement("div");
			prev_but.style.top = "360px";
			prev_but.style.left = "6px";
			prev_but.style.width = "97px";
			prev_but.style.height ="83px";
			prev_but.style.position = "absolute";
			prev_but.style.background = "url('" + but_prev + "')";
			prev_but.style.backgroundRepeat = "no-repeat";
			prev_but.style.cursor = "pointer";
			prev_but.style.display = "none";
			
			prev_but.onclick = function()
			{
				curId -= 2;
				me.showGame(curId);
			}
			
			suiv_but = document.createElement("div");
			suiv_but.style.top = "360px";
			suiv_but.style.left = "500px";
			suiv_but.style.width = "98px";
			suiv_but.style.height ="84px";
			suiv_but.style.position = "absolute";
			suiv_but.style.background = "url('" + but_next + "')";
			suiv_but.style.backgroundRepeat = "no-repeat";
			suiv_but.style.cursor = "pointer";
			suiv_but.style.display = "none";
			
			suiv_but.onclick = function()
			{
				curId += 2;
				me.showGame(curId);
			}
			
			if( coms.length > 0 )
				this.showGame(curId);
			
			contained.appendChild(case1);
			contained.appendChild(case2);
			
			contained.appendChild(prev_but);
			contained.appendChild(suiv_but);
			
			var regles_but = document.createElement("a");
			regles_but.style.top = "410px";
			regles_but.style.left = "200px";
			regles_but.style.width = "200px";
			regles_but.style.height ="50px";
			regles_but.style.position = "absolute";
			regles_but.href = "././regle/conquetes.html";
			regles_but.target = "_blank";
			
			contained.appendChild(regles_but);
			
			var expl_but = document.createElement("a");
			expl_but.style.top = "88px";
			expl_but.style.left = "240px";
			expl_but.style.width = "125px";
			expl_but.style.height ="100px";
			expl_but.style.position = "absolute";
			expl_but.href = "http://fr.youtube.com/watch?v=YHeVhLHrgmk";
			expl_but.target = "_blank";
			
			contained.appendChild(expl_but);
			
			parent.appendChild(contained);
			
			if( parent == document.getElementById('map_contained') )redimMapContained( 0, 0, 613, 460 );

			isPlaying = true;
		}
	}
	
	this.stop = function()
	{
		if( isPlaying == true )
		{
			removeAllNodes( contained, true );
			isPlaying = false;
		}
	}
}





var conquete_winner_form = function()
{
	var id, left, top, width, height, back;
	var isPlaying;
	var contained;
	var formXHR;
	
	var nbTable;
	var coms = new Array();
	var formContained;
	var table = null;
	var me = null;
	var toChangeCell = null;
	var previousSelected = 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/getConqWinners.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 / 10 );
				if( nbT > nbTable * 10 )
					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( table != null )
		{
			removeAllNodes( table, true );
			table = null;
		}
		
		var widthRef = width - 60;
		
		table = document.createElement("table");
		table.style.position = "absolute";
		table.style.top = "40px";
		table.style.borderColor = "#0D5F05";
		table.style.borderStyle = "solid";
		table.style.borderWidth = "2px";
		table.width = widthRef;
		
		var tbody = document.createElement('tbody');
		table.appendChild(tbody);
		
		var row = document.createElement("tr");
		var cell1 = document.createElement("td");
		cell1.style.width = "100px";
		cell1.style.borderStyle = "solid";
		cell1.style.borderWidth = "2px";
		var celltxt1 = document.createTextNode("Date");
		var cell2 = document.createElement("td");
		cell2.style.width = "160px";
		cell2.style.borderStyle = "solid";
		cell2.style.borderWidth = "2px";
		var celltxt2 = document.createTextNode("Gagnant");
		var cell3 = document.createElement("td");
		cell3.style.width = "160px";
		cell3.style.borderStyle = "solid";
		cell3.style.borderWidth = "2px";
		var celltxt3 = document.createTextNode("Cadeau");
					
		cell1.appendChild(celltxt1);
		row.appendChild(cell1);
		cell2.appendChild(celltxt2);
		row.appendChild(cell2);
		cell3.appendChild(celltxt3);
		row.appendChild(cell3);
		tbody.appendChild(row);
					
		var i = 0;
		while( i < coms.length )
		{
			var curRow = document.createElement("tr");
			var curCell1 = document.createElement("td");
			curCell1.style.borderStyle = "solid";
			curCell1.style.borderWidth = "1px";
			curCell1.style.fontWeight = "normal";
			var curCell1txt1 = document.createTextNode(DateTimeToFrStr(coms[i][0]));
			var curCell2 = document.createElement("td");
			curCell2.style.borderStyle = "solid";
			curCell2.style.borderWidth = "1px";
			curCell2.style.fontWeight = "normal";
			curCell2.style.overFlow = "hidden";
			var curCelltxt2 = document.createTextNode(coms[i][1]);
			var curCelltxt3 = document.createTextNode(coms[i][2]);
			var curCell3 = document.createElement("td");
			curCell3.style.borderStyle = "solid";
			curCell3.style.borderWidth = "1px";
			curCell3.style.fontWeight = "normal";
			curCell3.style.overFlow = "hidden";
			
			curCell1.appendChild(curCell1txt1);
			curRow.appendChild(curCell1);
			curCell2.appendChild(curCelltxt2);
			curRow.appendChild(curCell2);
			curCell3.appendChild(curCelltxt3);
			curRow.appendChild(curCell3);
			tbody.appendChild(curRow);
		
			i++;
		}
		
		formContained.appendChild(table);
	}

	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";
			contained.style.background = "url('" + back + "')";
			contained.style.backgroundRepeat = "no-repeat";
			
			// Form contained
			formContained = document.createElement("div");
			formContained.style.left = "30px";
			formContained.style.color = "#0B0B64";
			formContained.style.position = "absolute";
			formContained.style.fontWeight = "bold";
			formContained.style.fontFamily = "times";
			
			var widthRef = width - 80;
			var divTitle = document.createElement("div");
			divTitle.style.textAlign = "center";
			divTitle.style.top = "10px";
			divTitle.style.width = widthRef + "px";
			divTitle.style.position = "absolute";
			divTitle.style.fontSize = "24px";
			var txt1 = document.createTextNode("Précédents gagnants");
			
			if( coms.length > 0 )
			{
				this.fillTable();
				
				if( nbTable > 1 )
				{
					currentPageOnTable = 1;
					currentTable = 0;
					me.makeNumTable(10, 10);
				}
			}
			else
			{
				var divNoCom = document.createElement("div");
				divNoCom.style.textAlign = "center";
				divNoCom.style.top = "180px";
				divNoCom.style.width = width - 80 + "px";
				divNoCom.style.position = "absolute";
				divNoCom.style.fontSize = "18px";
				var txtNoCom = document.createTextNode("Aucune partie terminée...");
				divNoCom.appendChild(txtNoCom);
				formContained.appendChild(divNoCom);
			}
			
			divTitle.appendChild(txt1);
			formContained.appendChild(divTitle);
			
			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.top = height - 46 + "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/getConqWinners.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/getConqWinners.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/getConqWinners.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;
		}
	}
}