var shop_form = function()
{
	var id, width, height, back;
	var isPlaying;
	
	var div1, div2, div3, div4;
	var divOrder = new Array();
	var cadeaux = new Array();
	var nbPages;
	var widthRef = 0;
	var nbRight = 4;
	var hooky = 0;
	
	var formXHR;
	
	var isLoading = false;
	
	this.start = function( node )
	{
		isPlaying = false;
		me = this;
		
		formXHR = initXHR();
		
		id = node.getAttribute('id');
		width = parseInt(node.getAttribute('width'));
		height = parseInt(node.getAttribute('height'));
		back = node.getAttribute('back');
		
		var params = "";
		if( paramsURL.length > 0 )
			params = "cat=" + paramsURL[0][1];
		isLoading = true;
		ajaxRequest( "././php/getCadeaux.php", formXHR, this.loadingDone, params );
	}
	
	this.loadingDone = function( result )
	{
		isLoading = false;
		if( trackSqlError( result ) )
		{
			if( !isPlaying )
			{
				if( result != "0" )
				{
					var lines = result.split( '&' );
					
					var nbT = parseInt(lines[0]);
					nbPages = parseInt( nbT / 6 );
					if( nbT > nbPages * 6 )
						nbPages++;
						
					hooky = parseInt(lines[1]);
					
					for( var i = 2; i < lines.length; i++ )
					{
						var values = lines[i].split('|');
						if( values.length == 12 )
						{
							var newLine = new Array(12);
							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];
							newLine[10] = values[10];
							newLine[11] = values[11];
							cadeaux.push( newLine );
						}
					}
				}
			}
		}
		
		// First time
		if( !isPlaying )
		{
			if( back != "" )
			{
				var preload = new Image();
				preload.onload = makeNextObject;
				preload.src = back;
			}
			else makeNextObject();
		}
	}
	
	this.commandDone = function(result)
	{
		isLoading = false;
		if( trackSqlError( result ) )
		{
			if( result == '0' )
				alert("Vous n'avez pas assez de hooky pour commande ce cadeau...Continuez à jouer pour gagner encore plus !");
			else if( result == '1' )
			{
				changeCurrentPage(openedFile);
	
				alert("Désolé ce cadeau n'est plus en stock...N'hésitez pas à repasser par ici pour vérifier les stocks...");
			}
			else
			{
				var lines = result.split( '&' );
				
				changeCurrentPage(openedFile);
				
				delHooky(lines[1]);
	
				alert("Votre commande vien d'être passée. N'oubliez pas de valider l'envoi sur la page de suivi des commandes !");
			}
		}
	}
	
	this.makePageContain = function( container, page )
	{
		removeAllNodes( container, false );
		var y = 0;
		var x = 0;
		var kdo = page * 6;
		while( y < 2 )
		{
			while( x < 3 )
			{
				if( kdo < cadeaux.length )
				{
					var curLot = document.createElement("div");
					
					curLot.style.left = x * 190 + 24 + "px";
					curLot.style.top = y * 212 + 10 + "px";
					curLot.style.width = "174px";
					curLot.style.height = "185px";
					curLot.style.position = "absolute";
					curLot.style.fontWeight = "bold";
					curLot.style.color = "#08289F";
					
					if( cadeaux[kdo][3] == "0" )
					{
						var titleDiv = document.createElement("u");
						titleDiv.style.position = "absolute";
						titleDiv.style.width = "174px";
						var addPts = "";
						if( cadeaux[kdo][4].length > 18 )
						{
							titleDiv.style.fontSize = "14px";
							addPts = "...";
						}
						else
							titleDiv.style.fontSize = "18px";
						titleDiv.style.textAlign = "center";
						titleDiv.title = cadeaux[kdo][4];
						
						var titleTxt = document.createTextNode( cadeaux[kdo][4].substr(0, 20) + addPts );
						
						var img = document.createElement("img");
						img.src = cadeaux[kdo][6];
						img.width = 74;
						img.height = 94;
						img.style.top = "30px";
						img.style.left = "4px";
						img.border = 2;
						img.style.position = "absolute";
						img.style.cursor = "pointer";
						img.id = kdo;
						img.onmouseover = function()
						{
							var kdo = parseInt(this.id);
							var titleVal = cadeaux[kdo][4];
							var imgVal = cadeaux[kdo][6];
							var descVal = cadeaux[kdo][5];
							var sendVal = cadeaux[kdo][9];
							var stockVal = cadeaux[kdo][10];
							var destVal = cadeaux[kdo][11];
							isOnLink = true;
							shopDialogPlay( this, titleVal, imgVal, descVal, sendVal, stockVal, destVal );
						}
						img.onmouseout = function()
						{
							isOnLink = false;
							shopDialogStop();
						}
						
						var prixDiv = document.createElement("div");
						prixDiv.style.position = "absolute";
						prixDiv.style.width = "80px";
						prixDiv.style.left = "86px";
						prixDiv.style.top = "30px";
						prixDiv.style.fontSize = "14px";
						prixDiv.style.textAlign = "center";
						prixDiv.style.border = "solid";
						if( connected && rang >= 3 )
							prixDiv.style.textDecoration = "line-through";
						
						var prixTitleTxt = document.createTextNode( "Prix" );
						var hr1 = document.createElement("div");
						hr1.style.width = "80px";
						hr1.style.borderColor = "#08289F";
						hr1.style.borderStyle = "solid";
						hr1.style.borderWidth = "1px";
						hr1.style.height = "1px";
						var prixTxt = document.createTextNode( cadeaux[kdo][7] + " H" );
						
						var prixVipDiv = document.createElement("div");
						prixVipDiv.style.position = "absolute";
						prixVipDiv.style.width = "80px";
						prixVipDiv.style.left = "86px";
						prixVipDiv.style.top = "82px";
						prixVipDiv.style.fontSize = "14px";
						prixVipDiv.style.textAlign = "center";
						prixVipDiv.style.border = "solid";
						if( connected && rang < 3 )
							prixVipDiv.style.textDecoration = "line-through";
						
						var prixVipTitleTxt = document.createTextNode( "Prix VIP" );
						var hr2 = document.createElement("div");
						hr2.style.width = "80px";
						hr2.style.borderColor = "#08289F";
						hr2.style.borderStyle = "solid";
						hr2.style.borderWidth = "1px";
						hr2.style.height = "1px";
						var prixVipTxt = document.createTextNode( cadeaux[kdo][8] + " H" );
						
						var footDiv = document.createElement("div");
						footDiv.style.position = "absolute";
						footDiv.style.width = "170px";
						footDiv.style.left = "2px";
						footDiv.style.top = "140px";
						footDiv.style.textAlign = "center";
						
						if( connected )
						{
							if( memberType )
							{
								var myPrice = cadeaux[kdo][7];
								if( rang >= 3 )
									myPrice = cadeaux[kdo][8];
								if( hooky < myPrice )
								{
									var footTxt = document.createTextNode( "Pas assez de hooky !" );
									footDiv.appendChild(footTxt);
								}
								else
								{
									var footBut = document.createElement("div");
									footBut.style.border = "double";
									footBut.style.background = "#A7CACD";
									footBut.style.cursor = "pointer";
									footBut.name = cadeaux[kdo][0];
									footBut.onclick = function()
									{
										if( !isLoading )
										{
											isLoading = true;
											ajaxRequest( "././php/commandCadeau.php", formXHR, me.commandDone, "id=" + this.name );
										}
									}
									
									var footTxt = document.createTextNode( "Commander !" );
									footBut.appendChild(footTxt);
									footDiv.appendChild(footBut);
								}
							}
							else
							{
								var footTxt = document.createTextNode( "Veuillez remplir vos coordonées !" );
								footDiv.appendChild(footTxt);
							}
						}
						else
						{
							var footTxt = document.createTextNode( "Inscrivez vous pour commander" );
							footDiv.appendChild(footTxt);
						}
						
						titleDiv.appendChild(titleTxt);
						curLot.appendChild(titleDiv);
						curLot.appendChild(img);
						prixDiv.appendChild(prixTitleTxt);
						prixDiv.appendChild(hr1);
						prixDiv.appendChild(prixTxt);
						curLot.appendChild(prixDiv);
						prixVipDiv.appendChild(prixVipTitleTxt);
						prixVipDiv.appendChild(hr2);
						prixVipDiv.appendChild(prixVipTxt);
						curLot.appendChild(prixVipDiv);
						curLot.appendChild(footDiv);
					}
					else
					{
						var img = document.createElement("img");
						img.src = cadeaux[kdo][6];
						img.width = 154;
						img.height = 145;
						img.style.top = "20px";
						img.style.left = "10px";
						img.border = 2;
						img.style.position = "absolute";
						
						curLot.appendChild(img);
						
						if( cadeaux[kdo][4] != "" )
						{
							var txtSize = 42 - cadeaux[kdo][4].length;
							if( txtSize < 16 )
								txtSize = 16;
							img.border = 0;
							var descDiv = document.createElement("div");
							descDiv.style.position = "absolute";
							descDiv.style.left = "5px";
							descDiv.style.top = "60px";
							descDiv.style.width = "160px";
							descDiv.style.fontSize = txtSize + "px";
							descDiv.style.textAlign = "center";
							img.title = cadeaux[kdo][4];
							
							var descTxt = document.createTextNode( cadeaux[kdo][4] );
							descDiv.appendChild(descTxt);
							curLot.appendChild(descDiv);
						}
						
						curLot.id = cadeaux[kdo][3];
						curLot.style.cursor = "pointer";
						
						curLot.onclick = function()
						{
							changeCurrentPage("./data/xml/boutique_ray.xml?cat=" + this.id);
						}
					}
					
					container.appendChild(curLot);
				}
				
				x++;
				kdo++;
			}
			y++;
			x = 0;
		}
	}
	
	this.moving = function()
	{
		if( nbPages > 4 )
		{
			if( move_x < 0 )
			{
				if( map_x < -2 * width && nbRight < nbPages  )
				{
					map_x = -width;
					var temp = divOrder[0];
					me.makePageContain( temp, nbRight );
					nbRight++;
					divOrder[0] = divOrder[1];
					divOrder[1] = divOrder[2];
					divOrder[2] = divOrder[3];
					divOrder[3] = temp;
					divOrder[0].style.left = "0px";
					divOrder[1].style.left = width + "px";
					divOrder[2].style.left = width * 2 + "px";
					divOrder[3].style.left = width * 3 + "px";
				}
			}
			else if( move_x > 0 )
			{
				if( map_x > -(2 * width - map_zone_width) && nbRight > 4 )
				{
					nbRight--;
					map_x -= width;
					var temp = divOrder[3];
					me.makePageContain( temp, nbRight - 4 );
					divOrder[3] = divOrder[2];
					divOrder[2] = divOrder[1];
					divOrder[1] = divOrder[0];
					divOrder[0] = temp;
					divOrder[0].style.left = "0px";
					divOrder[1].style.left = width + "px";
					divOrder[2].style.left = width * 2 + "px";
					divOrder[3].style.left = width * 3 + "px";
				}
			}
		}
	}
	
	this.play = function( parent )
	{	
		if( isPlaying == false )
		{
			blocMoveY = true;
			moveCB.push(this.moving);
			
			var div1 = document.createElement("div");

			div1.setAttribute("id", id + "1");

			div1.style.width = width + "px";
			div1.style.height = height + "px";
			div1.style.background = "url('" + back + "')";
			div1.style.position = "absolute";
			div1.style.backgroundRepeat = "no-repeat";
			
			divOrder.push(div1);
			
			parent.appendChild(div1);
			
			if( nbPages == 1 )
				fixPage = true;
			
			if( nbPages > 1 )
			{
				var div2 = document.createElement("div");

				div2.setAttribute("id", id + "2");

				div2.style.left = width + "px";
				div2.style.width = width + "px";
				div2.style.height = height + "px";
				div2.style.background = "url('" + back + "')";
				div2.style.position = "absolute";
				div2.style.backgroundRepeat = "no-repeat";
				
				divOrder.push(div2);

				parent.appendChild(div2);
			}
			if( nbPages > 2 )
			{
				var div3 = document.createElement("div");

				div3.setAttribute("id", id + "3");

				div3.style.left = width * 2 + "px";
				div3.style.width = width + "px";
				div3.style.height = height + "px";
				div3.style.background = "url('" + back + "')";
				div3.style.position = "absolute";
				div3.style.backgroundRepeat = "no-repeat";
				
				divOrder.push(div3);

				parent.appendChild(div3);
			}
			if( nbPages > 3 )
			{
				var div4 = document.createElement("div");

				div4.setAttribute("id", id + "4");

				div4.style.left = width * 3 + "px";
				div4.style.width = width + "px";
				div4.style.height = height + "px";
				div4.style.background = "url('" + back + "')";
				div4.style.position = "absolute";
				div4.style.backgroundRepeat = "no-repeat";
				
				divOrder.push(div4);

				parent.appendChild(div4);
			}

			var red = nbPages;
			if( red > 4 )
				red = 4;
			var p = 0;
			while( p < red )
			{
				me.makePageContain( divOrder[p], p );
				p++;
			}
			if( parent == document.getElementById('map_contained') )redimMapContained( 0, 0, width * red, height );
		}
	}
	
	this.stop = function()
	{
		if( isPlaying == true )
		{
			removeAllNodes( div1, true );
			removeAllNodes( div2, true );
			removeAllNodes( div3, true );
			isPlaying = false;
		}
	}
}