
var mapMuni = false;


// gets screen width
function get_window_width(value) {
  
  if( typeof( window.innerWidth ) == 'number' ) {
	  return window.innerWidth;
	} else if( document.documentElement && document.documentElement.clientWidth ) {
	  return document.documentElement.clientWidth;
	} else if( document.body && document.body.clientWidth ) {
	  return document.body.clientWidth;
  }
  return value;

}

 
// gets screen height
function get_window_height(value) {
  
  if( typeof( window.innerHeight ) == 'number' ) {
	  return window.innerHeight;
	} else if( document.documentElement && document.documentElement.clientHeight ) {
	  return document.documentElement.clientHeight;
	} else if( document.body && document.body.clientHeight ) {
	  return document.body.clientHeight;
  }
  return value;

}

 function _get_screen_parameters() {
 	var ratio = 1.50;
  var securityMargin = 0;
  var lowResolution = "La seva pantalla no té prou resolució; és possible que alguns elements no es visualitzin correctament. Canvïi a una resolució 1024 x 768 o superior.";		
 	var availHeight, availWidth;
	
	availWidth = self.screen.availWidth;
	availHeight = self.screen.availHeight;
	
	//real maximized window size
	if (self.innerWidth) {
		availWidth = availWidth + self.innerWidth - self.outerWidth;
		availHeight = availHeight + self.innerHeight - self.outerHeight;
	}
	//as two last lines don't work in IE, we make an approach
  else {
	  availHeight = availHeight - 150;
  }
	
	if(availWidth < availHeight * ratio) { //width is the limitant
		if(availWidth < minWidth - securityMargin) {
			alert (lowResolution);
			width = minWidth - securityMargin;
		} else if (availWidth < maxWidth) {
			width = availWidth - securityMargin;
		} else {
			width = maxWidth;
		}
	}

	else { //height is the limitant
		if(availHeight < (minHeight - securityMargin/ratio)) {
			alert (lowResolution);
			width = minHeight*ratio - securityMargin/ratio;
		} else if (availHeight < maxHeight + (securityMargin/ratio)) {
			width = availHeight*ratio - (securityMargin/ratio);
		} else {
			width = maxHeight*ratio;
		}
	}
			
	var wh = new Array  
	wh[0]= width;
	wh[1]= width/ratio; //height
	return wh;

}

function _centerweb(mode, wWindow) {
	switch(mode) {
		case "full":
		  return 0;	  
		break;
		case "fixed_screen":
		  return parseInt((self.screen.availWidth - wWindow) / 2);
		break;
		case "fixed_height":
		  return parseInt((get_window_width() - wWindow) / 2);
	  break;
	  default:
	    return 0;
  }
}

function _calculate(mode, minWidth, maxWidth, minHeight, maxHeight) {
 	
switch(mode) {
	case "full":
	  wWindow = get_window_width();
	  hWindow = get_window_height();
	  //if (!correction) correction = 28;  
	  //hWindow += ( 2 * hGap - correction);
	  //wWindow += ( 3 * wGap - correction);	  
	break;
	case "fixed_screen":
	  var arraySizes = _get_screen_parameters();
	  wWindow = arraySizes[0];
	  hWindow = arraySizes[1];
	break;
	case "fixed_height":

	  lowResolution = "La seva pantalla no té prou resolució; és possible que alguns elements no es visualitzin correctament. Canvïi a una resolució 1024 x 768 o superior.";		
	  wWindow = get_window_width();
	  hWindow = get_window_height();
	  
	  //if (!correction) correction = 28;  
	  //hWindow += ( 2 * hGap - correction);
	  //wWindow += ( 3 * wGap - correction);
	  // calculate page metrics
	  if (wWindow > maxWidth) wWindow = maxWidth;
	  if (wWindow < minWidth) wWindow = minWidth;
    if (hWindow > maxHeight) hWindow = maxHeight;
    if (hWindow < minHeight) hWindow = minHeight;
   break;

default:
alert("mode '" + mode + "' no implementat a la interfície");
}
	var wh = new Array  
	wh[0]= wWindow;
	wh[1]= hWindow;
	return wh;
}


function loadMap(mapName){

	var oMapPage = document.getElementById('map');
	if (oMapPage){
		oMapPage.src = serverURL+viewerPath+"/map.php?wmc="+appPath+"/xml/"+mapName+".xml&lang=ca&w="+wMapIntern+"&h="+hMapIntern+"&rnd="+Math.random();
		curMap = mapName;
		mapMuni = !(mapName.substring(0,4)== "zona");
		setTdMunisVisible(mapMuni);
		var oToolPage = document.getElementById('tool');
		if (oToolPage) {
			var oDoc = (oToolPage.contentWindow || oToolPage.contentDocument);
			oDoc.curProfile = (mapMuni) ? "topo" : "orto";
			oDoc.loadImages();
		}
	}
}

