var settings = {};

$(document).ready(function() {
	
	$("a[rel='external']").live("click", function() {
		window.open($(this).attr("href"));
		return false;
	});
	
	settings = {
		wWidth : $(window).width(),
		wHeight : $(window).height(),
		dWidth : $(document).width(),
		dHeight : $(document).height(),
		browser : $.browser,
		speed : 1000,
		body : $("body"),
		block : false
	};
	
	var defaults = {
		minHeight: 800,
		articleTop : 206,
		leftTop : 521,
		rightTop : 261
	}
	
	var functions = {
		refreshWindow : function() {
			settings.wWidth = $(window).width();
			settings.wHeight = $(window).height();
			settings.dWidth = $(document).width();
			settings.dHeight = $(document).height();

			$("#rightColumn").width(settings.wWidth - parseInt($("#rightColumn").css("left")));
			
			if(settings.dHeight < defaults.minHeight) var diff = defaults.minHeight - settings.dHeight;
			else var diff = 0;
			
			$("#leftColumn").css("top", defaults.leftTop - diff);
			$("#rightColumn").css("top", defaults.rightTop - diff);
			$("article").css("top", defaults.articleTop - diff);
		}
	};
	
	$(window).resize(function(){
		functions.refreshWindow();
	});
	
	functions.refreshWindow();
	
	
	Cufon.replace("h1, h2, h3, h4, h5, h6, nav, .cufon");
	
	/* Refresh cufons */
	$("a").mouseover(function() { 
		var opt = "";
		if($(this).parent().hasClass("external")) opt = "reverse";

		
		if (!$(this).parent().hasClass("active")) {
			switch (opt) {
				case "reverse":
					Cufon.replace($(this), {
						color: "#000000"
					});
					break;
				default:
					Cufon.replace($(this), {
						color: "#787878"
					});
					break;
			}
		}
		else if(!settings.browser.mozilla) Cufon.replace($(this));

	});
	$("a").mouseout(function() { 
		var opt = "";
		if(!$(this).parent().hasClass("external")) opt = "reverse";

		if (!$(this).parent().hasClass("active")) {
			switch (opt) {
				case "reverse":
					Cufon.replace($(this), {
						color: "#000000"
					});
					break;
				default:
					Cufon.replace($(this), {
						color: "#787878"
					});
					break;
			}
		}
		else if(!settings.browser.mozilla) Cufon.replace($(this));
	});
	
	// Initialize portfolio & navigation
	//$("aside nav a[href='#projects']").parent().parents("li").addClass("active");
	//$("article nav a[href='#projects']").parents("li").addClass("active");
	//$("aside nav a[href='#projects']").parent().parents("li").find(".external").fadeIn(settings.speed);
	//$("article#projects").fadeIn(settings.speed);
	//$("article#projects").addClass("visible");
	//$("article#projects #identity").addClass("visible");
	
	$("aside nav a[href='#about']").parents("li").addClass("active");
	$("article#about").fadeIn(settings.speed);
	$("article#about").addClass("visible");
	

	
	// Replace all images with loaders
	$("#works .samples img").each(function() {
		$(this).wrap("<div class=\"loading\"></div>");
		$(this).parent().attr("data-image",$(this).attr("src"));
		$(this).removeAttr("src");
		$(this).remove();
	});
	
	$("header .logo").bind("click", function() {
		$("aside nav a[href='#about']").trigger("click");
		return false;
	});
	
	$("aside nav a").not(".external a").bind("click", function() {
		if (!settings.block) {
			var id = $(this).attr("href");
			if (!$("article" + id).hasClass("visible")) {
				settings.block = true;
				if (id != "#projects") 
					$("aside nav .external").fadeOut(settings.speed);
				
				
				settings.body.find("nav").find(".active").removeClass("active");
				
				$("a[href='" + id + "']").parent().addClass("active");
				if ($("a[href='" + id + "']").parent().parents("li")[0] != undefined) {
					$("a[href='" + id + "']").parent().parents("li").addClass("active");
				}
				Cufon.replace($("nav a"));
				
				
				$("article.visible").fadeOut(settings.speed, function(){
					$(this).removeClass("visible");
					
					switch (id) {
						case "#projects":
							$("article" + id).fadeIn(settings.speed);
							$("article" + id).addClass("visible");
							$("aside nav .external").fadeIn(settings.speed, function(){
								settings.block = false;
							});
							$("aside nav a[href='#projects']").parent().parents("li").addClass("active");
							$("article nav a[href='#projects']").parents("li").addClass("active");
							//$("article#projects #identity").fadeIn(settings.speed);
							$("article#projects #identity").addClass("visible");
							$("article#projects #identity").removeAttr("style");
							break;
						default:
							$("article" + id).fadeIn(settings.speed, function(){
								settings.block = false;
							});
							$("article" + id).addClass("visible");
							$("article#projects #identity").removeAttr("style");
							$("aside nav a[href='#" + id + "']").parent().addClass("active");
							break;
					}
				});
			}
		}
		return false;
	});
	
	$("article nav a, .external a").bind("click", function() {
		
		if (!settings.block) {
		
			var id = $(this).attr("href");
			
			settings.block = true;
			
			settings.body.find("nav").find(".active").removeClass("active");
			
			$("a[href='" + id + "']").parent().addClass("active");
			if ($("a[href='" + id + "']").parent().parents("li")[0] != undefined) {
				$("a[href='" + id + "']").parent().parents("li").addClass("active");
			}
			Cufon.replace($("nav a"));
		
			
			if ($("article#projects").hasClass("visible")) {
			
				$("article .nextProject").css("display", "none");
				
				
				if (id == "#projects") 
					id = "#identity";
				
				if (!$("#works ul" + id).hasClass("visible")) {
				
					$("aside .external.active").removeClass("active");
					$("aside .external a[href='" + id + "']").parents("li").addClass("active");
					$("#works ul.visible .active .label").trigger("click");
					
					$("#works ul.visible").fadeOut(settings.speed, function(){
						$(this).removeClass("visible");
						$("#works ul" + id).fadeIn(settings.speed, function(){
							settings.block = false;
						});
						$("#works ul" + id).addClass("visible");
					});
				}
			}
		}
		
		return false;
	});
	
	/* Label actions */
	$("#works .label").bind("mouseover", function() {
		if (!$(this).parents("li").hasClass("active")) {
			var img = $(this).children("img");
			var src = img.attr("src");
			src = src.replace("-off", "-hover");
			img.attr("src", src);
		}
	});
	
	$("#works .label").bind("mouseout", function() {
		if (!$(this).parents("li").hasClass("active")) {
			var img = $(this).children("img");
			var src = img.attr("src");
			src = src.replace("-hover", "-off");
			img.attr("src", src);
		}
	});
	
	$("#works .label .closeProject").bind("mouseover", function() {
		Cufon.replace($(this));
	});
	
	$("#works .label .closeProject").bind("mouseout", function() {
		Cufon.replace($(this));
	});
	
	$("#works .label .closeProject").bind("click", function() {
		var that = $(this).parents(".label");
		
		if (!settings.block) {
			
			settings.block = true;
			
			var pos = that.offset().top;
			
			$('html,body').animate({
					scrollTop: pos
				}, settings.speed, function() {

				that.parents("li").removeClass("active");
				that.trigger("mouseout");
				
				that.parents("article").find(".nextProject").css("display", "none");
				
				that.find(".details").fadeOut(settings.speed, function(){
					that.find("img").fadeIn(settings.speed);
				});
				
				that.parents("li").find(".samples").animate({
					"height": "hide",
					"opacity": "hide"
				}, settings.speed, function(){
					settings.block = false;
				});
			});
		}
	});
	
	$("#works .samples img").live("click", function() {
		//console.log("ok");
		$(this).parents("li").find(".closeProject").trigger("click");
	});
	
	$("#works .label img").click(function() {
		
		var that = $(this).parents(".label");
		
		//settings.block = true;

		if (!settings.block) {
			settings.block = true;
			
			//var pos = that.offset().top;
			
			//$('html,body').animate({
			//	scrollTop: pos
			//}, settings.speed, function(){
			
			
			that.find("img").fadeOut(settings.speed, function(){
				that.find(".details").fadeIn(settings.speed);
			});
			
			that.trigger("mouseover");
			that.parents("li").addClass("active");
			if(that.parents("li").next()[0] != undefined)
				that.parents("article").find(".nextProject").css("display", "block");
			
			that.parents("li").find(".samples").delay(settings.speed + 200).animate({
				"height": "show",
				"opacity": "show"
			}, settings.speed, function(){

				settings.block = false;
				
				$(this).find(".loading").each(function(){
					var that = $(this);
					var src = that.attr("data-image");
					var img = new Image();
					
					// wrap our new image in jQuery, then:
					$(img) // once the image has loaded, execute this code
						.load(function(){
													// set the image hidden by default    
													$(this).hide();
													
													// with the holding div #loader, apply:
													that // remove the loading class (so no background spinner), 
						.removeClass('loading') // then insert our image
						.append(this);
													
													// fade our image in to create a nice effect
													$(this).fadeIn();
												}) // if there was an error loading the image, react accordingly
						.error(function(){
												// notify the user that the image could not be loaded
												}) // *finally*, set the src attribute of the new image to our image
						.attr('src', src);
											});
										});
			
			
			//});
			
			}
		
	});
	
	$("article .nextProject").bind("click", function() {
		var that = $(this);
		var active = $("article nav .active a").attr("href");
		
		if(active == "#projects")
			active = "#identity";
		
		var next = $("#works " + active + " .active").last().next().find(" .label img");
		//console.log(active);

		$('html,body').animate({
			scrollTop: next.offset().top
			}, settings.speed, function() {
				next.trigger("click");
				if(next.parents("li").next().find(".label")[0] == undefined)
					that.css("display","none");
			});
	});
	
});

