	function nodeName(obj){
		if(obj.nodeName=='A' || 
			obj.nodeName=='FRAME' || 
			obj.nodeName=='INPUT' || 
			obj.nodeName=='TEXAREA' || 
			obj.nodeName=='FRAME' || 
			obj.nodeName=='OPTION' || 
			obj.nodeName=='SCRIPT' || 
			obj.nodeName=='STYLE' || 
			obj.nodeName=='H1' || 
			obj.nodeName=='H2' || 
			obj.nodeName=='H3' || 
			obj.nodeName=='H4' || 
			obj.nodeName=='H5')
		return false;
		else return true;
	}
	var words; 
	var links = new Array();
	function PrintChild(obj){
		var els = obj.childNodes;
		for(var i=0;i<els.length;i++){
			if(!nodeName(els[i])) continue;
			if(els[i].hasChildNodes()) PrintChild(els[i]);
			if(els[i].nodeName=='#text'){
				if(!els[i].nodeValue || els[i].nodeValue.match(/^\s*$/)) continue;
				var text=els[i].nodeValue;
				for(var k=0;k<words.length;k++){
					text=text.replace(words[k]['word'],'__'+words[k]['word']+'__');
				}
				
				var temp = text.split('__');
				var div = document.createElement('span');
				if(temp.length>1){
					for(var j=0; j<temp.length;j++){
						if(temp[j]=='__') continue;
						var insert = false;
						for(var k=0;k<words.length;k++){
							if(temp[j]==words[k]['word']){
								if(!words[k]['use']){
									var a = document.createElement('a');
									a.className = 'tooltips';
									a.href="javascript:void(0)";
									a.innerHTML = words[k]['word'];
									var id=words[k]['id'];
									a.id =id;
									a.onmouseover=function(){JsTooltips.Show(this,this.id,1);}
									div.appendChild(a);
									words[k]['use'] =true;
								}else{
									div.appendChild(document.createTextNode(words[k]['word']));
								}
								insert = true;
								break;
							}
						}
						if(!insert)
							div.appendChild(document.createTextNode(temp[j]));
					}
					els[i].parentNode.replaceChild(div,els[i]);
				}
			}
		}
	}
	


	var JsTooltips = Class.create();
	
	JsTooltips.prototype = {
		initialize: function(url,id,all){
			this.menu_id = id;
			this.controlDiv = null;
			this.url = url;
			this.text = '';
			this.timer = 0;
			this.gtimer = 0;
			this.cash = new Array();
			this.id=0;
			this.mark = false;
			this.over = false;
			this.div = '';
			this.img = '';
			this.w = 0;
			this.h = 0;
			this.div = document.getElementById(this.menu_id+'_div');
			this.tbl = document.getElementById(this.menu_id+'_items');
			this.words = new Array();
			if(all)
				window.onload = this.GetWords.bindAsEventListener(this);
		
		},	
		
		GetWords: function(){
			var param = 'name=Tooltips&method=GetWords&ajax=yes';
			var AjaxForm = new Ajax.Request(this.url,{
				method:'post',
				parameters: param,
				onComplete: this.InitWords.bindAsEventListener(this)
			});
		},
		
		InitWords: function(originalRequest){
			var text = originalRequest.responseText;
			if(text){
				var json = eval('(' + text + ')');
				for(var i=0;i<json.words.length;i++){
					this.words[i] = new Array();
					this.words[i]['word'] = json.words[i].word;
					this.words[i]['id'] = json.words[i].id;
					this.words[i]['use'] = false;
				}
			}
			words = this.words;
			PrintChild(document.body);
		},		
		
		IsIE: function(){
			return (document.attachEvent && !this.IsOpera());
		},

		IsOpera: function(){
			return (navigator.userAgent.toLowerCase().indexOf('opera') != -1);
		},		
			
		ParseItem: function(item){
			if(!item || item.length == 0)
				return;
			var div = this.div;//document.getElementById(this.menu_id+'_div');
			div.style.left='0px';
			div.style.top='0px';
			div.style.width='auto';
			
			this.tbl.style.visibility = 'visible';
			while(this.tbl.rows.length>0) this.tbl.deleteRow(0);
			while(this.tbl.hasChildNodes()) this.tbl.removeChild(this.tbl.firstChild);
			
			var row = this.tbl.insertRow(-1);
			var cell = row.insertCell(-1);
			cell.style.padding = "5px";
			cell.innerHTML = item;
			
			if(this.tbl.caption){this.tbl.removeChild(this.tbl.caption);}
			if(this.tbl.parentNode.offsetWidth>400) this.tbl.parentNode.style.width='400px';
			this.w = this.tbl.parentNode.offsetWidth;
			this.h = this.tbl.parentNode.offsetHeight;
		},

		GetItem: function(id){
			this.id = id;
			var param = 'id='+id+'&name=Tooltips&ajax=yes';
			var AjaxForm = new Ajax.Request(this.url,{
				method:'post',
				parameters: param,
				onComplete: this.showResponse.bindAsEventListener(this)
			});
		},
		
		GetNews: function(id){
			this.id = id;
			var param = 'id='+id+'&name=Tooltips&method=GetNews&ajax=yes';
			var AjaxForm = new Ajax.Request(this.url,{
				method:'post',
				parameters: param,
				onComplete: this.showResponse.bindAsEventListener(this)
			});
		},
		
		GetHtml: function(id){
			this.id = id;
			this.text = document.getElementById(id).innerHTML;
			if(!this.text) return;
			this.cash[this.id] = this.text;
			this.ParseItem(this.text);
			this.ShowTooltip();
			return 1;			
		},
		
		showResponse: function(originalRequest){
			this.text = originalRequest.responseText;
			if(!this.text) return;
			this.cash[this.id] = this.text;
			this.ParseItem(this.text);
			this.ShowTooltip();
			return 1;
		},

		getViewportScroll:function(){
			var scrollY = 0;
			if( document.documentElement && document.documentElement.scrollTop ){
				scrollY = document.documentElement.scrollTop;
			}else if( document.body && document.body.scrollTop ){
				scrollY = document.body.scrollTop;
			}else if( window.pageYOffset ){
				scrollY = window.pageYOffset;
			}else if( window.scrollY ){
				scrollY = window.scrollY;
			}
			var scrollX = 0;
			if( document.documentElement && document.documentElement.scrollLeft ){
				scrollX = document.documentElement.scrollLeft;
			}else if( document.body && document.body.scrollLeft ){
				scrollX = document.body.scrollLeft;
			}else if( window.pageXOffset ){
				scrollX = window.pageXOffset;
			}else if( window.scrollY ){
				scrollX = window.scrollX;
			}
			return [scrollX,scrollY];
		},	
		
		Show: function(obj, id, type){
			if(this.div.style.visibility == 'visible' && this.controlDiv==obj) return;
			else this.Hide();
			this.controlDiv = obj;
			if(!this.cash[id]){
				if(type==1)
					this.GetItem(id);
				else if(type==2)
					this.GetNews(id);
				else if(type==3)
					this.GetHtml(id);
			}else{
				this.GetFromCash(id);
			}
		},

		GetFromCash: function(id){
			this.text = this.cash[id];
			if(!this.text) return;
			this.ParseItem(this.text);
			this.ShowTooltip();
			return 1;
		},
		
		ShowTooltip: function(){
			this.mark = false;		
			var pos = Position.cumulativeOffset(this.controlDiv);
			pos["top"] = pos[1]; 
			pos["left"]=pos[0];
			pos["right"]  = pos[0] + this.controlDiv.offsetWidth;
			pos["bottom"] = pos[1] + this.controlDiv.offsetHeight;

			var div = this.div;// document.getElementById(this.menu_id+'_div');
			if(!div) return;

			document.onmousemove = this.CheckMove.bindAsEventListener(this);
			document.onclick = this.CheckClick.bindAsEventListener(this);
			
			this.x = pos["left"], this.y = pos["bottom"];
			var body = document.body;
			var scroll = this.getViewportScroll();
			
			if((document.body.clientWidth + scroll[0]) - (pos["left"] + this.w) < 0){
				if(pos["right"] - this.w >= 0 )	this.x = pos["right"] - this.w; else this.x = scroll[0]+10;
			}
			if((document.body.clientHeight + scroll[1]) - (pos["top"] + this.h) < 0){
				if(pos["top"] - this.h >= 0) this.y = pos["top"]-this.h;	else this.y = scroll[1];
				this.y -= 5;
			}else
				this.y += 5;
			
			div.style.left = this.x + 'px';
			div.style.top = this.y + 'px';
			div.style.width = '8px';
			div.style.height = '8px';

			this.tbl.style.display = 'none';
			
			div.style.visibility = 'visible';

			if(this.IsIE()){
				var frame = document.getElementById(this.menu_id+"_frame");
				if(frame){
					frame.style.left = div.style.left;
					frame.style.top = div.style.top;
					frame.style.width = '8px';
					frame.style.height = '8px';
					frame.style.display = 'block';
				}
			}
			/*shadow*/
			var img = document.getElementById(this.menu_id+'_shadow_div');
			if(!img.style.filter)
				img = document.getElementById(this.menu_id+'_shadow');
			/*<!-- for Mozilla*/
			img.style.left = -1000;
			img.style.top = -1000;
			/* -->*/
			img.style.width = this.w+'px';
			img.style.height = this.h+'px';
			img.style.left = this.x+5+'px';
			img.style.top = this.y+5+'px';
			img.style.visibility = 'visible';
			this.img = img;
			this.div = div;
			this.frame = document.getElementById(this.menu_id+"_frame");
			this.GrowingShow();
		},
		
		GrowingShow: function(){
			var old_w = parseInt(this.div.offsetWidth);
			var old_h = parseInt(this.div.offsetHeight);
			var wr = Math.round(this.w/10);
			var hr = Math.round(this.h/10);
			if(old_w+wr>=this.w && old_h+hr>=this.h){

				this.tbl.style.display = 'block';
				this.tbl.style.visibility = 'visible';

				this.div.style.width = this.w + 'px';
				this.div.style.height = this.h + 'px';
				this.img.style.width = this.w+'px';
				this.img.style.height = this.h+'px';
				if(this.IsIE()){
					if(this.frame){
						this.frame.style.width = this.w+'px';
						this.frame.style.height = this.h+'px';
					}
				}

				return;
			}
			if(old_w+wr<this.w){
				this.div.style.width = old_w+wr+'px';
				this.img.style.width = old_w+wr+'px';
			}
			if(old_h+hr<this.h){
				this.div.style.height = old_h+hr+'px';
				this.img.style.height = old_h+hr+'px';
			}
			if(this.IsIE()){
				var frame = document.getElementById(this.menu_id+"_frame");
				if(frame){
					if(old_w+wr<this.w)
						frame.style.width = old_w+wr+'px';
					if(old_h+hr<this.h)
						frame.style.height = old_h+hr+'px';
				}
			}
			this.gtimer = setTimeout(this.GrowingShow.bindAsEventListener(this),1);
		},

		Hide: function(){
			if(this.over==true){
				this.over=false;
				this.mark = false;
				return;
			}
			var frame = document.getElementById(this.menu_id+"_frame");
			if(frame) frame.style.display = 'none';

			var img = document.getElementById(this.menu_id+'_shadow_div');
			if(!img.style.filter)
				img = document.getElementById(this.menu_id+'_shadow');
			if(img){
				img.style.visibility = 'hidden';
				img.style.left = '-1000px';
				img.style.top = '-1000px';
			}
			var div = this.div;// document.getElementById(this.menu_id+'_div');
			if(div)	div.style.visibility = 'hidden';
			this.tbl.style.visibility = 'hidden';
			
			this.controlDiv = null;
			
			Event.stopObserving(document,'click',this.CheckClick);
			Event.stopObserving(document,'mousemove',this.CheckMove);
			clearTimeout(this.timer); this.timer=0;
			clearTimeout(this.gtimer); this.gtimer=0;
			this.mark = false;
		},
		
		Check: function(e){
			var div = this.div;// document.getElementById(this.menu_id+'_div');
			if(!div) return;
			if(div.style.visibility != 'visible')
			return;
			var scroll = this.getViewportScroll();
			var x = e.clientX + scroll[0];
			var y = e.clientY + scroll[1];
			/*menu region*/
			var posLeft = parseInt(div.style.left);
			var posTop = parseInt(div.style.top);
			var posRight = posLeft + div.offsetWidth;
			var posBottom = posTop + div.offsetHeight;
			if(x >= posLeft && x <= posRight && y >= posTop && y <= posBottom){
				this.over = true;
				return false;
			}
			if(this.controlDiv){
				var obj = this.controlDiv;
				var pos = Position.cumulativeOffset(this.controlDiv);
				if(x >= pos[0] && x <= pos[0]+obj.offsetWidth && y >= pos[1] && y <= pos[1]+obj.offsetHeight){
					this.over = true;
					return false; 
				}
			}
			this.over = false;
			return true;
		},
		
		CheckClick: function(e){
			if(!this.Check(e)) return;
			this.Hide();
		},

		CheckMove: function(e){
			if(!this.Check(e)) return;
			if(!this.mark){
				this.mark = true;
				this.timer=setTimeout(this.Hide.bindAsEventListener(this), 300);
			}
		}
	}	