/*
	tipo = 'down','up','right',left  cambia la posizione della punta

*/



function showTooltip(obj,id,pos,eventoChiusura,opaco,showGifChiusura) {
	if (typeof(showGifChiusura)=='undefined') var showGifChiusura=0;
	if (opaco && opaco==true) opaco=true; else opaco=false;
	
  open_tooltip(obj,cmsTooltip_testo[id],pos,cmsTooltip_titolo[id] ? cmsTooltip_titolo[id]:'',showGifChiusura,eventoChiusura,opaco,id);

	return false;
}

function open_tooltip(obj,messaggio,tipo,titolo,closeIcon,eventoChiusura,opaco,id) {
	if (typeof(id)=='undefined') var id='x';
	
	if (obj.name=='') obj.setAttribute('name',"T"+id);
	
  if ($('oTable')) $('oTable').onclick(); // Chiudo tooltip precedentemente aperto
	
	if (opaco && opaco==true) opc='opc_'; else opc='';
	
	if (!titolo && !messaggio) return false;
	
	var idTool = obj.name+'_tooltip';
	
	if ($(idTool)) return false;
	
	
	if (closeIcon) imgCloseIcon='<img src="/js/tooltip/img/x.jpg" style="float:right;position:relative; right:-8px;top:-2px;cursor: hand;cursor: pointer;">'; else imgCloseIcon='';
	
	if (titolo!='') titolo=imgCloseIcon+'<strong>'+titolo+'</strong><br/>'; else if(imgCloseIcon) titolo=imgCloseIcon; 
	
	
	if (navigator.userAgent.indexOf('MSIE 6') >= 0) {
		filterPng = true;
	} else {
		filterPng = false;
	}
		
	imgPath = '/js/tooltip/img/'+opc;
	spcPath = '/js/tooltip/img/space.gif';
	
	var tool   = document.createElement("div");

	tool.setAttribute('id',idTool);
	//tool.setAttribute('onmouseout',"close_tooltip_byname('"+idTool+"')");
	
  

	
	//tool.setAttribute('style','display:block;');
   
  
  var oTable="<table id=\"oTable\" name=\"oTable\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"  onclick=\"close_tooltip_byname('"+obj.name+"_tooltip')\"  >";
 	oTable+='<tr>';
	oTable+='<td></td>';
  if (tipo=='down') {
		if (filterPng) {
			oTable+='<td align="right"><img src="'+spcPath+'" width="42" height="40" style="position:relative;top:+5px; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+imgPath+'up.png\', sizingMethod=\'scale\');"></td>';	
		}	else	{
			oTable+='<td align="right"><img id="'+idTool+'FrecciaUp" name="'+idTool+'FrecciaUp" src="'+imgPath+'up.png" style="position:relative;top:+5px;" width="42" height="40" ></td>'; 
		}
	}	else {
		oTable+='<td></td>';
	}
	oTable+='<td></td>';
	oTable+='</tr>';
  
  oTable+='<tr>';
  if (filterPng) {
			oTable+='<td><img src="'+spcPath+'" width="20" height="13" style="position:relative; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+imgPath+'back_01.png\', sizingMethod=\'scale\');"></td>';
			oTable+='<td style="filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+imgPath+'back_02.png\', sizingMethod=\'scale\');"></td>';
			oTable+='<td><img src="'+spcPath+'" width="21" height="13" style="position:relative; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+imgPath+'back_03.png\', sizingMethod=\'scale\');"></td>';
			oTable+='</tr>';
		
			            oTable+='<tr>';
			            oTable+='<td style="filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+imgPath+'back_04.png\', sizingMethod=\'scale\');"></td>';
			            oTable+='<td class="il_tooltip" style="filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+imgPath+'back_05.png\', sizingMethod=\'scale\');"">'+titolo+messaggio+'<div style="clear:both"></div></td>';
									oTable+='<td style="filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+imgPath+'back_06.png\', sizingMethod=\'scale\');"></td>';
									oTable+='</tr>';
		
			oTable+='<tr>';
			oTable+='<td><img src="'+spcPath+'" width="20" height="13" style="position:relative; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+imgPath+'back_07.png\', sizingMethod=\'scale\');"></td>';
			oTable+='<td style="filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+imgPath+'back_08.png\', sizingMethod=\'scale\');"></td>';
			oTable+='<td><img src="'+spcPath+'" width="21" height="13" style="position:relative; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+imgPath+'back_09.png\', sizingMethod=\'scale\');"></td>';
			oTable+='</tr>';
	}	else {
			oTable+='<td><img   src="'+imgPath+'back_01.png" width="20" height="13" ></td>';
			oTable+='<td background="'+imgPath+'back_02.png" ></td>';
			oTable+='<td><img   src="'+imgPath+'back_03.png" width="21" height="13"></td>';
			oTable+='</tr>';
		
			oTable+='<tr>';
			oTable+='<td background="'+imgPath+'back_04.png" ></td>';
			oTable+='<td background="'+imgPath+'back_05.png" class="il_tooltip">'+titolo+messaggio+'</td>';
			oTable+='<td background="'+imgPath+'back_06.png" ></td>';
			oTable+='</tr>';
		
			oTable+='<tr>';
			oTable+='<td><img    src="'+imgPath+'back_07.png" width="20" height="13"></td>';
			oTable+='<td background ="'+imgPath+'back_08.png" ></td>';
			oTable+='<td><img    src="'+imgPath+'back_09.png" width="21" height="13"></td>';
			oTable+='</tr>';
	}
	

	oTable+='<tr>';
	oTable+='<td></td>';
  if (tipo=='up') {
		if (filterPng) {
			oTable+='<td align="right"><img src="'+spcPath+'" width="42" height="40"  style="position:relative;top:-6px; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+imgPath+'down.png\', sizingMethod=\'scale\');" width="42" height="40"></td>'; 
		}	else {
		  oTable+='<td align="right"><img id="'+idTool+'FrecciaDown" name="'+idTool+'FrecciaDown" src="'+imgPath+'down.png" style="position:relative;top:-6px;" width="42" height="40"></td>'; 
		}
	}	else if (tipo=='left') {
		if (filterPng){
	     oTable+='<td align="right"><img src="'+spcPath+'" width="63" height="34"  style="position:relative;top:-6px;left:23px; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+imgPath+'right.png\', sizingMethod=\'scale\');" width="63" height="34"></td>'; 
		}	else {
		   oTable+='<td align="right"><img id="'+idTool+'FrecciaRight" name="'+idTool+'FrecciaRight" src="'+imgPath+'right.png" style="position:relative;top:-6px; left: 23px" width="63" height="34"></td>'; 
		}
	} else if (tipo=='right') {
		if (filterPng){
	     oTable+='<td align="left"><img src="'+spcPath+'" width="63" height="34"  style="position:relative;top:-6px;left:23px; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+imgPath+'right.png\', sizingMethod=\'scale\');" width="63" height="34"></td>'; 
		}	else {
			 
		   oTable+='<td align="left"><img id="'+idTool+'FrecciaLeft" name="'+idTool+'FrecciaLeft" src="'+imgPath+'left.png" style="position:relative;top:-6px; left: -23px" width="63" height="34"></td>'; 
		}
	}
  else oTable+='<td></td>';
	oTable+='<td></td>';
	oTable+='</tr>'; 
	oTable+='</table>';

  tool.innerHTML=oTable;

	document.body.appendChild(tool);
	
 
  
  refreshTooltip(obj,tipo);
  
  window.onresize = function () {
		refreshTooltip(obj,tipo);
	}
 
  refreshTooltip(obj,tipo);

	if (eventoChiusura && eventoChiusura !='' && eventoChiusura) obj.setAttribute(eventoChiusura,"return close_tooltip_byname('"+idTool+"')");

}

