
function windowsize() {

	// Calculate sizes (cross-browser) ---------------------------------
	if( typeof( window.innerWidth ) == 'number' ) {
	  wWindow = window.innerWidth;
	  hWindow = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	  wWindow = document.documentElement.clientWidth;
	  hWindow = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	  wWindow = document.body.clientWidth;
	  hWindow = document.body.clientHeight;
	}
	
	if (wWindow > maxWidth) wWindow = maxWidth;
	if (wWindow < minWidth) wWindow = minWidth;
	if (hWindow > maxHeight) hWindow = maxHeight;
	if (hWindow < minHeight) hWindow = minHeight;
	
}


function roundedFrame(id,top,left,width,height,href,scroll) {
	
	document.write('<div id="'+id+'_rounded" style="top:'+top+'px; left:'+left+'px; height:'+height+'px; width:'+width+'px">');
	document.write("<iframe name='"+id+"' id='"+id+"' style='position: relative; top:"+10+"px; left:"+10+"px; border: 0px solid #RGB(230,242,243)' width='"+(width-25)+"px' height='"+(height-30)+"px' src='"+href+"' frameborder='0' scrolling='"+scroll+"' allowtransparency='true'></iframe>");
	document.write('</div>');
	rounded.render(id+"_rounded");
	
}


function squaredFrame(id,top,left,width,height,href,scroll) {
	
	document.write("<iframe name='"+id+"' id='"+id+"' style='position: absolute; top:"+ top +"px; left:"+left+"px; border: 1px solid #RGB(230,242,243)' width='"+(width)+"px' height='"+(height)+"px' src='"+href+"' frameborder='0' scrolling='"+scroll+"' allowtransparency='true'></iframe>");
	
}


function roundedTabs(id,top,left,width,height) {
	
	document.write('<div id="'+id+'_rounded" style="top:'+top+'px; left:'+left+'px; height:'+height+'px; width:'+width+'px;">');
	document.write('<div id="tabber" class="tabber" style="position:relative;top:10px; left:3px; width:'+(width-7)+'px; visibility:hidden;">');
	
	for (i=0;i<aTabs.length;i++) {
		document.write('<div id="tabbertab_'+i+'" class="tabbertab">');
		document.write('<h2>'+aTabs[i].text+'</h2>');
		document.write('<iframe name="'+aTabs[i].id+'" id="'+aTabs[i].id+'" style="position: relative; top:10px; left:10px; border: 0px solid #RGB(230,242,243);" width="'+(width-25)+'px" height="'+(height-70)+'px" src="'+aTabs[i].src+'" frameborder="0" scrolling="auto" allowtransparency="true"></iframe>');
		document.write('</div>');	
	}
  document.write("<div id='headerGeodata'><a href='http://www.geodata.es' target='_blank'>www.geodata.es</a></div>");
	document.write('</div>');
	
	tabber = tabberAutomatic(tabberOptions);	
	rounded.render(id+"_rounded");

}


function roundedDiv(id,top,left,width,height,html) {
	
	document.write('<div id="'+id+'_rounded" style="position:absolute;top:'+top+'px; left:'+left+'px; height:'+height+'px; width:'+width+'px">');
	document.write("<div id='"+id+"' name='"+id+"'></div>");
	document.write('</div>');
	
	rounded.render(id+"_rounded");
	
	document.getElementById(id).innerHTML = html;
}		


function resizerounded(id,top,left,height,width) {

	document.getElementById(id+"_rounded").style.width = width+"px";
	document.getElementById(id+"_rounded").style.height = height+"px";
	document.getElementById(id+"_rounded").style.left = left+"px";
	document.getElementById(id+"_rounded").style.top = top+"px";
	
	document.getElementById(id).style.height = (height-30)+"px";
	document.getElementById(id).style.width = (width-25)+"px";
}	


function resizeframe(id,height,width) {

	//document.getElementById(id).height = height+"px";
	//document.getElementById(id).width = width+"px";
	frame = document.getElementById(id);
	height = (height)+"px";
	width = (width)+"px";
	frame.setStyle('height', height);
	frame.setStyle('height', width);
}


