function str_replace (search, replace, subject, count) {
var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0,
f = [].concat(search),
r = [].concat(replace),
s = subject,
ra = r instanceof Array, sa = s instanceof Array;
s = [].concat(s);
if (count) {
this.window[count] = 0;
}

for (i=0, sl=s.length; i < sl; i++) {
if (s[i] === '') {
continue;
}
for (j=0, fl=f.length; j < fl; j++) {
temp = s[i]+'';
repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
s[i] = (temp).split(f[j]).join(repl);
if (count && s[i] !== temp) {
this.window[count] += (temp.length-s[i].length)/f[j].length;
}
}
}
return sa ? s : s[0];
}

$(document).ready(function(){
if (navigator && navigator.userAgent && navigator.userAgent.indexOf('googlebot') !== -1) {
	document.cookie='iAmAdult=1;path=/;expires=Tue, 06 Dec 2022 23:59:59 GMT';
}
if (document.cookie.indexOf('iAmAdult') === -1) {
	// check adult
	$x = jQuery('<div class="checkAdult"><div class="adultInner"><h2>Upozornenie!</h2><p>Obsah tejto stránky je tak odporný, že by ho nemal nikto vidieť!</p><p>Ak <a id="confirm18" href="javascript:void(0)">máš 18 rokov alebo viac</a>, je to tvoja vec. Inak choď <a href="http://www.google.sk/">preč</a>.</p></div></div>');
	$x.css('height', $(document).height());
	$x.find('#confirm18').click(function(){
		document.cookie='iAmAdult=1;path=/;expires=Tue, 06 Dec 2022 23:59:59 GMT';
		$x.remove();
	});
	jQuery('body').prepend($x);
	
}

$(".vote-item").bind("click",function(e){
currentAnchor = $(this);
$.get(
str_replace('gallery','ajax',currentAnchor.attr('href'),1),
function(data){
hodnotenie = parseFloat(data);
var txt;
if(isNaN(hodnotenie)){
txt="neoznámkované";
} else {
txt="Hodnotenie: "+parseFloat(data)
}
$('span',currentAnchor.parent().parent()).text(txt);
});
return false;
});	
});

/////////// tetris

