$(document).ready(function(){
	
	var seccion = $("#seccion").attr("value");
	
	$("#"+seccion).addClass("menu_sel");
	
	if(seccion == "convocatorias" || seccion == "programas"){
		$("#financiacion").addClass("menu_sel");	
	}
	if(seccion == "blog" || seccion == "encuestas"){
		$("#participa").addClass("menu_sel");	
	}
	if(seccion == "cursos" || seccion == "jornadas" || seccion == "conferencias" || seccion == "concursos" || seccion == "dia_europa"){
		$("#actividades").addClass("menu_sel");	
	}
	if(seccion == "boletines" || seccion == "publicaciones"){
		$("#documentos").addClass("menu_sel");	
	}
	
	//##### ALTURAS CONTENEDORES #####//
	
	var right = $(".contenido_Right").height();
	var cleft = $(".contenido_Left").height();
	
	
	/*
	if(seccion == "convocatorias_ue" || seccion == "programas_ue" || seccion == "noticias_ue"){
		$(".contenido_Right").css({height:cleft+"px"});		
	} else {
		if(right > cleft){
			$(".contenido_Left").css({height:right+"px"});
		} else {
			$(".contenido_Right").css({height:cleft+"px"});	
		}
	}
	*/
	
	$("#logo_Wrapper").click(function(){
		document.location = "index.php";
	});
	
	//##### MENÚ PRINCIPAL #######//
	
	$("#menuNav li").hover(
		function(){
			$(this).find(".subMenu").show();	
		},
		function(){			
			$(this).find(".subMenu").hide();
		}
	);
	
	
	$(".b_menuNav").click(function(){
		var s = $(this).parent().attr("id");
		if(s != seccion && s != ""){
			seccion = s;
			document.location = "index.php?seccion="+s;				
		}
	});
	
	$("ul.subMenu li").click(function(){
		var s = $(this).attr("id");
		//if(s != seccion){
		seccion = s;
		document.location = "index.php?seccion="+s;				
		//}
	});
	
	


	$("#firma").click(function(){
		window.open("http://www.infodelmedia.com");	
	});
	
	$(".b_leer_mas").click(function(){
		var s = $("#seccion").attr("value");
		if(s == "inicio"){s = "noticias";}
		var id = $(this).parent().parent().attr("id");
		document.location = "index.php?seccion="+s+"&accion=ver&id="+id;
	});
	
	$(".b_volver").click(function(){
		if($("#busqueda").attr("value") == "ok"){
			history.back();
		} else {
			var seccion = $("#seccion").attr("value");
			document.location = "index.php?seccion="+seccion;
		}
	});

//####### PIE #############//

	$("#enlace_diputacion").click(function(){
		window.open("http://www.malaga.es");
	});
	
	$("#enlace_comision").click(function(){
		window.open("http://ec.europa.eu/index_es.htm");
	});
	
	$("#enlace_junta").click(function(){
		window.open("http://www.juntadeandalucia.es/presidencia/impe/presidencia/navegacion?idSeccion=830e90a6-30bf-11de-a10b-00127968a9ba&idMenuIzq=830e90a6-30bf-11de-a10b-00127968a9ba");
	});
//####### BÚSQUEDA ########//
	
	$("#clave").focus(function(){
		$(this).attr("value","");
	}).keyup(function(e){
		var keycode;
		var clave = $(this).attr("value");
		
		if (window.event) {
			keycode = window.event.keyCode;
		} else if (e) {
			keycode = e.which;
		} else {
			return true;
		}
		
		if (keycode == 13) {
			document.location = "index.php?seccion=busqueda&clave="+clave;	
		}			
	});
	
	$("#b_buscar").click(function(){
		var clave = $("#clave").attr("value");
		if(clave != "Buscar…"){
			document.location = "index.php?seccion=busqueda&clave="+clave;
		}
	});
	
	
//####### AGENDA ########//
	$(".dia_sel").click(function(){
		var fecha = $(this).attr("id");
		document.location = "index.php?seccion=busqueda&clave="+fecha;
	}).mouseover(function(){
		
		var clave = $(this).attr("id");
		
		var pos = $(this).find(".pos").attr("id").split("#");
		var fila = parseInt(pos[0]);
		var col = parseInt(pos[1])-1;
		
		//var ypos = (fila*27) + 57;
		//var xpos = (col*31) - 323;
		if(explorer()){
			var ypos = (fila*27) + 318;
			var xpos = (col*31) + 415;
		} else {
			var ypos = (fila*27) + 318;
			var xpos = (col*31) + 420;	
		}
		$("#info_Wrapper").css({top:ypos+"px", left:xpos+"px"});
		$(".info").load("Content/Scripts/ajax_agenda.php?accion=dia&clave="+clave,function(){
			$(this).parent().show();
		});
		
	}).mouseout(function(){
		$("#info_Wrapper").fadeOut(0,function(){
			$(".info").html("");
		});			
	});
	
	var num_meses = $(".mes").length;
	var mes = 1;
	$(".b_izq").hide();
			
	$("#meses").css({width:(num_meses*220)+"px"});
	
	$(".b_der").click(function(){
		if(mes < num_meses){
			$("#meses").animate({marginLeft:"-=220px"},"normal");
			mes++;
			
			$(".b_izq").show();
			if(mes == num_meses){
				$(this).hide();	
			}
		}	
	});
	
	$(".b_izq").click(function(){
		if(mes > 1){
			$("#meses").animate({marginLeft:"+=220px"},"normal");
			mes--;
			
			$(".b_der").show();
			if(mes == 1){
				$(this).hide();
			}
		}
	});
//####### NOTICIAS UE ########//

	$("#noticias_ue").cycle();
	
	
//####### ENCUESTA ########//
	$(".b_encuesta").live("click",function(){
		var voto = $(this).attr("id");
		var id = $(this).parent().attr("id");
		if($.cookie("opie"+id) == "voto"){
			alert("Ya ha votado en esta encuesta");
		} else {
			$.cookie("opie"+id,"voto",{expires:7});
			$(this).parent().load("Content/Scripts/ajax_encuestas.php?accion=votar&id_encuesta="+id+"&voto="+voto);
		}
	});

//####### PAGINACIÓN ######//
	$(".b_pagina, .b_pag_izq, .b_pag_der").click(function(){
		var p = $(this).attr("id");
		var s = $("#seccion").attr("value");
		
		document.location = "index.php?seccion="+s+"&p_inicio="+p;
	});
	
//##### BANNERS #######//
	
	$(".banner_Wrapper").hover(
		function(){
			$(this).find(".titulo_banner").css({background:"#000"});	
		},
		function(){			
			$(this).find(".titulo_banner").css({background:"#4186cb"});
		}
	).click(function(){
		window.open($(this).attr("id"));	
	});
	
//######### CONVOCATORIAS ##########//
	$(".convocatoria_listado h1").click(function(){
		var id = $(this).parent().attr("id");
		var s = $("#seccion").attr("value");
		document.location = "index.php?seccion="+s+"&accion=ver&id="+id;
	});

//######### CURSOS ##########//
	$(".cartel_curso_th").click(function(){
		var ruta = $(this).find("img").attr("src").replace("th/","");
		window.open(ruta);
	});
	$(".titulo_curso").click(function(){
		var id = $(this).parent().parent().attr("id");
		var s = $("#seccion").attr("value");
		document.location = "index.php?seccion="+s+"&accion=ver&id="+id;
	});
//######### NOTICIAS ##########//
	
	$(".noticias_Wrapper h1, .noticias_resumen_Wrapper h1").click(function(){
		var id = $(this).parent().attr("id");
		var s = $("#seccion").attr("value");
		if(s == "inicio"){
			s = "noticias";	
		}
		document.location = "index.php?seccion="+s+"&accion=ver&id="+id;
	});
		
	$(".listado_meses li").click(function(){
		var mes = $(this).attr("id").substr(4);
		var anio = $(this).attr("id").substr(0,4);
		var s = $("#seccion").attr("value");
		
		document.location = "index.php?seccion="+s+"&mes="+mes+"&anio="+anio;
	});
	
//######### BOLETINES ##########//

	$(".boletines_listado_meses li").click(function(){
		var aniomes = $(this).attr("id");
		window.open("Content/source/pdf/boletines/"+aniomes+"_boletin.pdf");
	});

//######### ENLACES ##########//

	$(".categoria_link h1").click(function(){
		var cat = $(this).parent().attr("id");
		document.location = "index.php?seccion=links&cat="+cat;		
	});

//######### GALERÍAS ##########//

	$(".thumb").click(function(){
		var img = $(this).find("img").attr("src").replace("th/","");
		var pie = $(this).find(".titulo_foto").attr("value");
		$("#foto img").attr("src",img);
		$("#pie_foto").html(pie);
	});

//######### BLOG ##########//

	$("#b_submit_comentario").click(function(){
		if($("#email").attr("value").indexOf("@") > -1 && $("#email").attr("value") != ""){
			$("#comentarios_form").submit();		
		} else {
			alert("La dirección de correo electrónico no es válida");
		}
	});
	
	$(".email_comentario").click(function(){
		var email = $(this).attr("id");
		document.location = "mailto:"+email;
	});

});
