
jQuery.extend( jQuery.easing,
{	
	easeInExpo: function (x, t, b, c, d) {
		return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
	},
	easeOutExpo: function (x, t, b, c, d) {
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	},
	easeInOutExpo: function (x, t, b, c, d) {
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	}
});

var windowHeight = $(window).height();
var windowWidth = $(window).width();

jQuery(function(){

	var init = function() {
		
		$(window).resize(function(){
			$("body").css("overflow","hidden");
			windowHeight = $(window).height();
			windowWidth = $(window).width();
			$("#iw, #images, #tags").width(windowWidth).height(windowHeight);
			$("body").css("overflow-y","auto").css("overflow-x","hidden");
			resetImages();
			$("#images img:eq(0)").click();
		});
		$("a").attr("title","");

		$("#iw, #images").width(windowWidth).height(windowHeight);
		//$("#tags").width(windowWidth*2).height(windowHeight).css("display","block").animate(windowWidth, 1000, "easeOutExpo");
		$("#tags li").each(function(){
			var liWidth = $(this).width();
			$(this).css("margin-left",liWidth/3);
		
		});
		$("body").css("overflow-x", "hidden");
		
		// ! ABRIR 1º NIVEL - hover
		$("#tw .b_header").hover(
			function(){
				var toHeight = $(this).find(".entry").height()+41;
				$(this)
				.stop()
				.animate({height: toHeight}, 350, "easeOutExpo" )
				.find("h1").addClass("hover");
			},
			function(){
				$(this)
				.stop()
				.animate({height: 41}, 350, "easeOutExpo" )
				.find("h1").removeClass("hover");
		});
		
		// ! NAVEGACIÓN IMÁGENES
		var resetImages = function() {
			$("#images div").width(10000);
			var divHeight = $("#images div").height();
			var divPaddingTop = (windowHeight/2)-(divHeight/2);
			//console.log("divHeight: "+divHeight);
			//console.log("divPaddingTop: "+divPaddingTop);
			$("#images div").css({
				paddingLeft: windowWidth,
				paddingBottom:windowHeight,
				paddingRight:windowWidth,
				paddingTop: divPaddingTop
			});
			
			$("#images img")
				.each(function(){
					var thisParentHeight = ($(this).parent().height()/2)-($(this).height()/2);
					$(this).css("margin-bottom",thisParentHeight);
					var fotoWidth = Math.floor($(this).width()/2);
					var fotoHeight = Math.floor($(this).height()/2);
					$(this).animate({ opacity:0.15 }, 1000, "easeInOutExpo");
				})
				.unbind("hover")
				.hover(
					function(){
						$(this).not("img.active").animate({ opacity: 0.5}, 300,"easeOutExpo" );
					},
					function(){
						$(this).not("img.active").animate({ opacity: 0.15}, 300,"easeOutExpo" );
				})
				.unbind("click")
				.click(function(){
					$("#images img.active").removeClass("active").animate({ opacity: 0.15}, 800,"easeOutExpo" );
					$(this).stop().addClass("active").animate({ opacity: 1}, 800,"easeOutExpo" );
					var imgWidthHalf = $(this).width()/2;
					var imagesLeftOffset = Math.round(-(windowWidth/2)-200+imgWidthHalf);
					var imagesTopOffset = Math.round(-(windowHeight/2)+($(this).height()/2));
					//console.log("imagesTopOffset: "+imagesTopOffset);
					$("#images").scrollTo( $(this), 800, {axis: 'xy', offset: {top:imagesTopOffset ,left:imagesLeftOffset} ,easing:"easeOutExpo" } );
				})
				.eq(0)
				.load(function(){ 
					$(this).click();
					$("#iw #logo img").css("visibility","visible");
					// delete title & alt 
					$("#iw img").attr("title","").attr("alt","");
				});
		}
		resetImages();
		
		
		// ! NAVEGACIÓN 2º NIVEL
		$("#tw h2").not(".zonaclientes").click(function(){
			var toHeight = $(this).next().children().height();
			var correctHeight = 0;
			// entry height is unpair so bg-pattern is neat
			if (toHeight % 2 != 0) {
				toHeight++;
				correctHeight++;
			}

			var isGeneral = false;
			if ($(this).parents("#home").length == 0) isGeneral = true;
			
			//console.log(toHeight);
			$("#tw h2.open").removeClass("open").next().animate({ height:0 }, 300, "easeOutExpo", function() {
				
			});

			$(this)
			.addClass("open")
			.next()
			.animate({ height:toHeight }, 300, "easeOutExpo", function() { $(this).css("height", "auto") })
			.children()
			.css("margin-bottom",correctHeight);
			
			var linkHref = $(this).children().attr("href");
			var divHeight = $("#images div").height();
			var divPaddingTop = Math.abs((windowHeight/2)-(divHeight/2));
			var divPaddingTopString = -divPaddingTop+"px";
			//console.log(divPaddingTopString);
			$("#images").scrollTo( 
				{top:divPaddingTopString, left:'0px'}, 
				500, 
				{axis: 'xy', easing:"easeInExpo", onAfter: function(){
					if(isGeneral) $("#iw #logo img").css("visibility","hidden");
					$("#images").load(linkHref+" #images div", function(){ resetImages(); });
				}
			});
			
			return false;
		})
		.next()
		.css({
			height: 0,
			overflow: "hidden"
		});
		
		$("#tw h2").hover(
			function() {
				var tags = $(this).children().attr("rel");
				var tags_arr = tags.split(" ");
				
				// highlight tags only if not actual opening 1º level
				if (!openingH1) {
					for ( var i=0; i<tags_arr.length; i++) 
					{
						$("#tags a").each(function(n){
							var html = $(this).html();
							if(tags_arr[i] == html) {
								//var fontSizeTo = $(this).css("font-size");
								//$(this).css("font-size","0").animate({ fontSize: fontSizeTo }, 1000);
								$(this).addClass("visited active");
								
							}
						});
					}
				}
			
			},
			function() {
				$("#tags a").removeClass("active");
				//.animate({color: "#2f2c29"},2000, function() {	$(this).css("color","").removeClass("visited");	});
				//css({marginTop: "", marginLeft:""});
			}
		);
		
		// var fistTimOpen = false;
		var openingH1 = false;
		// ! NAVEGACIÓN 1º NIVEL - click
		$("#tw h1 a").click(function(){
			openingH1 = true;
			var posts = $(this).parents(".b_header").next().children();
			var postTotalHeight = 0;
			posts.each(function(){
				postTotalHeight += $(this).height()+1;
				
			});
			
			// If already open close again
			if ($(this).parents(".block").hasClass("open")) {
				$("#tw div.open").removeClass("open").children("ul").animate({height:0 }, 800, "easeInOutExpo");
			} 
			// if closed close open and open clicked
			else {
				$("#tw div.open").removeClass("open").children("ul").animate({height:0 }, 800, "easeInOutExpo");
				$(this).parents(".block, .b_header").addClass("open").children("ul").animate({height:postTotalHeight }, 800, "easeInOutExpo", function() { 
					$(this).css("height", "auto") 
					openingH1 = false;
				});
			}
			return false;
		})
		.each(function(){
			$(this).attr("title","");
		})
		.parents(".b_header").next().css({
			 height: 0,
			 overflow: "hidden"
		});
		
		$("body").addClass("introanim").animate({ backgroundColor: "#292622"},2000, "easeInOutExpo").animate({ backgroundColor: "#2f2c29"},2000, "easeInOutExpo", function(){
			$(this).removeClass("introanim");
		
		});
		
	}
	
	
	

	// SINGLE POSTS
	if($("#tw").hasClass("single")) {
		var generalCat = $("#tw").children(".block").attr("id");
		var siglePostID = $("#tw .b_list li").attr("id");

		if ($("#clientes").length != 0) {
			init();
			$("#"+siglePostID+" h2").addClass("open");
			  $("#"+siglePostID+" .entry").css("height","auto");
			  $("#"+generalCat+" h1 a").click();
			  $("#"+generalCat+" h1 a").unbind("click");
		} else {
			$("#tw").load(baseURL+" #tw div.block,#tw div#footer",function(){ 
				init();
				//console.log(generalCat);
				// If general cat doesn't exist in ajaxed html then it's not part of normal tasks so open Genaral tab
				
				$("#"+siglePostID+" h2").addClass("open").click();
				$("#"+siglePostID+" .entry").css("height","auto");
				
				if($("#"+generalCat).length != 0) $("#"+generalCat+" h1 a").click();
				else $("#home h1 a").click();
			});
		}

	} else {
		// PAGES
		init();
		// Home Page
		if($("#tw").hasClass("home")) {
			$("div.activeBlock .post:first h2").addClass("open");
			var fistEntry = $("div.activeBlock .post:first .entry");
			fistEntry.css("height","auto");
			var toHeight = fistEntry.height();
			if (toHeight % 2 != 0) fistEntry.height(fistEntry.height()+1);
		} 
		// Subpages
		else {
			$("#home h1 a").unbind("click");
		}
		$("div.activeBlock h1 a").click();	
	};
	
	
});

