$(document).ready(function(){

//######### VARIABLES ##########//
	var t = $(".tag:not([id*='todos'])");	
	var tagSel = $("#tag").attr("value");
	var tagsSel = "";
	var proyectos = $(".proyecto");
	
	set_idioma();
	chequeo_idioma();
	
	$("#idioma").attr("value",$.cookie("info3_idioma"));
	var idioma = $.cookie("info3_idioma");
	
//##############################//
	$(".cerrar").css({opacity:"0.2"}).hover(
		function(){
			$(this).stop().animate({opacity:"1"},150);
		},
		function(){
			$(this).stop().animate({opacity:"0.2"},150);
		}
	);
		
//######## MENÚ DE NAVEGACIÓN ######//
	$("#clientes, #clients, #estudio, #portfolio, #bobina, #contacto, #situacion, #noticias, #studio, #portfolio_in, #reel, #contact, #location, #news").hover(
		function(){
			$(this).stop().animate({opacity:"0.3"},250);	
		},
		function(){
			$(this).stop().animate({opacity:"1"},250);	
		}
	).click(function(){
		var s = $(this).attr("id");
		document.location = "index.php?seccion="+s+"&idioma="+idioma;	
	});
	
	$("#rss, #twitter, #facebook").hover(
		function(){
			$(this).find("img").stop().animate({opacity:"0.3"},250);	
		},
		function(){
			$(this).find("img").stop().animate({opacity:"1"},250);	
		}
	);
	
	//---TWITTER
	$("#twitter").click(function(){
		window.open("http://www.twitter.com/infodelmedia");
	});


//############## CONTENIDO PRINCIPAL ###############//
	$("#contenidoWrapper").fadeIn();
	
	$(".cliente").mouseover(function(){
		
		$(this).find("img").stop().animate({opacity:"0.75"},250);
		$(this).find(".infoProyecto").stop().animate({top:"89px"},300);
			
	}).mouseout(function(){
		
		$(this).find("img").stop().animate({opacity:"1"},250);
		$(this).find(".infoProyecto").stop().animate({top:"119px"},300);
		
	});
	
	$(".proyecto").click(function(){
		var id = $(this).find(".infoProyecto").attr("id");
		$("#contenidoWrapper").fadeOut("normal",function(){
			document.location = "index.php?seccion=proyectos&accion=ver&id_proyecto="+id+"&idioma="+idioma;
		});
	}).mouseover(function(){
		
		$(this).filter("[class='proyecto']").find("img").stop().animate({opacity:"0.75"},250);
		$(this).find(".infoProyecto").stop().animate({top:"89px"},300);
			
		if(tagSel == ""){	
			var id = $(this).attr("id");
			var tags = id.split(", ");
			var tag = "";
			var i = "";
		
			t.stop().fadeTo(500,"0.2");
				
			for(i in tags){
				tag = tags[i];
				$(".tag[id*='"+tag+"']").stop().fadeTo(500,"1");
			}
		}
	
	}).mouseout(function(){
		
		$(this).filter("[class='proyecto']").find("img").stop().animate({opacity:"1"},250);
		$(this).find(".infoProyecto").stop().animate({top:"119px"},300);
		
		if(tagsSel == ""){
			t.stop().fadeTo(500,"1");
		} else {
			$(".tag").each(function(){
				var este_tag = $(this).attr("id");
				$(this).stop().fadeTo(500,"1");				
			});
		}
	});
	
	$(".cerrar").click(function(){
		document.location = "index.php?idioma="+idioma;
	});
	
//######## TAGS ########//
	if(tagSel != ""){
		$(".proyecto:not([id*='"+tagSel+"'])").hide();	
	}
	$(".hiderProyecto").css({opacity:"0"});

	t.mouseover(function(){
		var tag = $(this).attr("id");
		//if(tagSel == ""){
			$(".proyecto:not([id*='"+tag+"'])").find(".hiderProyecto").stop().fadeTo(500,"0.8");
			$(".tag:not([id*='"+tag+"'])").stop().fadeTo(200,"0.2");
		//}
	}).mouseout(function(){
		//if(tagSel == ""){
			proyectos.find(".hiderProyecto").stop().fadeTo(500,"0");		
			$(".tag").stop().fadeTo(200,"1");
		//}
	}).click(function(){
		var tag = $(this).attr("id");
		tagSel = tag;
		if($(".proyecto").length > 0){
			$(".proyecto").show();
			$(".proyecto:not([id*='"+tag+"'])").hide();									
		} else {
			document.location = "index.php?tag="+tag+"&idioma="+idioma;	
		}
	
	});
	
	$(".tagTodos").hover(
		function(){
			$(this).stop().animate({opacity:"0.65"});
		},
		function(){
			$(this).stop().animate({opacity:"1"});
		}	
	);
	
	$(".tagTodos, #logoInfodelmedia").click(function(){
		if($(".proyecto").length > 0){
			$("#tag").attr("value","");
			tagSel = "";
			t.stop().fadeTo(500,"1");
			$(".proyecto").show();
			$(".hiderProyecto").css({opacity:"0"});
		} else {
			document.location = "index.php?idioma="+idioma;	
		}
	});

//######## HD ########//
	$("#hd").hover(
		function(){
			$(this).stop().animate({opacity:"0.5"},150);	
		},
		function(){
			$(this).stop().animate({opacity:"1"},150);
		}	
	);
	
	hd_mode();
	
	$("#hd, #hd2").click(function(){
		if($.cookie("info3_hd") == "on"){
			$.cookie("info3_hd","off")	
		} else {
			$.cookie("info3_hd","on")		
		}
		hd_mode();
	});
	
//######## IDIOMA ########//
	
	$("#idioma").fadeTo(0,"0.5").hover(
		function(){
			$(this).stop().fadeTo(250,"1");	
		},
		function(){
			$(this).stop().fadeTo(250,"0.5");	
		}
	).click(function(){
		cambio_idioma();
		if($.cookie("info3_idioma") == "es"){
			$(this).css({backgroundPosition:"0px 0px"});	
		} else {
			$(this).css({backgroundPosition:"0px -23px"});
		}
	});
	
	
//######### PROYECTO #########//
	$("#b_desplegar").hover(
		function(){
			$(this).stop().animate({opacity:"0.2"},150);
		},
		function(){
			$(this).stop().animate({opacity:"1"},150);
		}
	).toggle(
		function(){
			$(this).attr("src","Content/Images/flecha_arriba.png");
			$(".descripcionWrapper").slideDown("normal",
				function(){
					$(this).css({marginTop:"3px"});	
				}			
			);
		},
		function(){
			$(this).attr("src","Content/Images/flecha_abajo.png");
			$(".descripcionWrapper").css({marginTop:"0px"});
			$(".descripcionWrapper").slideUp("normal");
		}	
	);


//########### NOTICIAS #############//
	$(".noticiaWrapper iframe").css({width:"640px", height:"360px"});
	$(".noticiaWrapper iframe").attr({width:"640px", height:"360px"});
	$(".resumenNoticias iframe").css({width:"320px", height:"180px"});
	$(".resumenNoticias iframe").attr({width:"320px", height:"180px"});
	
	//##########//
		
	$(".noticiaEnlaceProyecto").click(function(){
		var id_proyecto = $(this).attr("id");
		document.location = "index.php?seccion=proyectos&accion=ver&id_proyecto="+id_proyecto+"&idioma="+idioma;
	});
	
	$(".noticiaEnlace").click(function(){
		var id = $(this).attr("id");
		open(id);
	});
		
	$("#b_archivo").hover(
		function(){
			$(this).stop().fadeTo(250,"0.5");	
		},
		function(){
			$(this).stop().fadeTo(250,"1");
		}	
	).toggle(
		function(){
			$(".archivoNoticias ul").stop().slideDown();	
		},
		function(){
			$(".archivoNoticias ul").stop().slideUp();	
		}
	);
	
	
	$(".archivoNoticias li").click(function(){
		var mes = $(this).attr("id");
		document.location = "index.php?seccion=noticias&accion=ver&mes="+mes+"&idioma="+$.cookie("info3_idioma");
	});
	
	$(".resumenNoticias .noticiaTitular").click(function(){
		var id = $(this).attr("id");
		document.location = "index.php?seccion=noticias&accion=ver_noticia&idioma="+$.cookie("info3_idioma")+"&id_noticia="+id;
	});
	

//########### PORFOLIO #############//
		
	$("#enlacePorfolio a").click(function(e){
		e.preventDefault();
	});
	
	$("#enlacePorfolio").hover(
		function(){
			$(this).find("a").css({color:"white"});	
		},
		function(){
			$(this).find("a").css({color:"#777777"});	
		}
	).click(function(){
		document.location = $(this).find("a").attr("href");
	});



//########### BOBINA ############//

	
//############## CONTACTO ################//
	
	$("#b_submit_contacto, #b_borrar_contacto").hover(
		function(){
			$(this).stop().animate({opacity:"0.5"},200);
		},
		function(){
			$(this).stop().animate({opacity:"1"},200);
		}		
	);
		
	$("#b_submit_contacto").click(function(){
		if($("#nombre").attr("value") == ""){ 
			msj("<p><img src=\"Content/Images/icono_error_grande.png\">El campo \"Nombre y apellidos\" está vacío.</p>");	
					
		} else if($("#email").attr("value") == ""){
			msj("<p><img src=\"Content/Images/icono_error_grande.png\">El campo \"Comentarios\" está vacío.</p>");	
			
		} else {
			$.ajax({
				url:$("#form_contacto").attr("action"),
				data: $("#form_contacto").serialize(),
				type: 'POST',
				success: function(data){
					var res = $(data).filter("p.res").html();
					if(res == "ok"){
						msj("<p><img src=\"Content/Images/icono_ok_grande.png\">El mensaje se ha enviado correctamente.<br/><br/> Gracias</p>");	
					} else if(res == "error"){
						msj("<p><img src=\"Content/Images/icono_error_grande.png\">Ha ocurrido un error al enviar el mensaje. Inténtelo más tarde.<br/><br/> Sentimos las molestias.</p>");	
					}
				}		
			});	
			return false;
		}	
					
	});
	
	$("#b_borrar_contacto").click(function(){
		$("#form_contacto input, #form_contacto textarea").attr("value","");
	});
	
	
//############# SITUACIÓN ##############//
		
	$("#boton_textos").change(function(){
		if($(this).attr("checked")){
			$("#capa_textos").show();	
		} else {
			$("#capa_textos").hide();	
		}
	});
	
	$("#boton_iconos").change(function(){
		if($(this).attr("checked")){
			$("#capa_iconos").show();	
		} else {
			$("#capa_iconos").hide();	
		}
	});
	
	$("#leyenda_mapa").hover(
		function(){
			$(this).stop().animate({opacity: "1"},200);	
		},
		function(){
			$(this).stop().animate({opacity: "0.75"},200);		
		}
	);
	
	// NUBES
	$("#nubes_01").animate({
			top:"520px",
			left: "1195px",
			opacity: "0"
		},100000
	);
	
	$("#nubes_02").animate({
			top:"520px",
			left: "2000px",
			opacity: "0"
		},100000
	);
	
	$("#nubes_03").animate({
			top:"115px",
			left: "100px",
			opacity: "0"
		},100000
	);
	
	$("#nube").animate({
			top:"115px",
			left: "-740px",
			opacity: "0"
		},100000
	);

	
	//spain();
	//navidad();
	/*
	
	$("#proyecto").data("hd","off");
	$("body").data("idioma","es");
	//$("body").data("seccion","contacto");
	$("body").data("seccion",$("#seccion").attr("value"));
	
	
	//######## COMPORTAMIENTO PRINCIPAL ######//
	
	var seccion = $("body").data("seccion");
	
	if(seccion == "contacto"){
		$("body").data("seccion","contacto");
		$("#thumbnails").load("Content/Scripts/contacto.php?idioma="+$("body").data("idioma"));
		
	} else if(seccion == "situacion"){
		$("body").data("seccion","situacion");
		$("#thumbnails").load("Content/Scripts/situacion.php?idioma="+$("body").data("idioma"));
				
	} else if(seccion == "porfolio"){
		$("body").data("seccion","porfolio");
		$("#thumbnails").load("Content/Scripts/porfolio.php?idioma="+$("body").data("idioma"));
					
	} else if(seccion == "estudio"){
		$("body").data("seccion","estudio");
		$("body").data("id_proyecto","0");
		$("#thumbnails").load("Content/Scripts/proyectos.php?accion=ver&id_proyecto=0&idioma="+$("body").data("idioma"));
				
	} else if(seccion == "noticias"){
		var f = new Date();
		var mes = f.getMonth()+1+"";
		if(mes < 10){mes = "0"+mes;}
		var anio = f.getFullYear()+"";
		var fecha = anio+mes;
		$("body").data("seccion","noticias");
		$("#thumbnails").load("Content/Scripts/noticias.php?idioma="+$("body").data("idioma"));
				
	} else if(seccion == "bobina"){
		$("body").data("seccion","bobina");
		$("#thumbnails").load("Content/Scripts/bobina.php?idioma="+$("body").data("idioma"));
				
	} else {
		$("body").data("seccion","proyectos");
		$("#thumbnails").load("Content/Scripts/proyectos.php");
		
	}

	
		
	//######## MENSAJE ERROR ######//
	$("#mensajeWrapper .cerrar").click(function(){
		$("#mensajeWrapper").fadeOut(350);
	});
	
	$("#ayudaWrapper").click(function(){
		$(this).fadeOut("1000");
	});
	
		
	//######## MENÚ DE NAVEGACIÓN ######//
	$("#estudio, #portfolio, #bobina, #contacto, #situacion, #noticias, #studio, #portfolio_in, #reel, #contact, #location, #news").hover(
		function(){
			$(this).stop().animate({opacity:"0.3"},250);	
		},
		function(){
			$(this).stop().animate({opacity:"1"},250);	
		}
	);
	
	$("#rss, #twitter, #facebook").hover(
		function(){
			$(this).find("img").stop().animate({opacity:"0.3"},250);	
		},
		function(){
			$(this).find("img").stop().animate({opacity:"1"},250);	
		}
	);
	
	//---TWITTER
	$("#twitter").click(function(){
		window.open("http://www.twitter.com/infodelmedia");
	});
	
	
	//--- IDIOMA ---//
	$("#idioma").fadeTo(0,"0.5").hover(
		function(){
			$(this).stop().fadeTo(250,"1");	
		},
		function(){
			$(this).stop().fadeTo(250,"0.5");	
		}
	).click(function(){
		cambio_idioma($("body").data("idioma"));
		if($("body").data("idioma") == "es"){
			$(this).css({backgroundPosition:"0px 0px"});	
		} else {
			$(this).css({backgroundPosition:"0px -23px"});
		}
	});

	
	//--- CONTACTO ---//
	$("#contacto, #contact").click(function(){
		if($("body").data("seccion") != "contacto"){
			$("body").data("seccion","contacto");
			$("#thumbnails").load("Content/Scripts/contacto.php?idioma="+$("body").data("idioma"));
			return false;			
		}
	});
	
	//--- SITUACION ---//
	$("#situacion, #location").click(function(){
		if($("body").data("seccion") != "situacion"){
			$("body").data("seccion","situacion");
			$("#thumbnails").load("Content/Scripts/situacion.php?idioma="+$("body").data("idioma"));
			return false;			
		}
	});
	
	//--- PORFOLIO ---//
	$("#portfolio, #portfolio_in").click(function(){
		if($("body").data("seccion") != "porfolio"){
			$("body").data("seccion","porfolio");
			$("#thumbnails").load("Content/Scripts/porfolio.php?idioma="+$("body").data("idioma"));
			return false;			
		}
	});
	
	//--- ESTUDIO ---//
	$("#estudio, #studio").click(function(){
		if($("body").data("seccion") != "estudio"){
			$("body").data("seccion","estudio");
			$("body").data("id_proyecto","0");
			$("#thumbnails").load("Content/Scripts/proyectos.php?accion=ver&id_proyecto=0&idioma="+$("body").data("idioma"));
			return false;			
		}
	});
	
	//--- NOTICIAS ---//
	$("#noticias, #news").click(function(){
		if($("body").data("seccion") != "noticias"){
			var f = new Date();
			var mes = f.getMonth()+1+"";
			if(mes < 10){mes = "0"+mes;}
			var anio = f.getFullYear()+"";
			var fecha = anio+mes;
			$("body").data("seccion","noticias");
			$("#thumbnails").load("Content/Scripts/noticias.php?idioma="+$("body").data("idioma"));
			return false;
		}
	});
	
	//--- BOBINA ---//
	$("#bobina, #reel").click(function(){
		if($("body").data("seccion") != "bobina"){
			$("body").data("seccion","bobina");
			$("#thumbnails").load("Content/Scripts/bobina.php?idioma="+$("body").data("idioma"));
			return false;			
		}
	});
	*/
});





