/* функционал для всплывающих подсказок */
//---------------------------------------------------------------
var HintBgColor="#FFFFE6";
var HintTextFont="Arial";
var HintTextColor="black";
var HintTextSize="11px";
var HintBorderWidth="1";
var HintBorderColor="black";
var MaxHintWidth=250;
var HintPadding=0;
var withiFrame=false;
var isIE=(window.attachEvent) ? true:false;

function trim(s) { return s != null ? s.replace(/(^\s*)|(\s*$)/,"") : s;}
function InitAllHints(useIframe, maxWidth, bgColor, txtFont, txtColor, txtSize, borderWidth, borderColor, padding) {
	var node;
	if (useIframe != null) withiFrame=useIframe;
	if (maxWidth != null) MaxHintWidth=maxWidth;
	if (bgColor != null) HintBgColor=bgColor;
	if (txtFont != null) HintTextFont=txtFont;
	if (txtColor != null) HintTextColor=txtColor;
	if (txtSize != null) HintTextSize=txtSize+"px";
	if (borderWidth != null) HintBorderWidth=borderWidth;
	if (borderColor != null) HintBorderColor=borderColor;
	if (padding != null) HintPadding=padding;
	for (var j = 0; (node = document.getElementsByTagName("*")[j]); j++) {
		if (node.getAttribute("hint") == null ) continue;
		addHint(node);
	}
}
function addHint(obj) {
	if (obj==null) return;
    if (obj.getAttribute("hintInstalled") == "true") return;
	if (obj.addEventListener) {
		obj.addEventListener("mouseover", showHint, false);
		obj.addEventListener("mouseout", hideHint, false);
		obj.addEventListener("mousemove", dragHint, false);
	}
	else if (obj.attachEvent){
		obj.attachEvent("onmouseover", showHint);
		obj.attachEvent("onmouseout", hideHint);
		obj.attachEvent("onmousemove", dragHint);
	}

    obj.setAttribute ("hintInstalled", "true");
}
function HintBody() {
	var str="";
	var b="border:"+HintBorderColor+" solid "+HintBorderWidth+"px;";
	str+='<table cellspacing=0 cellpadding=1 style="width:100%" ><tr><td style="'+b+'cursor:default;background:'+HintBgColor+';" id="hintHolder" align=center>';
	if (withiFrame) str+='<iframe style="margin:0px;width:100%" name="hintFrame" id="hintFrame" frameborder=0 scrolling=no ></iframe>';
	str+='</td></tr></table>';
	return str;
}
function hintStyle() {
	return "<style type='text/css'>\n .hint {background:"+HintBgColor+";color:"+HintTextColor+";font-family:"+HintTextFont+";font-size:"+HintTextSize+";text-align:justify;}</style>\n";
}
function hintHeader() {
	return "<html><head>\n"+hintStyle()+"\n</head>\n<body style='margin:0px;cursor:default;' >\n";
}
function resizeHint(obj, frobj, flag) { //flag:  0- use iframe 1- dont use iframe as holder for hint
	if (obj==null) obj=document.getElementById("HintLayer");
	if(!obj) return;
	if (frobj==null) frobj=(flag)?document.getElementById("hintHolder"):document.getElementById("hintFrame");
	if(!frobj) return;

	if (flag) var fr=document.getElementById("mt"); 
	else {
		var fr= (frobj.contentDocument) ? frobj.contentDocument : (window.frames) ? window.frames["hintFrame"].document:document.frames["hintFrame"].document; 
		fr=fr.getElementById("mt");
	}
	if(!fr) return;
	var ContentWidth = (document.clientWidth) ? fr.clientWidth : fr.offsetWidth ;
	var ContentHeight = (document.clientHeight) ? fr.clientHeight : fr.offsetHeight;
	// alert("w="+ContentWidth+"     h="+ContentHeight);

	// Изменение ширины подсказки
	var curWidth= parseInt(obj.style.width,10); 
	if ( isNaN(curWidth) == true) {curWidth=0;}
	if (ContentWidth< curWidth || curWidth==0) {
		obj.style.width=ContentWidth+10+"px";
		frobj.style.width=ContentWidth+"px"; 
	}

	// Изменение высоты подсказки
	obj.style.height=ContentHeight+"px";
	frobj.style.height=ContentHeight+"px";
	replaceHint(obj);
}
function replaceHint(obj,_x,_y) {	//изменение положения подсказки на экране если не входит в экран
	if (obj==null) obj=document.getElementById("HintLayer");
	if(!obj) return;
	var winW=window.document.body.clientWidth;
	var winH=window.document.body.clientHeight;
	var hintWidth=obj.clientWidth;
	var hintHeight=obj.clientHeight;
	var x=(_x==null || typeof(_x)=="undefined")?parseInt(obj.style.left):_x;
	x=(isNaN(x))?0:x;
	var y=(_y==null || typeof(_y)=="undefined")?parseInt(obj.style.top):_y;
	y=(isNaN(y))?0:y;

	//alert(obj+" x="+x+"   y="+y+"\n\nwidth="+hintWidth+"  h:"+hintHeight+"\n\nWinW="+winW+" - WinH="+winH);
	
	if ((x+hintWidth)>winW) {
		obj.style.left=((x-hintWidth)<0)?(x-Math.round(hintWidth/2)):x-hintWidth;
	}
	if ((y+hintHeight)>winH) {
		obj.style.top=((y-hintHeight)<0)?(y-Math.round(hintHeight/2)):y-hintHeight;
	}
}

