var new_things = false,
	ajax_loading = false,
	slide_down_time = 400,
	slide_up_time = 400,
	fade_in_time = 400,
	fade_out_time = 400,
	scroll_time = 400,
	href = "",
	bg_loading_image = new Image(),
	small_loading_image = new Image(),
	current_article_height = 0,
	work_items_height = 0,
	google_account = 'UA-7973133-4';
function fromFlash(tag_name)
{
	switch (tag_name)
	{
	case "Adobe Photoshop" :
		window.location = url + "/#/using/ps/";
		break;
	case "Adobe After Effects" :
		window.location = url + "/#/using/ae/";
		break;
	case "Adobe InDesign" :
		window.location = url + "/#/using/id/";
		break;
	case "Adobe Illustrator" :
		window.location = url + "/#/using/ai/";
		break;
	case "Tumblr" :
		window.location = "";
		break;
	case "Facebook" :
		window.location = "http://www.facebook.com/adrianrizell";
		break;
	case "YouTube" :
		window.location = "http://www.youtube.com/user/adrizell1";
		break;
	case "Vimeo" :
		window.location = "http://vimeo.com/user1263934";
		break;
	case "Twitter" :
		window.location = "http://twitter.com/rizell";
		break;
	case "Spotify" :
		window.location = "http://open.spotify.com/user/adrizell";
		break;
	case "Mail" :
		window.location = "mailto:hello@adrianrizell.com";
		break;
	}
}
function ajax_success_nt_nt(data)
{
	$("ul.newThings li").addClass("old");
	$("ul.newThings").append(data);
	$("ul.workItems a[href^="+url+"]").not(".rss").each(function()
	{
		href = $(this).attr("href").split("#");
		$(this).attr("href",url+"/#"+href[0].slice(url.length));
	});
	if ($("ul.newThings .morePosts").size())
	{
		href = $("ul.newThings .morePosts").attr("href");
		href = href.split("#");
		$(".morePosts").not("ul.newThings .morePosts").attr("href",url+"/#"+href[0].slice(url.length));
	}
	else
	{
		$(".morePosts").remove();
	}
	if ($("ul.newThings .rss").size())
	{
		$("#navigation .rss").attr("href",$("ul.newThings .rss").attr("href"));
		$("ul.newThings .rss").remove();
	}
	$("ul.newThings .morePosts").remove();
	$("ul.newThings li.old").each(function()
	{
		$("ul.newThings #"+$(this).attr("id")).not(".old").remove();
	});
	if ($("ul.newThings li").not(".old").size())
	{
		$("ul.newThings li").removeClass("old");
		$("ul.newThings li").slideDown(slide_down_time,function()
		{
			$(this).fadeTo(fade_in_time,1,function()
			{
				$(".morePosts").removeClass("loading");
				ajax_loading = false;
				on_scroll();
			});
		});
	}
	else
	{
		$("ul.newThings li").removeClass("old");
		$(".morePosts").removeClass("loading");
		ajax_loading = false;
		on_scroll();
	}
}
function ajax_success_w_nt(data)
{
	$(".currentArticle").fadeTo(fade_out_time,0,function()
	{
		$(this).slideUp(slide_up_time,function()
		{
			$(this).remove();
		});
	});	
	$(".workItems").fadeTo(fade_out_time,0,function()
	{
		$(this).slideUp(slide_up_time,function()
		{
			$(this).remove();
			$("#navigation").after("<ul class=\"newThings\"></ul>");
			$("ul.newThings").html(data);
			$("ul.newThigns a[href^="+url+"]").not(".rss").each(function()
			{
				href = $(this).attr("href").split("#");
				$(this).attr("href",url+"/#"+href[0].slice(url.length));
			});
			if ($("ul.newThings .rss").size())
			{
				$("#navigation .rss").attr("href",$("ul.newThings .rss").attr("href"));
				$("ul.newThings .rss").remove();
			}
			if ($("ul.newThings .morePosts").size())
			{
				href = $("ul.newThings .morePosts").attr("href").split("#");
				if ($(".morePosts").not("ul.newThings .morePosts").size() == 0)
				{
					$("ul.newThings").after("<a class=\"morePosts\">More posts</a>");
				}
				$(".morePosts").not("ul.newThings .morePosts").attr("href",url+"/#"+href[0].slice(url.length));
				$("ul.newThings .morePosts").remove();
				$(".scrollContainer").animate({scrollTop: $("#navigation").position().top},slide_down_time);
				$("ul.newThings li").slideDown(slide_down_time,function()
				{
					$(this).fadeTo(fade_in_time,1,function()
					{
						$("h2.newThings").css("backgroundImage","");
						ajax_loading = false;
						on_scroll();
					});
				});
			}
		});
	});
}
function ajax_success_nt_w(data)
{
	$("ul.newThings").fadeTo(fade_out_time,1,function()
	{
		$(this).slideUp(slide_up_time,function()
		{
			$(this).remove();
			$(".morePosts").remove();
			$("#navigation").after("<ul class=\"workItems\"></ul>");
			$("ul.workItems").html(data);
			$("ul.workItems a[href^="+url+"]").not(".rss").each(function()
			{
				href = $(this).attr("href").split("#");
				$(this).attr("href",url+"/#"+href[0].slice(url.length));
			});
			$("ul.workItems").before($("ul.workItems .currentArticle"));
			if ($("ul.workItems .rss").size())
			{
				$("#navigation .rss").attr("href",$("ul.workItems .rss").attr("href"));
				$("ul.workItems .rss").remove();
			}
			$("#navigation").append($("ul.workItems .categories"));
			$("#navigation .categories").show().fadeTo(fade_in_time,1);
			$(".scrollContainer").animate({scrollTop: $("#navigation").position().top},slide_down_time);
			$(".currentArticle").slideDown(slide_down_time,function()
			{
				$(this).fadeTo(fade_in_time,1);
			});
			$(".workItems li").slideDown(slide_down_time,function()
			{
				$(this).fadeTo(fade_in_time,1,function()
				{
					$("h2.work").css("backgroundImage","");
					ajax_loading = false;
				});
			});							
		});
	});
}
function ajax_success_w_w(data)
{
	$("ul.workItems li").addClass("old");
	$("body").append("<div class=\"container tempContainer\" style=\"visibility:hidden;position:absolute;top:-9999px;left:-9999px;\"><ul class=\"workItems\">"+data+"</ul></div>");
	$(".tempContainer ul.workItems .rss, .tempContainer ul.workItems .categories").remove();
	$(".tempContainer ul.workItems li, .tempContainer ul.workItems .currentArticle").css("display","block");
	$(".tempContainer").prepend($(".tempContainer ul.workItems .currentArticle"));
	current_article_height = 0;
	work_items_height = 0;
	if ($(".tempContainer .currentArticle").size())
	{
		current_article_height = $(".tempContainer .currentArticle").height();
	}
	if ($(".tempContainer ul.workItems li").size())
	{
		work_items_height = $(".tempContainer ul.workItems").height();
	}
	$(".tempContainer").remove();
	$("ul.workItems").append(data);
	$("ul.workItems a[href^="+url+"]").not(".rss, .old a").each(function()
	{
		href = $(this).attr("href").split("#");
		$(this).attr("href",url+"/#"+href[0].slice(url.length));
	});
	if ($("ul.workItems .rss").attr("href") != $("#navigation .rss").attr("href"))
	{
		$("#navigation .rss").attr("href",$("ul.workItems .rss").attr("href"));
		if ($("#navigation ul").size())
		{
			$("#navigation ul").html($("ul .categories").html());
			$("ul .categories").remove();
		}
		else
		{
			$("#navigation").append($("ul.workItems .categories"));
			$(".categories").show().fadeTo(fade_in_time,1);
		}
		if ($(".currentArticle").not("ul.workItems .currentArticle").size())
		{
			$(".currentArticle").not("ul.workItems .currentArticle").fadeTo(fade_out_time,0,function()
			{
				if (current_article_height == 0)
				{
					$(this).slideUp(slide_up_time,function()
					{
						$(this).remove();
					});
				}
				else
				{
					$(this).animate({height: current_article_height},slide_down_time,function()
					{
						$(this).html($("ul.workItems .currentArticle").html()).css("display","block");
						$("ul.workItems .currentArticle").remove();
						$(this).fadeTo(fade_in_time,1);
					});
				}
			});
		}
		else if (current_article_height != 0)
		{
			$("#navigation").after("<div class=\"currentArticle\" style=\"display:none;opacity:0;\"></div>");
			$(".currentArticle").not("ul.workItems .currentArticle").animate({height: current_article_height},slide_down_time,function()
			{
				$(this).html($("ul.workItems .currentArticle").html()).css("display","block");
				$("ul.workItems .currentArticle").remove();
				$(this).fadeTo(fade_in_time,1);
			});
		}
		$("ul.workItems li.old").fadeTo(fade_out_time,0,function()
		{
			$(".scrollContainer").animate({scrollTop: $("#navigation").position().top},slide_down_time);
			$("ul.workItems").animate({height: work_items_height},slide_down_time,function()
			{
				$("ul.workItems .old, ul.workItems .rss, ul.workItems .categories").remove();
				$("ul.workItems li").css("display","block").fadeTo(fade_in_time,1,function()
				{
					$("h2.work").css("backgroundImage","");
					ajax_loading = false;
				});
				
			});
		});
	}
	else
	{
		if ($(".currentArticle").not("ul.workItems .currentArticle").size())
		{
			$(".currentArticle").not("ul.workItems .currentArticle").fadeTo(fade_out_time,0,function()
			{
				if (current_article_height == 0)
				{
					$(this).slideUp(slide_down_time,function()
					{
						$(this).remove();
						$("ul.workItems .rss, ul.workItems .categories, ul.workItems .currentArticle").remove();
						$("ul.workItems li.old").remove();
						$("ul.workItems li").css("display","block").css("opacity","1");
						$("h2.work").css("backgroundImage","");
					});
				}
				else
				{
					$(".scrollContainer").animate({scrollTop: $("#navigation").position().top},slide_down_time);
					$(this).animate({height: current_article_height},slide_down_time,function()
					{
						$(this).html($("ul.workItems .currentArticle").html());
						$("ul.workItems .rss, ul.workItems .categories, ul.workItems .currentArticle").remove();
						$("ul.workItems li.old").remove();
						$("ul.workItems li").css("display","block").css("opacity","1");
						$(this).fadeTo(fade_in_time, 1, function()
						{
							$("h2.work").css("backgroundImage","");
						});
					});
				}
			});
		}
		else
		{
			$(".workItems .rss, ul.workItems .categories").remove();
			$("#navigation").after($("ul.workItems .currentArticle"));
			$("ul.workItems li.old").remove();
			$("ul.workItems li").css("display","block").css("opacity","1");
			$(".scrollContainer").animate({scrollTop: $("#navigation").position().top},slide_down_time);
			$(".currentArticle").slideDown(slide_down_time,function()
			{
				$(this).fadeTo(fade_in_time,1,function()
				{
					$("h2.work").css("backgroundImage","");
				});
			});
		}
	}
}
function on_hash_change(hash)
{
	if ((hash == "#/") || (hash == "#") || (hash == ""))
	{
		var new_hash = window.location.href.split("#");
		new_hash = "#"+new_hash[0].slice(url.length);
		if (new_hash != "#/")
		{
			on_hash_change(new_hash);
		}
		else
		{
			if ($(".currentArticle").size())
			{
				_gaq.push
					(
						['_setAccount', google_account],
						['_trackPageview', '/']
					);
				$(".scrollContainer").animate({scrollTop: $("#navigation").position().top},scroll_time);
				var element = $("a.work");
				element.after("<h2 class=\"work\">"+element.html()+"</h2>");
				element.remove();
				element = $("h2.newThings");
				element.after("<a href=\""+url+"/#/new/\" class=\"newThings\">"+element.html()+"</a>");
				element.remove();
				$("h2.work").css("backgroundImage","url(\""+template_url+"/img/bg_arrow_animated.gif\")");
				$(".currentArticle").fadeTo(fade_out_time,0,function()
				{
					$(this).slideUp(slide_up_time,function()
					{
						$(this).remove();
						$("ul.workItems li").removeClass("active");
						$("h2.work").css("backgroundImage","");
					});
				});
			}
		}
	}
	else
	{
		if (hash.slice(2,6) == "new/")
		{
			if (new_things)
			{
				/* new things -> new things */
				if (!ajax_loading)
				{
					ajax_loading = true;
					$(".morePosts").addClass("loading");
					$.ajax(
					{
						type: "get",
						url: url+hash.slice(1),
						timeout: 20000,
						error: function(request, error)
							{
								var element = $("a.work");
								element.after("<h2 class=\"work\">"+element.html()+"</h2>");
								element.remove();
								element = $("h2.newThings");
								element.after("<a href=\""+url+"/#/new/\" class=\"newThings\">"+element.html()+"</a>");
								element.remove();
								$("h2.work").css("backgroundImage","url(\""+template_url+"/img/bg_arrow_animated.gif\")");
								ajax_success_nt_w(request.responseText);
								new_things = false;
							},
						success: function(request)
							{
								_gaq.push
									(
										['_setAccount', google_account],
										['_trackPageview', hash.slice(1)]
									);
								ajax_success_nt_nt(request);
							}
					});
				}
			}
			else
			{
				if (!ajax_loading)
				{
					/*  work -> new things */
					$(".scrollContainer").animate({scrollTop: $("#navigation").position().top},scroll_time);
					var element = $("a.newThings");
					element.after("<h2 class=\"newThings\">"+element.html()+"</h2>");
					element.remove();
					element = $("h2.work");
					element.after("<a href=\""+url+"/#/?filter=none\" class=\"work\">"+element.html()+"</a>");
					element.remove();
					$("#navigation ul").fadeTo(fade_out_time,0,function()
					{
						$(this).remove();
					});
					$("h2.newThings").css("backgroundImage","url(\""+template_url+"/img/bg_arrow_animated.gif\")");
					$.ajax(
					{
						type: "get",
						url: url+hash.slice(1),
						timeout: 20000,
						error: function(request, error)
							{
								var element = $("a.work");
								element.after("<h2 class=\"work\">"+element.html()+"</h2>");
								element.remove();
								element = $("h2.newthings");
								element.after("<a href=\""+url+"/#/new/\" class=\"newThings\">"+element.html()+"</a>");
								element.remove();
								$("h2.work").css("backgroundImage","url(\""+template_url+"/img/bg_arrow_animated.gif\")");
								ajax_success_w_w(request.responseText);
							},
						success: function(request)
							{
								_gaq.push
									(
										['_setAccount', google_account],
										['_trackPageview', hash.slice(1)]
									);
								ajax_success_w_nt(request);
								new_things = true;
							}
					});
				}
			}
		}
		else if (hash.slice(0,2) == "#/")
		{
			if (new_things)
			{
				/* new things -> work */
				$(".scrollContainer").animate({scrollTop: $("#navigation").position().top},scroll_time);
				var element = $("a.work");
				element.after("<h2 class=\"work\">"+element.html()+"</h2>");
				element.remove();
				element = $("h2.newThings");
				element.after("<a href=\""+url+"/#/new/\" class=\"newThings\">"+element.html()+"</a>");
				element.remove();
				$("h2.work").css("backgroundImage","url(\""+template_url+"/img/bg_arrow_animated.gif\")");
				$.ajax(
				{
					type: "get",
					url: url+hash.slice(1),
					timeout: 20000,
					error: function(request, error)
						{
							ajax_success_nt_w(request.responseText);
							new_things = false;
						},
					success: function(request)
						{
							_gaq.push
								(
									['_setAccount', google_account],
									['_trackPageview', hash.slice(1)]
								);
							ajax_success_nt_w(request);
							new_things = false;
						}
				});
			}
			else
			{
				/* work -> work */
				$(".scrollContainer").animate({scrollTop: $("#navigation").position().top},scroll_time);
				var element = $("a.work");
				element.after("<h2 class=\"work\">"+element.html()+"</h2>");
				element.remove();
				element = $("h2.newThings");
				element.after("<a href=\""+url+"/#/new/\" class=\"newThings\">"+element.html()+"</a>");
				element.remove();
				$("h2.work").css("backgroundImage","url(\""+template_url+"/img/bg_arrow_animated.gif\")");
				$.ajax(
				{
					type: "get",
					url: url+hash.slice(1),
					timeout: 20000,
					error: function(request, error)
						{
							ajax_success_w_w(request.responseText);
						},
					success: function(request)
						{
							_gaq.push
								(
									['_setAccount', google_account],
									['_trackPageview', hash.slice(1)]
								);
							ajax_success_w_w(request);
						}
				});
			}
		}
	}
}
function on_scroll()
{
	var load_ajax = false;
	if ((new_things) && (!ajax_loading) && ($(".morePosts").size()))
	{
		load_ajax = (($(".scrollContainer").scrollTop()+$(window).height()) >= $("ul.newThings li:last").position().top);
		if (load_ajax)
		{
			href = $(".morePosts").attr("href").split("#");
			on_hash_change("#"+href[1]);
		}
	}
}
$(function()
{
	$("a[href^="+url+"]").not(".rss").each(function()
	{
		href = $(this).attr("href").split("#");
		$(this).attr("href",url+"/#"+href[0].slice(url.length));
	});
	on_hash_change(window.location.hash);
	$(window).bind("hashchange",function()
	{
		on_hash_change(window.location.hash);
	});
	if ($(".newThings li").size())
	{
		new_things = true;
	}
	on_scroll();
	$(".scrollContainer").bind("scroll",function()
	{
		on_scroll();
	});
	$(window).bind("resize",function()
	{
		on_scroll();
	});
	$(".workItems a").live("click",function()
	{
		$(".scrollContainer").animate({scrollTop: $("#navigation").position().top},scroll_time);
	});
	bg_loading_image.src = template_url+"/img/bg_arrow_animated.gif";
	small_loading_image.src = template_url+"/img/small_arrow_animated.gif";
	$(".morePosts").live("click",function(e)
	{
		e.preventDefault();
	});
});
