
var timer_pop = 0;
var timer_pop_close = 0;
if (!top.ui_cache) {
	var ui_cache = {};
}
var ui_last = 0;
var popInfo = 0;
var msg_cache = {};
var detail_type = '';
var captcha_key = Math.round(Math.random()*10000);
var is_spammer = 0

if(!top.URL_BLOG) {
	var URL_BLOG = "http://web.libimseti.cz";
}
if(!top.URL_VZKAZY) {
	var URL_VZKAZY = "http://vzkazy.libimseti.cz";
}
if(!top.URL_CHAT) {
	var URL_CHAT = "http://chat.libimseti.cz";
}
var url_quickmsg = URL_VZKAZY+'/ajax.py';

function detail_init() {

	popInfo = document.createElement('div');
	var body = document.getElementsByTagName("body")[0];
	//popInfo.style.position = "absolute";
	//popInfo.style.background = "red";
	popInfo.id = 'popInfo';
	popInfo.className = 'popInfo';
	popInfo.style.position = 'absolute';
	body.appendChild(popInfo);

	elem_list = document.getElementsByTagName('*')
	for (i=0;i<elem_list.length;i++) {
		p = elem_list[i]
		try { arr = p.id.split('_') }
		catch(err) { continue }
		if (arr[0]!='detail') {
			continue
		}
		var user_ID = arr[1];
		addEvent(p, 'mouseover', detail_start);
		addEvent(p, 'mouseout', detail_close);
	}
	addEvent(popInfo, 'mouseover', detail_close_stop);
	addEvent(popInfo, 'mouseout', detail_close);
	if(!uid) {
		uid = '';
	}
}

