var map;
var geocoder;

$(document).ready(function(){
	
	
    destination_body = $("body").offset().top;
	
    destination_content = $("#content").offset().top;
	
    destination_content2 = destination_body;
	
    $('#featurewrap').slide({
        autoplay: true,
        duration: 10000,
        showSlideIndex: true
    });
	
    $("#header .home, #header .contact, #header .rss").fadeTo("fast", 0.2);
	
    $("#header .home, #header .contact, #header .rss").hover(function() {
        $(this).stop().fadeTo("fast", 1.0); 	//Aplica opacidad 1,0 al menu header
    }, function() {
        $(this).stop().fadeTo("fast", 0.2); 	//Restablecemos la opacidad de origen
    });
	
    $(".noticia-secundaria,.noticia-secundaria-bg").find(".img_gris").css({
        "opacity":"0.4"
    });
	
    $(".noticia-secundaria,.noticia-secundaria-bg").hover(function() {
        $(".img_noticia_pic span",this).stop().fadeTo("fast", 1.0);
    }, function() {
        $(".img_noticia_pic span",this).stop().fadeTo("slow", 0);
    });
	
    $('.subir').click(function(){
        $("html:not(:animated),body:not(:animated)").animate({
            scrollTop: destination_body
        }, 1100 );
    });
	
    if(sub){
        destination_content2 = $("#content").offset().top;
        $("html:not(:animated),body:not(:animated)").animate({
            scrollTop: destination_content2
        }, 1100);
    }
	
    $("html:not(:animated),body:not(:animated)").scrollTop(destination_content2);
	
    $("#header-col p a").hover(function() {
        $("img.lol",this).stop().fadeTo("slow", 0);
        $("span",this).stop().fadeTo("slow", 1.0);
    }, function() {
        $("img.lol",this).stop().fadeTo("slow", 1.0);
        $("span",this).stop().fadeTo("slow", 0);
    });
	
    $('.miembro').hover(function() {
        map = $(this).find('#map_canvas');
        if(map.is(':visible')){
			
        }else{
            $(this).css({
                'background-color':'#f6f6f7'
            });
        }
    }, function() {
        map = $(this).find('#map_canvas');
        if(map.is(':visible')){
			
        }else{
            $(this).css({
                'background-color':'#fff'
            });
        }
    });
    
	
    $('.miembro p.direc').click(function(e){
        $("#map_canvas").fadeOut().remove();
        $(this).parent().append('<div id="map_canvas"></div>');
        $("#map_canvas").fadeIn();
		
        $('.miembro').css({
            "background-color":"#ffffff"
        });
        $(this).parent().css({
            "background-color":"#f6f6f7"
        });
		
        rel = $(this).attr("rel").split("#");
		
        address = rel[0];
		
        imgagen_direc = rel[1];
		
        map = new GMap2(document.getElementById("map_canvas"));
        map.setCenter(new GLatLng(34, 0), 14);
        geocoder = new GClientGeocoder();
      
        findLocation(address)
        $('.miembro p.direc').css({
            "font-weight":"normal"
        });
        $(this).css({
            "font-weight":"bold"
        });
		
        /*$(".img_lateral img").fadeOut(function(){
				if(imgagen_direc){
					$(".img_lateral img").attr("src","img/miembros/"+imgagen_direc);
				}else{
					$(".img_lateral img").attr("src","img/miembros/anonimo_img.jpg");
				}
				$(".img_lateral img").fadeIn();
		});*/

        /*$("#" + $(this).parent().attr("id") + ".img_lateral img").fadeOut(function(){
				if(imgagen_direc){
					$(this).attr("src","img/miembros/"+imgagen_direc);
				}else{
					$(this).attr("src","img/miembros/anonimo_img.jpg");
				}
				$(this).fadeIn();
		});*/
        $(".img_lateral").fadeOut();
        id_act = $(this).parent().attr("id");
        if(imgagen_direc){
            $("#" + $(this).parent().attr("id") + " .img_lateral img").attr("src","/asociados/"+imgagen_direc).load(function(){$("#" + id_act + " .img_lateral").fadeIn();});
        }else{
            $("#" + $(this).parent().attr("id") + " .img_lateral img").attr("src","img/miembros/anonimo_img.png").load(function(){$("#" + id_act + " .img_lateral").fadeIn();});
        }

        //alert($(this).parent().attr("id"));

        
		
    });
		
	
    prevCalendario = function(){
        $(".prev").click(function(e){
            fragmentoTexto = this.id.split(':');
			
            var mes = parseInt(fragmentoTexto[0]);// mes
            var ano = parseInt(fragmentoTexto[1]);// año
	
            $.ajax({
                beforeSend : function (){
                    $(".calendar").fadeIn();
                    $('#content-text .content-text-midle').append('<div class="loading"><img src="img/ajax-loader.gif" alt="" /></div>');
                },
                type: "POST",
                url: "bin/newcalendario.ajax.php",
                data:"anoNuevo="+ano+"&mesNuevo="+mes,
                success: function(msg){
                    $("#calendario").html(msg);
                    $('#content-text .content-text-midle .loading').fadeOut().remove();
                },
                complete : function (){
                    $(".calendar").fadeIn("slow",function(){
                        nextCalendario();
                        prevCalendario();
                    });
                }
            });
        });
    }

    nextCalendario = function(){
        $(".next").click(function(e){
            fragmentoTexto = this.id.split(':');
			
            var mes = parseInt(fragmentoTexto[0]);// mes
            var ano = parseInt(fragmentoTexto[1]);// año
	
			
            $.ajax({
                beforeSend : function (){
                    $(".calendar").fadeIn();
                    $('#content-text .content-text-midle').append('<div class="loading"><img src="img/ajax-loader.gif" alt="" /></div>');
                },
                type: "POST",
                url: "bin/newcalendario.ajax.php",
                data:"anoNuevo="+ano+"&mesNuevo="+mes,
                success: function(msg){
                    $("#calendario").html(msg);
                    $('#content-text .content-text-midle .loading').fadeOut().remove();
                },
                complete : function (){
                    $(".calendar").fadeIn("slow",function(){
                        nextCalendario();
                        prevCalendario();
                    });
                }
            });
        });
    }
	
    $("#multimedia ul li a").fancybox({
        'centerOnScroll'			: true
    });
    $("#aviso").fancybox({
        'width'				: 500,
        'height'			: 315,
        'centerOnScroll'	: true,
        'type'				: 'iframe'
    });
    $("#terminos").fancybox({
        'width'				: 650,
        'height'			: 450,
        'centerOnScroll'	: true,
        'type'				: 'iframe'
    });
	
});


