var Website = {
	currentItem: 0,
	items: null,
	slugs: null,
	widths: null,
	itemsVisible: 0,
	pageWidth: 0,
	windowWidth: 0,
	init: function() {
		var transform, article, slug, element;
		this.slugs = [];
		this.widths = [];
		$(".project h1, .header h1 a").each(function(){
			transform = .5 + Math.round(Math.random() * 6) / 2;
			$(this).css({
				"-webkit-transform": "rotate(-"+transform+"deg)",
				"-o-transform": "rotate(-"+transform+"deg)",
				"-moz-transform": "rotate(-"+transform+"deg)"
			});
		});
		$(".project .link, .coming-soon").each(function(){
			transform = .5 + Math.round(Math.random() * 4) / 2;
			$(this).css({
				"-webkit-transform": "rotate("+transform+"deg)",
				"-o-transform": "rotate("+transform+"deg)",
				"-moz-transform": "rotate("+transform+"deg)"
			});
		});
		this.items = $("section.projects li");
		this.items.each(function(){
			element = $(this);
			article = element.find("article");
			slug = element.data("slug");
			Website.slugs.push(slug);
			Website.widths.push(article.hasClass("double") ? 2 : 1);
			element.find(".link a").bind("click", function(){
				Website.trackLink(this);
			});
		});
		$(window).bind("resize", function(){
			Website.resize();
		}).bind("swipe", function(event, info){
			alert(info.direction);
		}).trigger("resize");
		var hash = document.location.hash;
		if (hash) {
			this.showProject(hash);
		}
	},
	trackLink: function(element) {
		var slug = $(element).parent().parent().parent().data("slug");
		_gaq.push(['_trackEvent', 'portfolio', slug]);
	},
	trackAction: function(action) {
		_gaq.push(['_trackEvent', 'interface', action]);
	},
	trackInfo: function(action) {
		_gaq.push(['_trackEvent', 'info', action]);
	},
	resize: function() {
		var windowHeight = $(window).height();
		if (windowHeight < 690) { // ipad height is the limit
			var projectHeight = 330;
			var footerHeight = windowHeight - 330 - 180 - 20;
		} else {
			var projectHeight = windowHeight - 360;
			var footerHeight = 160;
		}
		$("footer").css("height", footerHeight);
		if (footerHeight < 122) {
			$(".copyright").css({
				left: "auto",
				right: 20
			});
		} else {
			$(".copyright").css({
				left: 20,
				right: "auto"
			});
		}
		$("section.projects, .project").css("height", projectHeight);
		$("section.projects .image img").css("top", -((1080 - projectHeight) / 2));
		var width = 0;
		var ul = $(".projects ul");
		$("article.project").each(function(){
			width += $(this).hasClass("double") ? 380 : 260;
		});
		ul.css("width", width);
		this.pageWidth = width;
		this.windowWidth = $(window).width();
		this.itemsVisible = Math.floor(this.windowWidth / 260);
		// this.show(true);
	},
	updateHash: function() {
		document.location.hash = "/"+this.slugs[this.currentItem]+"/";
	},
	showProject: function(hash) {
		hash = hash.substring(2).replace(/^\/+/,'').replace(/\/+$/,'');
		this.trackAction(hash);
		for (var i = 0; i < Website.slugs.length; i++) {
			if (Website.slugs[i] == hash) this.currentItem = i;
		}
		this.show(true);
	},
	show: function(dontAnimate) {
		if (this.currentItem < 0) this.currentItem = 0;
		if (this.currentItem > this.items.length - 1) this.currentItem = this.items.length - 1;
		var left = 0;
		for (var i = 0; i < this.currentItem; i++) {
			if (this.widths[i] == 1) {
				left -= 260;
			} else {
				left -= 380;
			}
		}
		if (this.pageWidth + left <= this.windowWidth) {
			left = this.windowWidth - this.pageWidth - 20;
			var widthLeft = this.windowWidth;
			var skipItems = 0;
			for (var i = 0; i < this.itemsVisible; i++) {
				var width = this.widths[this.currentItem - i] == 2 ? 380 : 260;
				if (width < widthLeft) {
					skipItems++;
					widthLeft -= width;
				}
			}
			this.currentItem = this.items.length - skipItems;
		}
		if (dontAnimate) {
			$("section.projects > ul").css("left", left);
		} else {
			$("section.projects > ul").stop().animate({
				left: left
			}, 350);
		}
		this.updateHash();
	},
	next: function() {
		this.trackAction("next");
		var skipItems = 0;
		var widthLeft = $(window).width();
		for (var i = 0; i < this.itemsVisible; i++) {
			var width = this.widths[this.currentItem + i] == 2 ? 380 : 260;
			if (width < widthLeft) {
				skipItems++;
				widthLeft -= width;
			}
		}
		this.currentItem += skipItems;
		this.show();
	},
	previous: function() {
		this.trackAction("previous");
		var skipItems = 0;
		var widthLeft = $(window).width();
		for (var i = 0; i < this.itemsVisible; i++) {
			var width = this.widths[this.currentItem - i - 1] == 2 ? 380 : 260;
			if (width < widthLeft) {
				skipItems++;
				widthLeft -= width;
			}
		}
		this.currentItem -= skipItems;
		this.show();
	},
	info: function(label) {
		this.trackInfo(label);
		$("header.header li.page").each(function(){
			var element = $(this);
			if (element.attr("id") == label) {
				element.toggleClass("current");
			} else {
				element.removeClass("current");
			}
		});
	},
	vision: function(label) {
		this.trackInfo(label);
		$(".content ol li").each(function(){
			var element = $(this);
			if (element.attr("id") == label) {
				element.toggleClass("active");
			} else {
				element.removeClass("active");
			}
		});
	}
};
$(function(){Website.init();});

