atheaTTcfg={
// CSS classes
// trigger atheaTT
  triggerClass:'previewTT',
// class of the popup
  popupClass:'popup'
}
var globalXCoord;
var globalYCoord;
var previewError = "<table class='error' width=100% height=100%><tr><td align=center><br><br>ошибка загрузки</td></tr></table>";
var previewWidth = 360;
var previewHeight = 190;
var loadMessage = "<table class='gensmallb' width=100% height='170'><tr><td align=center><br><img src='../atheatt/indicator_snake.gif' border=0><br><br>&nbsp;Загрузка..<br><br></td></tr></table>";

if (tooltipDelay);
else
	tooltipDelay = 1000;
if (tooltipHideDelay);
else
	tooltipHideDelay = 500;


function genTooltipTable(user_id){
	var tooltipTable = '<table cellSpacing=0 cellPadding=0 border=0 width=260>';
	tooltipTable += '<TR><TD height=11 width=11><img src="../image/ramka/r_lt.png"></TD><TD width=240 background="../image/ramka/r_t.png"></TD><TD width=11><img src="../image/ramka/r_rt.png"></TD></TR>';
	tooltipTable += '<TR><TD background="../image/ramka/r_l.png"></TD><td id="tooltipcontent'+user_id+'" class=td_cont></td><TD width=11 background="../image/ramka/r_r.png"></TD></tr>';
	tooltipTable += '<TR><TD height=11 width=11><img src="../image/ramka/r_lb.png"></TD><TD background="../image/ramka/r_b.png"></TD><TD><img src="../image/ramka/r_rb.png"></TD></TR>';
	tooltipTable += '</table>';
	return tooltipTable;
}

function defPosition(event) {
      var x = y = 0;
      if (document.attachEvent != null) { // Internet Explorer & Opera
            x = window.event.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
            y = window.event.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
      }
      if (!document.attachEvent && document.addEventListener) { // Gecko
            x = event.clientX + window.scrollX;
            y = event.clientY + window.scrollY;
      }
      return {x:x, y:y};
}

document.onmousemove = function(event) {
      var event = event || window.event;
      globalXCoord = defPosition(event).x;
	  globalYCoord = defPosition(event).y;
}

