﻿(function(){if(document.getElementById && document.getElementsByTagName){document.documentElement.className = "js";}})();

var AddEvento = function(el){
	addEvent(el,'click', function(){
		return clickWT(el);
	});
};

function addEvent(obj, evType, fn){ 
	
	   if(obj!=null){
			if(obj.addEventListener){ 
				obj.addEventListener(evType, fn, false); 
				return true; 
			}else if (obj.attachEvent){ 
				return obj.attachEvent("on" + evType, fn); 
			}else{ 
				return false; 
			} 
	   }             
}


////////************************INICI MARCATGE ANALITICA 11/2011*******************//////////////////////

var metaTags = {};

function addMeta(){
 
	if(document.createElement && document.getElementsByTagName){
		//Per afegir al head, o al document si no existis
		var objHead;
		if(document.getElementsByTagName("head").length>0){
			objHead = document.getElementsByTagName("head")[0];
		}else{
			objHead = document;
		}
		
		//Elements a afegir: metaTags
		var objElement;
		
		for(var clau in metaTags){
			if(metaTags[clau] && metaTags[clau]!="" && metaTags[clau]!="null" && metaTags[clau]!=null && metaTags[clau]!="undefined"){
				objElement = null;
				objElement = document.createElement("meta");
				objElement.setAttribute("name", clau);
				objElement.setAttribute("content", metaTags[clau]);
				objHead.appendChild(objElement);
			}
		}
	}
}


function WTMeteocat(){

	if(!document.getElementsByTagName) return false;
	
	var strURI = window.location.pathname;
	strURI = strURI.toLowerCase();
	
	//títol sense accents
	metaTags["WT.ti"] = sense_accents(document.title.toLowerCase());
		if (!(document.domain.match('www20.gencat'))) { 
		metaTags["DCS.dcssip"] = "www.meteo.cat"; // NOM DOMINI PRINCIPAL
	}

	var grup=subgrup=w2=w3="";
	var gc;
	
	if(typeof gc!="object"){
		gc=obteNomW(strURI);
	}
					
	w2 = gc.w2;
	w3 = gc.w3;

	//urls invàlides
	if (document.domain.substr(0,3)=='pre'){	
		metaTags["DCS.dcsid"] = "xxxx";
		//return false; 
	} else if (!(document.domain.match('meteocat.')) && !(document.domain.match('meteo.cat')) && !(document.domain.match('www20.gencat'))) { 	
		metaTags["DCS.dcsid"] = "xxxx";
		//return false; 	
	}
	
	addMeta();
	return {w2: w2, w3: w3, grup: grup, subgrup: subgrup};

}