function setTdMunisVisible(value){
	var oToolBarPage = document.getElementById('toolbar');
	var oDoc = (oToolBarPage.contentWindow || oToolBarPage.contentDocument);
  if (oDoc.document) oDoc = oDoc.document;
	var oTd = oDoc.getElementById('tdMunis');
	oTd.style.display = (value) ? "" : "none" ;
	var oTd = oDoc.getElementById('tdSlider');
	oTd.style.display = (value) ? "none" : "" ;
	if (!value){
		oToolBarPage.contentWindow.slider.setValue(50);
	}
	
	
}



function showInfoPage(urlInfo){
	var oInfoPage = document.getElementById('info');
	if (oInfoPage){
		oInfoPage.src = serverURL+"/"+urlInfo;
	}	
}



function download(fileName){
	var oInfoPage = document.getElementById('info');
	if (oInfoPage){
		oInfoPage.src = serverURL+"/simuc/download.php?f="+fileName;
	}	
}

function roundedFrame(ttitle,ttop,tleft,twidth,theight,tname,tid,tsrc, bgColor)
{
	var result = "";
	
	var htcorner = 10;
	var hbcorner = 10;
	var wcorner = 10;
	
	result += "<table border='0' id='table_"+tname+"' cellspacing='0' cellpadding='0' style='position: absolute; margin-top: "+ttop+"px; margin-left: "+tleft+"px' >";

	result += "<tr>";
	result += "<td class='tl' width='"+wcorner+"px' height='"+htcorner+"px'></td>";
	result += "<td class='tc' >"+ttitle+"</td>";
	result += "<td class='tr' width='"+wcorner+"px' height='"+htcorner+"px' ></td>";
	result += "</tr>";

	result += "<tr>";
	result += "<td class='ll' width='"+wcorner+"px' ></td>";
	result += "<td><iframe id='"+tname+"' name='"+tname+"' style='border: 2px solid #F5FAFD;  background-color:"+bgColor+" ' width='"+(twidth-2*wcorner)+"px' height='"+(theight-(htcorner+hbcorner))+"px' src='"+tsrc+"' frameborder='0' scrolling='auto' allowtransparency='true'></iframe></td>";
	result += "<td class='lr' width='"+wcorner+"px' ></td>";
	result += "</tr>";

	result += "<tr>";
	result += "<td class='bl' width='"+wcorner+"px' height='"+hbcorner+"px'></td>";
	result += "<td class='bc'></td>";
	result += "<td width='"+wcorner+"px' class='br'></td>"
	result += "</tr>";

	result += "</table>";
	
	return result;
}

function roundedFrameNoTitle(ttop,tleft,twidth,theight,tname,tid,tsrc, bgColor)
{
	var result = "";
	
	var htcorner = 10;
	var hbcorner = 10;
	var wcorner = 10;
	var w=10;
	var h= 10;
	
	result += "<table border='0' id='table_"+tname+"' cellspacing='0' cellpadding='0' style='position: absolute; margin-top: "+ttop+"px; margin-left: "+tleft+"px' >";

	result += "<tr>";
	result += "<td class='mtls' width='"+wcorner+"px' height='"+htcorner+"px'></td>";
	result += "<td class='mtc' width='"+(twidth - 2*w)+"'></td>";
	result += "<td class='mtrs' width='"+wcorner+"px' height='"+htcorner+"px' ></td>";
	result += "</tr>";

	result += "<tr>";
	result += "<td class='ll' width='"+wcorner+"px' ></td>";
	result += "<td><iframe id='"+tname+"' name='"+tname+"' style='border: 2px solid #F5FAFD;  background-color:"+bgColor+" ' width='"+(twidth-2*wcorner)+"px' height='"+(theight-(htcorner+hbcorner))+"px' src='"+tsrc+"' frameborder='0' scrolling='auto' allowtransparency='true'></iframe></td>";
	result += "<td class='lr' width='"+wcorner+"px' ></td>";
	result += "</tr>";

	result += "<tr>";
	result += "<td class='bl' width='"+wcorner+"px' height='"+hbcorner+"px'></td>";
	result += "<td class='bc'></td>";
	result += "<td width='"+wcorner+"px' class='br'></td>"
	result += "</tr>";

	result += "</table>";
	
	return result;
}

