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 addEvent(obj, evType, fn){ 

	if(obj.addEventListener){ 
		obj.addEventListener(evType, fn, false); 
		return true; 
	}else if (obj.attachEvent){ 
		return obj.attachEvent("on" + evType, fn); 
	}else{ 
		return false; 
	} 
}


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=""
																}
			);
		}

		if(document.getElementById("cercaLocal")){
			document.getElementById("cercaLocal").blur()
			addEvent(document.getElementById("cercaLocal"), 'focus', function(){
																		 if(document.getElementById("cercaLocal").value=="cercar" || 
																			document.getElementById("cercaLocal").value=="buscar" ||
																			document.getElementById("cercaLocal").value=="search" 
																			) 
																		 document.getElementById("cercaLocal").value=""
																}
			);
		}		

		//qui ?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(formcercador, caixacerca){

		if(!document.getElementById) return;
		if(!document.getElementById(formcercador)) return;

		var objFORM = document.getElementById(formcercador);

		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(caixacerca).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(caixacerca).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?" + (new Date()).getTime());
		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', 

				function(){
					if(document.getElementById){
						//cercador
						if(document.getElementById("cercador")){
							ValidaParaulaCerca("cercador", "cerca");
						}

						if(document.getElementById("cercadorLocal")){
							ValidaParaulaCerca("cercadorLocal", "cercaLocal");
						}
					}
				}
);

//Inclusio del tag d'analitica
var strDomini = new String(window.location.hostname);
var strPath = new String(window.location.pathname);
var gDcsId="dcso26u0400000c5ozrsb4268_8f7p";


/*if(strDomini.indexOf("www.gencat.cat")>-1 || strDomini.indexOf("www.gencat.net")>-1 || strDomini.indexOf("www.gencat.es")>-1 ){
	if(strPath!="" && strPath!="/" && strPath!="/index.htm" && strPath!="/index_cas.htm" && strPath!="/index_eng.htm"){
		var execWT = true;
		document.write("<scr" + "ipt type='text/javascript' src='/js/gencatAnalitica.js'></scr" + "ipt>");
	}
}*/

//beta funcionament
if((new String(window.location)).indexOf("beta=true")>-1){

	document.write("<scr" + "ipt type='text/javascript' src='/js/cercador/cercador-suggest.js'></scr" + "ipt>");
	document.write("<link rel='stylesheet' type='text/css' href='/js/cercador/cercador-suggest.css' />"); 
 
	addEvent(window, 'load', function(){
								try{
									var _objects = document.getElementsByTagName("object");
									var _param;
									for(var i=0; i<_objects.length; i++){
										_param = document.createElement("param"); 
										_param.setAttribute("name", "wmode"); 
										_param.setAttribute("value", "transparent");
										_objects[i].appendChild(_param);
									}
								}catch(e){}
								
								_cerca_suggest.load("cerca", 
									{
										url : "http://cercador.gencat.cat/cercador/AppJava/xml.jsp?q=",
										extraparams : {
											suggest : true,
											suggest_def_enc : true,				
											format : "rich"				
										},
										parse : function(data){
											var files = new Array();
											for(var i=0; i<data.results.length; i++){
												files.push({"data":data.results[i], "name":data.results[i].name, "value":data.results[i].name});
											}											
											return files;
										},
										delay : 300,
										leftOffset : function(){
											if(_cerca_suggest._ua.indexOf("msie 6.0")>-1) return 630;
											else return 0;
										},
										topOffset : function(){
											if(_cerca_suggest._ua.indexOf("msie 6.0")>-1) return -14;
											else return 0;
										}
									}
								);
							}
			);

	document.write("<scr" + "ipt type='text/javascript' src='http://www.domini.cat/giny/giny-1-g.js'></scr" + "ipt>");

			
}