function obteNomW(strURI){
	
	var w3;
	
		strURI = strURI.replace("servmet/","");
		if(strURI.match(/^\/(radar|llamps|satelit)\//)){
			if(strURI.match(/^\/(radar)\//)){ w3 = "Radar" }
			if(strURI.match(/^\/(llamps)\//)){ w3 = "Llamps" }
			if(strURI.match(/^\/(satelit)\//)){ w3 = "Satelit" }
			return {w2: "Dades Meteo Prediccio", w3: w3};
			
		} else if(strURI.match("/prediccio/ctermini")){
			if(strURI.match("/cat")){ w3 = "Catalunya" }
			if(strURI.match("/comarcal")){ w3 = "Comarques" }
			if(strURI.match("/paicat")){ w3 = "PPCC" }
			if(strURI.match("/llegenda")){ w3 = "" }
			return {w2: "Prediccio a Curt Termini", w3: w3};
			
		} else if(strURI.match(/^\/(modelitzacio)\//)){
			return {w2: "Prediccio a Curt Termini", w3: "Models"};
			
		} else if(strURI.match(/^\/(smr)\//)){
			return {w2: "Prediccio a Curt Termini", w3: "Avisos"};
			
		} else if(strURI.match("/prediccio/mtermini")){
			return {w2: "Prediccio a Mig Termini", w3: "Prediccio Municipal a 8 Dies"};
	
		
		} else if(strURI.match("/prediccio")){
			if(strURI.match("/pirineu")){ w3 = "Pirineu" }
			if(strURI.match("/maritim")){ w3 = "Mar" }
			if(strURI.match("/pedra")){ w3 = "Calamarsa" }
			if(strURI.match("/uvi")){ w3 = "UVI" }
			return {w2: "Prediccio a Curt Termini", w3: w3};
			
		} else if(strURI.match(/^\/(radioson)\//)){
			return {w2: "Dades RAM", w3: "Radiosondatge"};
	
		} else if(strURI.match(/^\/(atles)\//)){
			return {w2: "Clima", w3: "Cartografia"};
			
		} else if(strURI.match(/^\/(botiga)\//)){
			return {w2: "Serveis Ciutada", w3: "Botiga"};
			
		} else if(strURI.match(/^\/(xema|xom)\//)){
			if(strURI.match(/^\/(xema)\//)){ w3 = "XEMA" }
			if(strURI.match(/^\/(xom)\//)){ w3 = "XOM" }
			return {w2: "Dades Meteo Clima", w3: w3};
			
			
		}else{
			return {w2: "", w3: ""};
			
		}
	
}


////////************************FI MARCATGE ANALITICA 11/2011*******************//////////////////////


function WTGencat(){

	if (!(document.domain.match('www20.gencat'))) { 
		metaTags["DCS.dcssip"] = "www.meteo.cat"; // NOM DOMINI PRINCIPAL
	}

	// marcatge grups i subgrups
	var strURI = window.location.pathname;
	strURI = strURI.toLowerCase();
	strURI = strURI.replace("servmet/","");
	strURI = strURI.replace("portal/site/","");

	metaTags["DCS.dcsuri"] = strURI;
	
	//títol sense accents
	metaTags["WT.ti"] = sense_accents(document.title.toLowerCase());

	var grup = obteNomGrup(strURI);
	var sGrup = obteNomSGrup(strURI);
	
	/*if(grup!=""){		
		metaTags["WT.cg_n"] = grup;
			if(sGrup!=""){		
				metaTags["WT.cg_s"] = sGrup;	
			
	}}*/
	

	if(!document.getElementsByTagName) return;
	var ancores = document.getElementsByTagName("a");
	for(var i=0, z=ancores.length; i<z; i++){
		/*if(ancores[i].onclick==null){			//20091221: idescat
			ancores[i].onclick = function(){ 
				return clickWT(this); 
			} 
		}*/
		AddEvento(ancores[i]);
		

	}

	//urls invàlides
	if (document.domain.substr(0,3)=='pre'){	
		return false; 
	} else if (!(document.domain.match('meteocat.')) && !(document.domain.match('meteo.cat')) && !(document.domain.match('www20.gencat'))) { 	
		return false; 	
	}
}

function clickWT(ancora){

	var href = ancora.href;
	var extensio = "";
	var protocol = ""; 
	var strURI = "";
	var domini = "";
	if(href.lastIndexOf(".")>-1){
		extensio =
		href.slice(href.lastIndexOf(".")+1).toLowerCase();
		if(extensio=="pdf" || extensio=="doc" || extensio=="xls" || extensio=="zip"){
			domini = href.slice(href.indexOf("/")+2);
			if(domini.indexOf("/")>-1){
				domini = domini.slice(0,domini.indexOf("/"));
			}
			if(href.indexOf("//")>-1){
				protocol = href.slice(0, href.indexOf("//")+2);
			}

			strURI = href.replace(protocol + domini, "");
			strURI = strURI.toLowerCase();
			strURI = strURI.replace("servmet/","");
			strURI = strURI.replace("portal/site/","");
			//dcsMultiTrack('DCS.dcssip', domini,'DCS.dcsuri', strURI, 'WT.ti', ancora.innerHTML);
			dcsMultiTrack('DCS.dcssip', domini, 'DCS.dcsuri', strURI, 'WT.ti', ancora.innerHTML);
		}
	}
}

function obteNomGrup(strURI){

	if(strURI.indexOf("/meteocat")==-1){ //DINS METEO.CAT
		if((strURI.indexOf("/prediccio/mtermini/")>-1)||(strURI.indexOf("/modelitzacio/")>-1)){
			return "Prediccio automatica";
			
		}else if ((strURI.indexOf("/prediccio/")>-1) || (strURI.indexOf("/smr/")>-1)){
			return "Prediccio manual";
				
		}else if((strURI.indexOf("/radar/")>-1) || (strURI.indexOf("/llamps/")>-1) || (strURI.indexOf("/satelit/")>-1)){
			return "Teledeteccio";		

		}else if(strURI.indexOf("/xom/")>-1){
			return "Dades XOM";		
			
		}else if(strURI.indexOf("/appjava/")>-1){
			return "Dades EMA";	
			
		}else if(strURI.indexOf("/radioson/")>-1){
			return "RAM";	
			
		}else if((strURI.indexOf("/atles")>-1)){
			return "Atles climatic";	
					
		}else if ((strURI=='/') || (strURI=='/index.html')){ //pagina inicial meteocat
			return "Pagina inicial";
			
		}else{
			return "Altres";
		}
		
	}else{ 	
		return "GECO";//DINS GECO
		/*if ((strURI=='/meteocat')) { //pagina inicial meteocat
			return "GECO. Pagina inicial";
		}else{
			return "GECO. Altres";
		
		}*/
	}
}

function obteNomSGrup(strURI){
	if(strURI.indexOf("/meteocat")==-1){ //DINS METEO.CAT
		
		if((strURI.indexOf("/prediccio/mtermini/")>-1)){
			return "Prediccio mitja termini";
			
		}else if (strURI.indexOf("/modelitzacio/")>-1){
			return "Models numerics";
			
		}else if (strURI.indexOf("/prediccio/ctermini/cat")>-1){
			return "Prediccio general";
			
		}else if (strURI.indexOf("/prediccio/ctermini/comarcal")>-1){
			return "Prediccio comarcal";
			
		}else if (strURI.indexOf("/prediccio/pedra")>-1){
			return "Prediccio calamarsa";
			
		}else if (strURI.indexOf("/prediccio/maritim")>-1){
			return "Prediccio maritima";
			
		}else if (strURI.indexOf("/prediccio/pirineu")>-1){
			return "Prediccio Pirineu";
					
		}else if (strURI.indexOf("/prediccio/ctermini/paicat")>-1){
			return "Prediccio Paisos Catalans";
			
		}else if(strURI.indexOf("/prediccio/uvi")>-1){
			return "Prediccio UVI";		
	
		}else if(strURI.indexOf("/smr/")>-1){
			return "Avisos SMR";		
			
		}else if(strURI.indexOf("/radar/")>-1){
			return "Radar";		

		}else if(strURI.indexOf("/llamps/")>-1){
			return "Llamps";		

		}else if(strURI.indexOf("/satelit/")>-1){
			return "Satel.lit";	
			
		}else if(strURI.indexOf("/xom/vigilancia/")>-1){
			return "Vigilancia";
		
		}else if(strURI.indexOf("/xom/observacio/")>-1){
			return "Observacio";

		}else if(strURI.indexOf("dadesdiapermapa")>-1){
			return "Darreres 24 hores. Visualitzacio geografica";	
		
		}else if(strURI.indexOf("selecciopercomarca")>-1){
			return "Seleccio per comarca";	

		}else if(strURI.indexOf("selecciopermunicipi")>-1){
			return "Seleccio per municipi";	
			
		}else if(strURI.indexOf("seleccioperestacio")>-1){
			return "Seleccio per EMA";	
			
		}else if(strURI.indexOf("selecciototesEstacions")>-1){
			return "Seleccio totes les EMA";	
			
		}else if(strURI.indexOf("seleccioaltamuntanya")>-1){
			return "Seleccio alta muntanya";		
			
		}else if(strURI.indexOf("resumdiari")>-1){
			return "Resum diari comarca";	
			
		}else if(strURI.indexOf("detall24estacio")>-1){
			return "Dades diaries estacio";			

		}else if(strURI.indexOf("grafiquesestacio")>-1){
			return "Grafiques estacio";	
			
		}else if(strURI.indexOf("fitxaestacio")>-1){
			return "Fitxa Estacio";	
		
		}else if(strURI.indexOf("mapper.do")>-1){
			return "Mapper.do: últimes dades/selecció data";	

		}else if(strURI.indexOf("/radioson/")>-1){
			return "Radiosondatge";	
			
		}else if(strURI.indexOf("/atles/ACC/temperatura")>-1){
			return "Atles Climatic de Catalunya (ACC): Temperatura";	
			
		}else if(strURI.indexOf("/atles/ACC/")>-1){
			return "Atles Climatic de Catalunya (ACC): Precipitacio";	
			
		}else if(strURI.indexOf("atles/ACC61_90/temperatura")>-1){
			return "61-90: Temperatura";
			
		}else if(strURI.indexOf("atles/ACC61_90/")>-1){
			return "61-90: Precipitacio";
			
		}else if ((strURI=='/') || (strURI=='/index.html')){
			return "Pagina inicial";		

		}else{
			return "";
		}
		
	}
}

function sense_accents(s){
    var vocals;
    var res, c, v;
    vocals = new Array("aàáâä", "eèéëê", "iíìïî", "oóòöô", "uúùüû");

    var i, j;
    res = s + '';
    for(i=0;i<vocals.length;i++){
        v = vocals[i]; // TRACTAMENT MINÚSCULES
        c = v.slice(0,1);
        for (j = 1 ; j<vocals[i].length;j++){
            res = res.replace(v.slice(j, j+1), c);
		}
	}

	/* for(i=0;i<vocals.length;i++){
        v = vocals[i].toUpperCase(); // TRACTAMENT MAJÚSCULES
        c = v.slice(0,1);
        for (j = 1 ; j<vocals[i].length;j++){
            res = res.replace(v.slice(j, j+1), c);
		}
	}*/
    return res;
}


	// codifica un string en utf8
	function encode_utf8( string )
	{
		if (encodeURIComponent) {
			stringAux = encodeURIComponent(string);
		} else {
			// versions antigues de navegadors IE i Nestcape, no es traduira a utf8
			stringAux = escape(string);
		}
	
		return unescape(stringAux);
	}

	// codifica tots els camps del formulari en encoding utf8
    function codifica_form_UTF8(theForm) {
	
	   for(i=0; i<theForm.elements.length; i++) {
		   var alertText = "";
	   
		   var elm_obj =  theForm.elements[i];  
		   var elm_name = elm_obj.name;
		   var elm_type = elm_obj.type;
		   var elm_value = elm_obj.value;
		   
		  if(elm_type != "submit" && (!(elm_type == "textarea" && elm_name != "meta_inf")) && elm_type != "button" && elm_type != "reset" && elm_type != "file" && elm_type != "image" && elm_type != "password") {
			   alertText += "Element Name: " + elm_name + "\n"; 
			   alertText += "Element Type: " + elm_type + "\n"; 
				
			  if(elm_type == "text" || (elm_type == "textarea" && elm_name != "meta_inf") || elm_type == "hidden"){
	  		      elm_obj.value = encode_utf8(elm_value);
  				  alertText += "Element Value: " + elm_value + "\n";
			  }
			  else if(elm_type == "checkbox"){
				  elm_obj.value = encode_utf8(elm_value);
				  alertText += "Element Value: " + theForm.elements[i].value + "\n";
			  }
			  else if(elm_type == "radio"){
 				  elm_obj.value = encode_utf8(elm_value); 	
  				  alertText += "Element Value: " + elm_obj.value + "\n";
			  }
			  else if(elm_type == "select-one"){
				  elm_obj.options[elm_obj.selectedIndex].text = encode_utf8(elm_obj.options[elm_obj.selectedIndex].text);
  				  alertText += "Element Value: " + elm_obj.options[elm_obj.selectedIndex].text + "\n";
			  }
			  else if(elm_type == "select-multiple"){

				  var opt_selected = new Array();
    	          var index = 0;
        		  for (var k=0; k < elm_obj.options.length;k++)
	              {
					  if (elm_obj.options[k].selected)
					  {
						 elm_obj.options[k].text = encode_utf8(elm_obj.options[k].text);
 						 opt_selected[index] = elm_obj.options[k].text;
						 index++;
					  }
	              }
				  alertText += "Element Value: " + opt_selected + "\n";
			  }
	  	      //alert(alertText);
		  }
	   }

	}




//GEGO FUNCIONS

function EnllacaIdioma(intOrigen,bIdioma1,bIdioma2){

//catala=1,castellà=2,angles=3
var Idiomes = new Array()
Idiomes[0] = "cat"   	//directori
Idiomes[1] = "cas"
Idiomes[2] = "eng"


	if(document.getElementById){
	
		var strURL = new String(window.location)
		
		if(document.getElementById("idioma1_link")){
			var idioma1 = document.getElementById("idioma1_link")
			idioma1.href = strURL.replace("/"+Idiomes[intOrigen-1]+"/","/"+Idiomes[bIdioma1-1]+"/")
		}

		if(document.getElementById("idioma2_link")){
			var idioma2 = document.getElementById("idioma2_link")
			idioma2.href = strURL.replace("/"+Idiomes[intOrigen-1]+"/","/"+Idiomes[bIdioma2-1]+"/")
		}
	}
	
}

function SelectIdioma(){
var testIdioma = ""

	try{testIdioma=idioma}
	catch(e){}

	if(testIdioma!=""){
		switch(testIdioma){
			case "cat":
				EnllacaIdioma(1,2,3) 
				break
			case "cas":
				EnllacaIdioma(2,1,3) 
				break
			case "eng":
				EnllacaIdioma(3,1,2) 
				break
		}
	}
}

function TreureTextInput(){

	if(document.getElementById){
		//cercador
		if(document.getElementById("cerca")){
			document.getElementById("cerca").blur()
			addEvent(document.getElementById("cerca"), 'focus', function(){
																		 if(document.getElementById("cerca").value=="cercar" || 
																			document.getElementById("cerca").value=="buscar" ||
																			document.getElementById("cerca").value=="search" 
																			) 
																		 document.getElementById("cerca").value=""
																}
			);
		}

		//qui és qui
		if(document.getElementById("query")){
			document.getElementById("query").blur()
			addEvent(document.getElementById("query"), 'focus', function(){
																		 if(document.getElementById("query").value=="Cerca de persones i organismes" ) 
																		 document.getElementById("query").value=""
																}
			);
		}
	}
	
}


function ValidaParaulaCerca(){
	
		if(!document.getElementById) return;
		if(!document.getElementById("cercador")) return;
		
		var objFORM = document.getElementById("cercador");

		if(objFORM.nodeName.toLowerCase()!="form"){
			objFORM = objFORM.getElementsByTagName("form");
			
			if(objFORM.length>0){
				objFORM = objFORM[0];
			}else{
				return;
			}
		
		}
		
		objFORM.onsubmit = function(){

			var ca = "Si us plau, introduïu les paraules a cercar.";
			var es = "Por favor, introduzca las palabras a buscar.";
			var en = "Type your search please.";
			var continuar = true;
			var idioma = "";
			
			if(document.documentElement.getAttribute && (document.documentElement.getAttribute("lang")!="" || document.documentElement.getAttribute("xml:lang")!="" )){
				idioma = (document.documentElement.getAttribute("lang")?document.documentElement.getAttribute("lang"):(document.documentElement.getAttribute("xml:lang")?document.documentElement.getAttribute("xml:lang"):""));
			}

			switch(document.getElementById("cerca").value){
				
				case "":
					alert(eval(idioma));
					continuar = false;
					break;
				case "cercar":
					alert(idioma!=""?eval(idioma):ca);
					continuar = false;
					break;
				case "buscar":
					alert(idioma!=""?eval(idioma):es);
					continuar = false;
					break;
				case "search":
					alert(idioma!=""?eval(idioma):en);
					continuar = false;
					break;
			}
		
			if(continuar)
				return true;
			else{
				document.getElementById("cerca").focus();
				return false;
			}
			
		}
		
}

function ObjectsIE(){

	if(navigator.userAgent.toLowerCase().indexOf("msie")>-1 && document.getElementsByTagName && document.createElement){
		var script = document.createElement("script");
		script.setAttribute("type","text/javascript");
		script.setAttribute("src","http://www.gencat.net/js/objects_ie.js");
		document.getElementsByTagName('head')[0].appendChild(script);		
	}
	
}


if(navigator.userAgent.toLowerCase().indexOf("minimo")>-1){
	var blocEstil = document.createElement("style");
	blocEstil.innerHTML = "body{width:100%!important;}"
	blocEstil.innerHTML += "div{width:100%!important;float:none!important;clear:both!important;	margin:0!important;	padding:0!important;}"
	blocEstil.innerHTML += ".border_left,.contingut_pastilla_bg,.contingut_pastilla_bg_nofloat{background-image:none!important;}"
	blocEstil.innerHTML += "div#marc div.menu_superior, div#marc div.menu_superior ul, div#capcalera div#menu_cap ul{float:left !important;}"
	blocEstil.innerHTML += "div#capcalera div#menu_cap ul li{display:inline; float:left; margin-left:0; background-color:#818181; color: #fff;}"
	blocEstil.innerHTML += "div.titulomenu{background-image:none!important;}"
	blocEstil.innerHTML += "div#impacte, div#marc div.imatge_impacte, div#marc div.imatge_impacte_petita,h1, div#bloc_dades_img{display:none;}"
	blocEstil.innerHTML += "div.ocult{position: inherit; height:auto; width:auto; overflow: visible ; margin: .2em auto 0 auto !important; font-size:.7em;}"
	document.getElementsByTagName('head')[0].appendChild(blocEstil);		
}

addEvent(window, 'load', ObjectsIE);
//addEvent(window, 'load', SelectIdioma);
addEvent(window, 'load', TreureTextInput);
addEvent(window, 'load', ValidaParaulaCerca);


