/* функционал для всплывающих подсказок */
//---------------------------------------------------------------

var HintBgColor="#FFFFE6";
var HintTextColor="black";
var HintTextSize="11px";
var HintBorderWidth="1";
var HintBorderColor="black";
var MaxHintWidth=250;
var withiFrame=false;

function trim(s) { return s != null ? s.replace(/(^\s*)|(\s*$)/,"") : s;	} 

function InitAllHints(useIframe, maxWidth, bgColor, txtColor, txtSize, borderWidth, borderColor) {
  var node;
   if (useIframe != null) withiFrame=useIframe;
   if (maxWidth != null) MaxHintWidth=maxWidth;
   if (bgColor != null) HintBgColor=bgColor;
   if (txtColor != null) HintTextColor=txtColor;
   if (txtSize != null) HintTextSize=txtSize+"px";
   if (borderWidth != null) HintBorderWidth=borderWidth;
   if (borderColor != null) HintBorderColor=borderColor;
   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.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);   }
}

function HintBody() {
   var str="";
   var b="border:"+HintBorderColor+" solid "+HintBorderWidth+"px;";
   str+='<table cellspacing=0 cellpadding=4 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-size:"+HintTextSize+";text-align:justify;}</style>\n";
}

function hintHeader() {
  return "<html><head>\n"+hintStyle()+"\n</head>\n<body style='margin:5px;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";
}

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();
  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 cellspacing=0 id='mt'><tr><td "+((withiFrame)?"class='hint'":"style='font-size:"+HintTextSize+";padding:5px;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\n"+hintDiv.innerHTML);

   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) {
     obj.style.left=((e.pageX) ? e.pageX : e.x+document.body.scrollLeft)+15;
     obj.style.top=((e.pageY) ? e.pageY : e.y+document.body.scrollTop)+10;
  }
  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) {
  if (e) 
    {e.cancelBubble=true;
      if (e.stopPropagation) e.stopPropagation();}
}
/*------------------------------------------------------*/