 var contador;

 function initLevel () {
	levelWidth = 12700;//ancho del nivel minimo 960
	levelHeight = 8000;//alto de nivel minimo 560
	imgData = ["tariacuri.png"/*0*/, "imagen/tierra.jpg"/*1*/, "cielo.jpg"/*2*/, "imagen/suelo.png"/*3*/,"imagen/tierrahoyo.png"/*4*/, 
	"imagen/grass2.png"/*5*/, "imagen/tree.png"/*6*/, "imagen/piedra.png"/*7*/, "imagen/cesta.png"/*8*/, "imagen/sueloInvisible.png"/*9*/, 
	"imagen/arbol01.png"/*10*/, "imagen/arbol02.png"/*11*/, "imagen/arbol03.png"/*12*/, "imagen/bloquecito.png"/*13*/, "imagen/tomato.png"/*14*/, 
	"imagen/casacerrada.png"/*15*/, "imagen/tiendacerrada.png"/*16*/, "imagen/casa1.png"/*17*/, "imagen/cocina1.png"/*18*/, "imagen/cocina2.png"/*19*/, 
	"imagen/tiendacanasta1.png"/*20*/, "imagen/tiendacobre1.png"/*21*/, "imagen/tiendacomida2.png"/*22*/, "imagen/viejito.png"/*23*/, 
	"imagen/viejita.png"/*24*/, "imagen/sueloInv.png"/*25*/, "imagen/sueloInvVer.png"/*26*/, "imagen/rama.png"];
	audioData=["can"];
	var dialogo1 = ["Sesasi: O no!, se me cayo mi canasta de jitomates. La canasta esta|hecha de las mejores ramas de palma; necesito recuperarla.","Sesasi: oye niño, me ayudarias por favor. Me llamo Sesasi que significa|bonita y sí me ayudas a recuperar me canasta, te rencompensare|dandote un jitomate."]
	var dialogo2 = ["Heeka: un primate se robo mi jitomate!!!|malvadas creaturas.", "Heeka: hey tu niño, ¿ocupas un jitomate?|entonces sube la palmera y tomalo, yo ya no lo quiero"];
	var dialogo3 = ["Lunt: Lo siento chaval, ya no me quedan más jitomates|como es un lugar muy cálido, la agricultura es algo difícil de practicar", "Lunt: pero sí te urge conseguir uno, todavia hay uno en el huerto", "Lunt: Pero se me hace imposible obtenerlo porque|misteriosamente cambia de posición.", "Lunt: Sí logras capturarlo, sera todo tuyo", "Lunt: por cierto, sabías que mi nombre significa cosecha."];
	var capa0 = [[0, false], [2, 0, 0]];
	var nubes = [[0.5, false], new Clouds(0, 0, 8000, 8000)];
	var capa1 = [[1, false], [12, 8150, 3900],[12, 8150, 3200], [12, 8462, 4100], [12, 8464, 3500], [12, 8780, 3200], [12, 9093, 3075], [12, 9410, 3100], [12, 9725, 3900], [12, 9725, 3200]];//tierrahoyo
	var capa2 = [[1, true], [26, -96, 4500], [26, 5000, 4750], [26, 5795, 4750], [7, 5106, 4740], [7, 5726, 4740], [7, 5106, 5040], [7, 5306, 4890], [7, 5526,4890], [7, 5306, 5190], [7, 5526, 5190], [7, 5726, 5040], [7, 5106, 5340], [7, 5726, 5340],
	[13, 8304, 4350], [13, 8304, 3900], [13, 8627, 4515], [13, 8627, 4125], [13, 8627, 4515], [13, 8942, 3850], [13, 9257, 4530], [13, 9257, 4050], [13, 9257, 4530], [13, 9574, 3700], [13, 9574, 3950], [13, 9887, 4530], [13, 9887, 3800], [26, 12701, 4500]];//tierra
	var capa3 = [[1, false], new Canasta (5400, 5400, 0, 0), new Tomate1 (9578, 3625, 0, 0), new Tomate2 (11000, 4425, 1500, 1500)];//tari y obj
	var casa = [[1, false], new NPC (24, 5000, 4530, dialogo1), new NPC (23, 9150, 4530, dialogo2), new NPC (24, 10750, 4530, dialogo3), new Cocina (1350, 4410, 0, 0), [17, 100, 4305], [16, 570, 4405], [20, 960, 4412], [15, 1745, 4410], [21, 2155, 4410]];
	var frontrees = [[0.6, false]];
	var backtrees = [[0.8, false]];
	levelData = [capa0, nubes, frontrees, backtrees, capa1, capa2, capa3, casa];
	respawn = [[1, 500], [8000, 500]];


	levelData.init = function () {
		/*Invi1*/levelData[5] = levelData[5].concat(insertTiles(25, 0, 4740, 5, 1, 0, 0));
		/*suelo1*/levelData[6] = levelData[6].concat(insertTiles(3, 0, 4685, 23, 1, 0, 0));
		/*tierra1*/levelData[6] = levelData[6].concat(insertTiles(1, 0, 4907, 23, 4, 0, 0));
		/*arbolfro1*/levelData[2] = levelData[2].concat(insertTiles(6, 1700, 2440, 3, 1, 0, 0));
		/*arbolbac1*/levelData[3] = levelData[3].concat(insertTiles(6, 2000, 3270, 4, 1, 0, 0));
		/*pasto1*/levelData[6] = levelData[6].concat(insertTiles(9, 2550, 4679, 3, 1, -220, 0));
		/*hoyo*/levelData[4] = levelData[4].concat(insertTiles(4, 5000, 4750, 2, 2, -2, -2));
		/*suelohoyo*/levelData[6] = levelData[6].concat(insertTiles(3, 5106, 5462, 3, 2, 0, -15));
		/*Invihoyo*/levelData[5] = levelData[5].concat(insertTiles(25, 5106, 5480, 1, 1, 0, 0));
		/*Invi2*/levelData[5] = levelData[5].concat(insertTiles(25, 5772, 4740, 7, 1, 0, 0));
		/*suelo2*/levelData[6] = levelData[6].concat(insertTiles(3, 5772, 4685, 32, 1, 0, 0));
		/*tierra2*/levelData[6] = levelData[6].concat(insertTiles(1, 5772, 4907, 32, 4, 0, 0));
		/*pasto2*/levelData[6] = levelData[6].concat(insertTiles(9, 5742, 4679, 6, 1, -220, 0));
		/*arbolfro2*/levelData[2] = levelData[2].concat(insertTiles(6, 3650, 2440, 3, 1, 0, 0));
		/*arbolbac2*/levelData[3] = levelData[3].concat(insertTiles(6, 4722, 3270, 4, 1, 0, 0));
		levelData[4] = levelData[4].concat(insertTiles(11, 11000, 4442, 1, 7, 0, 0));
		levelData[4] = levelData[4].concat(insertTiles(11, 11300, 4442, 1, 7, 0, 0));
		levelData[4] = levelData[4].concat(insertTiles(11, 11600, 4442, 1, 7, 0, 0));
		levelData[4] = levelData[4].concat(insertTiles(11, 11900, 4442, 1, 7, 0, 0));
		levelData[4] = levelData[4].concat(insertTiles(11, 12200, 4442, 1, 7, 0, 0));
		levelData[4] = levelData[4].concat(insertTiles(11, 12500, 4442, 1, 7, 0, 0));
		/*cosecha*/levelData[4] = levelData[4].concat(insertTiles(27, 11000, 4425, 6, 1, -50, 0));
		

		levelData[4] = levelData[4].concat(insertTiles(10, 8300, 4650, 6, 1, 185, 0));
		levelData[4] = levelData[4].concat(insertTiles(11, 8314, 3571, 1, 27, 0, 0));
		levelData[4] = levelData[4].concat(insertTiles(11, 8629, 3891, 1, 19, 0, 0));
		levelData[4] = levelData[4].concat(insertTiles(11, 8944, 3571, 1, 27, 0, 0));
		levelData[4] = levelData[4].concat(insertTiles(11, 9259, 3451, 1, 30, 0, 0));
		levelData[4] = levelData[4].concat(insertTiles(11, 9574, 3451, 1, 30, 0, 0));
		levelData[4] = levelData[4].concat(insertTiles(11, 9889, 3451, 1, 30, 0, 0));

		player = new Player(0, [1, 1, 10, 10, 1, 1, 1, 1], 111, 160, 10, 4582);
		ivan = new Ivan (200, 4500);
		levelData[7] = levelData[7].concat(player, ivan);
		camera = new Camera();
		contador = new Counter(10, 10, 14);

	}
	levelData.logic = function () {
		if(cutscene==undefined&&fade.state=='idle')
			cutscene = new Cutscene([[0, 4300]], [["Tariácuri hemos llegado a un nuevo pueblo, vayamos a recorrerlo para |conocer más sobre él."]]);
	}

	levelData.draw = function (i, displacementRatio) {
		contador.draw();
	}

}
function Canasta (x_pos, y_pos, width, height) {
	this.x_pos = x_pos;
	this.y_pos = y_pos;
	this.width = width;
	this.height = height;
	var exists = true;
	var player_collided = false;
	var time = 0;
	this.draw = function (displacementRatio) {
		if(!exists)
			return;
		drawOnCanvas(imgData[8], x_pos-camera.x_pos*displacementRatio, y_pos-camera.y_pos*displacementRatio);
		if(player.isColliding(x_pos, y_pos, imgData[8].width, imgData[8].height)||player_collided){
			if(!player_collided)
				cutscene = new Cutscene([[5000, 5200]], [["¡Has recuperado la canasta!"]]);
			player_collided = true;;
			if(cutscene.finished){
				exists = false;
				contador.add();
			}
		} else {
			time += dt;
			if(time>=2){
				time = 0;
				x_pos = this.x_pos + width*Math.random();
			}
		}
	}
}
function Tomate1 (x_pos, y_pos, width, height) {
	this.x_pos = x_pos;
	this.y_pos = y_pos;
	this.width = width;
	this.height = height;
	var exists = true;
	var player_collided = false;
	var time = 0;
	this.draw = function (displacementRatio) {
		if(!exists)
			return;
		drawOnCanvas(imgData[14], x_pos-camera.x_pos*displacementRatio, y_pos-camera.y_pos*displacementRatio);
		if(player.isColliding(x_pos, y_pos, imgData[14].width, imgData[14].height)||player_collided){
			if(!player_collided)
				cutscene = new Cutscene([[x_pos-canvas.width/2+imgData[14].width/2, y_pos-canvas.height/2+imgData[14].height/2]], [["¡Has conseguido un jitomate!"]]);
			player_collided = true;;
			if(cutscene.finished){
				exists = false;
				contador.add();
			}
		} else {
			time += dt;
			if(time>=2){
				time = 0;
				x_pos = this.x_pos + width*Math.random();
			}
		}
	}
}

