var browser_width,
	browser_height,
	touch_enabled = false,
	fixed_el,
	available_width,
	available_height,
	loading_html = '<div id="loading" class="loader"><div class="header"><h3>Loading</h3></div></div>',
	filterScroll,
	loaded = false,
	filterScroll,
	stopped= true,
	currentSlide,
	maxSlides;

/*
 * jQuery postMessage - v0.5 - 9/11/2009
 * http://benalman.com/projects/jquery-postmessage-plugin/
 * 
 * Copyright (c) 2009 "Cowboy" Ben Alman
 * Dual licensed under the MIT and GPL licenses.
 * http://benalman.com/about/license/
 */
(function($){var g,d,j=1,a,b=this,f=!1,h="postMessage",e="addEventListener",c,i=b[h]&&!$.browser.opera;$[h]=function(k,l,m){if(!l){return}k=typeof k==="string"?k:$.param(k);m=m||parent;if(i){m[h](k,l.replace(/([^:]+:\/\/[^\/]+).*/,"$1"))}else{if(l){m.location=l.replace(/#.*$/,"")+"#"+(+new Date)+(j++)+"&"+k}}};$.receiveMessage=c=function(l,m,k){if(i){if(l){a&&c();a=function(n){if((typeof m==="string"&&n.origin!==m)||($.isFunction(m)&&m(n.origin)===f)){return f}l(n)}}if(b[e]){b[l?e:"removeEventListener"]("message",a,f)}else{b[l?"attachEvent":"detachEvent"]("onmessage",a)}}else{g&&clearInterval(g);g=null;if(l){k=typeof m==="number"?m:typeof k==="number"?k:100;g=setInterval(function(){var o=document.location.hash,n=/^#?\d+&/;if(o!==d&&n.test(o)){d=o;l({data:o.replace(n,"")})}},k)}}}})(jQuery);

/* Vimeo FROOGALOOP */

var Froogaloop=function(){function e(a){return new e.fn.init(a)}function h(a,c,b){if(!b.contentWindow.postMessage)return!1;var f=b.getAttribute("src").split("?")[0],a=JSON.stringify({method:a,value:c});"//"===f.substr(0,2)&&(f=window.location.protocol+f);b.contentWindow.postMessage(a,f)}function j(a){var c,b;try{c=JSON.parse(a.data),b=c.event||c.method}catch(f){}"ready"==b&&!i&&(i=!0);if(a.origin!=k)return!1;var a=c.value,e=c.data,g=""===g?null:c.player_id;c=g?d[g][b]:d[b];b=[];if(!c)return!1;void 0!==a&&b.push(a);e&&b.push(e);g&&b.push(g);return 0<b.length?c.apply(null,b):c.call()}function l(a,c,b){b?(d[b]||(d[b]={}),d[b][a]=c):d[a]=c}var d={},i=!1,k="";e.fn=e.prototype={element:null,init:function(a){"string"===typeof a&&(a=document.getElementById(a));this.element=a;a=this.element.getAttribute("src");"//"===a.substr(0,2)&&(a=window.location.protocol+a);for(var a=a.split("/"),c="",b=0,f=a.length;b<f;b++){if(3>b)c+=a[b];else break;2>b&&(c+="/")}k=c;return this},api:function(a,c){if(!this.element||!a)return!1;var b=this.element,f=""!==b.id?b.id:null,d=!c||!c.constructor||!c.call||!c.apply?c:null,e=c&&c.constructor&&c.call&&c.apply?c:null;e&&l(a,e,f);h(a,d,b);return this},addEvent:function(a,c){if(!this.element)return!1;var b=this.element,d=""!==b.id?b.id:null;l(a,c,d);"ready"!=a?h("addEventListener",a,b):"ready"==a&&i&&c.call(null,d);return this},removeEvent:function(a){if(!this.element)return!1;var c=this.element,b;a:{if((b=""!==c.id?c.id:null)&&d[b]){if(!d[b][a]){b=!1;break a}d[b][a]=null}else{if(!d[a]){b=!1;break a}d[a]=null}b=!0}"ready"!=a&&b&&h("removeEventListener",a,c)}};e.fn.init.prototype=e.fn;window.addEventListener?window.addEventListener("message",j,!1):window.attachEvent("onmessage",j);return window.Froogaloop=window.$f=e}();

/*
  * Normalized hide address bar for iOS & Android
  * (c) Scott Jehl, scottjehl.com
  * MIT License
*/
(function( win ){
	var doc = win.document;
	
	// If there's a hash, or addEventListener is undefined, stop here
	if( !location.hash && win.addEventListener ){
		
		//scroll to 1
		window.scrollTo( 0, 1 );
		var scrollTop = 1,
			getScrollTop = function(){
				return win.pageYOffset || doc.compatMode === "CSS1Compat" && doc.documentElement.scrollTop || doc.body.scrollTop || 0;
			},
		
			//reset to 0 on bodyready, if needed
			bodycheck = setTimeout(function(){
				if( doc.body ){
					clearInterval( bodycheck );
					scrollTop = getScrollTop();
					win.scrollTo( 0, scrollTop === 1 ? 0 : 1 );
				}	
			}, 15 );
		
		win.addEventListener( "load", function(){
			setTimeout(function(){
				//at load, if user hasn't scrolled more than 20 or so...
				if( getScrollTop() < 20 ){
					//reset to hide addr bar at onload
					win.scrollTo( 0, scrollTop === 1 ? 0 : 1 );
				}
			}, 0);
		} );
	}
})( this );

overlap_lines = function(){

	var line_offset = ($(window).height() - 34 - 57 - $(".end-line").height()) - (183 + $(".meta").height());
	var scroll_bottom = $(document).height() - $(window).height() - $(window).scrollTop();
		
	// if((($(".meta").offset().top - $(window).scrollTop()) <= 186) && (browser_width >= 1406) && (browser_width < 1832))
	/*if(($(".description").height() <= $(window).scrollTop()) && (browser_width >= 1406) && (browser_width < 1832))
	{
		$(".info").css("padding-top", $(".meta").outerHeight(true));
		$(".meta").addClass("fixed-meta");
	}*/
	
	// if($(".fixed-meta").length && ($(window).scrollTop() < ($(".info").height() + 34)))
	/*if($(".fixed-meta").length && ($(".description").height() > $(window).scrollTop()))
	{
		$(".meta").removeClass("fixed-meta");
		$(".info").removeAttr("style");
	}*/
		
	if(scroll_bottom < (Math.abs(line_offset)*3) && (browser_width >= 1406))
	{
		if(line_offset < 0)
			fixed_el.css("margin-top", (line_offset + (scroll_bottom/3)) + "px");
		else
		{
			fixed_el.css("margin-top", (line_offset - (scroll_bottom/3)) + "px");
		}
	}
	else
	{
		fixed_el.removeAttr("style");
	}

}

thumbs_sequence = function(){
	var sequence = [1, 4, 2, 7, 5, 3, 10, 8, 6, 11, 9, 12];
	
	$(".thumbs article img").each(function(){
		var el = $(this);
		var waitABit = setTimeout(function () {
			el.attr("style","opacity:1;");
			clearTimeout(waitABit);
		}, 100*(jQuery.inArray($(".thumbs article img").index(el) + 1, sequence)));		
	});
}

filter_scrollbar = function(){
	var filter_height = $("#filter").find(".header").outerHeight() + $("#filter .open ul").outerHeight();
		
	if((browser_width >= 768) && (filter_height > $("#isotope").outerHeight()) && $("body").hasClass("filter-open"))
	{
		$("body").css("min-height", filter_height  + "px");
		if($("#isotope").outerHeight() < browser_height)
		{
			$("#isotope").addClass("fixed_thumbs").css("width", (browser_width - $("#isotope").css("margin-left").replace("px", "") - $("#isotope").css("margin-right").replace("px", "")) + "px");
			
		}
		else
			$("#isotope").removeClass("fixed_thumbs").css("width", "");
	}
	else
	{
		$("body").css("min-height", "");
		$("#isotope").removeClass("fixed_thumbs").css("width", "");
	}
}

resize_magic = function() {
	
	var old_width = browser_width;
	var old_height = browser_height;
	var triangleSide = 88;
	
	browser_width = $(window).width();
	browser_height = $(window).height();
	
	available_width = browser_width - 250;
	available_height = browser_height - 220;
		
	var browser_width_no_scroll = Math.min($(window).innerWidth(), window.innerWidth);
	
	setTimeout(function()
	{
		if(!$("html").hasClass("ielt9") && !Modernizr.mq('only screen and (min-width: 1832px)') && $("body").hasClass("work"))
			$(".info").css("padding-top", $(".meta").outerHeight(true));
		else
			$(".info").css("padding-top", 0);
	}, 250);
	
	if((browser_width_no_scroll >= 1406) && $(".info").length)
	{
		$(".content").css("min-height", ($(".info").offset().top + $(".info").outerHeight())  + "px");
	}
	else
	{
		$(".content").removeAttr("style");
	}
	
	if($("body").hasClass("home"))
	{
		available_height = browser_height - 68;
	}
	
	if(!Modernizr.touch && $("#isotope").length)
	{
		filter_scrollbar();
	}
	
	if((browser_width < 1024) && (browser_width >= 768))
	{
		available_width = browser_width - 204;
		available_height = browser_height - 216;
		if($("body").hasClass("home"))
		{
			available_height = browser_height - 60;
		}
	}
	else if(browser_width < 768)
	{
		available_width = browser_width - 40;
		available_height = available_width;
	}
	
	if(((browser_width < 1024) && (old_width >= 1024)) || ((browser_width >= 1024) && (old_width < 1024)))
	{	
		var timeout_duration = 50;
		
		if(touch_enabled && $("#isotope").length)
		{
			timeout_duration = 250;
		}
		
		if($("#isotope").length)
			var waitForResize = setTimeout(function()
			{
				$("#isotope").addClass("transition_height").isotope("reLayout", function(){ $("#isotope").removeClass("transition_height") });
				clearTimeout(waitForResize);
			}, timeout_duration);
	}
		
	/*$(".work iframe").each(function(){
		$(this).parent().css("height", ($(this).width() * $(this).attr("data-height") / $(this).attr("data-width")) + "px");
	});*/
	
	if($("body").hasClass("single") && $("body").hasClass("work") && $(".meta").length)
	{
		if((browser_width_no_scroll < 1406) || (browser_width_no_scroll >= 1832))
		{
			$(".meta").removeClass("fixed-meta").removeAttr("style");
			fixed_el = $(".description");
		}
		else
		{
			fixed_el = $(".meta");
			$(".description").removeAttr("style");
		}
		
		// overlap_lines();
	}
	
	// map.setCenter(mapCenter);
		
	if($("#latest").length && (browser_width >= 1024))
	{
		var $children = $("li", "#latest");
		
		$("#slider").css("width", available_width + "px");
		$children.css("width", available_width + "px");
		$("#latest").addClass("no_transition").css("width", available_width*$children.length + "px").css("height", available_height + "px").css("left", "-" + $(".selectedSlide").attr("data-id")*available_width  + "px");
		
		$children.each(function(){
			if($(this).hasClass("scaleImage"))
			{
				var imgObj = $("img", this),
				possible_height = available_width*imgObj.attr("data-height")/imgObj.attr("data-width");
				
				if(possible_height >= available_height)
					imgObj.removeClass("abs").css("width", available_width + "px").css("height", possible_height + "px").css("margin-left", 0); 
				else
				{			
					var possible_width = available_height*imgObj.attr("data-width")/imgObj.attr("data-height")
					imgObj.addClass("abs").css("width", possible_width + "px").css("height", available_height  + "px").css("margin-left", "-" + possible_width/2 + "px"); 
				}
			}
		});
		
		setTimeout(function(){
			$("#latest").removeClass("no_transition");
		},500);
	}
	else
	{
		$("#latest, #slider").removeAttr("style");
		$("li", "#latest").removeAttr("style");
		$("img", "#latest").removeAttr("style").removeClass("abs");
	}
	
	if($("body").hasClass("home") && (browser_width >= 1024)) $("html").addClass("hideScroll");
	else $("html").removeClass("hideScroll");
	
	$(".scroller", "#filter").each(function(){
		$(this).css("height", (available_height + 34) + "px");
	});
	
	if(filterScroll)
		setTimeout(function () {
			filterScroll.refresh();
		}, 0);
	
	// resize canvas triangle
		
	if(Modernizr.canvas && (typeof ST != "undefined") && loaded)
	{
		if(browser_width < 768)
			triangleSide = 44;
		
		if(((browser_width >= 1024) && (old_width < 1024)) || ((old_width >= 1024) && (browser_width < 1024)) || ((browser_width >= 768) && (old_width < 768)) || ((old_width >= 768) && (browser_width < 768)))
		{
			ST.updateSize($("#triangle").width(),$("#triangle").height(),triangleSide);
		}
	}
};

// Update colors

update_triangle = function(){
	
	if(colorArray)
	{
		colorArray = $.grep(colorArray,function(n){
		    return(n != "#");
		});
				
		if(colorArray.length)
		{
			ST.updateColors(colorArray);
		}
		else
		{
			ST.updateColors();
		}
	}
	else
	{
		ST.updateColors();
	}
	
}

// Initialize static / ajax content

reinitialise_content = function(){

	if($("#isotope").length)
	{
		$("#isotope").isotope({
			layoutMode: 'fitRows',
			transformsEnabled: false,
			animationOptions: {
		    	duration: 750,
		     	easing: 'linear',
		     	queue: false
		   }
		});
		
		if(!touch_enabled)
		{
			$("#isotope").find("article").each(function(){			
				
				$(this).on({
					mouseenter: function(){
						$(this).addClass("hovered");
					},
					mouseleave: function(){
						var current = $(this);
						var waitForOpacity = setTimeout(function () {
							current.removeClass("hovered");
							clearTimeout(waitForOpacity);
						}, 250);
						
					}
				});
			});
		}
	}
	
	$("a[rel=external], a[href^='http:']:not([href*='" + window.location.host + "'])").each(function(){
		$(this).attr("target", "_blank");
	});
	
	$("#goTop").on('click', function() {
		$('html,body').animate({scrollTop: $("html, body").offset().top},'fast');
		return false; 
	});
	
	$('[placeholder]').focus(function() {
	  var input = $(this);
	  if (input.val() == input.attr('placeholder')) {
	    input.val('');
	    input.removeClass('placeholder');
	  }
	}).blur(function() {
	  var input = $(this);
	  if (input.val() == '' || input.val() == input.attr('placeholder')) {
	    input.addClass('placeholder');
	    input.val(input.attr('placeholder'));
	  }
	}).blur().parents('form').submit(function() {
	  $(this).find('[placeholder]').each(function() {
	    var input = $(this);
	    if (input.val() == input.attr('placeholder')) {
	      input.val('');
	    }
	  })
	});
	
	/*
	
	// Google Maps styles + init
	
	if($("#map").length)
	{
		var map;	
		
		map = new GMaps({
			div: '#map',
			lat: 51.52340384551881,
			lng: -0.08042346032720804,
			zoom: 16,
			mapTypeControl: false,
			overviewMapControl: false,
			panControl: false,
			scaleControl: false,
			streetViewControl: false,
			zoomControlOptions: {
		        style: google.maps.ZoomControlStyle.SMALL,
		        position: google.maps.ControlPosition.TOP_RIGHT
		    },
			styles: [
			  {
			    featureType: "road.highway",
			    stylers: [
			      { saturation: -100 }
			    ]
			  },{
			    featureType: "road.arterial",
			    stylers: [
			      { saturation: -100 }
			    ]
			  },{
			    featureType: "road.local",
			    stylers: [
			      { saturation: -100 }
			    ]
			  },{
			    featureType: "transit.line",
			    stylers: [
			      { saturation: -100 }
			    ]
			  },{
			    featureType: "water",
			    stylers: [
			      { saturation: -100 },
			      { visibility: "off" }
			    ]
			  },{
			    featureType: "landscape",
			    stylers: [
			      { color: "#dddddd" },
			      { visibility: "simplified" }
			    ]
			  },{
			    featureType: "poi",
			    stylers: [
			      { saturation: -100 },
			      { visibility: "simplified" }
			    ]
			  },{
			    featureType: "transit.station",
			    stylers: [
			      { saturation: -100 }
			    ]
			  },{
			    featureType: "administrative",
			    stylers: [
			      { saturation: -100 },
			      { visibility: "on" }
			    ]
			  },{
			    featureType: "road.arterial",
			    elementType: "geometry.fill",
			    stylers: [
			      { color: "#ffffff" }
			    ]
			  },{
			    featureType: "road.highway",
			    elementType: "geometry.fill",
			    stylers: [
			      { color: "#ffffff" }
			    ]
			  },{
			    featureType: "road.highway",
			    elementType: "labels.text.stroke",
			    stylers: [
			      { visibility: "off" }
			    ]
			  },{
			    featureType: "road.arterial",
			    elementType: "labels.text.stroke",
			    stylers: [
			      { visibility: "off" }
			    ]
			  },{
			    featureType: "poi",
			    stylers: [
			      { visibility: "off" }
			    ]
			  },{
			    featureType: "transit",
			    elementType: "labels.text.stroke",
			    stylers: [
			      { visibility: "off" }
			    ]
			  },{
			    featureType: "water",
			    elementType: "geometry.fill",
			    stylers: [
			      { visibility: "on" },
			      { color: "#999999" }
			    ]
			  },{
			    featureType: "transit.line",
			    stylers: [
			      { visibility: "off" }
			    ]
			  },{
			    featureType: "road.highway",
			    elementType: "geometry.stroke",
			    stylers: [
			      { visibility: "off" }
			    ]
			  },{
			    featureType: "road.arterial",
			    elementType: "geometry.stroke",
			    stylers: [
			      { visibility: "off" }
			    ]
			  },{
			    featureType: "road.local",
			    elementType: "geometry.stroke",
			    stylers: [
			      { visibility: "off" }
			    ]
			  },{
			  }
			]
		});
		
		marker = map.addMarker({
		  lat: 51.522803,
		  lng: -0.078916,
		  title: 'sennep',
		  icon: 'h',
		  click: function(e) {
		    //console.log(map.getCenter());
		  }
		});
		
		//mapCenter = map.getCenter();
	}*/
	
	if($("#latest").length)
	{
		currentSlide = 0;
		maxSlides = $("li", "#latest").length - 1;
		stopped = true;
		
		colorArray = $("li", "#latest").eq(0).attr("data-colours").split(',');
		
		for(var i=0; i <= maxSlides; i++)
		{
			$("#slidernav").append('<a href="#" data-id="'+ i +'">'+ (i+1) +'</a>');
		}
		
		$("a", "#slidernav").eq(0).addClass("selectedSlide");
		
		if($("li", "#latest").eq(0).hasClass("lightText"))
   			$("#slider").addClass("lightText");
		
		transition_slides = function(){
			
			if($("li", "#latest").eq(currentSlide).hasClass("lightText"))
	   			$("#slider").addClass("lightText");
	   		else
	   			$("#slider").removeClass("lightText");
	   		
	   		$(".selectedSlide").removeClass("selectedSlide");
	   		$("a", "#slidernav").eq(currentSlide).addClass("selectedSlide");
	   		
	   		if(Modernizr.canvas && (typeof ST != "undefined") && loaded)
	   		{
	   			colorArray = $("li", "#latest").eq(currentSlide).attr("data-colours").split(',');
	   			update_triangle();
	   		}
	   		
			$("#latest").css("left", "-" + available_width*currentSlide + "px");
			
		}
		
		if(Modernizr.touch)
		
			$("#latest").on("swipe", function(e) {
		   		if((browser_width >= 1024) && stopped)
		   		{
		   			stopped = false;
		   		
			   		if(e.direction == "right")
				   	{	
				   		currentSlide--;
						if (currentSlide < 0) currentSlide = 0;
					}
					else if(e.direction == "left")
					{
						currentSlide++;
						if (currentSlide > maxSlides) currentSlide = maxSlides;
			   		}
			   		
			   		transition_slides();
					
					setTimeout(function(){
						stopped = true;
					}, 500);
			    }
			});
	
		else
			
			$("#latest").on("click", function(e) {
		   		if((browser_width >= 1024) && stopped)
		   		{
					currentSlide++;
					if (currentSlide > maxSlides) currentSlide = 0;
					
					transition_slides();
					
					setTimeout(function(){
						stopped = true;
					}, 500);
			    }
			});
			
		$("#slidernav").on("click", "a", function(e) {
	   		if((browser_width >= 1024) && stopped)
	   		{
				currentSlide = $(this).attr("data-id");
				
				transition_slides();
				
				setTimeout(function(){
					stopped = true;
				}, 500);
		    }
		});
	}
	
	$(".video").each(function(){
		
		var videoObj = $(this),
		videoContainer = $(".video-container", this),
		placeholderImg = videoObj.find(".placeholder"),
		videoColour = "aaaaaa";
		
		//$(".video-container", this).css("padding-bottom", (100 * (placeholderImg.attr("height") / placeholderImg.attr("width")))  + "%");
		if((placeholderImg.attr("height") / placeholderImg.attr("width")) > .57) $(".video-container", this).addClass("fourbythree");
		
		if(videoObj.attr("data-vimeo-colour")) videoColour = videoObj.attr("data-vimeo-colour");
		
		//if(videoObj.attr("data-vimeo-colour")) videoColour = videoObj.attr("data-vimeo-colour");
		
		var videoHTML = '<iframe src="http://player.vimeo.com/video/' + videoObj.attr("data-vimeo-id") + '?title=0&amp;byline=0&amp;portrait=0&amp;color=' + videoColour + '&amp;autoplay=1&amp;api=1" data-width="'+ placeholderImg.attr("width") +'" data-height="'+ placeholderImg.attr("height") +'" width="'+ placeholderImg.attr("width") +'" height="'+ placeholderImg.attr("height") +'" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>';
		
		//$(this).css("height", ($(this).width() * placeholderImg.attr("height")  / placeholderImg.attr("width")) + "px");
			
		$(".play", this).on("click", function(e){
			videoContainer.empty().append(videoHTML);
			
			var iframe = videoObj.find("iframe")[0],
			player = $f(iframe);
			
			player.addEvent('ready', function() {
				player.api('setColor', videoColour);
			});

			e.preventDefault();
		});
		
		if(Modernizr.touch)
		{
			videoContainer.empty().append(videoHTML);
			
			var iframe = videoObj.find("iframe")[0],
			player = $f(iframe);
						
			player.addEvent('ready', function() {
				player.api('setColor', videoColour);
			});
		}
		
	});
	
	if($("body").hasClass("home")) $("html").addClass("hideScroll");
	else $("html").removeClass("hideScroll");
	
	if(loaded && Modernizr.canvas && (typeof ST != "undefined"))
	{
		setTimeout(function(){
			update_triangle();
		}, 700);
	}
}

// Ready to go

$(document).ready(function() {
		
	if($("html").hasClass("touch"))
	{
		touch_enabled = true;
	}

	resize_magic();
	
	fixed_el = $(".description");
	
	ST.updateSize($("#triangle").width(),$("#triangle").height(), $(window).width()<768?44:88);
	ST.init();
	
	if(!touch_enabled)
  	{	
		$(window).resize(function() {
	  		resize_magic();
		});
		
		$(window).scroll(function()
		{	
			//if($("body").hasClass("single") && $(".meta").length && !$("body").hasClass("about"))
			//	overlap_lines();
			
			var menu_height = $("#filter").find(".header").outerHeight() + $(".open ul", "#filter").outerHeight();
				
			if(menu_height > browser_height)
			{
				if($(document).scrollTop() <= (menu_height - browser_height))
				{
					 $(".open ul", "#filter").css("margin-top", "-" + $(document).scrollTop() + "px");
				}
				else
				{
					  $(".open ul", "#filter").css("margin-top", "-" + (menu_height - browser_height) + "px");
				}
			}
				
			//if($("html").hasClass("fake_fixed"))
			//{
			//	$(".menu").css("top", $(document).scrollTop() + "px");
			//}
		});
		
		$(document).keydown(function(e) {
		  	    
			  switch(e.keyCode) { 
			     case 39:
					if($("#latest").length && (browser_width >= 1024) && stopped)
					{	
						stopped = false;
						
						currentSlide++;
						if (currentSlide > maxSlides) currentSlide = 0;
						
						transition_slides();
						
						setTimeout(function(){
							stopped = true;
						}, 500);
					}
					
					else if($("#next-project").length)
					{
						$("#next-project").click();
					}
					
			        return false;
			     break;
			     case 37:
			     	if($("#latest").length && (browser_width >= 1024) && stopped)
			     	{		
				     	stopped = false;
				     
				     	currentSlide--;
						if (currentSlide < 0) currentSlide = maxSlides;
						
						transition_slides();
						
						setTimeout(function(){
							stopped = true;
						}, 500);
					}
					
					else if($("#prev-project").length)
					{
						$("#prev-project").click();
					}
					
			        return false;
			     break;
			}
		});
  	}
  	else
  	{
  		$('html.touch body').bind('orientationchange',function(event){
	        resize_magic();
	    })
	    	    
	    //$("#filter").on("touchmove", function(e) { e.preventDefault(); })
	    
	    if(navigator.userAgent.match("OS 4") || navigator.userAgent.match("OS 3"))
		{
			$("html").addClass("fake_fixed");
		}
	}
	
	/*$("#find").on("click", function(e){

		var thumb_width = $("#isotope article").eq(0).width() + 12;
		var per_row = Math.floor($("#isotope").width() / thumb_width);
		
		var forceIsotopeResize = setInterval(function () {
			if(Math.floor($("#isotope").width() / thumb_width) !== per_row)
			{
				$("#isotope").addClass("transition_height").isotope("reLayout", function(){ $("#isotope").removeClass("transition_height") });		
				clearInterval(forceIsotopeResize);
			}
		}, 50);
		
		$("#filter").css("opacity", 1).removeClass("just_opacity");
		
		if($("body").hasClass("filter-open"))
		{
			$("body").removeClass("filter-open");
			$(this).removeClass("selected");
			
			$("#frame").off();
		}
		else
		{
			$("#filter").removeClass("no_transition");
			$("body").addClass("filter-open");
			$(this).addClass("selected");
			
			if($("body").hasClass("single"))
				$("#frame").on("touchmove", function(e) { e.preventDefault(); })
						
			$("#frame").on("click", function(e){			
				$("#find").trigger("click");
				e.preventDefault();
			});
		}
		
		setTimeout(function(){
			if(!Modernizr.touch && $("#isotope").length)
			{
				filter_scrollbar();
			}
		}, 100);
		
		$(this).removeClass("no-ajax");
				
		return false;
	});*/
	
	$("#filter .header").on("click", "a", function(){		
		filter_actions($(this));
		
		return false;
	});
	
	filter_actions = function(el)
	{
		if(!el.hasClass("selected"))
		{
			var cur_el = $("#filter .header .selected");
			var cur_id = cur_el.attr("id").replace("toggle-","");
			var future_el = el;
			var futured_id = future_el.attr("id").replace("toggle-","");
			
			$(".selected", "#filter").removeAttr("class");
			
			cur_el.removeClass("selected");
			future_el.addClass("selected");
			
			$("ul", "#" + cur_id).removeAttr("style");
			$("#" + cur_id).removeClass("open");
			
			$("a", "#" + futured_id).eq(0).trigger("click");
						
			var waitForOpacity = setTimeout(function(){
				$("#" + cur_id).addClass("hidden");
				$("#" + futured_id).removeClass("hidden")
				
				var waitForHiding = setTimeout(function(){
					$("#" + futured_id).addClass("open");
					
					if(!Modernizr.touch && $("#isotope").length)
					{
						filter_scrollbar();
					}

					clearTimeout(waitForHiding);
				}, 100);
				clearTimeout(waitForOpacity);
			}, 300);
		}
	}
	
	reinitialise_content();
	
});

$(window).load(function() {

	loaded = true;
	
	if(Modernizr.touch)
	setTimeout(function(){
		filterScroll = new iScroll('clients', { vScrollbar:false, hScrollbar: false });
	}, 100);

	resize_magic();
	
	if(Modernizr.canvas && (typeof ST != "undefined"))
	{
		update_triangle();
	}
	
	$("#frame").css("opacity", 1);
	
});