var mouseX;
var mouseY;
var fixedLayers = new Array();
var isOver=null;
var popupMargin=10;
var zindex=null;
var popUpName=null;
var popUpNum=null;
// Detect if the browser is IE or not.
// If it is not IE, we assume that the browser is NS.
//var IE = document.all?true:false

// If NS -- that is, !IE -- then set up for mouse capture
//if (!IE) document.captureEvents(Event.MOUSEMOVE)

// Set-up to use getMouseXY function onMouseMove
document.onmousemove = getMouseXY;

// Temporary variables to hold mouse x-y pos.s
var tempX = 0
var tempY = 0

// Main function to retrieve mouse x-y pos.s

function getMouseXY(e) {
  if (IE) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.documentElement.scrollLeft
    tempY = event.clientY + document.documentElement.scrollTop
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX + document.documentElement.scrollLeft
    tempY = e.pageY + document.documentElement.scrollTop
  }  
  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}  
  // show the position values in the form named Show
  // in the text fields named MouseX and MouseY
  mouseX=tempX;
  mouseY=tempY;
  //seguir();
  return true
}
function seguir(y){
	if(isOver!=null){
		var navegador = browser_detect();
		if(navegador == "Netscape Navigator"){
			var y = 0;//window.scrollY;
			var myX=mouseX+popupMargin
			var myY=mouseY+popupMargin+y+5;
		}else{
			if(navegador == "Internet Explorer"){
				var y = document.body.scrollTop;
				var myX=mouseX+popupMargin
				var myY=mouseY+popupMargin+y+5;
			}
		}
		document.getElementById(isOver).style.left=myX+"px";
		document.getElementById(isOver).style.top=myY+"px";
		setTimeout('seguir()',100);
	}
}

function isFixed(id){
	var fix=false;
	for(var i=0;i<fixedLayers.length;i++){
		if(id==fixedLayers[i]){
			fix=true;
		}
	}
	return fix;
}

function showPopup(id) {
	
	var navegador = browser_detect();
	if(navegador == "Netscape Navigator"){
		var y = 0;//window.scrollY;
		var myX=mouseX+popupMargin
		var myY=mouseY+popupMargin+y+5;
	}else{
		if(navegador == "Internet Explorer"){
			var y = document.body.scrollTop;
			var myX=mouseX+popupMargin
			var myY=mouseY+popupMargin+y+5;
		}
	}

	//alert(myY);
	
	if(!isFixed(id)){
		document.getElementById(id).style.left=myX+"px";
		document.getElementById(id).style.top=myY+"px";
		if(isOver!=id){
			document.getElementById(id).style.zIndex = zindex;
			zindex++;
			document.getElementById(id).style.visibility = "visible";
		}
		isOver=id;
		seguir(y);
	}

}

function hidePopup(id) {
	if(!isFixed(id)){
		isOver=null;
		document.getElementById(id).style.visibility = "hidden";
	}
}

function fixPopup(id){
	if(!isFixed(id)){
		showPopup(id);
		isOver=null;
		fixedLayers.push(id);
	}
	
}

function unFixPopup(id){
	for(var i=0;i<fixedLayers.length;i++){
		if(id==fixedLayers[i]){
			fixedLayers[i]=null;
		}
	}
	resetFixedLayers();
	hidePopup(id);
}

function resetFixedLayers(allLayers){
	if(allLayers!=undefined && allLayers==true){
		fixedLayers=new Array();
	}else{
		var res=false;
		for(var i=0;i<fixedLayers.length;i++){
			if(null!=fixedLayers[i]){
				res=true;
			}
		}
		if(!res){
			fixedLayers=new Array();
		}
	}
}


function iniPopups(popupname,num){
	popUpName=popupname;
	if(popUpName!=null){
		popUpNum=num;
		zindex==null;
		for(var i=1;i<num+1;i++){	
			document.getElementById(popupname+i).style.left="0px";
			document.getElementById(popupname+i).style.top="0px";
			document.getElementById(popupname+i).style.visibility="hidden";
		}
		if(zindex==null){
			zindex=num+1;
		}
		resetFixedLayers(true);
		isOver=null;
	}
}

document.onkeypress = keyhandler;
// If it is not IE, we assume that the browser is NS.
var IE = document.all?true:false
function keyhandler(e) {
    if (!IE)
        Key = e.which;
    else
        Key = window.event.keyCode;
    if (Key != 0){
        //alert("Key pressed! ASCII-value: " + Key);
		if(Key==45){
			zoomOut();
		}else if(Key==43){
			zoomIn();
		}
	}
}

//////// BROWSER DETECT: detecta el navegador i sistema operatiu
function browser_detect(){
	var detect = navigator.userAgent.toLowerCase();
	var OS,browser,version,total,thestring;

	if (checkIt('konqueror'))
	{
		browser = "Konqueror";
		OS = "Linux";
	}
	else if (checkIt('safari')) browser = "Safari"
	else if (checkIt('omniweb')) browser = "OmniWeb"
	else if (checkIt('opera')) browser = "Opera"
	else if (checkIt('webtv')) browser = "WebTV";
	else if (checkIt('icab')) browser = "iCab"
	else if (checkIt('msie')) browser = "Internet Explorer"
	else if (!checkIt('compatible'))
	{
		browser = "Netscape Navigator"
		version = detect.charAt(8);
		}
		else browser = "desconegut";

	// lo seguent detecta el sistema operatiu
	if (!version) version = detect.charAt(place + thestring.length);

	if (!OS)
	{
		if (checkIt('linux')) OS = "Linux";
		else if (checkIt('x11')) OS = "Unix";
		else if (checkIt('mac')) OS = "Mac"
		else if (checkIt('win')) OS = "Windows"
		else OS = "an unknown operating system";
	}

	return(browser)

function checkIt(string)
{
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}
}

//  FINAL BROWSER DETECT