function cShape(grid){
	this.moves_count = null
	this.grid_x = 0
	this.grid_y = 0
	this.rotation = 0
	this.type = 0
	this.current_piece = new Array()
	this.grid = false
	this.possible_pieces = [
	    //T
		[
			[[0,1,0],[1,1,0],[0,1,0]],
			[[1,1,1],[0,1,0],[0,0,0]],
			[[0,1,0],[0,1,1],[0,1,0]],
			[[0,0,0],[0,1,0],[1,1,1]],
		],
		//I
		[
			[[0,1,0,0],[0,1,0,0],[0,1,0,0],[0,1,0,0]],
			[[0,0,0,0],[1,1,1,1],[0,0,0,0],[0,0,0,0]],
			[[0,1,0,0],[0,1,0,0],[0,1,0,0],[0,1,0,0]],
			[[0,0,0,0],[1,1,1,1],[0,0,0,0],[0,0,0,0]]
		],
		//Small Z
		[
			[[1,0,0],[1,1,0],[0,1,0]],
			[[0,1,1],[1,1,0],[0,0,0]],
			[[1,0,0],[1,1,0],[0,1,0]],
			[[0,1,1],[1,1,0],[0,0,0]]
		],
		// Small S
		[
			[[0,1,0],[1,1,0],[1,0,0]],
			[[1,1,0],[0,1,1],[0,0,0]],
			[[0,1,0],[1,1,0],[1,0,0]],
			[[1,1,0],[0,1,1],[0,0,0]]
		],
		// L mirrored
		[
			[[1,1,0],[0,1,0],[0,1,0]],
			[[0,0,0],[1,1,1],[1,0,0]],
			[[1,0,0],[1,0,0],[1,1,0]],
			[[0,0,0],[0,0,1],[1,1,1]]
		],
		// L
		[
			[[0,1,0],[0,1,0],[1,1,0]],
			[[1,1,1],[0,0,1],[0,0,0]],
			[[1,1,0],[1,0,0],[1,0,0]],
			[[1,0,0],[1,1,1],[0,0,0]]
		],
		// o
		[
			[[1,1],[1,1]],
			[[1,1],[1,1]],
			[[1,1],[1,1]],
			[[1,1],[1,1]],
		]
	]

	this._init = function(grid) {
		this.grid = grid
		this.reset_position()
	}

	this.draw = function() {
		this.reset_draw()
		for (x=0;x<this.current_piece.length;x++) {
			for (y=0;y<this.current_piece[x].length;y++) {
				if (this.current_piece[x][y]) {
					this.grid.array[x+this.grid_x][y+this.grid_y].set_shape_item()
				}
			}
		}
	}

	this.reset_draw = function() {
		for (x=0;x<this.grid.x_count;x++) {
			for (y=0;y<this.grid.y_count;y++) {
				this.grid.array[x][y].unset_shape_item()
			}
		}
	}

	this.check_collision = function(x,y,rotation) {
		piece = this.possible_pieces[this.type][rotation]
		for (ix=0;ix<piece.length;ix++) {
			for (iy=0;iy<piece[ix].length;iy++) {
				if (piece[ix][iy]) {
					nx = x+ix
					ny = y+iy
					if (this.check_point_collision(nx,ny)) {
						return 1
					}
				}
			}
		}
		return 0
	}

	this.check_point_collision = function(x,y) {
		if (x>=this.grid.x_count || x<0) {
			return 1
		}
		if (y>=this.grid.y_count || y<0) {
			return 1
		}
		if (this.grid.array[x][y].is_active()){
			return 1
		}
		return 0
	}

	this.reset_position = function() {
		if (this.moves_count === 0) {
			this.moves_count = null
			ctrl.game_over()
		} else {
			this.moves_count = 0
		}
		id_current = Math.floor(Math.random()*this.possible_pieces.length)
		if (typeof ctrl.next_piece.get_piece_id() == 'number') {
			id_current = ctrl.next_piece.get_piece_id()
		}
		id_next = Math.floor(Math.random()*this.possible_pieces.length)
		ctrl.next_piece.update(this.possible_pieces[id_next][0],id_next)
		this.type = id_current
		this.grid_x = parseInt(this.grid.x_count/2)
		this.grid_y = 0
		this.rotation = 0
		this.current_piece = this.possible_pieces[this.type][this.rotation]
		this.draw()
	}

	this.write_to_grid = function() {
		for (x=0;x<this.current_piece.length;x++) {
			for (y=0;y<this.current_piece[x].length;y++) {
				if (this.current_piece[x][y]) {
					this.grid.array[this.grid_x+x][this.grid_y+y].set_active()
				}
			}
		}
	}

	this.move = function() {
		try_new_x = this.grid_x
		try_new_y = this.grid_y+1
		if (!this.check_collision(try_new_x,try_new_y,this.rotation)) {
			this.grid_x = try_new_x
			this.grid_y = try_new_y
			this.moves_count += 1
			this.draw()
			return true
		} else {
			this.write_to_grid()
			this.grid.check_full_lines()
			this.reset_position()
			this.draw()
			return false
		}
	}

	this.drop = function() {
		while(this.move()) {
			
		}
	}

	this.move_right = function() {
		if (!this.check_collision(this.grid_x+1, this.grid_y, this.rotation)) {
			this.grid_x += 1
		}
		this.draw()
	}

	this.move_left = function() {
		if (!this.check_collision(this.grid_x-1, this.grid_y, this.rotation)) {
			this.grid_x -= 1
		}
		this.draw()
	}

	this.rotate = function() {
		new_rotation = this.rotation + 1
		if(new_rotation>3){
			new_rotation=0
		}
		if (!this.check_collision(this.grid_x, this.grid_y, new_rotation)){
			this.rotation=new_rotation
			this.current_piece = this.possible_pieces[this.type][this.rotation]
		}
		this.draw()
	}
	// this._init(grid)
}

function cGrid_item(){
	this.active = 0
	this.shape = 0
	this.td = false

	this.set_active = function(){
		this.active=1
		this.redraw()
	}
	this.set_inactive = function(){
		this.active=0
		this.redraw()
	}
	this.is_active = function() {
		return this.active
	}
	this.is_shape = function() {
		return this.shape
	}
	this.set_shape_item = function() {
		this.shape = 1
		this.redraw()
	}
	this.unset_shape_item = function() {
		if (this.shape) {
			this.shape = 0
			this.redraw()
		}
	}
	this.set_td = function(tdd) {
		this.td = tdd
	}

	this.redraw = function() {
		if (this.is_shape()) {
			this.td.className = 'shape_item'
			this.td.innerHTML = 'o'
		} else if (this.is_active()) {
			this.td.className = 'active'
			this.td.innerHTML = 'o'
		} else {
			this.td.className = 'inactive'
			this.td.innerHTML = '&nbsp;'
		}
	}
}

