$(document).ready(function() {	
	// Variables
	
	// Preparamos los datos para cualquier llamada ajax
	$.ajaxSetup({
		global: false,
		type: "POST",
		dataType: "html"
	});
	
	//Precarga de imagenes del css
	if(window.location.href.indexOf('localhost') == -1){ //Parche para evitar su uso en localhost
		$.preloadCssImages();
	}
        
        ///******** Destacados de portada (noticias y eventos) **********/
        //Desactivamos enlace
        $('ul.listado_destacados li a').bind('click',function(event){
            event.preventDefault();
        });
        //Añadimos clase hover al pasar por encima y quitamos al salir
        $('ul.listado_destacados li').bind('mouseenter',function(){
            $(this).addClass('tr-hover')
        });
        $('ul.listado_destacados li').bind('mouseleave',function(){
            $(this).removeClass('tr-hover')
        });
        //Al clickar vamos al enlace
        $('ul.listado_destacados li').bind('click',function(){
           var elemento_actual = $(this).attr('id');
           var enlace_actual = $('ul.listado_destacados li#' + elemento_actual + ' a').attr('href');
           window.location.href = enlace_actual;
        });
         ///******** Fin destacados de portada (noticias y eventos) **********/
        
        ///******** MOSTRAR MENUS DESPEGABLES DE RUTA GENERAL **********/
	function desplegarMenu(menuDesplegar,position_left,position_top){
            //Escondemos resto capas
            $('#contenedor_ruta_general div').each(function(){
                    $(this).hide();
                    //Ponemos todo el resto de enlaces sin classe hover
                    $('#ruta_general a').each(function(){
                    if($(this).hasClass('a-ruta-hover') && $(this).attr('rel') != menuDesplegar){
                            $(this).removeClass('a-ruta-hover');
                            $(this).parent().removeClass('li-ruta-hover');
                    }
            });

            });
            $('a[rel="' + menuDesplegar + '"]').addClass('a-ruta-hover');
            $('a[rel="' + menuDesplegar + '"]').parent().addClass('li-ruta-hover');
            //Mostramos la capa que se nos marca
            $('div.' + menuDesplegar).css({'position':'absolute','left':'' + position_left + 'px', 'top':'' + position_top + 'px','z-index':'100'});
            $('div.' + menuDesplegar).show();
            
            //Sacamos union enlace-capa
            existe_capa = 0;
            hay_siguiente = 0;
            $('#contenedor_ruta_general div').each(function(){
                 if($(this).hasClass(menuDesplegar)){
                     existe_capa = 1;
                 } 
            });
            level_actual = menuDesplegar.replace('niv_','');
            level_siguiente = parseInt(level_actual) + 1;
            rel_siguiente = 'niv_' + level_siguiente;
            $('#contenedor_ruta_general li a').each(function(){
                 if($(this).attr('rel') == rel_siguiente){
                     hay_siguiente = 1;
                 } 
            });
            $('#contenedor_ruta_general div').each(function(){
                 if($(this).hasClass(menuDesplegar)){
                     existe_capa = 1;
                 } 
            });
            if(existe_capa == 1){
                var anchura = $('a[rel="'+ menuDesplegar + '"]').width();
                var anchura_final = (hay_siguiente == 1) ? anchura + 36 : anchura + 52;
                var id_li = $('a[rel="'+ menuDesplegar + '"]').parent().attr('id');
               $('#' + id_li + ' div.union').css({width : anchura_final + 'px', display : 'block', height: '1px', background: '#191919', position : 'absolute', 'left':'' + position_left + 'px', 'top':'' + (position_top-1) + 'px','z-index':'100'})
            }
            
	}
	
	function cerrarMenu(){
            //Escondemos todas capas
            $('#contenedor_ruta_general div').each(function(){
                    $(this).hide();
            });
            $('#ruta_general a').each(function(){
                    if($(this).hasClass('a-ruta-hover')){
                            $(this).removeClass('a-ruta-hover');
                            $(this).parent().removeClass('li-ruta-hover');
                    }
            });
	}
	
	function esperarCerrar(){
            closetimer = window.setTimeout(cerrarMenu,500);
	}
	
	function cancelarCerrar(){
            if(typeof closetimer != "undefined"){
                    window.clearTimeout(closetimer);
                    closetimer = null;
            }
	}
	
	$('#ruta_general a').bind('mouseenter',function(){
            var posicion = $(this).position();
            var pos_left = posicion.left;
            var pos_top = posicion.top;
            var pos_left_final = pos_left-12;
            var pos_top_final = pos_top + 28;
            var capa_actual = $(this).attr('rel');
            cancelarCerrar();
            desplegarMenu(capa_actual,pos_left_final,pos_top_final);
	});
	$('#contenedor_ruta_general div').bind('mouseenter',function(){
            var posicion = $(this).position();
            var pos_left = posicion.left;
            var pos_top = posicion.top;
            var capa_actual = $(this).attr('id');
            cancelarCerrar();
            desplegarMenu(capa_actual,pos_left,pos_top_final);
	});
	$('#ruta_general a').bind('mouseleave',function(){
            esperarCerrar();
	});
	$('#contenedor_ruta_general div').bind('mouseleave',function(){
            esperarCerrar();
	});
	$('body').bind('click',function(){
            cerrarMenu();
	})
	///******** FIN MOSTRAR MENUS DESPEGABLES DE RUTA GENERAL **********/
        
        ///******** ARCHIVOS ADJUNTOS **********/
	//Mouseover-Mouseleave de la capa
	$('.contenedor-adjunto').bind('mouseover',function(){
		$(this).addClass('adjunto-hover');
	});
	$('.contenedor-adjunto').bind('mouseleave',function(){
		$(this).removeClass('adjunto-hover');
	});
	//Prevenimos el enlace por defecto
	$('.contenedor-adjunto a').bind('click',function(event){
		event.preventDefault();
	});
	//Abrimos enlace al clickar en cualquier parte de la capa
	$('.contenedor-adjunto').bind('click',function(){
		var enlace_rel = $(this).attr('id');
		var enlace = $('a[rel="' + enlace_rel + '"]').attr('href');
		window.open(enlace);
	});
	
	///******** FIN ARCHIVOS ADJUNTOS **********/
        
        ///******** AREA CLIENTES **********/
        //Obtencion de formulario y esconderlo
        $('#menu_principal .area-clientes a').bind('click',function(event){
            event.preventDefault();
            if($(this).parent().hasClass('active')){
                $(this).parent().removeClass('active');
                $('#form-area-clientes').hide();
            }else{
                $(this).parent().addClass('active');
                var posicion = $(this).position();
                $('#form-area-clientes').css({'position':'absolute','left':posicion.left + 'px','top':(posicion.top+30) + 'px'});
                $('#form-area-clientes').show();
            }
        });
        
        $('#form-area-clientes input[type="submit"]').bind('click',function(){
            $('#form-area-clientes').hide();
            $('#menu_principal .area-clientes').removeClass('active');
        });
        
        //Input hover-out
        $('#form-area-clientes input[type="text"]').bind('focusin',function(){
            $(this).addClass('input-hover');
        });
        
        $('#form-area-clientes input[type="password"]').bind('focusin',function(){
            $(this).addClass('input-hover');
        });
        
         $('#form-area-clientes input[type="text"]').bind('focusout',function(){
            $(this).removeClass('input-hover');
        });
        
        $('#form-area-clientes input[type="password"]').bind('focusout',function(){
            $(this).removeClass('input-hover');
        });
        
         ///******** FIN AREA CLIENTES **********/	
       
});