function close_tooltip(obj) {
	var idTool = obj.name+'_tooltip';
	if ($(idTool))	document.body.removeChild($(idTool));
}

function close_tooltip_byname(name) {
	if ($(name))	document.body.removeChild($(name));
	return false;
}

function findPosX(obj) {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
}

function findPosY(obj) {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
 }
 
 
function refreshTooltip(obj,tipo) {
	
	var idTool = obj.name+'_tooltip';
	if (!$(idTool)) return false;
	tool = $(idTool);
  
  
  
  tool.style.position='absolute';
  tool.style.zIndex="20";
  
  var H = tool.offsetHeight; //if (H<100) H=104;
  var W = tool.offsetWidth;
	//alert(findPosX(obj));
	//alert(H+'*'+W);
  
  switch (tipo) {
  	case 'up':
  	  PosY = (findPosY(obj)-H+4);
 	    PosX = (findPosX(obj)-W+obj.offsetWidth);
 	    
 	    if (PosX<0) {
      	if ($(idTool+'FrecciaDown')) $(idTool+'FrecciaDown').style.left = 	(PosX-obj.offsetWidth/2)+"px";
      	PosX=10;
      }
 	    
  	  tool.style.top =(PosY)+'px';
 		  tool.style.left=(PosX)+'px';
 		  break;
 	  case 'down':
 	    PosY = (findPosY(obj)+obj.offsetHeight-4);
 	    PosX = (findPosX(obj)-W+obj.offsetWidth);
 	    
 	    if (PosX<0) {
      	if ($(idTool+'FrecciaUp')) $(idTool+'FrecciaUp').style.left = 	(PosX-obj.offsetWidth/2)+"px";
      	PosX=10;
      }
 	    
  	  tool.style.top =(PosY)+'px';
 		  tool.style.left=(PosX)+'px';
 		  break;	  
	 case 'left':
 	    PosY = findPosY(obj)-H+obj.offsetHeight;
 	    PosX = findPosX(obj)-W-4;
			
      if (PosX<0) {
      	//alert($('freccia').style.left);
      	if ($(idTool+'FrecciaRight')) $(idTool+'FrecciaRight').style.left = 	(PosX+13)+"px";
      	PosX=10;
      }
			
  	  tool.style.top =(PosY)+'px';
 		  tool.style.left=(PosX)+'px';
 		  break;	
 	 case 'right':
 	    PosY = findPosY(obj)-H+obj.offsetHeight;
 	    PosX = findPosX(obj)+obj.offsetWidth+4;
 	    
 	   
 	    
  	  tool.style.top =(PosY)+'px';
 		  tool.style.left=(PosX)+'px';
 		  break;	
  }
 	
  
 	 
}