function Tomate2 (x_pos, y_pos, width, height) {
	this.x_pos = x_pos;
	this.y_pos = y_pos;
	this.width = width;
	this.height = height;
	var exists = true;
	var player_collided = false;
	var time = 0;
	this.draw = function (displacementRatio) {
		if(!exists)
			return;
		drawOnCanvas(imgData[14], x_pos-camera.x_pos*displacementRatio, y_pos-camera.y_pos*displacementRatio);
		if(player.isColliding(x_pos, y_pos, imgData[14].width, imgData[14].height)||player_collided){
			if(!player_collided)
				cutscene = new Cutscene([[x_pos-canvas.width/2+imgData[14].width/2, y_pos-canvas.height/2+imgData[14].height/2]], [["¡Has conseguido un jitomate!"]]);
			player_collided = true;;
			if(cutscene.finished){
				exists = false;
				contador.add();
			}
		} else {
			time += dt;
			if(time>=.5){
				time = 0;
				x_pos = this.x_pos + width*Math.random();
			}
		}
	}
}
function Cocina (x_pos, y_pos) {
	this.imageCode = 12;
	this.x_pos = x_pos;
	this.y_pos = y_pos;
	var talked = 0;
	this.draw = function (){
		drawOnCanvas(imgData[19], this.x_pos-camera.x_pos, this.y_pos-camera.y_pos);
		if(player.isColliding(this.x_pos, this.y_pos, 1010, 788)&&pressed_control&&!on_cutscene){
			var string = ["It’awil: Hola joven y bienvenido a Tacámbaro,|tambien conocido como lugar de palmeras", "It’awil: Mi nombre es It’awil que significa Lumbre y estoy haciendo|unas ricas corundas pero aún me falta preparar la salsa", 
			"It’awil: Necesito 3 jitomates para hacerla|y en este momento no cuanto con ninguno", "It’awil: ¿Me harías el favor de ir a buscarlos por mí?|Te regalare una corunda como agradecimiento"];
			if(contador.counter>=3)
				string = string.concat("It’awil: muchas gracias!, ya podre terminar de prepara estas corundas.", "It’awil: Espera un segundo|hmmm, lalala y listo!", "It’awil: Aquí tienes muchacho, una rica corunda");
			else
				string = string.concat("It’awil: Lo siento, pero aun no tienes los jitomates necesarios|para preparar la salsa");
			cutscene = new Cutscene([[1350, 4410]], [string]);
			talked+=1;
		}
		if(talked == 2&&contador.counter>=3) {
			ending.init("Tacámbaro");
			if(cutscene.finished){
				if(fade.state != 'fadeout'){
					fade.out();
				}
			}
		}
	}
}