var $ = jQuery.noConflict();
//zone de non chevauchement en px
var width = 80;
var height = 114;
var sizeimgheight = 134;
var sizeimgwidth = 100;

$(document).ready(function(){
	var screenWidth = $("#my_canvas").width() - sizeimgwidth;
	var screenHeight = $("#my_canvas").height() - sizeimgheight;
	// table
	var table = new Array();
	var i = 0;
	//position
	var l = -1;
	var t = -1;
	
	//thumb images
	$("#my_canvas img").each(function(intIndex){	
		l = rand(screenWidth);
		t = rand(screenHeight);
		
		while(verif(table,t,l) == false){
			l = rand(screenWidth);
			t = rand(screenHeight);
		}
		table[i] = position(l,t);	
		$(this).animate({
			marginTop: t + "px",
			marginLeft: l + "px"
		}, "fast" );
		
		$(this).fullsize({
			shadow: true, 
			iconOffset: 2, 
			parentSteps: 2
		});  
		
		//incrémentation
		i += 1;
	});
	
	//Big image
	$("#my_canvas div").each(function(intIndex){
		l = rand(screenWidth);
		t = rand(screenHeight);
		
		while(verif(table,t,l) == false){
			l = rand(screenWidth);
			t = rand(screenHeight);
		}
		table[i] = position(l,t);
		
		var id = $(this);
		id.attr("id");
		
		$(id).animate({
			marginTop: t + "px",
			marginLeft: l + "px"
		}, "fast");
		
		$(id).fullsize({
			shadow: true, 
			iconOffset: 2, 
			parentSteps: 2
		}); 
		
		//incrémentation
		i += 1;
	});
	
	//animate imag
	$("#slo").oneTime(12000, function() {
		$(this).sprite({
			fps:25,
			no_of_frames: 50,
			play_frames:50
		});
	});

	$("#dlu").oneTime(2000, function() {
		$(this).sprite({
			fps:5,
			no_of_frames: 13,
			play_frames:13
		});
	});
	
	$("#oeb").oneTime(6000, function() {
		$(this).sprite({
			fps:5,
			no_of_frames:7,
			play_frames:7
		});
	});
	
	$("#ydr").oneTime(9000, function() {
		$(this).sprite({
			fps:5,
			no_of_frames:8,
			play_frames:8
		});
	});
});



function verif(table,t,l){
	var ok = true;
	if(table.length == 0){
		ok = true;
	} else{
		var tabletemp = position(l,t);
		for(var compt = 0; compt < table.length; compt++) {
		// calcul des futures emplacement et vérifier si pas de chevauchement
			if(table[compt]['x2'] > tabletemp['x1'] && table[compt]['x1'] < tabletemp['x2'] && table[compt]['y2'] > tabletemp['y1'] && table[compt]['y1'] < tabletemp['y2']){
				ok = false;
			}
		}
	}
	return ok;
}
function rand(pos){
	return Math.round(Math.random() * pos);
}

function position(l,t){
	var table = new Array(4);
	var top = t
	var left = l
	table['y1'] = top;
	table['x1'] = left;
	table['y2'] = top + height;
	table['x2'] = left + width;
	return table;
}
