window.onload = init;

function init()
{
	var linkShowPrivacy = document.getElementById('linkshowprivacy');
	linkShowPrivacy.onclick = showPrivacy;

	var linkHidePrivacy = document.getElementById('linkhideprivacy');
	linkHidePrivacy.onclick = hidePrivacy;

	var linkShowLegal = document.getElementById('linkshowlegal');
	linkShowLegal.onclick = showLegal;

	var linkHideLegal = document.getElementById('linkhidelegal');
	linkHideLegal.onclick = hideLegal;
}



function showPrivacy() {

    load("http://www.ibcmass.com/eng/calidad.txt","cont_dinamico_calidad");
	var divPrivacy = document.getElementById('privacy');
	divPrivacy.style.display = "block";
	//return false;
}

function hidePrivacy()
{
	var divPrivacy = document.getElementById('privacy');
	divPrivacy.style.display = "none";
	return false;
}

function showLegal() {
  load("http://www.ibcmass.com/eng/privacidad.txt","cont_dinamico_privacidad");
	var divLegal = document.getElementById('legal');
	divLegal.style.display = "block";
	//return false;
}

function hideLegal()
{
	var divLegal = document.getElementById('legal');
	divLegal.style.display = "none";
	return false;
}



function bot_proyectos(activo)
{
init();
//alert('proy: '+activo);
var proyShw = 'ctn_project_home_'+activo;
//alert('proy2: '+proyShw);
var proy= document.getElementById(proyShw);
//alert('proy3: '+proy);
proy.style.display='block';
var i=0;

for (i=0;i<=7;i++)
{
var boton_img = 'btnprj'+i;
//alert (boton_img);
var bot = document.getElementById(boton_img);

var padre = bot.parentNode;

//alert(bot.src);

//alert('for');
if (i!=activo)
   { 
    //alert ('if '+ i);
     proyShw = 'ctn_project_home_'+i;
     proy= document.getElementById(proyShw);
	 proy.style.display='none';
	 bot.src='img/another-project.png';
	 //alert(bot.src);
	// padre.class= 'another-project';
	 }
	 else
	 { 
	 bot.src='img/current-project.png'; 
	 //alert('else ' +bot.src);
	// padre.class= 'current-project';
	 }
}

//activar y desactivar botones
//desactivar



}

function removeAllChilds(a)
 {
 var a=document.getElementById(a);
 while(a.hasChildNodes())
	a.removeChild(a.firstChild);	
 }
 
 function aleatorio(inferior,superior){
numPosibilidades = superior - inferior
aleat = Math.random() * numPosibilidades
aleat = Math.round(aleat)
return parseInt(inferior) + aleat
} 
 
 
 function generar_proyecto()
{
 var random=aleatorio(0,7);
bot_proyectos(random);

}

 
 
 function generar_clientes()
 {
 //alert("eliminamos los cleimntes");
 removeAllChilds('cont_cliente_home');

 //creamos los  arrays
 
fotos_clientes = new Array(); 

fotos_clientes[0]='<img src="img/clientes-rim-100x75.png" width="100" height="75" alt="RIM" />';
fotos_clientes[1]='<img src="img/clientes-santillana_100x75.png" width="100" height="75" alt="Santillana" />';
fotos_clientes[2]='<img src="img/clientes-mcdonalds_100x75.png" width="100" height="75" alt="McDonalds" />';
fotos_clientes[3]='<img src="img/clientes-michelin_100x75.png" width="100" height="75" alt="Michelin" />';
fotos_clientes[4]='<img src="img/clientes-cajastur_100x75.png" width="100" height="75" alt="Cajastur" />';
fotos_clientes[5]='<img src="img/clientes-dgt-100x75.png" width="100" height="75" alt="DGT" />';
fotos_clientes[6]='<img src="img/clientes-injuve_100x75.png" width="100" height="75" alt="INJUVE" />';
fotos_clientes[7]='<img src="img/clientes-ceei_100x75.png" width="100" height="75" alt="CEEI" />';
fotos_clientes[8]='<img src="img/clientes-icex_100x75.png" width="100" height="75" alt="ICEX" />';
fotos_clientes[9]='<img src="img/clientes-ministerio-100x75.png" width="100" height="75" alt="Ministerio" />';
fotos_clientes[10]='<img src="img/clientes-fiab-100x75.png" width="100" height="75" alt="FIAB" />';
fotos_clientes[11]='<img src="img/clientes-fundetec_100x75.png" width="100" height="75" alt="FUNDETEC" />';
fotos_clientes[12]='<img src="img/clientes-medicossinfronteras_100x75.png" width="100" height="75" alt="Médicos sin fronteras" />';
fotos_clientes[13]='<img src="img/clientes-upta_100x75.png" width="100" height="75" alt="UPTA" />';
fotos_clientes[14]='<img src="img/clientes-eoi-100x75.png" width="100" height="75" alt="EOI" />';

var max=14;//valor maximo del random en el array

//alert(fotos_clientes);
 //alert("y ahora añadimos 6 aleatorios");
 var cont=0;
 var random=aleatorio(0,max);

 while (cont<7)
 {
 
  var li = document.createElement('LI');
  // Añadimos el valor introducido al nuevo elemento
  m= random + cont;
  //alert ('m= ' +m );
  if (m > 10) {  m=0; random=0;} 
  
  li.innerHTML = fotos_clientes[m];
  //alert (fotos_clientes[random]);
  // Añadimos el elemento LI a la lista UL
  var ul = document.getElementById('cont_cliente_home');
  ul.appendChild(li);
  // Vaciamos la caja de texto
 cont ++;
 
 }
 generar_proyecto();
}