function detail_start(e) {


	if(timer_pop_close) { clearTimeout(timer_pop_close); timer_pop_close=0; }

	elem = e.target?e.target:e.srcElement;
	arr = elem.id.split('_')
	user_ID = arr[1]

	detail_type_item = detail_type;
	if ( arr.length == 3) {
		detail_type_item = arr[2];
	}

	if (ui_cache[user_ID]) {
		timer_pop = setTimeout("detail_cache('"+user_ID+"')", 1);
	}
	else {
		timer_pop = setTimeout("fetchData('"+user_ID+"')", 400);
	}
	//obj = popInfo;//document.getElementById("popInfo")
	//obj.style.display = "none";
	detail_x = (e.clientX + document.documentElement.scrollLeft);
	detail_y = (e.clientY + document.documentElement.scrollTop);
}
function detail_cache(user_ID) {
	detail_show(ui_cache[user_ID]);
}
function detail_show(ui) {
	//if (ui_last == ui) {
	//	if (popInfo) {
	//		popInfo.style.display = "block";
	//	}
	//	return;
	//}

	var txt = '';
	ui_last = ui;
	if (ui) {
		if (detail_type_item=='msg') {
			txt += '<img src="http://img.libimseti.cz/i/visit-close.gif" onclick="detail_close2()" style="position: absolute; right:0;">';
		}
		ui_cache[ui.userId] = ui;
		if (detail_type_item=='msg') {
			txt += '<div class="nowrap">';
		}
		else {
			txt += '<div class="nowrap">';
		}
		txt += '<a title="Jít na profil" class="sex_'+ui.sex+'" href="http://libimseti.cz/'+ui.username+'/?uid='+uid+'">'+ui.username+'</a>';

		if (ui.age>1 && ui.age<100) {
			txt += ' ('+ui.age+' let)';
		}
		if (detail_type_item=='msg') {
			txt += " - rychlý vzkaz";
		}
		txt += '</div>';
		if (detail_type_item) {
			txt += '<div style="float:left;">';
		}
		txt += '<a title="Jít na profil" class="sex_'+ui.sex+'" href="http://libimseti.cz/'+ui.username+'/?uid='+uid+'">';
		//onclick="detail_photo_switch(this)"
		txt += '<img  src="'+ui.photoUrlThumb+'" '
		if (ui.photoWidthThumb>0)
			txt += ' width="'+ui.photoWidthThumb+'" ';
		if (ui.photoHeightThumb>0)
			txt += ' height="'+ui.photoHeightThumb+'">';
		txt += '</a>';
		if (ui.regionId>0) {
			txt += '<div class="nowrap">'+ui.regionName+'</div>';
		}
		if (detail_type_item) {
			txt += '</div>';
		}
		if (detail_type_item=='menu') {
			txt += '<div style="float:left; text-align:left; margin-left:3px; padding: 3px;">';
			txt += '- <a href="http://libimseti.cz/'+ui.username+'/?uid='+uid+'">Jít na profil</a><br/>';
			txt += '- <a href="'+URL_VZKAZY+'/?act=message_form&toUser='+ui.username+'&uid='+uid+'">Poslat vzkaz</a><br/>';
			if(ui.blogExist) {
				txt += '- <a href="'+URL_BLOG+'/'+ui.username+'" target="_blank">Web & Blog</a><br/>';
			}
			online_info = onlineDot.online_ID_dict[ui.userId];
			if(online_info) {
				txt += '- <a href="'+URL_CHAT+'/minichat.py?act=enter&username='+ui.username+'&uid='+uid+'" target="_blank">Pozvat na chat</a><br/>';
			}
			//txt += '- Za nim do chatu<br/>';
			txt += '</div>';
		}
		if (detail_type_item=='msg') {
			var text = ''
			if (msg_cache[ui.userId]) {
				text = msg_cache[ui.userId]
			}
			//wday = new Date().getDay();
			//if (wday==0) { wday=7 }
			//mday = new Date().getDate();
			//var captcha = mday+wday;

			// rychly vzkaz
			txt += '<div style="float:left; text-align:left; margin-left:3px; padding: 3px;">';
			txt += '<form id="quickmsg_form">';
			//txt += ' <input type="hidden" name="uid" value="'+uid+'">';
			//txt += ' <input type="hidden" name="act" value="quickmsg">';
			txt += ' <input type="hidden" name="toUser" value="'+ui.username+'">';
			txt += ' <input type="hidden" name="toUserId" value="'+ui.userId+'">';
			txt += ' <input type="hidden" name="captcha_key" value="'+captcha_key+'">';
			txt += ' <textarea name="text" style="width:140px; height: 80px;">'+text+'</textarea>';
			txt += ' <div style="text-align: right;">';
			txt += ' <div id="captcha_box"></div>';
			txt += ' <span style="font-weight: bold;" id="quickmsg_result"></span>';
			txt += '   <input type="button" value="Odeslat" onclick="sendQuickMsg();" style="height:22px;" id="quickmsg_submit" class="submit">';
			txt += ' </div>';
			txt += '</form>';
			txt += '</div>';
		}
	}
	else {
		txt = '<div class="nowrap">nenalezeno nebo chyba</div>';
	}

	var obj = popInfo; //document.getElementById("popInfo")
	obj.style.left = detail_x + 3 + 'px';
	obj.style.top  = detail_y + 3 + 'px';
	obj.style.display = "block";
	obj.innerHTML = txt;

	if (is_spammer==1 && detail_type_item=='msg') {
		showCaptcha()
	}

}
function detail_photo_switch(img) {
	if (!ui_last) { return }
	ui = ui_last;
	if (img.src != ui_last.photoUrl) {
		img.src = ui.photoUrl;
		img.width = ui.photoWidth;
		img.height = ui.photoHeight;
	}
	else {
		img.src = ui.photoUrlThumb;
		img.width = ui.photoWidthThumb;
		img.height = ui.photoHeightThumb;
	}
}
function detail_close(e) {
	// opustil username
	var fobj = document.getElementById('quickmsg_form')
	if (fobj) {
		// ulozim si obsah textarei vzdy (zajisti smazani textu kdyz ho smazu v inputu)
		if (fobj.text.disabled==false) {
			msg_cache[fobj.toUserId.value] = fobj.text.value;
		}
	}
	cancelBubble(e);
	if(timer_pop) { clearTimeout(timer_pop); timer_pop=0; }
	// schovam div
	timer_pop_close = setTimeout("detail_close2()", 800);
}
function detail_close2(e) {
	// schovam div
	var obj = popInfo; //document.getElementById("popInfo")
	obj.style.display = "none";
	//obj.innerHTML = "";
}
function detail_close_stop(e) {
	if(timer_pop) { clearTimeout(timer_pop); timer_pop=0; }
	if(timer_pop_close) { clearTimeout(timer_pop_close); timer_pop_close=0; }
}
function fetchData(user_ID) {
	var id = "uiscript";
	var oScript = document.getElementById(id);
	var head = document.getElementsByTagName("head")[0];
	if (oScript) { head.removeChild(oScript); }

	oScript = document.createElement("script");
	head.appendChild(oScript);
	oScript.id  = id;
	oScript.charset="utf-8";
	oScript.type = 'text/javascript';
	oScript.src = URL_CHAT+"/ajax.py?act=user_info&fce=detail_show&user_ID="+user_ID;
}
function addEvent(obj, evType, fn)
{
        if (obj.addEventListener)
        {
                obj.addEventListener(evType, fn, false);
                return true;
        }
        else if (obj.attachEvent)
        {
                var r = obj.attachEvent("on"+evType, fn);
                return r;
        }
        return false;
}
function cancelBubble(e) {
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
	e.returnValue  = false;
}

