var cartes = new Array();
var couleurs = new Array("n", "r", "r", "n");
var types = new Array("tr", "ca", "co", "pi");

function cartes_init() {
	for (i=0; i<52; i++) {
		cartes[i] = new carte(i);
	}
}

function carte(n) {
	this.numero = n;
	this.valeur = n%13;
	this.type = Math.floor(n/13);
	this.couleur = couleurs[this.type];
	this.classe = this.couleur+this.valeur;
	if (this.valeur==12) this.classe += " roi";
	this.carte = types[this.type]+this.valeur;
	this.visible = true;
	this.used = false;
	this.children = new Array();
	this.display = function() {
		txt = "<div id='carte"+n+"' class='carte "+this.classe+" "+this.carte+"'>";
		if (this.visible==true) {
			txt += "<img src='cartes/"+n+".jpg' width='72px' /></div>";
		}
		else {
			txt += "<img src='cartes/endos.jpg' width='72px' /></div>";
		}
		$("body").append(txt);
		$("#carte"+n).css({
			position: "absolute",
			width: "72px",
			height: "104px",
			cursor: "pointer"
		});
	};
	this.hide = function() {
		$("#carte"+n).DraggableDestroy().remove();
	};
	this.retourne = function() {
		$("#carte"+this.numero+" img").animate({width: "0px", paddingLeft: "36px", height: "104px"}, 200, "",
			function() {  
				cartes[n].retourneCB();
			});
	};
	this.retourneCB = function() {
		if (cartes[n].visible) { cartes[n].visible=false; d = "endos"; }
		else { cartes[n].visible=true; d = this.numero; }
		$("#carte"+this.numero+" img").attr("src", "cartes/"+d+".jpg");
		$("#carte"+this.numero+" img").animate({width: "72px", paddingLeft: "0", height: "104px"}, 200);
	};
	this.flashretourne = function() {
		if (cartes[n].visible) { cartes[n].visible=false; d = "endos"; }
		else { cartes[n].visible=true; d = this.numero; }
		$("#carte"+this.numero+" img").attr("src", "cartes/"+d+".jpg");
	}
}
function cartes_nouvelle() {
	var boucle = true;
	while (boucle) {
		var rnd = parseInt(Math.random()*52);
		if (!cartes[rnd].used) {
			boucle = false;
		}
	}
	cartes[rnd].used = true;
	return rnd;
}
