// Javascript Pexeso [beta 0.2] by Krencl

var pexeso = {

	size_x	: 0,
	size_y	: 0,
	count	: 0,
	fields	: new Array(),
	images	: new Array(),
	img_source	: "",
	img_mask	: "",
	img_count	: 0,
	img_back	: "",
	img_reverse	: "",
	img_bg	: "",
	img_win	: "",
	field	: document.getElementById("pex_main"),
	
	turned	: new Array(),
	found	: new Array(),
	game_status	: 0,
	stat_clicks	: 0,
	stat_stime	: "",
	time_timer	: "",
	
	//
	// inicializace
	//
	
	initialize	: function() {
	
		
		// upozornime na zakladni chyby
		this.count = this.size_x * this.size_y;
		if (this.count%2!=0) {
			window.alert("Pocet poli musi byt sudy.");
			return "";
		}
		if ((this.count/2)>this.img_count) {
			window.alert("Pocet obrazku musi byt vetsi nez pocet poli.");
			return "";
		}		
		
		this.resetGame();
		
	},
	
	drawImages	: function() {

		// vytvorime pole s cisly podle poctu obrazku
		var array_tmp = new Array();
		var array_img = new Array();
		for (i=1;i<=this.img_count;i++) {
			array_tmp[i] = i;
		}
		// mixer
		array_tmp.sort(function() {return 0.5 - Math.random()});
		// vybereme nahodne usporadane cisla, pridame je do pole 2x
		for (i=0;i<(this.count/2);i++) {
			array_img[i] = array_tmp[i];
			array_img[i+this.count/2] = array_tmp[i];
		}
		// opet zamichame
		array_img.sort(function() {return 0.5 - Math.random()});
		// ulozime do pole cisla + obrazky
		for (i=0;i<this.count;i++) {
			this.fields[i] = array_img[i];
			this.images[i] = this.img_source + this.img_mask.replace(/##/,array_img[i]);
		}

		this.img_back = this.img_source + this.img_reverse;

	},	

	createField	: function() {

		// vytvorime hraci plochu o velikosti size_x * size_y
		var c = 0;
		for (y=1;y<=this.size_y;y++) {
			for (x=1;x<=this.size_x;x++) {
				var new_field = '<div id="pex_f'+c+'" onclick="pexeso.click('+c+');" class="pex_field" style="background-image:url(\''+this.img_back+'\');"></div>';
				this.field.innerHTML = this.field.innerHTML + new_field; 
				c++;			
			}
			this.field.innerHTML = this.field.innerHTML + '<div class="pex_clear"></div>'; 
		}

	},

	
	//
	// funkce pri hrani
	//
	
	click	: function(id) {

		// je hra spustena?
		if (this.game_status == 0) {
			this.startGame();
		}		

		// klik na otevrenou kartu
		for (i=0;i<this.turned.length;i++) {
			if (id==this.turned[i]) {
				if (this.turned.length==2) {
					this.closeOpened();
				}
				return "";
			}
		}
		// nebo jiz sebranou kartu
		for (i=0;i<this.found.length;i++) {
			if (id==this.found[i]) {
				return "";
			}
		}

		this.stat_clicks = this.stat_clicks + 1;

		// otevirame druhou kartu
		if (this.turned.length==1) {
			
			// uhodli jsme par
			if (this.fields[this.turned[0]]==this.fields[id]) {

				this.found[this.found.length] = id;
				this.found[this.found.length] = this.turned[0];

				var arr = document.getElementById("pex_f"+id);				
				arr.style.visibility="hidden";
				
				var arr = document.getElementById("pex_f"+this.turned[0]);				
				arr.style.visibility="hidden";
				
				if (this.found.length==this.count) {
					this.endGame();
				}
				
			}
			
		}
		
		// zavreme, mame-li dve otevreny
		if (this.turned.length==2) {
			this.closeOpened();
		}
		
		// ulozime, kterou sme to otocili
		this.turned[this.turned.length] = id;

		// a jeste by sme ji mohli ukazat
		var arr = document.getElementById("pex_f"+id);
		arr.style.backgroundImage="url('"+this.images[id]+"')";
		
		// obnovime informace
		this.refreshInfo();
		
	},
	
	// zavreni otevrenych karet
	closeOpened	: function() {

		for (i=0;i<this.turned.length;i++) {
			var arr = document.getElementById("pex_f"+this.turned[i]);
			arr.style.backgroundImage="url('"+this.img_back+"')";
		}
		this.turned = Array();
			
	},
	
	// obnoveni zobrazeni udaju
	refreshInfo	: function() {
		// cas
		if (this.game_status!=0) {
			var time_now = new Date();
			var time_sec = Math.floor(Date.parse(time_now)/1000) - Math.floor(Date.parse(this.stat_stime)/1000);
			document.getElementById("pex_panel_time").innerHTML =  Math.floor(time_sec/60) + ":" + ((time_sec%60<10)?("0"):("")) + (time_sec%60);
		} else {
			document.getElementById("pex_panel_time").innerHTML =  "0:00";
		}
		// kliknuti
		document.getElementById("pex_panel_click").innerHTML = this.stat_clicks;
		// zbyvajici
		document.getElementById("pex_panel_left").innerHTML = (this.count - this.found.length) / 2;
		
		// obnovujeme po sekunde
		if (this.game_status==1) {
			this.time_timer = setTimeout('pexeso.refreshInfo()',1000);
		}
	},
	
	// start hry	
	startGame	: function() {
		if (this.game_status==0) {
			this.game_status = 1;
			document.getElementById("pex_button_start").disabled = true;
			document.getElementById("pex_button_reset").disabled = false;
			this.stat_stime = new Date();
			this.refreshInfo();
		}
	},

	// konec hry - vitezstvi	
	endGame	: function() {
		this.game_status = 2;
		document.getElementById("pex_main").style.backgroundImage = "url('"+this.img_source+this.img_win+"')";
		document.getElementById("pex_button_start").disabled = true;
		document.getElementById("pex_button_reset").disabled = false;
		clearTimeout(this.time_timer);
	},
	
	// reset hry	
	resetGame	: function() {

			clearTimeout(this.time_timer);

			this.fields = Array(),
			this.images	= Array(),
	
			this.turned	= Array(),
			this.found	= Array(),
			this.game_status	= 0,
			this.stat_clicks	= 0,
			this.stat_stime	= "",
			this.time_timer	= "",			

			this.field.innerHTML = "";
			this.drawImages();
			this.createField();

			this.refreshInfo();

			document.getElementById("pex_main").style.backgroundImage = "url('"+this.img_source+this.img_bg+"')";
			document.getElementById("pex_button_start").disabled = false;
			document.getElementById("pex_button_reset").disabled = true;


	}

}
