function addDraggable(el)
{
	el.draggable
	(
		{
			revert: 'invalid',
			zIndex: 1000,
			helper: 'clone',
			containment: '.centrecontainer',
			cursor: 'pointer',
			opacity: 0.8,
			start: function(ev, ui)
			{
				$tips_id = "#"+$(this).attr('id')+"_tips";
				$($tips_id).hide();
			}
		}
	);
	el.mousedown(function () {

      $tips_id = "#"+$(this).attr('id')+"_tips";
		$($tips_id).hide();
    });
};


function free_equip_slot(equip_slot,handed_dragged)
{
	handed_equiped = false;
	// Si on droppe sur une main
	equip_slot_id = equip_slot.attr('id');
	if (equip_slot_id == 'rhand' || equip_slot_id == 'lhand')
	{
		// Si il y a quelque chose
		if ($(equip_slot).children().size() > 0 || handed_dragged)
		{
			// On regarde si cet item est 2 handed
			current_item = $($(equip_slot).children()[0]) ;
			handed_equiped = ('SBOWSPESTALBOW'.indexOf(current_item.attr('ctyp')) != -1);
			// Si il y a un 2handed dans l'histoire, on doit vider les 2 slots
			if (handed_equiped || handed_dragged)
			{
				if ($('#rhand').children().size() > 0)
				{
					drop_back($('#rhand').children()[0],0);
				}
				if ($('#lhand').children().size() > 0)
				{
					if (handed_equiped)
					{
						// La main gauche n'est qu'un clone => destruction
						$($('#lhand').children()[0]).remove();
					}
					else
					{
						drop_back($('#lhand').children()[0],0);
					}
				}
			}
			else
			{
				drop_back(current_item,0);
			}
		}
	}
	else if ($(equip_slot).children().size() > 0 )
	{ // Si pas sur une main, on vire l'éventuel item déja présent
		current_item = $(equip_slot).children()[0] ;
		drop_back(current_item,0);
	}
}

function drop_back(drag_item,post_ajax)
{
	ctyp = $(drag_item).attr('ctyp');
	equip_slot = $(drag_item).parent().attr('id');
	hero_Id = $('#hero_id').val();
	former_item_id = $(drag_item).attr('id');
	ctyp = (ctyp == 'SSWO' || ctyp == 'LSWO') ? 'SWO' : ctyp ;
	ctyp = (ctyp == 'SBOW' || ctyp == 'LBOW') ? 'BOW' : ctyp ;
	target_section = '#section-'+ctyp;
	$(target_section).append(drag_item);
	if (post_ajax == 1)
	{
		$.ajax({
				type: "POST",
				url: "ajax_gen.php",
				data: "a=he&hid="+hero_Id+"&es="+equip_slot+"&fei="+former_item_id,
				success: function(data, msg)
				{
					//alert('drop back :'+data);
				}
			});
		}
}