/* GOOGLE MAPS */

   
 
    var map;
    var gdir;
    var geocoder = null;
    var addressMarker;

    function initialize() {
      if (GBrowserIsCompatible()) {      
        map = new GMap2(document.getElementById("map_canvas"));
		 map.setCenter(new GLatLng(43.368066176809386, -5.870368480682373), 17);
		 map.addControl(new GLargeMapControl());
          map.addControl(new GMapTypeControl());
        gdir = new GDirections(map, document.getElementById("directions"));
        GEvent.addListener(gdir, "load", onGDirectionsLoad);
        GEvent.addListener(gdir, "error", handleErrors);
		
        var latlng = new GLatLng(43.368066176809386, -5.870368480682373);
       // setDirections(latlng, latlng, "es");
		
		
		  // Create a base icon for all of our markers that specifies the
        // shadow, icon dimensions, etc.
        var baseIcon = new GIcon(G_DEFAULT_ICON);
        baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
        baseIcon.iconSize = new GSize(20, 34);
        baseIcon.shadowSize = new GSize(37, 34);
        baseIcon.iconAnchor = new GPoint(9, 34);
        baseIcon.infoWindowAnchor = new GPoint(9, 2);

        // Creates a marker whose info window displays the letter corresponding
        // to the given index.
        function createMarker(point, index) {
          // Create a lettered icon for this point using our icon class
          var letter = String.fromCharCode("A".charCodeAt(0) + index);
          var letteredIcon = new GIcon(baseIcon);
          letteredIcon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";

          // Set up our GMarkerOptions object
          markerOptions = { icon:letteredIcon };
          var marker = new GMarker(point, markerOptions);

          GEvent.addListener(marker, "click", function() {
            marker.openInfoWindowHtml("<ul>	<li class=\"maincolor3\">IBCmass</li><li class=\"maincolor3\">Paseo de la Florida 7 1&ordm; E</li><li class=\"maincolor3\">33012 - Oviedo - Asturias</li>	<li class=\"maincolor3\">T: 985 207 684 F: 985 208 700</li>	</ul>");
          });
          return marker;
        }

		//creamos el punto en el centro de nuestro mapa
		//var latlng = new GLatLng(43.368066176809386, -5.870368480682373);
          map.addOverlay(createMarker(latlng, 0));
		
      }
    }
    
	 function setDirectionsToIbcmass(fromAddress, toAddress, locale) {
	 
	 setDirections(fromAddress,new GLatLng(43.368066176809386, -5.870368480682373),locale);
    }

	
	
     function setDirections(fromAddress, toAddress, locale) {
	  gdir.load("from: " + fromAddress + " to: " + toAddress,
                { "locale": locale });
    }


    function handleErrors(){
	   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	     alert("No se ha podido localizar la posici\u00f3n exacta de la direcci\u00f3n introducida. Puede ser que la direcci\u00f3n sea relativamente nueva, o incorrecta. Intente de nuevo a\u00f1adiendo mas informaci\u00f3n." );
	   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	     alert("La peticion del codigo geografico o la dirección no ha podido ser procesada con exito por una causa desconocida.\n Error code: " + gdir.getStatus().code);
	   
	   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	    // alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
       alert("parametros insuficientes, se ha especificado una dirección nula por favor introduzca una dirección correcta.\n Error code: " + gdir.getStatus().code);
	//   else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
	//     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);
	     
	   else if (gdir.getStatus().code == G_GEO_BAD_KEY)
	     alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);

	   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	     alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
	    
	   else alert("An unknown error occurred.");
	   
	}

	function onGDirectionsLoad(){ 
      // Use this function to access information about the latest load()
      // results.

      // e.g.
      // document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
	  // and yada yada yada...
	}
    
	

	function submitformmoodle()
	{
	  document.myform.submit();
	}

	
	// Para cargar contenido externo desde jscript
	
	
	  function ahah(url, target) {
  document.getElementById(target).innerHTML = ' Fetching data...';
  if (window.XMLHttpRequest) {
    req = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (req != undefined) {
    req.onreadystatechange = function() {ahahDone(url, target);};
    req.open("GET", url, true);
    req.send("");
  }
}  

function ahahDone(url, target) {
  if (req.readyState == 4) { // only if req is "loaded"
    if (req.status == 200) { // only if "OK"
      document.getElementById(target).innerHTML = req.responseText;
    } else {
      document.getElementById(target).innerHTML=" AHAH Error:\n"+ req.status + "\n" +req.statusText;
    }
  }
}

function load(name, div) {
	ahah(name,div);
	return false;
}

   
	
	

