/* Вспомогательные функции
--------------------------------------------------------------------------------------------------*/

//
// Обертка вокруг getElementById.
// Возвращает узел с заданным ID.
//

function $(id) {
	var obj;
	try	  {obj = document.getElementById(id);}
	catch(e) {obj = null;}
	return obj;
}


//
// Обертка вокруг getElementsByTagName.
// Возвращает либо список узлов, либо один узел (если в переменной num задан его номер в списке).
// Параметры parent и num - необязательные.
//

function $$(tag, parent, num) {
	var objs, obj;

	if (parent == null) parent = document;

	try	  {objs = parent.getElementsByTagName(tag);}
	catch(e) {objs = null;}

	if (num != null && objs != null) {
		obj = objs.item(num);
		return obj;
	}
	else return objs;
}


//
// Создает новый элемент
//

function createEl(name, parent, text) {
	var el = document.createElement(name);
	if (text != null) {
		var text_el = document.createTextNode(text);
		el.appendChild(text_el);
	}
	if (parent != null) parent.appendChild(el);

	return el;
}


function addImg() {
	var i = 0;
	while (img = arguments[i++]) {
		var obj = new Image();
		obj.src = img;
	}
}


/* Табы
--------------------------------------------------------------------------------------------------*/

var but_players, but_rules, el_players, el_rules, cur_player;

function playersInfo() {
	var players_list = $('players_list');
	var players_details = $('players_details');
	var but_back = $('back');

	var players_list_names = $$('span', players_list);
	var i = 0;
	while (player_name = players_list_names.item(i++)) {
		player_name.onclick = function() {
			var player_info = $(this.id + '_info');
			if (player_info != null) {
				players_list.className = 'hidden';
				if (cur_player) cur_player.className = 'hidden';
				player_info.className = '';

				cur_player = player_info;
				players_details.className = '';

				but_back.className = '';
			}
		}
	}

	but_back.onclick = function() {
		players_details.className = 'hidden';
		cur_player.className = 'hidden';
		cur_player = null;
		this.className = 'hidden';

		players_list.className = '';
	}
}


/* Форма
--------------------------------------------------------------------------------------------------*/

var form;

function Field(name, hint) {
	this.name = name;
	this.hint = hint;
	this.el = $(this.name);

	if (this.el != null) {
		this.el.obj = this;

		this.el.onfocus = this.hideHint;
		this.el.onblur = this.showHint;
		this.el.onblur();
	}
}

Field.prototype.showHint = function() {
	if (!this.obj.hintShowing && this.value == '') {
		this.value = this.obj.hint;
		this.obj.hintShowing = true;
	}
}

Field.prototype.hideHint = function() {
	if (this.obj.hintShowing) {
		this.value = '';
		this.style.color = '';
		this.obj.hintShowing = false;
	}
}

function hintsInit() {
	form = $('req');

	if (form != null) {
		var fields = new Array();
		fields.push(
			new Field('name', hint_name),
			new Field('email', hint_email),
			new Field('msg', hint_msg)
		);

		form.onsubmit = function() {
			var i = 0;
			while (field = fields[i++]) {
				if (field.hintShowing) field.el.value = '';
			}
		}
	}

}


/* Инициализация
--------------------------------------------------------------------------------------------------*/

function start() {
	but_players = $('t_players');
	but_rules   = $('t_rules');
	el_players  = $('players');
	el_rules	= $('rules');

	if (but_players != null && but_rules != null && el_players != null && el_rules != null) {
		but_players.onclick = function() {
			el_rules.className   = 'hidden';
			el_players.className = '';
			but_rules.className = 'off';
			but_players.className = 'on';
		}
		but_rules.onclick = function() {
			el_players.className   = 'hidden';
			el_rules.className = '';
			but_players.className = 'off';
			but_rules.className = 'on';
		}
	}

	playersInfo();
	hintsInit();

	addImg(
		'/i/bg_players_off.ru.jpg',
		'/i/bg_players_off.en.gif',
		'/i/bg_rules_on.ru.jpg',
		'/i/bg_rules_on.en.gif',
		'/i/bg_rules_m.jpg',
		'/i/bg_rules_t.jpg',
		'/i/bg_rules_b.jpg',
		'/i/but_back.gif'
	);
}
