var cur_page = 1;
var last_tile = null;

function set_handlers() {
	document.getElementById('min_score').onmouseover = mouseover_score;
	document.getElementById('max_score').onmouseover = mouseover_score;
	document.getElementById('min_score').onchange = change_score;
	document.getElementById('max_score').onchange = change_score;
	document.getElementById('min_votes').onchange = change_score;

	for (var obj = document.getElementById('tiles').firstChild; obj != null; obj = obj.nextSibling) {
		if (obj.tagName.toLowerCase() == 'div' || obj.tagName.toLowerCase() == 'p')
			continue;
		obj.onmouseover = mouseover_tile;
		obj.onmouseout = mouseout_tile;
		obj.onfocus = mouseover_tile;
		obj.onblur = mouseout_tile;
	}
}

function set_cur_page(p) {
	cur_page = p;
}

function mouseover_tile(e) {
	var targ;
	if (!e) var e = window.event;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	else return;
	if (targ.nodeType == 3) targ = targ.parentNode;

	if (targ.tagName.toLowerCase() == 'a')
		targ = targ.firstChild;

//	targ.parentNode.focus();

	if (last_tile != targ && last_tile != null)
		shrink_tile(last_tile);

	grow_tile(targ);
	last_tile = targ;
}

function mouseout_tile(e) {
	var targ;
	if (!e) var e = window.event;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	else return;
	if (targ.nodeType == 3) targ = targ.parentNode;

	if (targ.tagName.toLowerCase() == 'a')
		targ = targ.firstChild;

	shrink_tile(targ);
}

function grow_tile(t) {
	t.style.marginTop = '-14px';
	t.style.marginLeft = '-14px';
	t.style.borderWidth = '2px';
	t.style.padding = '6px';
}

function shrink_tile(t) {
	t.style.marginTop = '0px';
	t.style.marginLeft = '0px';
	t.style.borderWidth = '0px';
	t.style.padding = '1px';
}

function color_select(which, min_val, max_val) {
	for (var obj = which.firstChild; obj != null; obj = obj.nextSibling) {
		if (parseInt(obj.value) >= parseInt(min_val) && parseInt(obj.value) <= parseInt(max_val))
			obj.style.backgroundColor = 'rgb(252,190,78)';
		else
			obj.style.backgroundColor = 'white';
	}
}

function mouseover_score(e) {
	var targ;
	if (!e) var e = window.event;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	else return;
	if (targ.nodeType == 3) targ = targ.parentNode;

	var min_score = document.getElementById('min_score');
	var max_score = document.getElementById('max_score');

	if (targ.parentNode.id == 'min_score')
		color_select(min_score, parseInt(targ.value), parseInt(max_score.value));
	else if (targ.parentNode.id == 'max_score')
		color_select(max_score, parseInt(min_score.value), parseInt(targ.value));
}

function change_score(e) {
	var targ;
	if (!e) var e = window.event;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	else return;
	if (targ.nodeType == 3) targ = targ.parentNode;

	var min_score = document.getElementById('min_score');
	var max_score = document.getElementById('max_score');
	var min_votes = document.getElementById('min_votes');

	if (parseInt(min_score.value) > parseInt(max_score.value)) {
		if (targ.id == 'min_score')
			max_score.value = min_score.value;
		else
			min_score.value = max_score.value;
	}


	color_select(min_score, parseInt(min_score.value), parseInt(max_score.value));
	color_select(max_score, parseInt(min_score.value), parseInt(max_score.value));

	var url = WWW + '/browse/tiles';
	var regex = /\/tag\/\w+/i;
	var filter = regex.exec(location);
	if (filter)
		url += filter;
	if (min_votes.value != 1)
		url += '/' + min_score.value + '/' + max_score.value + '/' + min_votes.value + '/' + cur_page + '/';
	else if (max_score.value != min_score.value && (min_score.value != 0 || max_score.value != 10))
		url += '/' + min_score.value + '/' + max_score.value + '/' + cur_page + '/';
	else if (min_score.value != 0)
		url += '/' + min_score.value + '/' + cur_page + '/';
	else
		url += '/' + cur_page + '/';

	document.getElementById('browse').href = url;
}
