$(document).ready(function(){
	
	var seccion = $("#seccion").attr("value");
	var seccion_ant = seccion;
	
	//############ IDIOMA ##########//
	var idioma = $("#idioma").attr("value");
	$.cookie("pd_idioma",idioma);
	
	$("#b_idioma").click(function(){
		if(idioma == "es"){
			idioma = "en";
			$(".es").hide();
			$(".en").show();
			$(this).removeClass().addClass("b_idioma_en");
		} else {
			idioma = "es";
			$(".es").show();
			$(".en").hide();
			$(this).removeClass().addClass("b_idioma_es");
		}
		
		$("#idioma").attr("value",idioma)
		$.cookie("pd_idioma",idioma);
		
		
	});
	
	//############ CORRECCIÓN POSICIÓN CSS ##########//
	var ventana = $(window).width();
	
	if(ventana < 1297){
		$("#main").css({left:"0",marginLeft:"0"});
	} else {
		$("#main").css({left:"50%",marginLeft:"-648px"});
	}
		
	$(window).resize(function(){
		var ventana = $(window).width();
		if(ventana < 1297){
			$("#main").css({left:"0",marginLeft:"0"});
		} else {
			$("#main").css({left:"50%",marginLeft:"-648px"});
		}
	});
		
	var th_flag = false;
	
	
	//##### MENÚ PRINCIPAL #######//
	$("#logo").click(function(){
		seccion = "home";
		document.location = "index.php?seccion="+seccion;		
	});
	
	$("#b_twitter").click(function(){
		window.open("http://www.twitter.com/agumendez");
	});
	
	$("#b_portfolio").click(function(){
		window.open("http://www.behance.net/agumendez");	
	});
	
	$("#b_somethoughts").click(function(){
		seccion = "somethoughts";
		document.location = "index.php?seccion="+seccion;		
	});
	
	$(".menu_nosel, .menu_nosel2").click(function(){
		var s = $(this).attr("id");
		
		if(s != "about" && s != "contact"){
			if(s == "blog"){
				window.open("http://www.pensamientosdeformados.com/blog");	
			} else {
				seccion = s;
				document.location = "index.php?seccion="+s;
			}
		} else {
			seccion_ant = seccion;
			$("#menuNav li#"+seccion+"_Wrapper:visible").slideUp("slow");
			seccion = s;
			$("#menuNav li#"+s+"_Wrapper:hidden").slideDown("slow");
		}
	});
	
	$(".menu_sel, .menu_sel2").click(function(){
		if(seccion != "giftshit"){
			if(th_flag){
				th_flag = false;
				$("#galeria_imagen_Wrapper").hide();
				$("#thumbs_Wrapper").show();				
			}
		} else {
			document.location = "index.php?seccion=giftshit";
		}
	});
	
	//###### INTRO APARICIÓN #######//
	
	$("#intro_Wrapper").click(function(){
		if(seccion != "somethoughts"){
			document.location = "index.php?seccion=home";
		}
	});
		
	var oi1 = $("#oi1").val();
	var oi2 = $("#oi2").val();
	var oi3 = $("#oi3").val();
	var od1 = $("#od1").val();
	var od2 = $("#od2").val();
	var od3 = $("#od3").val();
	
	var ti1 = $("#ti1").val()*1000;
	var ti2 = $("#ti2").val()*1000;
	var ti3 = $("#ti3").val()*1000;
	var td1 = $("#td1").val()*1000;
	var td2 = $("#td2").val()*1000;
	var td3 = $("#td3").val()*1000;	
	
	if(oi1 > 0){
		setTimeout("bocadillo('#b_izq_1')",ti1);
	}
	if(oi2 > 0){
		setTimeout("bocadillo('#b_izq_2')",ti2);
	}
	if(oi3 > 0){
		setTimeout("bocadillo('#b_izq_3')",ti3);
	}
	
	if(od1 > 0){
		setTimeout("bocadillo('#b_der_1')",td1);
	}
	if(od2 > 0){
		setTimeout("bocadillo('#b_der_2')",td2);
	}
	if(od3 > 0){
		setTimeout("bocadillo('#b_der_3')",td3);
	}
	
	
	//########## INTROS #########//
	$(".b_next_month,.b_prev_month").click(function(){
		var m = $(this).attr("id");
		document.location = "index.php?seccion=somethoughts&mes="+m;		
	});
	
	$(".mes_cal td.dia_sel").hover(
		function(){
			$(this).find(".b_zoom").addClass("b_zoom_hover");
		},
		function(){			
			$(this).find(".b_zoom").removeClass("b_zoom_hover");
		}
	).click(function(){
		var fecha = $(this).attr("id");
		document.location = "index.php?seccion=somethoughts&accion=ver&fecha="+fecha;
	});
	
	$(".b_prev_intro").click(function(){
		var fecha = $("#intro_ant").attr("value");
		document.location = "index.php?seccion=somethoughts&accion=ver&fecha="+fecha;		
	});
	$(".b_next_intro").click(function(){
		var fecha = $("#intro_sig").attr("value");
		document.location = "index.php?seccion=somethoughts&accion=ver&fecha="+fecha;		
	});
	
	//########## ILLUSTRATION Y DESIGN #########//
	var pag_actual = 1;
	$(".pag#1").css({color:"#fff"});
		
	$(".pag").click(function(){
		var p = $(this).attr("id");
		$(".listado_proyectos").hide();
		$("#pag"+p).show();
		
		$(".listado_thumbs").hide();
		$("#thumbs"+p).show();
		
		pag_actual = p;
		
		$(".pag").css({color:"#86919b"});
		$(this).css({color:"#fff"});
	});
	
	$(".pag_ant").click(function(){
		if(pag_actual > 1){
			pag_actual--;
			$(".listado_proyectos").hide();
			$("#pag"+pag_actual).show();
			$(".listado_thumbs").hide();
			$("#thumbs"+pag_actual).show();
			$(".pag").css({color:"#86919b"});
			$(".pag:eq("+(pag_actual-1)+")").css({color:"#fff"});
		}
	});
	
	$(".pag_sig").click(function(){
		var total = $(".pag").length;
		if(pag_actual < total){
			pag_actual++;
			$(".listado_proyectos").hide();
			$("#pag"+pag_actual).show();
			$(".listado_thumbs").hide();
			$("#thumbs"+pag_actual).show();
			$(".pag").css({color:"#86919b"});
			$(".pag:eq("+(pag_actual-1)+")").css({color:"#fff"});
		}
	});
	
	var img_actual = 0;
	
	$(".b_next").live("click",function(){
		var total = $("#total").attr("value");
		
		if(img_actual < total-1){
			img_actual++;
		} else {
			img_actual = 0;	
		}
				
		var ancho = $(".anchos:eq("+img_actual+")").attr("value");
		var alto = $(".altos:eq("+img_actual+")").attr("value");
		var archivo = $(".imgs:eq("+img_actual+")").attr("value");
		var fecha = $(".fechas:eq("+img_actual+")").attr("value");
		var fecha_en = $(".fechas_en:eq("+img_actual+")").attr("value");
		
		$(".img_galeria_Wrapper img").attr("src","");

		if(parseInt(ancho) > parseInt(alto)){
			if(ancho > 570){
				$(".img_galeria_Wrapper img").attr("style","").css({width:"570px"});
			}		
		} else {
			if(alto > 570){
				$(".img_galeria_Wrapper img").attr("style","").css({height:"570px"});
			}
		}
		
		$(".img_galeria_Wrapper img").attr("src","Content/source/img/"+archivo);
		
		$("#fecha_Wrapper").html(fecha);
		$("#fecha_en_Wrapper").html(fecha_en);
		
	});
	
	$(".b_prev").live("click",function(){
		var total = $("#total").attr("value");
		
		if(img_actual > 0){
			img_actual--;
		} else {
			img_actual = total-1;	
		}
		
		var ancho = $(".anchos:eq("+img_actual+")").attr("value");
		var alto = $(".altos:eq("+img_actual+")").attr("value");
		var archivo = $(".imgs:eq("+img_actual+")").attr("value");
		var fecha = $(".fechas:eq("+img_actual+")").attr("value");
		var fecha_en = $(".fechas_en:eq("+img_actual+")").attr("value");
		
		$(".img_galeria_Wrapper img").attr("src","");

		if(parseInt(ancho) > parseInt(alto)){
			if(ancho > 570){
				$(".img_galeria_Wrapper img").attr("style","").css({width:"570px"});
			}		
		} else {
			if(alto > 570){
				$(".img_galeria_Wrapper img").attr("style","").css({height:"570px"});
			}
		}
		
		$(".img_galeria_Wrapper img").attr("src","Content/source/img/"+archivo);;
		
		$("#fecha_Wrapper").html(fecha);
		$("#fecha_en_Wrapper").html(fecha_en);
		
	});

	
	$(".listado_proyectos li, .thumb").click(function(){
		var id = $(this).attr("id");
		var categoria = $("#seccion").attr("value");
		th_flag = true;
		
		$("#galeria_imagen_Wrapper").load("Content/Scripts/ajax_galeria.php?accion=galeria_ver&id_grupo="+id+"&categoria="+categoria, function(){
			$("#galeria_imagen_Wrapper").show();
			$("#thumbs_Wrapper").hide();
		});
				
	});
	
	$(".b_thumbnails").click(function(){
		if(th_flag){
			th_flag = false;
			$("#galeria_imagen_Wrapper").hide();
			$("#thumbs_Wrapper").show();	
		} else {
			th_flag = true;
			$("#galeria_imagen_Wrapper").show();
			$("#thumbs_Wrapper").hide();
		}
	});
	
	//########## GIFTSHIT #########//
	
	$(".mes").click(function(){
		var id = $(this).attr("id");
		document.location = "index.php?seccion=giftshit&accion=ver&id_grupo="+id;
	});
	
	
	$("#b_flecha_derecha").click(function(){
		
		var total = $("#total").attr("value");
		
		if(img_actual < total-1){
			img_actual++;
		} else {
			img_actual = 0;	
		}
		
		var archivo = $(".imgs:eq("+img_actual+")").attr("value");
		var ancho = parseInt($(".anchos:eq("+img_actual+")").attr("value"));
		var alto = $(".altos:eq("+img_actual+")").attr("value");
		
		$(".img_Wrapper img").attr("src","");
		if(parseInt(ancho) > parseInt(alto)){
			if(ancho > 570){
				$(".img_Wrapper img").attr("style","").css({width:"570px"});
			}		
		} else {
			if(alto > 570){
				$(".img_Wrapper img").attr("style","").css({height:"570px"});
			}
		}
		$(".img_Wrapper img").attr("src","Content/source/img/"+archivo);
		
		var ajuste_ancho = (760 - ancho)/2;
		$(".img_Wrapper img").attr("style","").css({marginLeft:ajuste_ancho+"px"});	

	});
	
	$("#b_flecha_izquierda").click(function(){
		
		var total = $("#total").attr("value");
		
		if(img_actual > 0){
			img_actual--;
		} else {
			img_actual = total-1;	
		}
		
		var archivo = $(".imgs:eq("+img_actual+")").attr("value");
		var ancho = $(".anchos:eq("+img_actual+")").attr("value");
		var alto = $(".altos:eq("+img_actual+")").attr("value");
		
		$(".img_Wrapper img").attr("src","");
		if(parseInt(ancho) > parseInt(alto)){
			if(ancho > 570){
				$(".img_Wrapper img").attr("style","").css({width:"570px"});
			}		
		} else {
			if(alto > 570){
				$(".img_Wrapper img").attr("style","").css({height:"570px"});
			}
		}
		$(".img_Wrapper img").attr("src","Content/source/img/"+archivo);
		
		var ajuste_ancho = (760 - ancho)/2;
		$(".img_Wrapper img").attr("style","").css({marginLeft:ajuste_ancho+"px"});
			
	});
});
