var frarr = '';
var frcnt = 0;
var flashing = 0;
var alert_flash = 0;

function send_xmlhttprequest(obsluha, method, url, content, headers) {
	var xmlhttp = (window.XMLHttpRequest ? new XMLHttpRequest : (window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : false));
	if (!xmlhttp) return false;
	xmlhttp.open(method, url);
	xmlhttp.onreadystatechange = function() {
		obsluha(xmlhttp);
	};
	if (headers) {
		for (var key in headers) {
			xmlhttp.setRequestHeader(key, headers[key]);
		}
	}
	xmlhttp.send(content);
	return true;
}


function flash() {
	if (!flashing) {
		if (alert_flash) window.focus();
		flashing = 1;
	}
}

function info_fresh_showit(xmlhttp) {
	if (xmlhttp.readyState == 4) {
		data = xmlhttp.responseText.split('|');

		dd = 0;
		o = document.getElementById('infodata-post');
		if (data[dd++] == '1') {
			o.innerHTML = '<img src="/img/icons/obalka_act.gif" title="nový vzkaz" />';
			flash();
		} else o.innerHTML = '';

		o = document.getElementById('infodata-visit');
		if (data[dd++] == '1') {
			o.innerHTML = '<img src="/img/icons/navsteva_act.gif" title="nová návštěva" />';
			flash();
		} else o.innerHTML = '';

		o = document.getElementById('infodata-forum');
		if (data[dd] > 0) {
			o.innerHTML = '<a href="/forum/'+ data[dd] +'/"><img src="/img/icons/forum_act.gif" title="nový vzkaz na fóru" /></a>';
			flash();
		} else o.innerHTML = '';
		dd++;

		for(q = 0; q < frcnt; q++) {
			o = document.getElementById('infodata-foto-'+ q);
			if (data[dd++] == '1') o.innerHTML = '<img src="/img/icons/fotak_act.gif" alt="fotografie" />'; else o.innerHTML = '<img src="/img/icons/fotak.gif" alt="fotografie" />';

			o = document.getElementById('infodata-post-'+ q);
			if (data[dd++] == '1') o.innerHTML = '<img src="/img/icons/obalka_act.gif" alt="pošta" />'; else o.innerHTML = '<img src="/img/icons/obalka.gif" alt="pošta" />';

			o = document.getElementById('infodata-profil-'+ q);
			if (data[dd++] == '1') o.innerHTML = '<img src="/img/icons/profil_act.gif" alt="profil" />'; else o.innerHTML = '<img src="/img/icons/profil.gif" alt="profil" />';

			o = document.getElementById('infodata-chat-'+ q);
			if (data[dd++] == '1') o.innerHTML = '<img src="/img/icons/chat_act.gif" alt="chat" />'; else o.innerHTML = '<img src="/img/icons/chat.gif" alt="chat" />';

			o = document.getElementById('infodata-online-'+ q);
			if (data[dd++] == '1') o.innerHTML = '&nbsp;<img class="no-float" src="/img/icons/online.gif" alt="je právě online" />'; else o.innerHTML = '';
		}
		if (data[dd++] == '1') fresh_usrlist();

	}
}

function info_fresh() {
	url = '/info_fresh.php?usrlst='+ frarr;
	if (!send_xmlhttprequest(info_fresh_showit, 'GET', url)) return false;
	info_fresh_warp();
	return true;
}


var info_fresh_timeout;
function info_fresh_warp() {
	if (info_fresh_timeout) window.clearTimeout(info_fresh_timeout);
	info_fresh_timeout = window.setTimeout("info_fresh()", 30000);
}

function info_fresh_start() {
	info_fresh_warp();
}




function fresh_usrlist() {
	if (!send_xmlhttprequest(show_usrlist, 'GET', '/info_usrlist.php')) return false;
}

function show_usrlist(xmlhttp) {
	if (xmlhttp.readyState == 4) {
		o = document.getElementById('infodata-usrlist');
		data = xmlhttp.responseText.split('^');
		o.innerHTML = data[0];
		frcnt = data[1];
		frarr = data[2];
	}
}







