﻿//NOTE: HTML must have doctype HTML 4.01 Transitional
var ie6, ie7, ie8, ff, opera, safari, ie
ie6 = ie7 = ie8 = ff = opera = safari = ie = false;
var vertoffset = 10;
var horoffset = 50;
var overdiv = false;

function ShowPopup(html){
	//html = FilterString(html);
	var div = document.getElementById("popupdiv");
	if(div != null){
		div.innerHTML = html;
	}
	
	CaptureMouse();
	//PositionPopup();
	if(div != null){		
		//div.style.visibility = "visible";
		div.style.display = "block";	
	}
}

function HidePopup(){
	ReleaseMouse();
	var div = document.getElementById("popupdiv");
	if(div != null){
		//div.style.visibility = "hidden";
		div.style.display = "none";
	}
}

function DetectBrowser(){
	var agent = navigator.userAgent;	
	if (agent.indexOf('MSIE 6')>0) ie6=true;
	else if (agent.indexOf('MSIE 7')>0) ie7=true;
	else if (agent.indexOf('MSIE 8')>0) ie8=true;
	else if (agent.indexOf('Firefox')>0) ff=true;
	else if (agent.indexOf('Opera')>=0) opera=true;
	else ff = true;
	
	ie = ie6 || ie7 || ie8;
}

function GetViewport(){
	var width;
	var height;
	var viewport = {x:0,y:0,width:0,height:0};
	
	// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	if (typeof window.innerWidth != 'undefined')
	{
	  viewport.width = window.innerWidth;
	  viewport.height = window.innerHeight;
	}
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0)
	{
	   viewport.width = document.documentElement.clientWidth,
	   viewport.height = document.documentElement.clientHeight
	}
	// older versions of IE
	else
	{
	   viewport.width = document.getElementsByTagName('body')[0].clientWidth,
	   viewport.height = document.getElementsByTagName('body')[0].clientHeight
	}
	return viewport;
}

function CaptureMouse(){
	DetectBrowser();
	
	if(ff){				
		document.captureEvents(Event.MOUSEMOVE);	
	}	
	document.onmousemove = mouseMove;
}

function ReleaseMouse(){
	DetectBrowser();
	
	if(ff){				
		document.releaseEvents(Event.MOUSEMOVE);	
	}	
	document.onmousemove = null;
}

function mouseMove(e){
	var x, y, sx, sy;		

	if(ie8){
		x = event.x;
		y = event.y;		
	}
	else if(ie6 || ie7){
		sx = document.documentElement.scrollLeft;
		sy = document.documentElement.scrollTop;
		x = event.x + sx;
		y = event.y + sy;		
	}
	else if(opera){
		sx = document.documentElement.scrollLeft;
		sy = document.documentElement.scrollTop;	
		x = e.x +sx;
		y = e.y +sy;
	}
	else if(ff){		
		x = e.pageX;
		y = e.pageY;
	}		
	PositionPopup(x, y);
}

function PositionPopup(x, y){
	var view = GetViewport();	
	var div = document.getElementById("popupdiv");		

	if(div != null){
		var width = div.style.width;
		width = parseInt(width.substring(0, width.length - 2));
		var height = div.offsetHeight;//div.style.height;
		//height = parseInt(height.substring(0, height.length - 2));
		
		var scrollY;
		if(ie)		
			scrollY = document.documentElement.scrollTop;//document.body.scrollTop;						
		else
			scrollY = window.pageYOffset;
		
		//document.defaultView.getComputedStyle(document.getElementById("popupdiv"),'').getPropertyValue('height'));
		//var p = document.getElementById("popupdiv");		
		//document.getElementById("info").innerHTML = p.offsetHeight; //y + ", " + height + ", " + view.height + ", " + scrollY;
		//p.innerHTML += "<br>" + "height:" + p.offsetHeight;
		//window.status = p.offsetHeight+ ", " + y + ", " + height + ", " + view.height + ", " + scrollY;
		if((y + height) > (view.height + scrollY))
			y -= ((y + height) - view.height - scrollY);
		
		y += vertoffset;
		x += horoffset;
		//div.innerHTML = "view.width = " + view.width + ", view.height = " + view.height + "<br>view.x = " + view.x + ", view.y = " + view.y + "<br>" +
		//"x = " + x + ", y = " + y + "<br>bottom = " + (y + height) + "<br>scrolly = " + scrollY;
		//if(ie || opera){
			div.style.top = y + "px";
			div.style.left = x + "px";
		/*}
		else if(ff){			
			div.style.top = y + "px";
			div.style.left = x + "px";			
		}*/
	}
}

function popup2(text,texttitle) {	
var inputtext;

if (text !=''){
	inputtext = text.replace("'","\'");
}else{
	inputtext = 'Sorry, no description available';
}
return ' <body leftmargin="0" rightmargin="0" topmargin="0" bottommargin="0" bgcolor="#000000">' +
'<table width="368" border="2" cellpadding="0" cellspacing="0" bordercolor="#FFFFFF">' +
  '<tr>' +
    '<td><div align="center">' +
      '<table width="368" border="0" cellpadding="0" cellspacing="0" bgcolor="#000000">' +
      ' <tr>' +
          '<td width="12" height="37" background="images/popup_top.gif">&nbsp;</td>' +
         ' <td width="344" height="37" valign="middle" background="images/popup_top.gif"><span class="Popup_title_top">'+ texttitle +'</span></td>' +
         ' <td width="12" height="37" background="images/popup_top.gif">&nbsp;</td>' +
       ' </tr>' +
       ' <tr>' +
        '  <td width="12" height="20" background="images/popup_left01.gif">&nbsp;</td>' +
         ' <td width="344" height="20">&nbsp;</td>' +
         ' <td width="12" height="20">&nbsp;</td>' +
       ' </tr>' +
        '<tr>' +
         ' <td width="12" background="images/popup_left.gif">&nbsp;</td>' +
         ' <td width="344" align="left"><span class="Popup_content">N = Sedie numerate<br />' +
           ' T = Schermo TV<br />' +
           ' C = Tribune Coperte</span></td>' +
          '<td width="12">&nbsp;</td>' +
        '</tr>' +
        '<tr>' +
         ' <td width="12" height="31" background="images/popup_left02.gif">&nbsp;</td>' +
         ' <td width="344" height="31" background="images/popup_stripe.gif">&nbsp;</td>' +
         ' <td width="12" height="31">&nbsp;</td>' +
        '</tr>' +
        '<tr>' +
         ' <td width="12" background="images/popup_left.gif">&nbsp;</td>' +
         ' <td width="344" class="Popup_content">' + inputtext + '</td>' +
       ' <td width="12">&nbsp;</td>' +
        '</tr>' +
        '<tr>' +
         ' <td width="12" height="17" background="images/popup_left03.gif">&nbsp;</td>' +
         ' <td width="344" height="17">&nbsp;</td>' +
         ' <td width="12" height="17">&nbsp;</td>' +
        '</tr>' +
      '</table>' +
    '</div></td>' +
  '</tr>' +
'</table></body>' ;

}