jQuery(document).ready(function() {

/////////////////////////////////////////////////////////////////////////////////////////////
// abrir enlaces de clase 'externo' en ventana nueva 
$(".externo").click(function(){
 this.target = "_blank";
});
//-------------------------------------------------------------------------------------------

/////////////////////////////////////////////////////////////////////////////////////////////
// submenu
$(".enlace_destacado").mouseenter(function(){
 $(this).children('span').addClass("submenu_hover");
});
$(".enlace_destacado").mouseleave(function(){ $(this).children('span').removeClass("submenu_hover"); });

//-------------------------------------------------------------------------------------------


/////////////////////////////////////////////////////////////////////////////////////////////
// occultar enlace diseno web alvaroweb 
$("#alvaro_link").css("display","none"); // ocultar los enlaces
$("#acuarela_link").html("Dise&ntilde;o y Desarrollo web"); // ocultar los enlaces
//-------------------------------------------------------------------------------------------


/////////////////////////////////////////////////////////////////////////////////////////////
// acordeones de contenido a mano V.2, mandan bien el foco
	// first simple accordion with special markup
var acordeon_abierto = "ninguno";//para recoger el acordeon abierto
var titulo_clicado = "ninguno";//para recoger el titulo que se ha seleccionado
$(".acordeon_publica").hide();//ocultar todos los acordeones inicialmente

/*
$(".tit_acordeon_publica").click(function() {
   $(".acordeon_publica").hide();//cerrar
   $(this).next().toggle();//cerrar
});
//poner el cursor adecuado en los titulos
//$(".tit_acordeon_publica").hover(function() { $(this).css("cursor","pointer"); });
*/
//abrir y cerrar acordeones
/*$(".tit_acordeon_publica").click(function() {
 titulo_clicado = $(this);//guardar el titulo
 if (acordeon_abierto != "ninguno") {//si ya hay un acordeon abierto
  if ($(this).attr("id") == acordeon_abierto) {//si es este mismo
   $("#" + acordeon_abierto).next().hide();//cerrar
   $("#" + acordeon_abierto).removeClass("tit_acordeon_publica_select");//cerrar
   acordeon_abierto = "ninguno";//resetear la variable
  } else {//si no es este mismo
   $("#" + acordeon_abierto).removeClass("tit_acordeon_publica_select");//cerrar
   $("#" + acordeon_abierto).next().hide(0,function () {//cerrar, y al terminar...
	     //calcular si el elemento esta en pantalla
	     var y_elemento = $(titulo_clicado).offset().top;
	     var alto_ventana = $(window).height();
	     var y_ventana = $(window).scrollTop();
	     
	     if ( y_elemento > y_ventana && y_elemento < y_ventana + alto_ventana ) {
	      //si esta en pantalla, no enviamos el foco
	     } else {
	      location.hash = titulo_clicado.attr("id");//si no esta en pantalla, enviar el foco
	     }
	     $(titulo_clicado).next().show();//abrir el acordeon correspondiente
         $(titulo_clicado).addClass("tit_acordeon_publica_select");
   });
	 acordeon_abierto = $(this).attr("id");//guardar el id del acordeon abierto
  }
 } else { //si no hay un acordeon abierto
  $(this).next().show();//mostrar el acordeon
  $(this).addClass("tit_acordeon_publica_select");
  acordeon_abierto = $(this).attr("id");//guardar el id del acordeon abierto
 }
});*/



//abrir y cerrar acordeones
$(".tit_acordeon_publica").click(function() {
 titulo_clicado = $(this);//guardar el titulo
 if (acordeon_abierto != "ninguno") {//si ya hay un acordeon abierto
  if ($(this).attr("id") == acordeon_abierto) {//si es este mismo
   $("#" + acordeon_abierto).next().hide("fast");//cerrar
   $("#" + acordeon_abierto).removeClass("tit_acordeon_publica_select");//cerrar
   acordeon_abierto = "ninguno";//resetear la variable
  } else {//si no es este mismo
                    $("#" + acordeon_abierto).removeClass("tit_acordeon_publica_select");//cerrar
   $("#" + acordeon_abierto).next().hide("fast", function () {//cerrar, y al terminar...
	     //calcular si el elemento esta en pantalla
	     var y_elemento = $(titulo_clicado).offset().top;
	     var alto_ventana = $(window).height();
	     var y_ventana = $(window).scrollTop();
	     
	     if ( y_elemento > y_ventana && y_elemento < y_ventana + alto_ventana ) {
	      //si esta en pantalla, no enviamos el foco
	     } else {
	      location.hash = titulo_clicado.attr("id");//si no esta en pantalla, enviar el foco
	     }
	     $(titulo_clicado).next().show("slow");//abrir el acordeon correspondiente
                    $(titulo_clicado).addClass("tit_acordeon_publica_select");
   });
	 acordeon_abierto = $(this).attr("id");//guardar el id del acordeon abierto
  }
 } else { //si no hay un acordeon abierto
  $(this).next().show("slow");//mostrar el acordeon
  $(this).addClass("tit_acordeon_publica_select");
  acordeon_abierto = $(this).attr("id");//guardar el id del acordeon abierto
 }
});

//-------------------------------------------------------------------------------------------


/////////////////////////////////////////////////////////////////////////////////////////////
// mostrar/ocultar politicas de privacidad en los formularios
$(".privacidad").toggle();//ocultar todas inicialmente
$(".bot_privacidad").click(function() {
  $(this).parent().next().slideToggle("normal");
  return false;
} );
//-------------------------------------------------------------------------------------------


/////////////////////////////////////////////////////////////////////////////////////////////
// desactivar el estilo
var estado_estilo = true;
$(".quitar_estilo").click(function(){
 if (estado_estilo) {
  for(i=0;i<document.styleSheets.length;i++) {
   document.styleSheets[i].disabled=true;
  }
  estado_estilo = false;
 } else {
  for(i=0;i<document.styleSheets.length;i++) {
   document.styleSheets[i].disabled=false;
  }
  estado_estilo = true;
 }
});
//-------------------------------------------------------------------------------------------


/////////////////////////////////////////////////////////////////////////////////////////////
// manejo del texto del input del buscador
var contenido_searchcadena = $("#searchcadena").attr("value");
$("#searchcadena").focus(function() {//al hacer foco 
  if ($(this).attr("value") == contenido_searchcadena) {//si su valor es el inicial
    $(this).attr("value","");//borramos
	}
});

$("#searchcadena").blur(function() {//al quitar foco 
  if ($(this).attr("value") == "") {//si esta vacio
   $(this).attr("value",contenido_searchcadena);//ponemos el texto inicial
  } 
});
//-------------------------------------------------------------------------------------------


/////////////////////////////////////////////////////////////////////////////////////////////
// acordeon de noticias
var noticia_abierta = "ninguno";//recogera el acordeon abierto en todo momento
if ( $("#noticias .titulo_noticia").size() > 1 ) {//variable en true en caso de que haya solo una noticia
  var solo_una_noticia = false;
} else {
  var solo_una_noticia = true;
}

$("#noticias h3.primera").removeClass("abrir_noticia");// modificar el estilo del primer titulo             
$("#noticias h3.primera").addClass("desplegada");
$("#noticias .acordeon_noticias").hide();// ocultar todos los acordeones inicialmente
$("#noticias .acordeon_noticias.primera").show();// mostrar el primero
noticia_abierta = $("#noticias h3.primera").attr("id");// guardar su id			

// interactividad en los titulos
$("#noticias .titulo_noticia").hover(
  function () {
	  if ( !solo_una_noticia ) {//si hay mas de una noticia
      //$(this).css("cursor","pointer");
      $(this).addClass("titulo_noticia_over");
		}
  }, 
  function () {
	  if ( !solo_una_noticia ) {//si hay mas de una noticia
      //$(this).css("cursor","normal");
      $(this).removeClass("titulo_noticia_over");
		}
  }																 
);
$("#noticias .desplegada").hover(
  function () {
	  if ( !solo_una_noticia ) {//si hay mas de una noticia
      //$(this).css("cursor","pointer");
      $(this).addClass("titulo_noticia_over");
		}
  }, 
  function () {
	  if ( !solo_una_noticia ) {//si hay mas de una noticia
      //$(this).css("cursor","normal");
      $(this).removeClass("titulo_noticia_over");
		}
  }																 
);

//funcion para abrir el acordeon seleccionado
$("#noticias .titulo_noticia").click(function() {
	if ( !solo_una_noticia ) {//si hay mas de una noticia
	  $("#noticias .titulo_noticia").removeClass("desplegada");// modificar el estilo del titulo          
	  $("#noticias .titulo_noticia").addClass("abrir_noticia");

    if (noticia_abierta != "ninguno") {//si hay un acordeon abierto
      $("#" + noticia_abierta).next().hide("fast");//lo cierro animando
    };
 
    if ($(this).attr("id") != noticia_abierta) {//si no es el mismo que acabo de cerrar
      $(this).next().show("slow");//mostrar el acordeon
      noticia_abierta = $(this).attr("id");//guardo el id del acordeon abierto
		  $(this).removeClass("abrir_noticia");// modificar el estilo del titulo              
	  	$(this).addClass("desplegada");
    }  else {//si es el que acabo de cerrar
      noticia_abierta = "ninguno";//reseteo la variable, no hay ninguno abierto
    }
	//} else {//si solo hay una noticia
    //$(this).next().hide("fast");//cerrar y abrir de seguido
    //$(this).next().show("slow");
	}
});
//-------------------------------------------------------------------------------------------


/////////////////////////////////////////////////////////////////////////////////////////////
// boton de cerrar resultados de busqueda
$(".cerrar_search_results").click(function() {
  $("#search_results").hide("normal");
  $("#envoltura_contenido").removeClass("navegacion_interna");
  
	return false;
});
//-------------------------------------------------------------------------------------------


/////////////////////////////////////////////////////////////////////////////////////////////
// boton de cerrar resultados de busqueda en mapas
$(".cerrar_search_results_mapa").click(function() {
  $(".resultados_gmap").hide("normal");
	return false;
});
//-------------------------------------------------------------------------------------------

/////////////////////////////////////////////////////////////////////////////////////////////
// activar el scroll animado para los enlaces internos
enable_smooth_scroll();


/////////////////////////////////////////////////////////////////////////////////////////////
// fin del $(document).ready( function()
} ); 
//-------------------------------------------------------------------------------------------