function resizetabs(id,top,left,height,width) {
	if(document.all)
		resizetabsIE(id,top, left, height, width);
	else
		resizetabsFF(id,top, left, height, width);
}		


function resizetabsIE(id,top, left, height, width) {

	document.getElementById(id+"_rounded").style.width = width+"px";
	document.getElementById(id+"_rounded").style.height = height+"px";
	document.getElementById(id+"_rounded").style.left = left+"px";
	document.getElementById(id+"_rounded").style.top = top+"px";
	
	document.getElementById("tabber").style.width = (width-7)+"px";
	
	for (i=0;i<aTabs.length;i++) {
		document.getElementById(aTabs[i].id).style.width = (width-25)+"px";
		document.getElementById(aTabs[i].id).style.height = (height-70)+"px";
	}
	
}			


function resizetabsFF(id,top, left, height, width) {

  var tabs_div = document.getElementById(id+"_rounded");

	document.getElementById(id+"_rounded").style.width = width+"px";
	document.getElementById(id+"_rounded").style.height = height+"px";
	document.getElementById(id+"_rounded").style.left = left+"px";
	document.getElementById(id+"_rounded").style.top = top+"px";
	
	document.getElementById("tabber").style.width = (width-7)+"px";
	
	for (i=0;i<aTabs.length;i++) {
		document.getElementById(aTabs[i].id).width = (width-25)+"px";
		document.getElementById(aTabs[i].id).height = (height-70)+"px";
	}
	
}		


function ortotopoTool(id,top,left,width,height,topPadding,leftPadding) {	
	
	var current_background = "";
	document.write("<div name='"+id+"' id='"+id+"' style='display:none;z-index:100;position:absolute;width:"+width+"px;height:"+height+"px;top:"+(top+topPadding)+"px;left:"+(left+leftPadding)+"px;'>");
	document.write("<table cellspacing=0 width='"+(width-leftPadding)+"px'><tr>");
	for (i = 0; i < backgrounds.length; i++) {
		if(backgrounds[i][0].title != current_background) {
			document.write("<td id='"+backgrounds[i][0].name+"' class='ortotopo' onclick='changeBackground(\""+backgrounds[i][0].title+"\");ortotopoToolActivate(\""+backgrounds[i][0].name+"\");'>"+backgrounds[i][0].title+"</a></td>");
			document.write("<td></a></td>");
			current_background = backgrounds[i][0].title;
		}
	}
	document.write("</tr></table>");
	document.write("</div>");
	
	ortotopoToolActivate(id);
}


function ortotopoToolActivate(id) {
	var map = getMapObject();
	
	if (map) {
		document.getElementById(id).style.display = "";
		for (i = 0; i < backgrounds.length; i++) {
			if (map.getvisible(backgrounds[i][0].server, backgrounds[i][0].name)) {
				document.getElementById(backgrounds[i][0].name).style.borderStyle = "inset";
				document.getElementById(backgrounds[i][0].name).style.fontWeight = "bold";
			}
			else {
				document.getElementById(backgrounds[i][0].name).style.borderStyle = "outset";
				document.getElementById(backgrounds[i][0].name).style.fontWeight = "normal";
			}
		}
	}
	else
		setTimeout("ortotopoToolActivate('"+id+"');",500);
}


function toolPosition(id,top,left,topPadding,leftPadding) {
	
	var ortoTopo = document.getElementById(id);
	ortoTopo.style.top = (top+topPadding)+"px";
	ortoTopo.style.left = (left+leftPadding)+"px";
	
}


