// JavaScript Document
var doc = document;

function _geid( id ) {
	return doc.getElementById(id);
}

function el_to_vars(arr) {
	for (var k in arr) {
		eval("var " + arr[k] + " = _geid('" + arr[k] + "');")
	}
}

function _ce( type ) {
	var el = doc.createElement(type);
	return el;
}

function _ac(el, obj) {
	(obj || doc.body).appendChild(el);
}

function _ib(el, child, obj) {
	if (child) {
	  (obj || doc.body).insertBefore(el, child);
	}
	else {
		(obj || doc.body).insertBefore(el);
	}
}

function _insd(offset, text ,obj) {
	(obj || doc.body).insertData(offset, text);
}

function _ce_ac(type, obj) {
	var el = _ce(type);
	_ac(el, obj);
	return el;
}

function _ce_ib(type, child, obj) {
	var el = _ce(type);
	_ib(el, child, obj);
	return el;
}

function _ce_ac_id(type, id, obj) {
	var el = _ce(type);
	_ac(el, obj);
	el.id = id;
	return el;
}

function _ctn(text) {
	var tn = doc.createTextNode(text);
	return tn;
}

function _ctn_ac(text, obj) {
	var tn = _ctn(text);
	_ac(tn, obj);
	return tn;
}

function _ctn_ib(text, child, obj) {
	var tn = _ctn(text);
	_ib(tn, child, obj);
	return tn;
}

function _img(src, id, obj) {
	var img = _ce('img');
	_ac(img, obj);
	img.id = id;
	img.src = src;
	return img;
}

function _button(value, obj, name) {
	var button = _ce('button');
	_ac(button, obj);
	button.name = (name || '');
	button.value = value;
	button.id = (name || '');
	return button;
}

function _textfield(name, obj, val) {
	var t = _ce('<input name="' + name + '" type="text" value="'+(val || '')+'">');
	_ac(t, obj);
	return t;
}

function _select(opts, name, obj, multiple) {
	var s = _ce('<select name="' + name + '"></select>');
	var o;
	for (var k in opts) {
		o = _ce('<option value="' + k + '" ' + multiple?"multiple":"" + '></option>');
		_ctn_ac(opts[k], o);
		_ac(o, s);
	}
	_ac(s, obj);
	return s;
}


function _radio(name, obj, val) {
	var r = _ce('<input name="' + name + '" type="radio" value="'+val+'">');
	//r.type = 'radio';
	_ac(r, obj);
	//r.name = name;
	return r;
}

function _getradio( name ) {
	var c = doc.getElementsByName( name );
	for (i=0; i<c.length; i++) if (c(i).checked ) return c(i).value;
	return false;
}

function _get_select_value( obj ) {
	return obj.value;
}

function _pers(name, id, level, obj) {
	var pers = _ce_ac('span', obj);
	pers.to_chat = _ce_ac('span', pers); //  Заменить на
	pers.to_chat.innerText = '=>';       //картинку в ссылке
	pers.to_chat.title = 'Приватное сообщение';
	_ctn_ac(' ', pers); // Встатвить пробел
	pers.name = _ce_ac('a', pers);
	_ctn_ac(name, pers.name);
	pers.name.href = 'info.php?id=' + id;
	pers.name.target = '_blank';
	pers.name.title = 'Постмотреть инфо о ' + name;
	_ctn_ac(' ', pers); // Встатвить пробел
	pers.level = _ce_ac('span', pers);
	_ctn_ac('[' + level + ']', pers.level);
	pers.level.title = 'Уровень персонажа';
	pers.uid = id;
	return pers;
}

function show_properties(obj, step, mode) {
	var s = '';
	var i = 0;
	if (typeof(step) == 'boolean') {
		mode = step;
		step = 5;
	}
	step = (step || 5)
	for (var k in obj) {
		if ((obj[k] && mode) || !mode) {
			s += k + ' , ' + obj[k] + "\n";
			i++;
			if (i == step) {
				i = 0;
				alert(s);
				s = '';
			}
		}
	}
	if (s) {
	  alert(s);
	}
}

/*

*/

function Table ( rows, cols, obj, has_THEAD, td_align, td_valign) { // rows, cols e obj - iaycaoaeuiu, inoaeuiua iao
	var i,j;
	if (typeof(has_THEAD) != 'boolean') {
		if (!td_valign && !td_align && (has_THEAD == 'top' || has_THEAD == 'middle' || has_THEAD == 'baseline' || has_THEAD == 'bottom')) {
			td_valign = has_THEAD;
			td_align = 'left';
		}
		else {
			td_valign = (td_valign || 'middle');
			td_align = (td_align || 'left');
		}
		has_THEAD = false;
	}
	else {
		if (!td_valign && (td_align == 'top' || td_align == 'middle' || td_align == 'baseline' || td_align == 'bottom')) {
			td_valign = td_align;
			td_align = 'left';
		}
		else {
			td_valign = (td_valign || 'middle');
			td_align = (td_align || 'left');
		}
	}
	table = _ce_ac('table', obj);
	if (has_THEAD)	{
		table.h = _ce_ac('thead', table);
		table.h.tr = _ce_ac('tr', table.h);
		for (j = 0; j < cols; j++) {
			table.h.tr[j] = _ce_ac('th', table.h.tr);
			table.h.tr[j].tn = _ctn_ac('', table.h.tr[j]);
		}
	}
	table.b = _ce_ac('tbody', table);
	for (i = 0; i < rows; i++) {
		table.b[i] = _ce_ac('tr', table.b);
		for (j = 0; j < cols; j++) {
			table.b[i][j] = _ce_ac('td', table.b[i]);
			table.b[i][j].tn = _ctn_ac('', table.b[i][j]);
			//table.b[i][j].tn.data = 'qwert';
		}
	}
	return table;
}

function clear_arr_of_refs(arr) {
		for (i = 0; i < arr.length; i++) {
			if (arr[i]) {
				arr[i].removeNode(true);
			}
		}
		arr.splice(0, arr.length);
}

function clear_obj_of_refs(obj) {
		for (var k in obj) {
			if (obj[k]) {
				obj[k].removeNode(true);
			}
			obj[k] = null;
		}
}

function fnHandleDragStart()
{                                      
  var oData = window.event.dataTransfer;

  // Set the effectAllowed on the source object.
  oData.effectAllowed = "move";
}

// This function is called by the target 
//  object in the ondrop event.
function fnHandleDrop()
{
  var oTarg = window.event.srcElement;
  var oData = window.event.dataTransfer;

  // Cancel default action.
  fnCancelDefault();

  // Set the content of the oTarget to the information stored
  //  in the data transfer object in the desired format.
  /*oTarg.innerText += oData.getData("text");*/
}

// This function sets the dropEffect when the user moves the 
//  mouse over the target object.
function fnHandleDragEnter()
{
  var oData = window.event.dataTransfer;

  // Cancel default action.
  fnCancelDefault();

  // Set the dropEffect for the target object.
  oData.dropEffect = "move";
}

function fnCancelDefault()
{
  // Cancel default action.
  var oEvent = window.event;
  oEvent.returnValue = false;
}