var ratPack = $.sammy(function(){
	this.get('#/:name', function(){
		var segment = this.params['name'];
		$("#content").fadeOut(200, function(){
				$.ajax({
					url:"/main/get_content/" + segment,
					type:"GET",
					dataType: "html",
					success:function(data){
						$("#content").html(data).fadeIn(200);
						$(".menu").removeClass("active");
						var n_id = "#" + segment;
						var top_x = $(n_id).position().top + 3;
						$("#indicator").animate({
							"top": top_x + "px"
						}, 300, function(){
							$(n_id).find("a").addClass("active");
						});
					}
				});
		});
	});
});

$(document).ready(function(){
	
	ratPack.run("/#/about");
	
	$("a.menu").click(function(){
		document.location.href = "/#" + $(this).attr("href");
		return false;
	});
	
	$(window).scroll(function(){
		var offset = $(window).scrollTop();
		if(offset > 0){
			$("#backtotop").fadeIn(600);
		}else if(offset == 0){
			$("#backtotop").fadeOut(600);
		}
	});
	
	$("#backtotop").click(function(){
		$(window).scrollTo(0, 800);
	})
	
	$(".portfolio_small_image").hover(function(){
		$(this).find("img").stop(true, true).animate({
			"top":"+=100px"
		}, 200)
	}, function(){
		$(this).find("img").stop(true, true).animate({
			"top":"-=100px"
		}, 200)
	});
	
	$(".portfolio_small_image").click(function(){
		$("#dimmer, #close, #imac, #portfolio_holder, #left, #right, #belt").fadeIn(200, function(){
			$("#belt").stop().animate({
				"left":"0px"
			}, 0);
		});
		if($(this).parent().find(".launchable").length > 0){
			$("#launch").fadeIn(200);
			var e_href = $(this).parent().find(".launchable").attr("href");
			$("#launch").unbind("click").bind("click", function(){
				window.open("http://" + e_href);
				return false;
			})
		}
		var href = $(this).find("a").attr("href");
		$.ajax({
			url:href,
			type:"GET",
			dataType:"html",
			success:function(data){
				$("#belt").html(data);
				var images = $("#belt").children().length;
				if(images <= 1){
					$("#right, #left").hide();
				}else{
					$("#belt").data("current", 1);
				}
			}
		});
	});
	
	$("#close, #dimmer").live("click", function(){
		$("#dimmer, #close, #imac, #portfolio_holder, #left, #right, #launch").fadeOut(200);
	});
	
	$("#right").click(function(){
		var current = $("#belt").data().current;
		var images = $("#belt").children().length;
		current++;
		$("#belt").data("current", current);
		if(current > images){
			$("#belt").stop(true, true).animate({
				"left":"0px"
			});
			$("#belt").data("current", 1);
		}else{
			$("#belt").stop(true, true).animate({
				"left":"-=500px"
			});	
		}
	});
	
	$("#left").click(function(){
		var current = $("#belt").data().current;
		var images = $("#belt").children().length;
		current--;
		$("#belt").data("current", current);
		if(current < 1){
			var n_left = (images-1) * 500;
			$("#belt").stop(true, true).animate({
				"left":"-=" + n_left + "px"
			});
			$("#belt").data("current", images);
		}else{
			$("#belt").stop(true, true).animate({
				"left":"+=500px"
			});	
		}
	});
	
	$("#login_tab").click(function(){
		$("#login").slideToggle();
	});
	
	
});
