//= require jquery
//= require jquery_ujs
//= require jquery-ui





$(document).ready(function(){

	function initialize_jeu_1(){
		var element = $("#insectes_jeu_1")
	
		var html = element.find(".sortable").html();

		element.find(".sortable").sortable();
		element.find(".sortable").disableSelection();
	
		element.find(".verif").bind("click", function(){

			var win = true;
			
			var i = 0;
			
			element.find("img").each(function(){
				i = i+1;
				if(i != $(this).data("order")){
					win = false;
				}
	
				
			
			});
			if(win == true){
			element.find(".win").show();
			element.find(".loose").hide();
			}else{
			element.find(".win").hide();
			element.find(".loose").show();
			}
		});
	
	
	
		element.find(".reset").click(function(){
			element.find(".sortable").html(html);
			element.find(".sortable").sortable("refresh");
			element.find(".win").hide();
			element.find(".loose").hide();
		
		});
	
	}
	
	
	function initialize_jeu_2(){
		var element = $("#insectes_jeu_2")
	
		var html = element.find(".sortable").html();


		element.find(".receptable").sortable({
			connectWith: '.labels,.receptable',
			items: '.label',
			tolerance:"pointer",
			receive: function(event, ui) {
			            
			            if ($(this).children().length > 1) {
			              	var child = $(this).children().not(ui.item)[0]
						
						   element.find(".labels").append(child);
			 
			            }
			      }
		});
		element.find(".receptable ul").disableSelection();
		
		element.find(".labels").sortable({
			connectWith: '.receptable',
			tolerance:"pointer",
			items: '.label',
			receive: function(event, ui){ui.item.attr('style','');}
			
			
			
		});
		element.find(".labels").disableSelection();
	
		element.find(".verif").bind("click", function(){

			var win = true;
			
			var i = 0;
			
			element.find("img").each(function(){
				i = i+1;
				if(i != $(this).data("order")){
					win = false;
				}
	
				
			
			});
			if(win == true){
			element.find(".win").show();
			element.find(".loose").hide();
			}else{
			element.find(".win").hide();
			element.find(".loose").show();
			}
		});
	
	
	
		element.find(".reset").click(function(){
			element.find(".sortable").html(html);
			element.find(".sortable").sortable("refresh");
			element.find(".win").hide();
			element.find(".loose").hide();
		
		});
	
	}
	
	initialize_jeu_1();
	initialize_jeu_2();
	
	
	

});