$(document).ready(
	function()
	{
		$('#tabs_container').tabs();
		$('#tabs_weapon').tabs();

		//Nos élements draggable sont définis ici. Ce sont tous les éléments avec la class CSS '.drag'.
		addDraggable($('.drag'));

		$('.equip_tab').droppable(
		{
			accept : '[eq="1"]',
			tolerance: 'pointer',
			drop: function (ev,ui)
			{
				drag = ui.draggable;
				drag.attr('eq',0);
				handed = ('SBOWSPESTALBOW'.indexOf(drag.attr('ctyp')) != -1);
				if (handed)
				{
					drop_back($('#rhand').children()[0],1);
					$($('#lhand').children()[0]).remove();
				}
				else
				{
					drop_back(drag,1)
				}
			},
			fit: true
		}
		);


		$('.equip_slot').droppable(
		{
			accept : function (drag)
			{
				// Gestion de l'acceptation du drop des item sur les slots d'équipement
				switch($(this).attr('id'))
				{
					case "head" :
						return (drag.attr('ctyp') == 'HEA');
						break;
					case "body" :
						return (drag.attr('ctyp') == 'ARM');
						break;
					case "glove" :
						return (drag.attr('ctyp') == 'GAU');
						break;
					case "rhand" :
						return ('SSWODAGSBOWWANSPESTALSWOLBOW'.indexOf(drag.attr('ctyp')) != -1);
						break;
					case "lhand" :
						return ('SBOWSPESTALBOWBUC'.indexOf(drag.attr('ctyp')) != -1);
						break;
					case "bag" :
						return (drag.attr('ctyp') == 'BAG');
						break;
					case "boots" :
						return (drag.attr('ctyp') == 'BOO');
						break;
					case "acc1" :
						return (drag.attr('ctyp') == 'ACC');
						break;
					case "acc2" :
						return (drag.attr('ctyp') == 'ACC');
						break;
					default :
						return false;
				}
			},
			activate: function (ev,drag)
			{
				$(this).css( 'background-color', '#DBFFE4' );
			},
			deactivate: function (ev,drag)
			{
				$(this).css( 'background-color', '#E2EBED' );
			},
			tolerance: 'pointer',
			drop: function (ev,ui)
			{

				// On estime que l'item peut être droppé à cet emplacement car le accept est bon ;-)
				drag = ui.draggable;
				// Do nothing if the item have been dragged back to its initial position
				if ($($(this).children()[0]).attr('id') != drag.attr('id'))
				{
					// Detection d'équipement à 2 mains
					handed = ('SBOWSPESTALBOW'.indexOf(drag.attr('ctyp')) != -1);
					hero_Id = $('#hero_id').val();

					// On retire l'item si il y en a déja un.
					if (handed)
					{
						// Si qqc à gauche, on le vire
						if ($('#lhand').children().size() > 0)
						{
								// On vire si ce n'est pas le colne
								lhandid = $($('#lhand').children()[0]).attr('id');
								if (lhandid.indexOf('clone') == -1)
								{
									former_item_id = $($('#lhand').children()[0]).attr('id');
									$.ajax({
										type: "POST",
										url: "ajax_gen.php",
										data: "a=he&hid="+hero_Id+"&es=lhand&fei="+former_item_id,
										success: function(data, msg)
										{
											//alert('handed remove left hand :'+data);
										}
									});
								}
						}
						// Si qqc à doite on remplace
						if ($('#rhand').children().size() > 0)
						{
							new_item_id = drag.attr('id');
							former_item_id = $($('#rhand').children()[0]).attr('id');
							//alert("new item id"+new_item_id);
							//alert("former item id"+former_item_id);
							$.ajax({
									type: "POST",
									url: "ajax_gen.php",
									data: "a=heee&hid="+hero_Id+"&es=rhand&nei="+new_item_id+"&fei="+former_item_id,
									success: function(data, msg)
									{
										//alert('replace handed new item on used :'+data);
									}
								});
						}
						else // Sinon, on ajoute
						{
							new_item_id = drag.attr('id');
							//alert("new item id"+new_item_id);
							$.ajax({
									type: "POST",
									url: "ajax_gen.php",
									data: "a=hee&hid="+hero_Id+"&es=rhand&nei="+new_item_id,
									success: function(data, msg)
									{
										//alert('handed new item on free slot :'+data);
									}
								});
						}
					}
					else if($(this).children().size() > 0)
					{
						// Replace the equip on a not free slot
						equip_slot = $(this).attr('id');
						//alert("equip slot"+equip_slot);
						new_item_id = drag.attr('id');
						former_item_id = $($(this).children()[0]).attr('id');
						//alert("new item id"+new_item_id);
						//alert("former item id"+former_item_id);
						// Si ce n'est pas un clone, pas de souci
						if (former_item_id.indexOf('clone') == -1)
						{
							$.ajax({
								type: "POST",
								url: "ajax_gen.php",
								data: "a=heee&hid="+hero_Id+"&es="+equip_slot+"&nei="+new_item_id+"&fei="+former_item_id,
								success: function(data, msg)
								{
									//alert('new item on used :'+data);
								}
							});
						}
						else // Si c'est un clone, c'est du 2handed équippé donc l'equipslot est la lhand 
							  //mais c'est le rhand qu'on doit dééquipper puis équiper le lhand comme sur un slot free
						{
							former_item_id = $($('#rhand').children()[0]).attr('id');
							$.ajax({
								type: "POST",
								url: "ajax_gen.php",
								data: "a=he&hid="+hero_Id+"&es=rhand&fei="+former_item_id,
								success: function(data, msg)
								{
									//alert('rhand removed because drop on lhand :'+data);
									new_item_id = drag.attr('id');
									//alert("new item id"+new_item_id);
									$.ajax({
											type: "POST",
											url: "ajax_gen.php",
											data: "a=hee&hid="+hero_Id+"&es=lhand&nei="+new_item_id,
											success: function(data, msg)
											{
												//alert('new item on free slot after removing rhand :'+data);
											}
										});
								}
							});
						}
						
							//free_equip_slot($(this),handed);
					}
					else
					{
						// Put the equip on a free slot
						equip_slot = $(this).attr('id');
						//alert("equip slot"+equip_slot);
						new_item_id = drag.attr('id');
						//alert("new item id"+new_item_id);
						$.ajax({
								type: "POST",
								url: "ajax_gen.php",
								data: "a=hee&hid="+hero_Id+"&es="+equip_slot+"&nei="+new_item_id,
								success: function(data, msg)
								{
									//alert('new item on free slot :'+data);
								}
							});
					}
					free_equip_slot($(this),handed);

					drag.attr('eq',1);
					if (handed)
					{
						drag_clone = drag.clone();
						drag_clone_id = drag_clone.attr('id')+'-clone';
						drag_clone.attr('id',drag_clone_id);
						$('#lhand').append(drag_clone);
						$('#rhand').append(drag);
						addDraggable(drag_clone);
					}
					else
					{
						$(this).append(drag);
					}
				}
			},
			fit: true
			}
		);
	}
);
