$(document).ready(function(){
	
	var seccion = $("#seccion").attr("value");
	
	//##### IDIOMA #######//
	
	if($.cookie("fdm_idioma") == null){
		$.cookie("fdm_idioma","es");	
	}
	
	var idioma = $.cookie("fdm_idioma");
	$("#"+idioma).addClass("boton_idioma_sel");
	
	/*
	$(".boton_idioma").click(function(){
		$.cookie("fdm_idioma",$(this).attr("id"));
		document.location = "index.php?seccion="+seccion+"&idioma="+$(this).attr("id"); 
	});
	*/
		
	//####### BANNERS #######//	
	$("#banner_Wrapper").cycle({
		fx: 'fade', 
   		speed: 2000,
   		timeout: 5000	
	}).click(function(){
		document.location = "index.php";
	});
	
	//##### MENÚ PRINCIPAL #######//
	$("#menuNav li, #menuNav_en li, #menuPie li").click(function(){
		var s = $(this).attr("id");
		seccion = s;
		document.location = "index.php?seccion="+s;		
	});
	
	$("#b_contacto").click(function(){
		document.location = "index.php?seccion=contacto";
	});
	
	$("#b_facebook").click(function(){
		window.open("http://www.facebook.com/pages/Festival-de-Malaga-Cine-Espanol-Oficial/166082873439010");
	});
	
	$("#b_twitter").click(function(){
		window.open("http://twitter.com/festivalmalaga");
	});
	
	$("#b_youtube").click(function(){
		window.open("http://www.youtube.com/malagafestival");
	});
	
	var rss = false;
	
	$("#b_rss").click(function(){
		if(!rss){
			rss = true;
			$("#rss_Wrapper").show();
		} else {
			rss = false;
			$("#rss_Wrapper").hide();
		}		
	});
	
	$("#rss_Wrapper li a").click(function(){
		if(!rss){
			rss = true;
			$("#rss_Wrapper").show();
		} else {
			rss = false;
			$("#rss_Wrapper").hide();
		}	
	});
	
	$("#albeniz").click(function(){
		window.open("http://www.cinealbeniz.com");
	});
		
	$(".boton_volver").click(function(){
		if(seccion == "inscripcion"){
			document.location = "index.php";		
		} else {
			document.location = "index.php?seccion="+seccion;			
		}
	});
			
	//##### SUBMENÚ PORTADA #######//
	$("#subMenu li").click(function(){
		var s = $(this).attr("id");
		seccion = s;
		document.location = "index.php?seccion="+s;
							
	});
	
	//####### PAGINACIÓN ######//
	$(".b_pagina, .b_pag_izq, .b_pag_der").click(function(){
		var p = $(this).attr("id");
		var s = $("#seccion").attr("value");
		var a = $("#accion").attr("value");
		var orden = $("#orden").attr("value");
		var sentido = $("#sentido").attr("value");
		var cat =$("#id_categoria").attr("value");
		var aniomes = $("#aniomes").attr("value");
		document.location = "index.php?seccion="+s+"&accion="+a+"&p_inicio="+p+"&orden="+orden+"&sentido="+sentido+"&cat="+cat+"&aniomes="+aniomes;
		
	});
	
	//##### ESPACIOS #######//
	var num_espacios = $(".espacio_portada").length;
	var ancho_espacios = num_espacios*220;
	var total_pags_espacios = Math.ceil(num_espacios/4);
	var pag_espacios = 1;
	
	$("#espacios_portada").css({width:ancho_espacios+"px"});
	
	$("#b_der").click(function(){
		if(pag_espacios < total_pags_espacios){
			$("#espacios_portada").animate({marginLeft:"-=872px"},1000);
			pag_espacios++;
		}
	});
	
	$("#b_izq").click(function(){
		if(pag_espacios > 1){
			$("#espacios_portada").animate({marginLeft:"+=872px"},1000);
			pag_espacios--;
		}
	});
	
	$(".boton_google").click(function(){
		window.open($(this).attr("id"));
	});
	
	$(".espacio_enlace").click(function(){
		window.open($(this).attr("id"));
	});
	
	$(".espacio_portada").click(function(){
		document.location = "index.php?seccion=ediciones&accion=espacios";
	});
	
	//####### NOTICIAS ########//
	$(".noticia_listado, .noticia_portada").click(function(){
		var id = $(this).attr("id");
		document.location = "index.php?seccion=noticias&accion=ver&id_noticia="+id;		
	});
	
	
	//####### PELÍCULAS #######//
	$("#b_ver_obras").click(function(){
		var texto = $("#texto_presentacion").css("display");
		if(texto == "none"){
			if(!explorer()){
				$("#peliculas_Wrapper, #actividades_Wrapper, #pelis_Wrapper").hide(0,function(){
					$("#texto_presentacion").show();
					$(".paginacion").hide();
					$("#b_ver_obras").html("ver obras");
				});
			} else {
				$("#texto_presentacion").show();
				$(".paginacion").hide();
				$("#b_ver_obras").html("ocultar presentación");					
			}
		} else {
			if(!explorer()){
				$("#texto_presentacion").hide(0,function(){
				$("#peliculas_Wrapper").show();
					$("#actividades_Wrapper").show();
					$("#pelis_Wrapper").show();
					$(".paginacion").show();
					$("#b_ver_obras").html("ver presentación");
				});
			} else {
				$("#texto_presentacion").hide();
				$(".paginacion").show();
				$("#b_ver_obras").html("ver presentación");	
			}
		}	
	});
		
	var id_pelicula = 0;
		
	$(".pelicula_izq, .pelicula_der").click(function(){
		var id = $(this).attr("id");
		var clase = $(this).attr("class").split("_");
		var lado = clase[1];
		
		if(id_pelicula != id){
			id_pelicula = id;
						
			$(".ficha_pelicula").hide().html("");
			
			$(".marca_pelicula_th_izq, .marca_pelicula_th_der").hide();
			$(this).find(".marca_pelicula_th_izq, .marca_pelicula_th_der").show();
						
			$(this).parent().find(".ficha_pelicula").load("Content/Scripts/ajax_peliculas.php?accion=ver_ficha&id_pelicula="+id+"&lado="+lado,
				function(){
					var ancho = $("iframe").attr("width");
					var alto = $("iframe").attr("height");
					var ratio = ancho/alto;
					var ratio_16_9 = 16/9;
					var ratio_4_3 = 4/3;
					
					var ancho2 = 640;
					var alto2 = ancho2/ratio;
										
					$("iframe").css({width:ancho2+"px", height:alto2+"px"});
					$(this).fadeIn();
				}
			);
			
		} else {
			id_pelicula = 0;
			$(this).parent().find(".ficha_pelicula").fadeOut();			
			$(this).find(".marca_pelicula_th_izq, .marca_pelicula_th_der").hide();
			
		}
	});


//#######################//
	
	$(".subcabecera_menu li, .submenu_secciones li").click(function(){
		var id = $(this).attr("id");
		document.location = "index.php?seccion=secciones&cat="+id;
	});
	
	$(".descargas li").live("click",function(){
		var archivo = $(this).attr("id");
		window.open("Content/source/descargas/peliculas/"+archivo);
	});
	var ficha = false;
	$(".boton_descargas").live("click",function(){
		if(!ficha){
			ficha = true;
			$(this).next().fadeIn();	
		} else {
			ficha = false;
			$(this).next().fadeOut();
		}
	});
	
	//######## EDICIONES ANTERIORES #########//
	$(".submenu_ediciones li").click(function(){
		var accion = $(this).attr("id");
		document.location = "index.php?seccion=ediciones&accion="+accion;	
	});
	
	$(".boton_descarga_edicion").click(function(){
		var pdf = $(this).attr("id");
		window.open("Content/source/pdf/ediciones/"+pdf);
	});
	
	$(".edicion_cartel").click(function(){
		var loc = $(this).find("img").attr("src").replace("th/","");
		window.open(loc);	
	});
	
	//######## PREMIOS #########//
	$(".submenu_premios div").click(function(){
		var id = $(this).attr("id");
		document.location = "index.php?seccion=premios&id_premio="+id;
	});
	
	//######## JURADO ##########//
	var id_jurado = 0;
	
	$(".submenu_jurado li").click(function(){
		var secc = $(this).attr("id");
		var s = replaceAll(" ","[esp]",secc);
		s = replaceAll("ó","[o]",s);
		s = replaceAll("í","[i]",s);		
		document.location = "index.php?seccion=jurado&secc="+s;
	});
	
	$(".miembro_jurado").click(function(){
		var id = $(this).attr("id");
		
		if(id_jurado != id){
			id_jurado = id;
			
			$(".marca_pelicula_th_izq, .marca_pelicula_th_der").hide();
			$(this).find(".marca_pelicula_th_izq, .marca_pelicula_th_der").show();
			
			$(".jurado_info").hide();
			
			$(this).parent().find(".jurado_info").load("Content/Scripts/ajax_peliculas.php?accion=jurado&id_jurado="+id,
				function(){
					$(this).fadeIn();
				}
			);
					
		} else {
			id_jurado = 0;
			$(this).parent().find(".jurado_info").fadeOut();
			$(this).find(".marca_pelicula_th_izq, .marca_pelicula_th_der").hide();
		}
	});
	
	//######## PRENSA #########//
	$(".submenu_prensa div").click(function(){
		var accion = $(this).attr("id");
		document.location = "index.php?seccion=prensa&accion="+accion;
	});	
	
	//####### PATROCINADORES #######//
	$(".logo_patrocinador").click(function(){
		var u = $(this).parent().attr("id");
		window.open(u);	
	});
	
	//####### PUBLICACIONES #######//	
	$(".submenu_publicaciones div").click(function(){
		var sentido = $(this).attr("id");
		document.location = "index.php?seccion=publicaciones&sentido="+sentido;
	});
	
	//####### BASES OFICIALES ########//
	$(".b_bases").click(function(){
		var archivo = $(this).parent().attr("id");
		window.open("Content/source/pdf/documentos/"+archivo);	
	});
	
	//####### INSCRIPCIONES ########//
	$("#contenido").load("Content/Scripts/largometrajes_latino.html");
	
	$("#categoria").change(function(){
		var cat = $(this).attr("value");
		if(cat == "Largometraje"){
			$("#contenido").load("Content/Scripts/largometrajes.html");
		} else if(cat == "Cortometraje"){
			$("#contenido").load("Content/Scripts/cortometrajes.html");
		} else if(cat == "Largometraje Muestra de Cine Latinoamericano"){
			$("#contenido").load("Content/Scripts/largometrajes_latino.html");
		} else if(cat == "Documental"){
			$("#contenido").load("Content/Scripts/documentales.html");
		} else if(cat == "Videocreación"){
			$("#contenido").load("Content/Scripts/videocreacion.html");
		}
	});
	
	
	//############### VALIDACIÓN ###################//
	$("input[type='checkbox']").change(function(){
		if($("#acepto").attr("checked") && $("#privacidad").attr("checked")){
			$("#b_enviar").attr("disabled",false);	
		} else {
			$("#b_enviar").attr("disabled",true);
		}
	});
	
	$("#b_enviar").click(function(){
		if(confirm("Se va a enviar la información. ¿Está seguro?")){
			$("#form_inscripcion").submit();	
		}		
	});
	
//############### PREMIOS ###################//	
	$("#premiados li").click(function(){
		var id_premio = $(this).parent().parent().attr("id");
		var anio = $(this).attr("id");
		document.location = "index.php?seccion=premios&id_premio="+id_premio+"&anio="+anio;
	});

//############### ACREDITACIONES ###################//	

	$("#fecha_llegada").datepick();
	$("#fecha_salida").datepick();
	
	$(".submenu_acreditaciones li").click(function(){
		var tipo = $(this).attr("id");
		document.location = "index.php?seccion=acreditaciones&accion=form&tipo="+tipo;
	});
	
	$("#tipo").change(function(){
		var tipo = $(this).attr("value");
		document.location = "index.php?seccion=acreditaciones&accion=form&tipo="+tipo;		
	});
	
	$("#b_submit_acreditacion").click(function(){
		if($("#nombre").attr("value") == ""){
			alert("El campo \"Nombre\" está vacío");	
		} else if($("#apellidos").attr("value") == ""){
			alert("El campo \"Apellidos\" está vacío");	
		} else if($("#dni").attr("value") == ""){
			alert("El campo \"DNI\" está vacío");	
		} else if($("#foto").attr("value") == ""){
			alert("Hay que adjuntar foto reciente");
		} else if($("#carta_director").attr("value") == ""){
			alert("Hay que adjuntar carta del director del medio");
		} else {
			$("#acreditaciones_form").submit();	
		}			
	});
	
//######### EVENTOS #########//
	
	var id_categoria_actual = "";
	var id_espacio_actual = "";
	
	//----- TAMAÑO EVENTOS -----//
	var altura_programacion = $("#tabla_programa").height()-55;
	$("#eventos_Wrapper").height(altura_programacion);	
	
	//----- FILTROS -----//
		
	$("#logo_uniticket").click(function(){
		window.open("http://www.unientradas.es");	
	});
	
	// Filtro categorias	
	$("#b_filtro_eventos_todos").click(function(){
		$(".evento").show();
		$(".lista_filtros_eventos_categorias li").removeClass().addClass("filtro_on");
		$(".lista_filtros_eventos_espacios li").removeClass().addClass("filtro_on");
		$.cookie("filtros_espacios",espacios());
		$.cookie("filtros_categorias",categorias());
		
	});
	
	$("#b_filtro_eventos_ninguno").click(function(){
		$(".evento").hide();
		$(".lista_filtros_eventos_categorias li").removeClass().addClass("filtro_off");	
		$(".lista_filtros_eventos_espacios li").removeClass().addClass("filtro_on");
		$.cookie("filtros_espacios",espacios());
		$.cookie("filtros_categorias",categorias());		
	});
	
	// Comprobación inicial de filtro de estados
	if($.cookie("filtros_categorias") != null && $.cookie("filtros_categorias") != ""){
		
		$(".evento").hide();
		$(".lista_filtros_eventos_categorias li").removeClass().addClass("filtro_off");	 // Reset
		
		var cats = $.cookie("filtros_categorias").split(",");
		var i = 0;
		for(i=0;i<cats.length;i++){
			var id = cats[i];
			$(".lista_filtros_eventos_categorias li").each(function(){
				if($(this).attr("id") == id){
					$(this).removeClass().addClass("filtro_on");	
				}
			});
			$(".evento").each(function(){
				if($(this).find(".categoria").attr("value") == id){
					$(this).show();	
				}
			});
		}
	} else {
		// cookie
		$(".lista_filtros_eventos_categorias li").removeClass().addClass("filtro_on");
		$.cookie("filtros_categorias",categorias());			
	}	
		
	$(".lista_filtros_eventos_categorias li").click(function(){
		var id_categoria = $(this).attr("id");
		var clase = $(this).attr("class");
		
		if(clase == "filtro_on"){
			$(this).removeClass().addClass("filtro_off");	
		} else {
			$(this).removeClass().addClass("filtro_on");
		}
		
		$(".evento").each(function(){
			if($(this).find(".categoria").attr("value") == id_categoria){
				var disp = $(this).css("display");
				if(clase == "filtro_off"){
					$(this).show();
				} else {
					$(this).hide();
				}		
			}			
		});	
		
		$.cookie("filtros_espacios",espacios());
		$.cookie("filtros_categorias",categorias());
				
	});
	
	// Filtro espacios
	var altura_filtros_cat = $("#filtros_eventos_categorias").height();
	if(altura_filtros_cat > 255 && altura_filtros_cat < 310){
		$("#filtros_eventos_leyenda").height(altura_filtros_cat);
	} else if(altura_filtros_cat < 255){
		$("#filtros_eventos_leyenda").height(255);	
	}
	
	$("#b_filtro_espacios_todos").click(function(){
		$(".evento").show();
		$(".lista_filtros_eventos_categorias li").removeClass().addClass("filtro_on");	
		$(".lista_filtros_eventos_espacios li").removeClass().addClass("filtro_on");
		$.cookie("filtros_espacios",espacios());
		$.cookie("filtros_categorias",categorias());
	});
	
	$("#b_filtro_espacios_ninguno").click(function(){
		$(".evento").hide();
		$(".lista_filtros_eventos_categorias li").removeClass().addClass("filtro_on");
		$(".lista_filtros_eventos_espacios li").removeClass().addClass("filtro_off");
		$.cookie("filtros_espacios",espacios());
		$.cookie("filtros_categorias",categorias());
	});
	
	// Comprobación inicial de filtro de estados
	if($.cookie("filtros_espacios") != null){
		
		$(".evento").hide();
		$(".lista_filtros_eventos_espacios li").removeClass().addClass("filtro_off"); // Reset
		
		var cats = $.cookie("filtros_espacios").split(",");
		var i = 0;
		for(i=0;i<cats.length;i++){
			var id = cats[i];
			$(".lista_filtros_eventos_espacios li").each(function(){
				if($(this).attr("id") == id){
					$(this).removeClass().addClass("filtro_on");	
				}
			});
			$(".evento").each(function(){
				if($(this).find(".siglas").attr("id") == id){
					$(this).show();	
				}
			});
		}
	} else {
		// cookie
		$(".lista_filtros_eventos_espacios li").removeClass().addClass("filtro_on");
		$.cookie("filtros_espacios",espacios());			
	}
	
	$(".lista_filtros_eventos_espacios li").click(function(){
		var id_espacio = $(this).attr("id");
		var clase = $(this).attr("class");
		
		if(clase == "filtro_on"){
			$(this).removeClass().addClass("filtro_off");	
		} else {
			$(this).removeClass().addClass("filtro_on");
		}
		
		$(".evento").each(function(){
			if($(this).find(".siglas").attr("id") == id_espacio){
				var disp = $(this).css("display");
				if(clase == "filtro_off"){
					$(this).show();
				} else {
					$(this).hide();
				}		
			}		
		});
		
		$.cookie("filtros_espacios",espacios());
		$.cookie("filtros_categorias",categorias());		
		
	});
	
	$("#dias_festival li").click(function(){
		// cookie
		if($(".espacio").length > 0){
			$.cookie("filtros_espacios",espacios());
			$.cookie("filtros_categorias",categorias());
		}
		var fecha = $(this).attr("id");
		document.location = "index.php?seccion=eventos&accion=listar&fecha="+fecha;
	});
	
	$(".evento").hover(
		function(){
			var num_eventos = $(".evento").length;
			$(".evento").css({zIndex:"1"});
			$(this).css({zIndex:num_eventos+1});
			$(this).find(".evento_info_Wrapper").show();	
		},
		function(){
			$(this).find(".evento_info_Wrapper").hide();			
		}	
	);
	
	//######### ACTIVIDADES #############//
	
	$("#b_submit_inscripcion_act").click(function(){
		
		if($("#nombre").attr("value") == ""){
			alert("El campo \"Nombre\" está vacío");
		} else if($("#apellidos").attr("value") == ""){
			alert("El campo \"Apellidos\" está vacío");
		} else if($("#dni").attr("value") == ""){
			alert("El campo \"DNI\" está vacío");
		} else if($("#email").attr("value") == ""){
			alert("El campo \"Correo electrónico\" está vacío");
		} else {
			if(confirm("Se va a enviar la solicitud. ¿Está seguro?")){
				$("#inscripciones_actividades_form").submit();
			}
		}
	});
	
	$(".noticia_Right .boton").live("click",function(){
		var id = $(this).attr("id");
		document.location = "index.php?seccion=inscripciones_actividades&accion=form&id_actividad="+id;
	});	
	
	$("#b_ver_actividades").click(function(){
		var texto = $("#texto_presentacion").css("display");
		if(texto == "none"){
			if(!explorer()){	
				$("#actividades_Wrapper, #pelis_Wrapper").hide(0,function(){
					$("#texto_presentacion").show();
					$(".paginacion").show();
					$("#b_ver_actividades").html("ver actividades");
				});
			} else {
				$("#texto_presentacion").show();
				$(".paginacion").show();
				$("#b_ver_actividades").html("ocultar presentación");
			}
		} else {
			if(!explorer()){		
				$("#texto_presentacion").hide(0,function(){
					$("#actividades_Wrapper").show();
					$("#pelis_Wrapper").show();
					$(".paginacion").show();
					$("#b_ver_actividades").html("ver presentación");
				});
			} else {
				$("#texto_presentacion").hide();
				$("#b_ver_actividades").html("ver presentación");
			}
		}	
	});
	
	$(".subcabecera_act_menu li").click(function(){
		var subcat = $(this).attr("id");
		var cat = $("#id_categoria").attr("value");
		document.location = "index.php?seccion=actividades&cat="+cat+"&subcat="+subcat;
	});
	
	$(".submenu_actividades li").click(function(){
		var cat = $(this).attr("id");
		//var subcat = $("#id_subcategoria").attr("value");
		document.location = "index.php?seccion=actividades&cat="+cat;	
	});
	
		
	var id_actividad = 0;
	
	$(".actividad_izq, .actividad_der").click(function(){
		var padre = $(this).parent().parent().attr("id");
		
		var id = $(this).attr("id");
		var clase = $(this).attr("class").split("_");
		var lado = clase[1];
		
		if(id_actividad != id){
			id_actividad = id;
						
			$(".ficha_pelicula").hide().html("");
			
			$(".marca_pelicula_th_izq, .marca_pelicula_th_der").hide();
			$(this).find(".marca_pelicula_th_izq, .marca_pelicula_th_der").show();
			
			if(padre == "pelis_Wrapper"){			
				$(this).parent().find(".ficha_pelicula").load("Content/Scripts/ajax_peliculas.php?accion=ver_ficha&id_pelicula="+id+"&lado="+lado,
					function(){
						var ancho = $("iframe").attr("width");
						var alto = $("iframe").attr("height");
						var ratio = ancho/alto;
						var ratio_16_9 = 16/9;
						var ratio_4_3 = 4/3;
						
						var ancho2 = 480;
						var alto2 = ancho2/ratio;
											
						$("iframe").css({width:ancho2+"px", height:alto2+"px"});
						$(this).fadeIn();
					}
				);
			} else {
				$(this).parent().find(".ficha_pelicula").load("Content/Scripts/ajax_actividades.php?accion=ver_ficha&id_actividad="+id+"&lado="+lado,
					function(){
						var ancho = $("iframe").attr("width");
						var alto = $("iframe").attr("height");
						var ratio = ancho/alto;
						var ratio_16_9 = 16/9;
						var ratio_4_3 = 4/3;
						
						var ancho2 = 480;
						var alto2 = ancho2/ratio;
											
						$("iframe").css({width:ancho2+"px", height:alto2+"px"});
						$(this).fadeIn();
					}
				);				
			}
			
		} else {
			id_actividad = 0;
			$(this).parent().find(".ficha_pelicula").fadeOut();			
			$(this).find(".marca_pelicula_th_izq, .marca_pelicula_th_der").hide();
			
		}
	});
	
	
	//############### BLOG ##############//
	$(".blog_titulo").click(function(){
		var id = $(this).parent().attr("id");
		document.location = "index.php?seccion=blog&id_entrada="+id;
	});
	
	$(".listado_meses li").click(function(){
		var aniomes = $(this).attr("id");
		document.location = "index.php?seccion=blog&aniomes="+aniomes;
	});
	
	$("#b_submit_comentario").click(function(){
		if($("#email").attr("value").indexOf("@") > -1 && $("#email").attr("value") != ""){
			if($("#nombre").attr("value") == ""){
				alert("El campo \"Nombre\" está vacío");
			} else if($("#comentario").attr("value") == ""){
				alert("El campo \"Comentario\" está vacío");					
			} else {
				$("#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;
	});
});