function sendQuickMsg() {

	//var wday = new Date().getDay();
	//if (wday==0) { wday=7 }
	//var mday = new Date().getDate();
	//var captcha = mday+wday;
	//captcha_key = 0
	var captcha_val = 0
	//if document.getElementById('captcha_key') {
	//	captcha_key = document.getElementById('captcha_key').value
	//}

	var fobj = document.getElementById('quickmsg_form')
	var text = fobj.text.value;
	if (fobj.captcha_val) {
		captcha_val = fobj.captcha_val.value;
	}
	var toUser = fobj.toUser.value;
	var s = document.createElement("script");
	s.src = url_quickmsg+'?act=quickmsg&function=sendQuickCallback' +
		'&toUser='+ toUser+"&text="+encodeURIComponent(text)+"&uid="+uid+
		'&captcha_key='+captcha_key+
		'&captcha_val='+captcha_val+
		'&'+new Date().getTime();
	s.type="text/javascript";
	s.charset="utf-8";	// nutne (prekoduje vraceny i odeslany text)!
	document.getElementsByTagName("head")[0].appendChild(s);
}
function sendQuickCallback(status, statusMessage) {
	//alert(status+' '+statusMessage);
	var color='black';
	if(status==200) {
		document.getElementById('quickmsg_submit').style.display = 'none';
		var fobj = document.getElementById('quickmsg_form')
		fobj.text.value == '';
		msg_cache[fobj.toUserId.value] = '';
		fobj.text.disabled = true;
		color='green';
		statusMessage += '<a href="javascript:detail_show(ui_last)" title="Dalsi vzkaz">&gt;&gt;</a>';
		document.getElementById('captcha_box').innerHTML = '';
		captcha_key = Math.round(Math.random()*10000);
	}
	else {
		color = 'red';
	}
	if(status==402) {
		is_spammer = 1;
		showCaptcha()
	}
	var res = document.getElementById('quickmsg_result')
	res.innerHTML = '<span style="color: '+color+'">'+statusMessage+'</span>';
}
function showCaptcha() {
	var captcha_box = document.getElementById('captcha_box')
	if (captcha_box) {
		captcha_box.innerHTML = ' <input type="text" name="captcha_val" id="captcha_val" value="" size="3"> ' +
			'<img src="'+URL_VZKAZY+'/index.py?act=captcha&key='+captcha_key+'&small=1" title="Opiš kontrolní kód">';
	}
}


addEvent(window,'load', detail_init);
//addEvent(document, 'click', detail_close2);
//addEvent(popInfo,  'click', cancelBubble);