function createHint(obj,x,y) {
	if (obj==null) return false;
	var DivTag=document.createElement("DIV");
	DivTag.setAttribute("id","HintLayer");
	DivTag.style.position="absolute";
	DivTag.style.display="none";
	DivTag.style.zIndex="1000";
	DivTag.style.left=(!x)?0:x;
	DivTag.style.top=(!y)?0:y;
	DivTag.style.width=MaxHintWidth;
	self.document.body.appendChild(DivTag);
	DivTag.innerHTML=HintBody();
	if (isIE) {
		if (DivTag.getAttribute("mistikFrame")==null) {
			var fr=document.createElement("iframe");
			fr.setAttribute("frameborder","0");
			fr.className="hideSelectIframe";
			DivTag.appendChild(fr);
			DivTag.setAttribute("mistikFrame",true);
		}
	}
	return DivTag;
}
function showHint(e) {
	var e = window.event ? window.event : e;
	var src = window.event ? e.srcElement : e.target;
	if(!src) return;
	var hintTxt=src.getAttribute("hint");
	if (hintTxt==null || trim(hintTxt)=="") return;
	hintTxt="<table cellpadding="+HintPadding+" cellspacing=0 id='mt'><tr><td "+((withiFrame)?"class='hint'":"style='font-family:"+HintTextFont+";font-size:"+HintTextSize+";text-align:justify;color:"+HintTextColor+"'")+">"+hintTxt+"</td></tr></table>";
	var x=((e.pageX) ? e.pageX : e.x+document.body.scrollLeft)+15;
	var y=((e.pageY) ? e.pageY : e.y+document.body.scrollTop)+10;
	stopEvent(e);
	var hintDiv=document.getElementById("HintLayer");
	if (hintDiv== null) {
		hintDiv=createHint(src,x,y);
		if (!hintDiv) return;
	}

//	 alert(hintDiv+" x="+hintDiv.style.left+"   y="+hintDiv.style.top+"\n\nwidth="+hintDiv.clientWidth+"  h:"+hintDiv.clientHeight+"\n\nWinW="+window.document.body.clientWidth+" - WinH="+window.document.body.clientHeight);

	if (withiFrame) {
		var hint=hintHeader()+hintTxt+"</body></html>";
		hintDiv.style.display="";
		var hintObj=document.getElementById('hintFrame');
		if (hintObj!=null)  {
			var ifrDoc=  (hintObj.contentDocument) ? hintObj.contentDocument : (window.frames) ? window.frames["hintFrame"].document:document.frames["hintFrame"].document; 
			if (ifrDoc !=null) {
				with(ifrDoc) {
					open();
					write(hint);
					close();
				}
				resizeHint(hintDiv,hintObj,0)
			}
			else hintDiv.style.display="none";
		}
	}
	else {
		var hintObj=document.getElementById("hintHolder");
		hintObj.innerHTML=hintTxt;
		hintDiv.style.display="";
		resizeHint(hintDiv,hintObj,1);
	}
}
function dragHint(e) {
	var e = window.event ? window.event : e;
	var obj=document.getElementById("HintLayer");
	if (obj) {
		var x=((e.pageX) ? e.pageX : e.x+document.body.scrollLeft)+15;
		var y=((e.pageY) ? e.pageY : e.y+document.body.scrollTop)+10;
		replaceHint(obj,x,y);
	}
	stopEvent(e);
}
function hideHint(e) {
	var e = window.event ? window.event : e;
	stopEvent(e);
	var obj=document.getElementById("HintLayer");
	if (obj) {
		obj.style.display="none";
		self.document.body.removeChild(obj);
	}
}
function stopEvent(e) {
	var ev = window.event ? window.event : (e == null) ? null : e;
	if (ev!=null) {
		ev.cancelBubble=true;
		if (ev.stopPropagation) ev.stopPropagation();
	}
	 return false;
}
/*------------------------------------------------------*/
