var popup_key = 1;
var last_key = false;
var id_data={};
var post_http = null;
var ajaxing = false;
var last_loading = true;
var newajax = function()
{
	try{
		return new XMLHttpRequest();
	}catch(e1){
		try{
			return new ActiveXObject('Msxml2.XMLHTTP');
		}catch(e2){
			try{
				return new ActiveXObject('Microsoft.XMLHTTP');
			}catch(e3){}
		}
	}
	return null;
};
function loading_template(key)
{
	return '<table cellspacing="0" width="100%"><tr class="row2"><th>'+lang_loading+'...</th></tr></table>';
}
function show_popup_hint(key)
{
	if(!id_data[key].loaded)
	{
		if(!id_data[key].dom)
			id_data[key].dom=loading_template(key);
	}
	else
	{
		var res = id_data[key].res;
		if(res['e'] != 0)
		{
			$('#popup_cont').fadeOut();
			return;
		}
		id_data[key].dom = res['c'];
	}
	if((last_loading != id_data[key].loaded)||(last_key != key))
	{
		$('#popup_cont').empty();
		$('#popup_cont').append(id_data[key].dom);
		last_key = key;
		last_loading = id_data[key].loaded;
	}
	var ahr = $('#'+key);
	$('#popup_cont')
		.css('top',(ahr.offset().top+ahr.height()/2)+'px')
		.css('left',ahr.offset().left+'px').fadeIn();
	$('#popup_cont>*').hover(function()
	{
		if(id_data[key].act_to)
		{
			clearTimeout(id_data[key].act_to);
			id_data[key].act_to=false;
		}
	},function()
	{
		id_data[key].act_to = setTimeout(function()
		{
			$('#popup_cont').fadeOut('slow');
		},250);
	});
}
function setPersonalHandler(key)
{
	var lockey = key;
	id_data[lockey].loaded = false;
	id_data[lockey].handler = function()
	{
		if(post_http.readyState == 4)
		{
			if(post_http.status == 200)
			{
				eval("id_data['"+lockey+"'].res = " + post_http.responseText + ";");
				id_data[lockey].loaded = true;
				ajaxing = false;
				show_popup_hint(lockey);
			}
		}
	}
}
function gen_id(prefix)
{
	var newid = prefix+popup_key;
	popup_key++;
	return newid;
}
$(document).ready(function()
{
	var hint_starter=function(id)
	{
		if(!id) return;
		if(id_data[id])
			clearTimeout(id_data[id].hovt);
		if(!id_data[id].res)
		{
			if(!post_http)
			{
				post_http = newajax();
			}
			if(!post_http) return;
			if(ajaxing)
			{
				post_http.abort();
				ajaxing = false;
			}
			post_http.onreadystatechange = id_data[id].handler;
			post_http.open('GET',post_ajax_url+"&_c="+Math.random()+"&c="+id_data[id].c+"&p="+id_data[id].param);
			post_http.onreadystatechange = id_data[id].handler;
			post_http.send(null);
			ajaxing = true;
			show_popup_hint(id);
			return;
		}
		else
			show_popup_hint(id);	
	};
	var hrs = document.getElementsByTagName('a');
	var i =0;
	var regexp ={'postlink':
					{'viewtopic.php':new RegExp(board_url+'viewtopic.php','i'),
					'memberlist.php':new RegExp(board_url+'memberlist.php','i')},
				'postlink-local':
					{'viewtopic.php':new RegExp('viewtopic.php','i'),
					'memberlist.php':new RegExp('memberlist.php','i')}
				};
	for(var i =0 ; i< hrs.length; i++)
	{
		var c=0;
		var p = null;
		var r = null;
		var matches = null;
		if(regexp[hrs[i].className])
		{
			r = regexp[hrs[i].className];
			var hr = hrs[i].href;
			if(r['viewtopic.php'].test(hr))
			{
				if(matches = hr.match(/p=(\d+)/i))
				{
					c = 1;
					p = matches[1];
				}
				else if(matches = hr.match(/t=(\d+)/i))
				{
					c = 2;
					p = matches[1];
				}
			}else if(r['memberlist.php'].test(hr))
			{
				if(hr.match(/mode=viewprofile/i))
					if(matches = hr.match(/u=(\d+)/i))
					{
						c = 3;
						p = matches[1];
					}
			}
			if(c!=0)
			{
				var newid = gen_id('pl');
				hrs[i].id = newid;
				id_data[newid]={};
				id_data[newid].hovt = false;
				id_data[newid].c = c;
				id_data[newid].param = p;
				id_data[newid].loaded = false;
				setPersonalHandler(newid);
			}
		}
	}
	$('.postlink').hover(function()
	{
		var id = this.id;
		if(id_data[id])
			id_data[id].hovt=setTimeout(function(){hint_starter(id)},250);
	},function()
	{
		if(id_data[this.id])
		if(id_data[this.id].hovt)
			clearTimeout(id_data[this.id].hovt);
	});
	$('.postlink-local').hover(function()
	{
		var id = this.id;
		if(id_data[id])
			id_data[id].hovt=setTimeout(function(){hint_starter(id)},250);
	},function()
	{
		if(id_data[this.id])
		if(id_data[this.id].hovt)
			clearTimeout(id_data[this.id].hovt);
	});
});