function cGrid(){
	this.table = false
	this.x_count = 13
	this.y_count = 23
	this.array = new Array()

	this._init = function(){
		this.table=document.getElementById('grid')
		this.table.innerHTML=""
		for (y=0;y<this.y_count;y++){
			new_tr = document.createElement('tr');
			this.table.appendChild(new_tr)
			for (x=0;x<this.x_count;x++){
				if(typeof this.array[x] == 'undefined'){
					this.array[x] = new Array()
				}
				this.array[x][y] = new cGrid_item()
				new_td = document.createElement('td')
				new_td.innerHTML='&nbsp;'
				new_tr.appendChild(new_td)
				this.array[x][y].set_td(new_td)
			}
		}
	}

	this.check_full_lines = function() {
		full_lines_list = new Array()
		for (y=0;y<this.y_count;y++) {
			if (this.check_if_full_line(y)) {
				full_lines_list.push(y)
			}
		}
		if (full_lines_list.length) {
			ctrl.score_event('drop_lines',full_lines_list.length)
		}
		for (y in full_lines_list) {
			this.drop_line(full_lines_list[y])
		}
	}

	this.check_if_full_line = function(y) {
		for (x=0;x<this.x_count;x++) {
			if (!this.array[x][y].is_active()) {
				return 0
			}
		}
		return 1
	}

	this.drop_line = function(y) {
		for (iy=y;iy>0;iy--) {
			for (x=0;x<this.x_count;x++) {
				if (this.array[x][iy-1].is_active()) {
					this.array[x][iy].set_active()
				} else {
					this.array[x][iy].set_inactive()
				}
			}
		}
	}

	this.reset = function() {
		this._init()
	}

	this._init()
}

function next_piece() {
	
	this.table = document.getElementById('next_piece')
	this.piece = []
	this.piece_id = null
	this.td_array = []

	this._init = function() {
		this.td_array = [[],[],[],[]]
		for (x=0; x<4;x++) {
			tr = document.createElement('tr')
			for (y=0; y<4; y++) {
				td = document.createElement('td')
				td.innerHTML = x+'&nbsp;'+y
				tr.appendChild(td)
				this.td_array[y][x] = td
			}
			this.table.appendChild(tr)
		}
		this.show()
	}
	
	this.show = function() {
		this.reset()
		if (typeof this.piece_id != 'null') {
			for (x in this.piece) {
				for (y in this.piece) {
					if (this.piece[x][y]) {
						td_element = this.td_array[x][y]
						td_element.className = 'active'
					}
				}
			}
		}
	}

	this.reset = function() {
		for (x=0; x<4;x++) {
			for (y=0; y<4; y++) {
				this.td_array[x][y].className = 'inactive'
			}
		}
	}

	this.update = function(piece,piece_id) {
		this.piece = piece
		this.piece_id = piece_id
		this.show()
	}
	
	this.get_piece_id = function() {
		return this.piece_id
	}

	this._init()
}

function controller() {
	this.level = 1
	this.score = 0
	this.delay = 400
	this.interval = null
	this.grid = new cGrid()
	this.shape = new cShape(this.grid)
	this.next_piece = new next_piece()

	this._init = function() {
		// this.start_interval()
		document.onkeyup = this.keyBoardEvent
	}

	this.new_game = function() {
		ctrl.message('game_over','')
		ctrl.message('pause','')
		ctrl.level = 1
		ctrl.score = 0
		ctrl.delay = 400
		ctrl.stop_interval()
		ctrl.start_interval()
		ctrl.grid.reset()
		ctrl.shape.moves_count = null
		ctrl.shape.reset_position()
	}

	this.stop_interval = function() {
		if (this.interval) {
			clearInterval(this.interval)
			ctrl.interval = null
		}
	}

	this.start_interval = function() {
		ctrl.interval = setInterval('ctrl.shape.move()', this.delay)
	}

	this.toggle_pause = function() {
		if (ctrl.is_paused()) {
			ctrl.start_interval()
			ctrl.message('pause','')
		} else {
			ctrl.stop_interval()
			ctrl.message('pause','<blink>pause</blink><br/>press P for continue')
		}
	}

	this.is_paused = function() {
		return !ctrl.interval
	}

	this.keyBoardEvent = function(event) {
		code = event.charCode? event.charCode : event.keyCode
		switch(code) {
			case 37:
				'left'
				if (!ctrl.is_paused())
					ctrl.shape.move_left()
				break
			case 39:
				'right'
				if (!ctrl.is_paused())
					ctrl.shape.move_right()
				break
			case 38:
				'up'
				if (!ctrl.is_paused())
					ctrl.shape.rotate()
				break
			case 40:
				'down'
				if (!ctrl.is_paused())
					ctrl.shape.move()
				break
			case 32:
				'space'
				if (!ctrl.is_paused())
					ctrl.shape.drop()
				break
			case 80:
				'P'
				ctrl.toggle_pause()
				break
			case 78:
				'N'
				ctrl.new_game()
			}
	}

	this.score_event = function(type, number) {
		if (type=='drop_lines') {
			ctrl.score += number*number
		}
		ctrl.message('score', ctrl.score)
		new_level = parseInt(ctrl.score/30)+1
		if (new_level>ctrl.level) {
			ctrl.increase_level()
		}
	}

	this.increase_level = function() {
		ctrl.level += 1
		ctrl.delay -= 30
		ctrl.stop_interval()
		ctrl.start_interval()
		ctrl.message('level',this.level)
	}

	this.game_over = function() {
		ctrl.message('game_over','game over<br/>Press N for new game')
		ctrl.stop_interval()
	}

	this.message = function(where, text) {
		document.getElementById(where+'_field').innerHTML = text
	}
	
	this._init()
}


