var mevent;
var mposi;

function doStTooltip(e, msg, posi)
{                
	if ( typeof stTooltip == "undefined" || !stTooltip.ready ) return;
	if( typeof posi == "undefined" ) posi = 'left';
	var new_msg = st_template;
	var pos = st_template.indexOf("[[<content>]]");
	mevent = e;
	mposi = posi;
    msg = '<img onload="stTooltip.positionTip(mevent, mposi)" src=' + msg + '>'; 
	if(pos != -1) {
		new_msg = st_template.substring(0, pos) + msg + st_template.substr(pos + 13, st_template.length - 13 - pos);
	}
	stTooltip.show(e, new_msg, posi);
}

//Скрывает всплывающее окно
function hideStTip()
{
	if ( typeof stTooltip == "undefined" || !stTooltip.ready ) return;
	stTooltip.hide();
}

var stTooltip = 
{
	tipID: "st_tipDiv",
	    
	ready:false, timer:null, tip:null, 
	visible: false,
	  
	//Инициализируем окно
	init: function(time2show, time2autoclose, ofX, ofY) {
	  if ( document.createElement && document.body && !document.getElementById(this.tipID)) {
                    var el = document.createElement("DIV");
	    el.id = this.tipID;
	    el.className = "st_tipDiv"; 
	    document.body.appendChild(el);
	    this.ready = true;
	    this.showDelay = time2show;
	    this.hideDelay = time2autoclose;
	    this.offX = ofX;
	    this.offY = ofY;
	  }
	},
	    
	//Отображаем окно
	show: function(e, msg, pos) 
	{
		if (this.timer) 
		{ 
			clearTimeout(this.timer);	
			this.timer = 0; 
		}
		this.tip = document.getElementById(this.tipID);
		this.writeTip("");  // сначала очищаем строку
		this.writeTip(msg);
		
		viewport.getAll();
		this.positionTip(e, pos);
		//Задаем период обновления окна
		this.timer = setTimeout("stTooltip.toggleVis('" + this.tipID + "', 'visible')", this.showDelay);
		this.visible = true;
	},
	    
	//Записываем строку
	writeTip: function(msg) 
	{
		if (this.tip && typeof this.tip.innerHTML != "undefined") {
//alert(msg);
		  this.tip.innerHTML = msg;		  
		}
//alert(this.tip.innerHTML);
	},
	    
	//Вычисляем позицию окна
	positionTip: function(e, pos) {
	  if( this.tip && this.tip.style ) {
	    var x = e.pageX ? e.pageX: e.clientX + viewport.scrollX; // позиция курсора по горизонтали
		 var y = e.pageY ? e.pageY: e.clientY + viewport.scrollY; //             ... по вертикали
		 //Смещение по x
		 //ширина объекта относительно родительского объекта(260) + 230 
		 //if ( x + this.tip.offsetWidth + this.offX > viewport.width + viewport.scrollX ) {
		 //  x = x - this.tip.offsetWidth - this.offX;
		//	if ( x < 0 ) {
		//	  x = 0;
		//	}
		// } else {
		//	x = x + this.offX;
		// }

                // var2 x = viewport.width - this.tip.offsetWidth - st_offX;
                x = 0; //viewport.width - this.tip.offsetWidth;

		 //Смещение по y
		// if ( y + this.tip.offsetHeight + this.offY > viewport.height + viewport.scrollY ) {
		//   y = y - this.tip.offsetHeight - this.offY;
		//	if (y < viewport.scrollY) { 
		//	  y = viewport.height + viewport.scrollY - this.tip.offsetHeight;
		//	}
		// } else { 
		//   y = y + this.offY;
		// }
	        y= viewport.scrollY;

		 //Заносим результаты в стиль элемента
		 if(pos == 'right')
		 {		 	 
			 this.tip.style.left = viewport.width - this.tip.offsetWidth + "px";
		 }
	 	 else
	 	 {		 	 
			 this.tip.style.left = x + "px";		 			 	
		 } 
		 this.tip.style.top = y + "px";
	  }
	},
	    
	//Скрывает окно
	hide: function()
	{
		if (this.timer) 
		{ 
			clearTimeout(this.timer);	
			this.timer = 0; 
		}
		
		this.timer = setTimeout("stTooltip.toggleVis('" + this.tipID + "', 'hidden')", this.hideDelay);
		this.tip = null; 
		this.visible = false;
	},
	
	//Обновляет окно
	toggleVis: function(id, vis) 
	{
		var el = document.getElementById(id);
		if (el) {
		  el.style.visibility = vis;
		}
	},
	    
	//Обработчик мыши
	trackMouse: function(e) 
	{
		if(this.visible)
		{
			e = dw_event.DOMit(e);
			stTooltip.positionTip(e);
		}
	}
    
}

