jQuery(document).ready(function($){

	$('html').addClass('hasJS');
			
  	$("#view span:first-child").addClass("first-child");
			
	$('div.post:first').addClass('first');
	$('div.post:last').addClass('last');
	
	$('.permalink #notes ol').hide();
	
	$('#note-toggle a').append('<span> +</span>');
	
	$('.permalink #note-link a, #note-toggle a').toggle( 
		function () { $('#notes ol').slideDown(400); $('#note-toggle a span').empty().append(' &minus;'); }, 
		function () { $('#notes ol').slideUp(400);  $('#note-toggle a span').empty().append(' +');
	});
	
	$(window).scroll(function() {
        $('.tumblr #nav2').animate({ top: $(window).scrollTop() + 30 + "px"}, 
            { queue: false, duration: 0 });
    });
	    
    $('.projects #content').infinitescroll({
		navSelector  : "#page-nav",            
		nextSelector : "#page-nav a:first",    
		itemSelector : "#content .project",          
		loadingImg   : "http://workworkwork.co.nz/static/i/ajax-loader.gif",          
		loadingText  : "Loading more",      
		animate      : false,      
		donetext     : "That's all" ,
		bufferPx     : 150
	});
	
	$('#top-link').click(function() {
		$('html, body').animate({scrollTop:0}, 'normal');
	});

	// applies image alignment classes in the content areas
	$(".tumblr #content img").each(function(){
	     if ($(this).attr('align') == 'left') {
	         $(this).addClass('img-left');
	     } else if ($(this).attr('align') == 'right') {
	         $(this).addClass('img-right');
	     } else if ($(this).attr('align') == 'middle') {
	         $(this).addClass('img-big');
	     }
	});

	
	// Studio Silliness
					
	$('#fat').click(function() {
		$('html, body').animate({scrollTop:150}, 'normal');
		var e = $('#team'); 
		e.animate({ bottom: -400 }, { duration: 400, easing: 'easeOutSine' }); 
		e.queue(function(){ 
		  setTimeout(function(){ 
		    e.dequeue(); 
		  }, 1000 ); 
		}); 
		e.animate({ bottom: 0 }, { duration: 500, easing: 'easeOutSine' });
		e.toggleClass('fat-enabled');
		$(this).toggleClass('enabled');
		$(this).toggleClass('disabled');
	});
	
	$('#fat').toggle(function() {
		$('#team img').each(function(){
		    var src = $(this).attr("src").match(/[^\.]+/) + "-fat.png";
            $(this).attr("src", src);
		});
		}, function() {
		$('#team img').each(function(){
		    var src = $(this).attr("src").replace("-fat", "");
            $(this).attr("src", src);
		});
	});
		
	$('#fat').toggle( 
		function () { $(this).html('Disable <abbr title="Follicular Assistive Technology">F.A.T.</abbr>'); }, 
		function () { $(this).html('Enable <abbr title="Follicular Assistive Technology">F.A.T.</abbr>');
	});

	$('#team p').hide();
	
	$('#team img').hoverIntent(function() {
		$(this).parent().animate({ bottom: -1 }, { duration: 500, easing: 'easeOutSine' }),
		$('#team').addClass('over'),
		$(this).next('p').delay(1000).show(); 	
	}, function() {
		$(this).parent().animate({ bottom: -40 }, { duration: 500, easing: 'easeOutSine' }),
		$('#team').removeClass('over'),
		$(this).next('p').hide(); 	
	});
	
	$(function(){
		 $('#text-4 div.textwidget').tweetable({username: 'workandtumble', time: true, limit: 6});
	});
	
	
	// Enquire form
	
	$('#contact-form li input, #contact-form li textarea').focus(function() {
		$('#contact-form li').removeClass('focused');
		$(this).parent().addClass('focused');
	});
	
	$(window).load(function () {
		$(':input:visible:enabled:first').focus();
  	});

	
	// Filtering Projects by Tag
			
	$('#filter h3').toggle(function() { 
		$('#filter ul').slideDown();
		$('#filter h3 span').empty().append('&minus;');
	}, function() {
		$('#filter ul').slideUp('fast');
		$('#filter h3 span').empty().append('+');
	});
			    
	/*
		Widescreen YouTube Embeds by Matthew Buchanan & Hayden Hunter
		http://matthewbuchanan.name/451892574
		http://blog.haydenhunter.me
	
		Released under a Creative Commons attribution license:
		http://creativecommons.org/licenses/by/3.0/nz/
	*/

	$("object").each(function () {
		if ($(this).find("embed[src^='http://www.youtube.com']").length > 0) {
			// Identify and hide embed(s)
			var parent = $(this).parent();
			parent.css("visibility","hidden");
			var youtubeCode = parent.html();
			var params = "";
			if (youtubeCode.toLowerCase().indexOf("<param") == -1) {
				// IE doesn't return params with html(), soÉ
				$("param", this).each(function () {
					params += $(this).get(0).outerHTML;
				});
			}
			// Set colours in control bar to match page background
			var oldOpts = /rel=0/g;
			var newOpts = "rel=0&amp;color1=0xFFFFFF&amp;color2=0xFFFFFF";
			youtubeCode = youtubeCode.replace(oldOpts, newOpts);
			if (params != "") {
				params = params.replace(oldOpts, newOpts);
				youtubeCode = youtubeCode.replace(/<embed/i, params + "<embed");
			}
			// Extract YouTube ID and calculate ideal height
			var youtubeIDParam = $(this).find("embed").attr("src");
			var youtubeIDPattern = /\/v\/([0-9A-Za-z-_]*)/;
			var youtubeID = youtubeIDParam.match(youtubeIDPattern);
			var youtubeHeight = Math.floor(parent.find("object").width() * 0.75 + 25);
			var youtubeHeightWide = Math.floor(parent.find("object").width() * 0.5625 + 25);
			// Test for widescreen aspect ratio
			$.getJSON("http://gdata.youtube.com/feeds/api/videos/" + youtubeID[1] + "?v=2&alt=json-in-script&callback=?", function (data) {
				oldOpts = /height="?([0-9]*)"?/g;
				if (data.entry.media$group.yt$aspectRatio != null) {
					newOpts = 'height="' + youtubeHeightWide + '"';
				} else {
					newOpts = 'height="' + youtubeHeight + '"';
				}
				youtubeCode = youtubeCode.replace(oldOpts, newOpts);
				if (params != "") {
					params = params.replace(oldOpts, newOpts);
					youtubeCode = youtubeCode.replace(/<embed/i, params + "<embed");
				}
				// Replace YouTube embed with new code
				parent.html(youtubeCode).css("visibility","visible");
			});
		}
	});
	
	/*
		Better HTML5 Vimeo Embeds 2.0 by Matthew Buchanan
		Modelled on the Vimeo Embedinator Script
		http://mattbu.ch/971857304/
	
		Released under a Creative Commons attribution license:
		http://creativecommons.org/licenses/by/3.0/nz/
	*/
	var color = "e13f3c";
	$("object[data^='http://vimeo.com']").each(function() {
		var $obj = $(this);
		var data = $obj.attr("data");
		var temp = data.split("clip_id=")[1];
		var id = temp.split("&")[0];
		var server = temp.split("&")[1];
		var w = $obj.attr("width");
		var h = $obj.attr("height");
		$obj.replaceWith(
		  "<iframe src='http://player.vimeo.com/video/"+id+
		  "?"+server+"&title=0&byline=0&portrait=0&color="+color+"' "+
		  "width='"+w+"' height='"+h+"' frameborder='0'></iframe>"
		);
	});
});