atheaTT={
	div:null,
	overDiv:null,
	showDiv:null,
	timer:null,
	lastObject:null,
	init:function(){
		if(!document.getElementById || !document.createTextNode){return;}
		var allLinks=document.getElementsByTagName('a');
		for(var i=0;i<allLinks.length;i++){
			if(!atheaTT.cssjs('check',allLinks[i],atheaTTcfg.triggerClass)){continue;}
			atheaTT.addEvent(allLinks[i],'mouseover',atheaTT.showTooltipWithDelay,false);			
			atheaTT.addEvent(allLinks[i],'mouseout',atheaTT.hideTooltipFromAnchor,false);
			atheaTT.addEvent(allLinks[i],'click',atheaTT.hideTooltipFromAnchor,false);
		}
	},

	showTooltipWithDelay:function(e){
		var t=atheaTT.getTarget(e);
		var i=0;
		while((i<5) && (t.nodeName.toLowerCase()!='a')){
			i++;
			t=t.parentNode;	
		}
		if (t.nodeName.toLowerCase()!='a')
			return;
		atheaTT.lastObject = t;
		atheaTT.timer = setTimeout("atheaTT.openTooltip(atheaTT.lastObject)", tooltipDelay);
	},

	getUserIdFromHref:function(link){
		var href = link.getAttribute('href');
		var search = "mode=viewprofile&u=";
		var index = 0;
		var user_id = 0;
		if (index = href.lastIndexOf(search)){
			user_id = href.substr(index+19); 
			search = "&";
			if ((index = user_id.lastIndexOf(search))!=-1)
			{
				user_id = user_id.substr(0,index);
			}
		}
		return user_id;
	},

	hideTooltipByUserId:function(user_id){
		var div_id = 'tooltip'+user_id;
		var toolTip;
		if (toolTip=document.getElementById(div_id))
			atheaTT.hideTTLayer(toolTip);
	},

	hideTooltipFromAnchor:function(e){
		if (atheaTT.timer)
		{
			clearTimeout(atheaTT.timer);
			atheaTT.timer = null;
		}

		if (atheaTT.showDiv == "1")
		{
			atheaTT.overDiv = 0;
			setTimeout('atheaTT.hideTooltip()', tooltipHideDelay);
		}	
	},

	hideTooltip:function(){
		if (atheaTT.overDiv == "0")
		{
			atheaTT.hideTTLayer(atheaTT.div);
			atheaTT.showDiv = 0;
		}
	},
	
	hideTooltipFromDiv:function(){
		atheaTT.overDiv = 0;
		setTimeout('atheaTT.hideTooltip()', tooltipHideDelay);
	},

	hideTTLayer:function(object){
		object.style.visibility='hidden';
		atheaTT.div = 0;
		atheaTT.showDiv = 0;
	},

	setOverDiv:function(e){
		atheaTT.overDiv = 1;
	},

	openTooltip:function(t){
		if(atheaTT.showDiv){
			return;
		}
		var user_id = atheaTT.getUserIdFromHref(t);
		var div_id = 'tooltip'+user_id;
		if (!(atheaTT.div=document.getElementById(div_id)))
		{
			atheaTT.div=document.createElement('div');
			atheaTT.div.setAttribute('id',div_id);
			atheaTT.div.setAttribute('z-index','3');
			atheaTT.cssjs('add',atheaTT.div,atheaTTcfg.popupClass);
			atheaTT.div.innerHTML = genTooltipTable(user_id);
			atheaTT.positionPopup(t);
			atheaTT.addEvent(atheaTT.div,'mouseout',atheaTT.hideTooltipFromDiv,false);
			atheaTT.addEvent(atheaTT.div,'mouseover',atheaTT.setOverDiv,false);
			document.body.appendChild(atheaTT.div);
			atheaTT.positionPopup(t);
			atheaTT.div.setAttribute('loadcomplete','0');
			atheaTT.loadData(user_id);
			atheaTT.showDiv = 1;
		}
		else{
			if (atheaTT.showDiv != 1){
				atheaTT.positionPopup(t);
				atheaTT.div.style.visibility='visible';
				atheaTT.showDiv = 1;;
				if (atheaTT.div.getAttribute('loadcomplete')!='1')
					atheaTT.loadData(user_id);

			}
		}
		t.setAttribute('rel_div',div_id);
	},

	loadData:function(user_id){
	//id слоя имеет вид tooltip132, где 132- user_id
		var toolTip;
		var tooltipContent = 'tooltipcontent'+user_id;
		if (toolTip=document.getElementById(tooltipContent))
		{
			toolTip.innerHTML = loadMessage;
			JsHttpRequest.query(
			    '../inc/getprofilepreview.php', 
				{ user_id: user_id ,owner_user_id: current_user_id}, //current_user_id определяется в topa.php 
				function(responseJS, responseText) {
					if (responseJS.exist==1){//ok
						toolTip.innerHTML = responseJS.preview;
						atheaTT.div.setAttribute('loadcomplete','1');
					}
					else{
						toolTip.innerHTML = previewError;
						atheaTT.div.setAttribute('loadcomplete','0');
					}
			    },
			    true
		    );


		}
	},

	positionPopup:function(o){
		var x=0;
		var y=0;
		var h=o.offsetHeight;
/*		while (o != null){
			x += o.offsetLeft;
			y += o.offsetTop;
			o = o.offsetParent;
		}
*/		
		x=globalXCoord;
		y=globalYCoord;
		
		if (document.body.clientWidth && (document.body.clientWidth + document.body.scrollLeft - x) < previewWidth)
			x = x - previewWidth;
		else
			x = x + 15;
		if (document.body.clientHeight && (document.body.clientHeight + document.body.scrollTop - y) < previewHeight)
			y = y - previewHeight;

		atheaTT.div.style.left=x+'px';
		atheaTT.div.style.top=y+'px';
	},

	getTarget:function(e){
		var target = window.event ? window.event.srcElement : e ? e.target : null;
		if (!target){return false;}
		while(target.nodeType!=1 && target.nodeName.toLowerCase()!='body'){
			target=target.parentNode;
		}
		return target;
	},

	addEvent: function(elm, evType, fn, useCapture){
		if (elm.addEventListener){
			elm.addEventListener(evType, fn, useCapture);
			return true;
		} else if (elm.attachEvent) {
			var r = elm.attachEvent('on' + evType, fn);
			return r;
		} else {
			elm['on' + evType] = fn;
		}
	},

	cssjs:function(a,o,c1,c2){
		switch (a){
			case 'swap':
				o.className=!atheaTT.cssjs('check',o,c1)?o.className.replace(c2,c1):o.className.replace(c1,c2);
			break;
			case 'add':
				if(!atheaTT.cssjs('check',o,c1)){o.className+=o.className?' '+c1:c1;}
			break;
			case 'remove':
				var rep=o.className.match(' '+c1)?' '+c1:c1;
				o.className=o.className.replace(rep,'');
			break;
			case 'check':
				var found=false;
				var temparray=o.className.split(' ');
				for(var i=0;i<temparray.length;i++){
					if(temparray[i]==c1){found=true;}
				}
				return found;
			break;
		}
	}
}

atheaTT.addEvent(window,'load',atheaTT.init,false);			