// forum >>>
var ocsm = false;
function oc_smileys() {
	if (ocsm) {
		document.getElementById("smiley-fce").innerHTML = '<a href="#" onclick="return !oc_smileys()"><img src="/img/open.jpg" width="15" height="15" alt="otevřít smajllíky" /></a>';
		document.getElementById("smileys").className = 'smileys smileys-close';
		ocsm = false;
	} else {
		document.getElementById("smiley-fce").innerHTML = '<a href="#" onclick="return !oc_smileys()"><img src="/img/close.jpg" width="15" height="15" alt="zavřít smajllíky" /></a>';
		document.getElementById("smileys").className = 'smileys';
		ocsm = true;
	}
}

function write_smiley(i) {
	if (ocsm)	oc_smileys();
	document.getElementById('txt').value += ' :'+ i +': ';
	document.getElementById('txt').focus();
	return true;
}











// chat >>>
var chat_fresh_timeout;
var chat_usr_fresh_timeout;
function chat_start() {
	info_fresh_start(); // protoze to pretlouklo onload
	chat_fresh();
	chat_usr_fresh();
	document.getElementById("txt").focus();
}

function chat_fresh_warp(t) {
	if (chat_fresh_timeout) window.clearTimeout(chat_fresh_timeout);
	chat_fresh_timeout = window.setTimeout("chat_fresh()", t);
}

function chat_fresh() {
	url = '/chat_fresh.php?fresh=1&room='+ chat_room;
	if (!send_xmlhttprequest(chat_fresh_showit, 'GET', url)) return false;
	chat_fresh_warp(10000);
	return true;
}

function chat_fresh_showit(xmlhttp) {
	if (xmlhttp.readyState == 4) {
		document.getElementById('chatmsgs').innerHTML = xmlhttp.responseText;
	}
}

function chat_sendit() {
	str = document.getElementById("txt").value;
	if (str) {
		if (!send_xmlhttprequest(chat_sended, 'GET', '/chat_fresh.php?room='+ chat_room +'&send_text='+ escape(str))) return false;
	}
	return true;
}

function chat_sended() {
	document.getElementById("txt").value = '';
	document.getElementById("txt").focus();
	chat_fresh_warp(200);
	return true;
}




function chat_usr_fresh() {
	url = '/chat_fresh.php?usrlist=1&room='+ chat_room;
	if (!send_xmlhttprequest(chat_usr_fresh_showit, 'GET', url)) return false;
	chat_usr_fresh_warp();
	return true;
}

function chat_usr_fresh_warp() {
	if (chat_fresh_timeout) window.clearTimeout(chat_usr_fresh_timeout);
	chat_usr_fresh_timeout = window.setTimeout("chat_usr_fresh()", 25000);
}

function chat_usr_fresh_showit(xmlhttp) {
	if (xmlhttp.readyState == 4) {
		document.getElementById('chatusrs').innerHTML = xmlhttp.responseText;
	}
}





// nastenka >>>
var nastenka_pozice = 1;
function nastenka_fresh() {
	nastenka_pozice++;
	if (nastenka_pozice > 5) nastenka_pozice = 1;
	url = '/nastenka_fresh.php?pozice='+ nastenka_pozice;
	if (!send_xmlhttprequest(nastenka_fresh_showit, 'GET', url)) return false;
	nastenka_fresh_warp();
	return true;
}

var nastenka_fresh_timeout;
function nastenka_fresh_warp() {
	if (nastenka_fresh_timeout) window.clearTimeout(nastenka_fresh_timeout);
	nastenka_fresh_timeout = window.setTimeout("nastenka_fresh()", 11000);
}

function nastenka_fresh_start() {
	nastenka_fresh_warp();
}

function nastenka_fresh_showit(xmlhttp) {
	if (xmlhttp.readyState == 4) {
		document.getElementById('nastenkadata').innerHTML = xmlhttp.responseText;
	}
}

