/////////////////////////////////////////////////////////////////////////////////////////////
// inicio del $(document).ready( function()
$(document).ready( function() {
//-------------------------------------------------------------------------------------------

// ocultar enlace acuarela
$("#acuarelalink").hide();//ocultar todos los acordeones inicialmente


/////////////////////////////////////////////////////////////////////////////////////////////
// activar las galerias de prettyphoto
//normal
//$("a[rel^='prettyPhoto']").prettyPhoto(); 
//con configuraciones (controlar las comas)
$("a[rel^='prettyPhoto']").prettyPhoto({
  //animationSpeed: 'normal', // fast/slow/normal 
  //padding: 40, // padding for each side of the picture 
  opacity: 0.8 // Value betwee 0 and 1 
  //showTitle: false, // true/false 
  //allowresize: true, // true/false
  //counter_separator_label: '/', // The separator for the gallery counter 1 "of" 2 
  //theme: 'light_rounded', // light_rounded / dark_rounded / light_square / dark_square 
  //callback: function(){}
});
//-------------------------------------------------------------------------------------------


/////////////////////////////////////////////////////////////////////////////////////////////
// acordeones de contenido a mano V.2, mandan bien el foco

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

//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("fast");//cerrar
   acordeon_abierto = "ninguno";//resetear la variable
  } else {//si no es este mismo
   $("#" + 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
   });
	 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
  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;
} );
//-------------------------------------------------------------------------------------------


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


/////////////////////////////////////////////////////////////////////////////////////////////
// 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;
   window.location = "#web";
  }
  estado_estilo = false;
 } else {
  for(i=0;i<document.styleSheets.length;i++) {
   document.styleSheets[i].disabled=false;
   window.location = "#web";
  }
  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
if ( $("#noticias .titulo_noticia").size() > 1 ) {//variable en true en caso de que haya solo una noticia
  var solo_una_noticia = false;
  $("#noticias .acordeon_noticias").hide();// ocultar todos los acordeones inicialmente
} else {
  var solo_una_noticia = true;
}

//$("#noticias .acordeon_noticias:first").show();// mostrar el primero
//noticia_abierta = $("#noticias h3:first").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");
		}
  }																 
);

//abrir y cerrar acordeones de noticias
var noticia_abierta = "ninguno";//recogera el acordeon abierto en todo momento
var titulo_noticia_clicado = "ninguno";//para recoger el titulo que se ha seleccionado

$("#noticias .titulo_noticia").click(function() {
  titulo_noticia_clicado = $(this);//guardar el titulo
	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 ya hay un acordeon abierto
	    if ($(this).attr("id") == noticia_abierta) {//si es este mismo
	      $("#" + noticia_abierta).next().hide("normal");//cerrar
	      noticia_abierta = "ninguno";//resetear la variable
	    } else {//si no es este mismo
	      $("#" + noticia_abierta).next().hide("normal", function () {//cerrar, y al terminar...
	          //calcular si el elemento esta en pantalla
	          var y_elemento = $(titulo_noticia_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_noticia_clicado.attr("id");//si no esta en pantalla, enviar el foco
	          }
	          $(titulo_noticia_clicado).next().show("slow");//abrir el acordeon correspondiente
		        $(this).removeClass("abrir_noticia");// modificar el estilo del titulo              
	        	$(this).addClass("desplegada");
	      });
	      noticia_abierta = $(this).attr("id");//guardar el id del acordeon abierto
	    }
	  } else { //si no hay un acordeon abierto
	    $(this).next().show("slow");//mostrar el acordeon
	    noticia_abierta = $(this).attr("id");//guardar el id del acordeon abierto
	  }
	}
});
//-------------------------------------------------------------------------------------------


/////////////////////////////////////////////////////////////////////////////////////////////
// boton de cerrar resultados de busqueda
$(".cerrar_search_results").click(function() {
  $("#search_results").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[href*=#web]').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;
                        });
                    });
                }
        }
    });
}
//-------------------------------------------------------------------------------------------




/////////////////////////////////////////////////////////////////////////////////////////////
// funciones anteriores al Jquery 

// Envia un formulario
function enviar_form(nombreform){
 document.getElementById(nombreform).submit();		
}

// Envia un comentario
function enviar_comentario(){
 document.getElementById('comentario').submit();		
}
//-------------------------------------------------------------------------------------------


/////////////////////////////////////////////////////////////////////////////////////////////
// zona pruebas