/////////////////////////////////////////////////////////////////////////////////////////////
// scroll animado para los enlaces internos
function enable_smooth_scroll() {
    function filterPath(string) {
        return string
                .replace(/^\//,'')
                .replace(/(index|default).[a-zA-Z]{3,4}$/,'')
                .replace(/\/$/,'');
    }

    var locationPath = filterPath(location.pathname);
    
    var scrollElement = 'html, body';
    $('html, body').each(function () {
        var initScrollTop = $(this).attr('scrollTop');
        $(this).attr('scrollTop', initScrollTop + 1);
        if ($(this).attr('scrollTop') == initScrollTop + 1) {
            scrollElement = this.nodeName.toLowerCase();
            $(this).attr('scrollTop', initScrollTop);
            return false;
        }    
    });
    
    $('a.efecto_subir').each(function() {//para capar el efecto y que solo funciones para subir al div#web
    //$('a[href*=#]').each(function() {
        var thisPath = filterPath(this.pathname) || locationPath;
        if  (   locationPath == thisPath
                && (location.hostname == this.hostname || !this.hostname)
                && this.hash.replace(/#/, '')
            ) {
                if ($(this.hash).length) {
                    $(this).click(function(event) {
                        var targetOffset = $(this.hash).offset().top;
                        var target = this.hash;
                        event.preventDefault();
                        $(scrollElement).animate(
                            {scrollTop: targetOffset},
                            500,
                            function() {
                                location.hash = target;
                        });
                    });
                }
        }
    });
}
//-------------------------------------------------------------------------------------------

		  
///////////////////////
// menu desplegable
/*var is_ie6 = (
 window.external &&
 typeof window.XMLHttpRequest == "undefined"
);
var ul_actual;
var subul_actual;
if(is_ie6){

// DropDownMenu by Miha Hribar
// http://hribar.info

function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}

function prepareMenu() {
    // first lets make sure the browser understands the DOM methods we will be using
  	if (!document.getElementsByTagName) return false;
  	if (!document.getElementById) return false;
  	
  	// lets make sure the element exists
  	if (!document.getElementById("menu_desplegable")) return false;
  	var menu = document.getElementById("menu_desplegable");
  	
  	// for each of the li on the root level check if the element has any children
  	// if so append a function that makes the element appear when hovered over
  	var root_li = menu.getElementsByTagName("li");
  	for (var i = 0; i < root_li.length; i++) {
  	    var li = root_li[i];
  	    // search for children
  	    var child_ul = li.getElementsByTagName("ul");
  	    if (child_ul.length >= 1) {
  	        // we have children - append hover function to the parent
  	        li.onmouseover = function () {
  	            if (!this.getElementsByTagName("ul")) return false;
  	            var ul = this.getElementsByTagName("ul");
  	            ul[0].style.display = "block";
  	            return true;
  	        }
  	        li.onmouseout = function () {
  	            if (!this.getElementsByTagName("ul")) return false;
  	            var ul = this.getElementsByTagName("ul");
  	            ul[0].style.display = "none";
  	            return true;
  	        }
  	    }
  	}
  	
  	return true;
}

addLoadEvent(prepareMenu);

}*/
