// hash change event by Ben Alman
(function($,i,b){var j,k=$.event.special,c="location",d="hashchange",l="href",f=$.browser,g=document.documentMode,h=f.msie&&(g===b||g<8),e="on"+d in i&&!h;function a(m){m=m||i[c][l];return m.replace(/^[^#]*#?(.*)$/,"$1")}$[d+"Delay"]=100;k[d]=$.extend(k[d],{setup:function(){if(e){return false}$(j.start)},teardown:function(){if(e){return false}$(j.stop)}});j=(function(){var m={},r,n,o,q;function p(){o=q=function(s){return s};if(h){n=$('<iframe src="javascript:0"/>').hide().insertAfter("body")[0].contentWindow;q=function(){return a(n.document[c][l])};o=function(u,s){if(u!==s){var t=n.document;t.open().close();t[c].hash="#"+u}};o(a())}}m.start=function(){if(r){return}var t=a();o||p();(function s(){var v=a(),u=q(t);if(v!==t){o(t=v,u);$(i).trigger(d)}else{if(u!==t){i[c][l]=i[c][l].replace(/#.*/,"")+"#"+u}}r=setTimeout(s,$[d+"Delay"])})()};m.stop=function(){if(!n){r&&clearTimeout(r);r=0}};return m})()})(jQuery,this);
// clip animation by hon2a
(function(a){a.fx.step.clip=function(c){if($(c.elem).is(":visible")){if(c.start==0){var f=(parseInt($(c.elem).css("fontSize"))*1.333),d=$(c.elem).width()/100,b=$(c.elem).height()/100,j={parseSize:/^(\d{1,}(\.\d+)?)(px|em|%)$/,stripRect:/rect\(([^)]*)\)/,splitRect:/[,\s]\s*/,matchAuto:/^auto$/},h=["0px","101%","105%","0px"];var k=function(i){var l=((typeof i!="string")||(i=="")||(i=="auto"))?["auto","auto","auto","auto"]:i.replace(j.stripRect,"$1").split(j.splitRect);$.each(l,function(n,m){var o=j.parseSize.exec(m.replace(j.matchAuto,h[n]));l[n]=parseFloat(o[1]);switch(o[3]){case"em":l[n]*=f;break;case"%":l[n]*=((n%2)?d:b);break}});return l};c.start=k(c.elem.style.clip);c.end=k(c.end)}var g=[];for(var e=0;e<4;++e){g.push((c.pos*(c.end[e]-c.start[e]))+c.start[e])}c.elem.style.clip="rect("+g.join("px ")+"px)"}else{c.elem.style.clip="auto"}}})(jQuery);

function size() {
	var minwidth = 800, maxwidth = 2000, increment = 300, ratio = 20 / 11;
	if ((windowwidth = $(window).width()) / (windowheight = $(window).height()) < ratio)
		windowwidth = windowheight * ratio;
	if ((imagewidth = Math.ceil((windowwidth - minwidth) / increment) * increment + minwidth) < minwidth)
		imagewidth = minwidth;
	else if (imagewidth > maxwidth)
		imagewidth = maxwidth;
	$sites.add($sites.children()).add($nav).css("backgroundImage", function() {
		return $(this).css("backgroundImage").replace(/\d+\./, imagewidth + ".");
	});
}

function fadein() {
	$f = $('<div style="position:fixed;left:0;top:0;z-index:999;width:100%;height:' + $(window).height() + 'px;background:#fff;" />').appendTo("body");
	$(window).load(function() {
		$f.fadeOut(1000, function() {
			$f.remove();
		});
	});
}
$(document).ready(function() {
	fadein();
	$(document).scrollTop(0);
	($body = $("body")).addClass("script").append('<div id="preview"><a href="#">&#9668; return to portfolio</a><iframe></iframe></div>');
	$sites = $("section");
	if (!($.browser.msie && $.browser.version < 7))
		$sites.children("div").css("backgroundImage", function() {
			return $(this).parent().css("backgroundImage").replace(/_bg_/, "_shade_");
		});
	$nav_a = ($nav = $("nav")).find("a");
	size();
	($cover = $('<div>' + $nav.html() + '</div>').appendTo($nav)).find("a").contents().unwrap();
	$shade = $('<div id="shade" />').prependTo($nav);
	if (window.location.hash.length > 1) {
		$cover.css("backgroundColor", "#" + ($link = $nav_a.eq($sites.index($(window.location.hash).removeClass("hidden").addClass("active")))).attr("rel"));
		$sites.eq(0).removeClass("active").addClass("hidden");
		$nav.css("backgroundImage", $(window.location.hash).children().first().css("backgroundImage"));
		document.title += " > " + $link.text();
	} else {
		$link = $nav_a.eq(0);
		$nav.css("backgroundImage", $sites.eq(0).children().first().css("backgroundImage"));
	}
	$cover.css("clip", "rect(" + $link.parent().position().top + "px auto " + ($link.parent().position().top + $link.innerHeight()) + "px auto)")
});

$(window).load(function() {
	($a = $("a")).wrapInner('<span />').prepend('<span class="fadeout" />').live("hover", function(event) {
		(event.type == "mouseover") ? $(this).children().first().stop(true, true).show() : $(this).children().first().fadeOut();
	});
	($preview = $("#preview")).children("a").click(function(event) {
		$(this).animate({height: 0}, "slow").parent().animate({top: "100%"}, "slow");
		event.preventDefault();
	});
	$nav_a.live("click", function(event) {
		window.location.hash = ($nav_a.index(this)) ? $(this).attr("href") : "";
		event.preventDefault();
	});
	if ($.browser.mozilla)
		$a.filter(".design").find("img").each(function() {
			$(this).attr("width", $(this).width()).attr("height", $(this).height()).css("backgroundImage", "url(" + $(this).attr("src") + ")").removeAttr("src");
		});
	$sites.find("a[rel=external]").live("click", function(event) {				
		$preview.children("iframe").attr("src", this.href).load(function() {
			$(this).prev().animate({height: "2em"}, "slow").parent().animate({top: "8%"}, "slow");
		});
		event.preventDefault();
	});
	$("#contact").find("form").live("submit", function(event) {
		$form = $(this);
		$.get($form.attr("action"), $form.serialize(), function(data) {
			$form.html(data.response);
		}, "jsonp");
		event.preventDefault();
	});
});

$(function() {
	$(window).bind("hashchange", function() {
		index = $sites.index(($site = (window.location.hash) ? $(window.location.hash) : $sites.eq(0)));
		$site.css("opacity", 0).addClass("selected").removeClass("hidden").animate({opacity: 1}, function() {
			$("section.active").removeClass("active").addClass("hidden");
			$site.removeClass("selected").addClass("active");
		});
		$site.children().first().css("bottom", -10).animate({bottom: 0}, "slow");
		$shade.css({opacity: 0, backgroundImage: $site.children().first().css("backgroundImage")}).animate({opacity: 1}, function() {
			$nav.css("backgroundImage", $shade.css("backgroundImage"));	
		});
		$cover.animate({clip: "rect(" + ($link = $nav_a.eq(index)).parent().position().top + "px auto " + ($link.parent().position().top + $link.innerHeight()) + "px auto)", backgroundColor: "#" + $link.attr("rel")});
		document.title = document.title.replace(/ >.*/, "") + ((index) ? " > " + $link.text() : "");
	});
});

$(window).resize(size);