function createHeaderDiv(titleHeader,logoHref,logoSrc,logoAlt,hLogo) {
	
	var headerHTML = "";
	
	headerHTML += "<div id='headerLogo'><a href='http://"+logoHref+"' target='_blank'><img height='"+hLogo+"px' border='0' src='"+logoSrc+"' alt='"+logoAlt+"' /></a></div>";
	headerHTML += "<div id='headerTitle'><h1>"+titleHeader+"</h1></div>";
  headerHTML += "<div id='headerLogoSponsor3'><a href='http://www.bcn.cat/novaciutadania/' target='_blank'><img border='0' src='img/logo_ajuntament.png' alt='Ajuntament de Barcelona' /></a></div>";
  headerHTML += "<div id='headerLogoSponsor2'>Amb el suport de: <br/><a href='http://obrasocial.caixacatalunya.es/osocial/main.html?idioma=2' target='_blank'><img border='0' src='img/obra_social_small.png' alt='Obra Social Caixa Catalunya' /></a></div>";
  headerHTML += "<div id='headerLogoSponsor'>Amb la col·laboració de: <br/><a href='http://www.ub.edu/icagis' target='_blank'><img border='0' src='img/logo_ub.jpg' alt='Universitat de Barcelona' /></a></div>";
	
	return headerHTML;
	
}


function createHeader(titleHeader,logoHref,logoSrc,logoAlt,hLogo) {
	
	var headerHTML = "";
	
	headerHTML += "<table id='headerTable' cellpadding='0' cellspacing='0' height='65' width='100%' align='center' border='0'>";
	headerHTML += "<tr>";
	headerHTML += "<td width='5%'>&nbsp;</td>";
	headerHTML += "<td style='vertical-align:bottom;' width='1%'><a href='http://"+logoHref+"' target='_blank'><img height='"+hLogo+"px' border='0' src='"+logoSrc+"' alt='"+logoAlt+"' /></a></td>";
	headerHTML += "<td width='10%'>&nbsp;</td>";
	headerHTML += "<td valign='middle'><h1>"+titleHeader+"</h1></td>";
	headerHTML += "<td width='1%' align='right'><a href='http://www.geodata.es' target='_blank'>www.geodata.es</a></td>";
	headerHTML += "<td width='5%'>&nbsp;</td>";
	headerHTML += "</tr>";
	headerHTML += "</table>";
	
	return headerHTML;
	
}


function createMenu(tMenu,lMenu,wMenu,hMenu) {				
	//create Overlay
	//var overlay = new Element('div').setProperty('id', 'loading').setStyles({position:'absolute',width:'100%',height:'100%','background-color':'black',top:'0px'}).inject(document.body).fade(0).fade(0.8);
	//var loading = new Element('img',{'src':'img/loading.gif','styles':{'position':'absolute','top':'-100px','left':'-1000px'}}).inject(overlay);

	roundedDiv("menuDiv",tMenu,lMenu,wMenu,hMenu,"<div id='egvDiv'></div><div id='headerDiv'></div><div id='menuDiv'></div>");
		
	var menuDiv = $('menuDiv');
	menuDiv.setStyles({'padding':menuGap});
	var frm;

	var hMenuElement = hMenu - 100 - 2*menuGap;
	var wMenuElement = wMenu - 10 - 2*menuGap;
	
	for (i = 0;  i < elems.length; i++) {
		menuDiv.grab(new Element('div',{'id':'header'+i,'class':'headerClass'}).grab(new Element('span',{'text':elems[i].title,'class':'collapsed'})));
		frm = new Element('iframe',{'name':elems[i].id,'id':elems[i].id,'width':wMenuElement+'px','height':hMenuElement+'px','src':elems[i].src,'frameborder':'0','allowtransparency':'true'});
		if (elems[i].id == "info") frm.addEvent('load',function(){displayAccord('info');});
		menuDiv.grab(new Element('div',{'class':'expanderClass','styles':{'height':hMenuElement+'px'}}).grab(frm));
	}
	
	accord = new Accordion($('menuDiv'),'div.headerClass', 'div.expanderClass');
	
	accord.addEvent('onBackground', function(toggler, element){
			element.getChildren()[0].setProperty('height','0px');
	});
	accord.addEvent('onActive', function(toggler, element){
			$$('span.sdmenu').each(function(el){
			    el.setProperty('class','collapsed');
			});
			toggler.getChildren()[0].setProperties({'class':'sdmenu'});
			element.getChildren()[0].setProperty('height',hMenuElement+'px');
	});

}


function displayAccord(value) {
	for (i = 0; i < elems.length; i++) {
		if (elems[i].id == value)
			if (accord)
				accord.display(i);
	}
}