cargarNoticia = function(id){
	
    $.ajax({
        type: "POST",
        url: "ajax/cargarnoticias.php",
        data: "idNoticia="+id,
        async:true,
        global: true,
        ifModified: false,
        processData:true,
        dataType: "html",
        beforeSend: function(objeto){
            text_content_height = $("#text_content").height();
        		
        		
            $('#content-text .content-text-midle').append('<div class="loading"><img src="img/ajax-loader.gif" alt="" /></div>');
        /*$("#text_content .header_noticia h1, #text_content .header_noticia .fecha, #text_content .contenido_noticia").fadeOut(function(){
        			

        		});*/
        },
        error: function(objeto, quepaso, otroobj){
            alert("Error al cargar noticia");
        },
        success: function(datos){
            if(datos!="error"){
                noticia = datos.split("#");
					
                $("#text_content .header_noticia h1, #text_content .header_noticia .fecha, #text_content .contenido_noticia, .img_lateral img").fadeOut(function(){
                    $("#text_content .header_noticia h1").html(noticia[0]);
                    $("#text_content .header_noticia .fecha").html(noticia[1]);
                    $("#text_content .contenido_noticia").html(noticia[2]);
                    $(".img_lateral img").attr("src","noticias/pics/"+noticia[3]+"_lateral.jpg");
					
                    $("#text_content .header_noticia h1, #text_content .header_noticia .fecha, #text_content .contenido_noticia, .img_lateral img").fadeIn(function(){
                        $('#content-text .content-text-midle .loading').fadeOut().remove()
                    });
                });
            }
        },
        complete: function(objeto, exito){
            $("html:not(:animated),body:not(:animated)").animate({
                scrollTop: destination_content
            }, 1100 );
        }
    });

}


ajaxDiaCalendario = function(id){
    $.ajax({
        type: "POST",
        url: "ajax/cargarevento.php",
        data: "fecha_evento="+id,
        async:true,
        global: true,
        ifModified: false,
        processData:true,
        dataType: "html",
        beforeSend: function(objeto){
            $('#content-text .content-text-midle').append('<div class="loading"><img src="img/ajax-loader.gif" alt="" /></div>');
        },
        error: function(objeto, quepaso, otroobj){
            alert("Error al cargar evento");
        },
        success: function(datos){
            if(datos!="error"){
                $("#prox_evento").fadeOut(function(){
                    $(this).html(datos);
                    $("#prox_evento").fadeIn(function(){
                        $('#content-text .content-text-midle .loading').fadeOut().remove()
                    });
                });
            }
        },
        complete: function(objeto, exito){
            prox_evento = $("#prox_evento").offset().top;
            $("html:not(:animated),body:not(:animated)").animate({
                scrollTop: prox_evento
            }, 1100 );
        }
    });

}

		