function roundedMap(ttitle,ttop,tleft,twidth,theight,tname,tid,tsrc, bgColor)
{
	var result = "";
	
	var htcorner = 40;
	var hbcorner = 12;
	var wcorner = 10;
	var hline = 3;
	var bHeight = 0;
	var xWidth = 6;
	
	result += "<table id='table_"+tname+"' border='0' cellspacing='0' cellpadding='0' style='position: absolute; width:"+(twidth+xWidth)+"px; height:"+theight+"px; margin-top: "+ttop+"px; margin-left: "+tleft+"px' >";
	
	if (ttitle) {
	  
	  result += "<tr>";
	  result += "<td class='mtl' width='"+wcorner+"px' height='"+htcorner+"px'></td>";
	  result += "<td class='mtc'>"+ttitle+"</td>";
	  result += "<td class='mtr' width='"+wcorner+"px' height='"+htcorner+"px'></td>";
	  result += "</tr>";

	  result += "<tr>";
	  result += "<td class='mbll' height='"+hline+"px'></td >";
	  result += "<td class='mblc'></td>";
	  result += "<td class='mblr'></td>";
	  result += "</tr>";
	  
	  bHeight = (theight-(htcorner+hbcorner+2*hline));

	} else {

	  result += "<tr>";
	  result += "<td class='mtls' width='"+wcorner+"px' height='"+hbcorner+"px'></td>";
	  result += "<td class='mtc'></td>";
	  result += "<td width='"+wcorner+"px' height='"+hbcorner+"px' class='mtrs'></td>";
	  result += "</tr>";
	  
	  bHeight = (theight-2*hbcorner);

	}

	result += "<tr>";
	result += "<td class='mll'></td>";
	result += "<td class='mlc'>";
	result += "<iframe id='"+tname+"' name='"+tname+"' style='border: 2px solid #FFFFFF;  background-color:"+bgColor+" ' width='"+(twidth-2*wcorner-xWidth)+"px' height='"+bHeight+"px' src='"+tsrc+"' frameborder='0' scrolling='no' allowtransparency='true' ></iframe>";
	result += "</td>";
	result += "<td class='mlr'></td>";
	result += "</tr>";
	
	result += "<tr>";
	result += "<td class='mbl' width='"+wcorner+"px' height='"+hbcorner+"px'></td>";
	result += "<td class='mbc'></td>";
	result += "<td width='"+wcorner+"px' class='mbr'></td>";
	result += "</tr>";
	result += "</table>";
	
	return result;
} 

function preLoadImages(){
  var oImg = new Image();
  var i;
  // set image url
  imgs = new Array();
  imgs[0] = "img/rounded/ll.gif";
  imgs[1] = "img/rounded/lr.gif";
  imgs[2] = "img/rounded/bl.gif";
  imgs[3] = "img/rounded/br.gif";
  imgs[4] = "img/rounded/mbl.gif";
  imgs[5] = "img/rounded/mbr.gif";
  imgs[6] = "img/rounded/mbc.gif";
  imgs[7] = "img/rounded/mll.gif";
  imgs[8] = "img/rounded/mlr.gif";
  imgs[9] = "img/rounded/mtc.gif";
  imgs[10] = "img/rounded/mtrs.gif";
  imgs[11] = "img/rounded/mtls.gif";
  
	var ipl = new imgPreloader(imgs,false);

  
}



function getMapObject() {

	try {
		return parent.parent.map.myMap.map;
  } catch(e) { return null; }
}

function getMapObjectTabs()
{
	try {
		var oIframePage = document.getElementById("map")
		var oDoc = (oIframePage.contentWindow || oIframePage.contentDocument);// left ie, right firefox
		return oDoc.myMap.map;
	}
	catch(e) {return null;}
}

function getVisibleLayers()
{
	//get all the layers that are visible
  var sServiceName = "orto,topo";
  
  var layers_visible = new Array();
  var map = getMapObjectTabs();
  if (map!=null) 
  {
    for (var i = 0; i < map.connections.length; i++) 
    {
		  refresh = false;
		  if (sServiceName.indexOf(map.connections[i].name) == -1)
		  {
		  	refresh = true;
		  	for (var j = 0; j < map.connections[i].layers.length; j++) 
		  	{
        	if (map.getvisible(i,j)) 
        	{
         		layers_visible.push(map.connections[i].layers[j].name);
         	}
	    	}
  		}
  	}
		if (layers_visible.length > 0)
			return layers_visible.join(",");
		else
			return false;
	}
	
}
function changePage(tab,url) {
	if (tab!=null && url!=null) {
		var newUrl = url.replace("#","%23");
		if (tab=='text'){
			var oPage = parent.document.getElementById('text');
			if (oPage) {
				var oDoc = (oPage.contentWindow || oPage.contentDocument);
		    if (oDoc.document) oDoc = oDoc.document;
				oEl = oDoc.getElementById('textContainer');
				if (!oEl) {
					newUrl = "textos.php?text=" + newUrl;
					oEl = oPage;
				}
			}
		} else {
			oEl = parent.document.getElementById(tab);
		}
		if (oEl) {
			oEl.src= newUrl;
			if (parent.oTabManager.active != tab) parent.oTabManager.activate(tab);
		}
	
	}
}