
var kaca = {
	num: 4,
	telo: [],
	hranaCon: [],
	smer: 2,
	height: 30,
	width: 30,
	initialize: function(num, cas) {
		kaca.num = num;
		kaca.defineViewport();
		for (i =0;i<kaca.num;i++) {
			kva = new kvadrat(2, i*kaca.width, 0);
			kaca.telo.push(kva);
		}
		

		kaca.interval = window.setInterval(function() { kaca.premakni() }, cas);
		kaca.hrana();
	},
	premakni: function() {
		smer = kaca.smer
		//alert(this.telo)
		for (i=kaca.telo.length-1;i>=0;i--) {
			buf = kaca.telo[i].smer;
			kaca.telo[i].premik(smer);
			smer = buf;
			if (i == kaca.telo.length-1) {
				if (!kaca.preveri(kaca.telo[i].left,kaca.telo[i].top)) break;
			}
			
			kaca.telo[i].div.style.left = kaca.telo[i].left;
			kaca.telo[i].div.style.top = kaca.telo[i].top;
		}
	},
	preveri: function(levo, gor) {
		//robovi
		if (levo > kaca.widthPort) {
			kaca.konec();
			return false;
		}
		if (gor > kaca.heightPort) {
			kaca.konec();
			return false;
		}
		if (levo < 0) {
			kaca.konec();
			return false;
		}
		if (gor < 0) {
			kaca.konec();
			return false;
		}
		//vase
		for (n=kaca.telo.length-2;n>=0;n--) {
			if (levo == kaca.telo[n].left && gor == kaca.telo[n].top){
				kaca.konec();
				return false;
			}
		}
		return true;
		
	},
	konec: function() {
		clearInterval(kaca.interval);
		alert("game over");
	},
	defineViewport: function() {
		var myWidth = 0, myHeight = 0;
		if( typeof( window.innerWidth ) == 'number' ) {
			//Non-IE
			myWidth = window.innerWidth;
			myHeight = window.innerHeight;
		} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			myWidth = document.documentElement.clientWidth;
			myHeight = document.documentElement.clientHeight;
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
			myWidth = document.body.clientWidth;
			myHeight = document.body.clientHeight;
		}
		kaca.widthPort = myWidth-kaca.width;
		kaca.heightPort = myHeight-kaca.height;
	},
	hrana: function() {
		ver = kaca.heightPort/kaca.height;
		hor = kaca.widthPort/kaca.width;
		randomX = Math.floor((hor+1)*Math.random());
		randomY = Math.floor((ver+1)*Math.random());
		console.log(hor)
		console.log(randomY*kaca.height)
		div = document.createElement('div');
		div.style.height = kaca.height +"px";
		div.style.width = kaca.width +"px";
		div.className = 'kvadrat';
		kaca.hranaCon.push(div);
		$('zaslon').appendChild(div);
		
		div.style.left = randomX*kaca.width;
		div.style.top = randomY*kaca.height;
	}
}
var kvadrat = Class.create(kaca,{
	initialize: function(smer, zamikX, zamikY) {
		
		div = document.createElement('div');
		div.style.height = kaca.height +"px";
		div.style.width = kaca.width +"px";
		div.className = 'kvadrat';
		
		this.smer = smer;
		this.left = zamikX;
		this.top = zamikY;
		this.div = div;
		$('zaslon').appendChild(this.div);
	},
	premik: function(smer) {
		this.smer = smer;
		if (smer == 1) {
			this.top -= kaca.height;
		}
		if (smer == 2) {
			this.left += kaca.width;
		}
		if (smer == 3) {
			this.top += kaca.height;
		}
		if (smer == 4) {
			this.left -= kaca.width;
		}
	}
});
var obj = true;
window.onload = function() {
	kaca.initialize(10,100);
	//alert("aqa")
	/*$('gumb').onclick = function() {
		kvadrati.each(function(o) { o.pause(); })
	}*/
};
document.onkeydown =  function(e) {
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.keyCode;
	var object = Event.element(e);
	if (keycode == 37) {
		//levo ali 4
		if (kaca.smer != 2) kaca.smer =4;
	}
	if (keycode == 39) {
		//desno ali 2
		if (kaca.smer != 4) kaca.smer=2;
	}
	if (keycode == 40) {
		//dol ali 3
		if (kaca.smer != 1) kaca.smer=3;
	}
	if (keycode == 38) {
		//gor ali 1
		if (kaca.smer != 3) kaca.smer=1;
	}
}
