/*  =================================================

	Configuration du puzzle ADiFCO 

	Le script original vient de

	"Puzzle Demo, using jQuery and Interfaces"    

	http://mrnase.de/jquerypuzzle/

===================================================== */   



	var puzzle_moves = 0;  // do not change...
	var puzzle_id          = 'puzzle'; // the ID of the layer where the puzzle is added to
	var puzzle_pieces      = 6; // how many pieces does the puzzle have?
	var puzzle_imagefolder = 'http://adifco.com/images/puzzle'; // where are the images stored?
	var puzzle_imageprefix = 'equipe'; // it will generate a full path: img/lilies_1.jpg
	var puzzle_imageclass  = 'sortableitem'; // leave it the default value or change it later in the code.
	var puzzle_imageid     = 'bild'; // generates: id="bild1" or id="bild2"
	
	function shuffle(a,b){
		return (Math.random() < 0.5) ? -1 : 1;
	}
	function generatePuzzle(){
		var puzzle_code = [];
		for (i=1;i<=puzzle_pieces;i++)
		{
			puzzle_code[i] = '<img src="'+puzzle_imagefolder+'/'+puzzle_imageprefix+'_'+i+'.jpg" class="'+puzzle_imageclass+'" id="'+puzzle_imageid+i+'" />';	
		}
		$("div#"+puzzle_id).append(puzzle_code.sort(shuffle).join(''));
	}
	$(document).ready(
		function () {
			generatePuzzle(); // calls the images needed for the puzzle

			$('div#'+puzzle_id).Sortable(
				{
					accept : 		'sortableitem',
					helperclass : 	'sorthelper',
					activeclass : 	'sortableactive',
					hoverclass : 	'sortablehover',
					opacity: 		0.5,
					/*fx:				200,*/
					revert:			true,
					floats:			true,
					tolerance:		'pointer',
					onStop : function() {
						puzzle_moves++;
						var order = $.SortSerialize(puzzle_id).hash;
						var moves_html = 'Mouvements... : '+puzzle_moves;
						var correctorder_code = [];
						for (i=1;i<=puzzle_pieces;i++)
						{
							correctorder_code[i] = puzzle_id+'[]='+puzzle_imageid+i;
						}
					    var correctorder = correctorder_code.join('&').substr(1);
						if(order == correctorder) {
							var solved_html = ".<br />Vous avez fini en <i>&nbsp;" + puzzle_moves + "&nbsp;</i> mouvements!";
						}
						else {
							var solved_html = '';
						}
						$('div#note').html(moves_html+solved_html);
					}
				}
			)
		}

	);
 
