$(document).ready(function() {
	
	// initiate ExtendedType
	//var et = new ExtendedType({ path : "/assets/et/et.php" });
	//et.replace();
	
	// wrap each three projects in a div
	if ($(".tagged-projects").exists()) {
		var projects = $(".tagged-projects .work-project");
		for (i=1; i<=projects.length; i++) {
			if (i % 5 == 0) {
				$('.work-project:eq('+(i-5)+'), .work-project:eq('+(i-4)+'), .work-project:eq('+(i-3)+'),  .work-project:eq('+(i-2)+'),  .work-project:eq('+(i-1)+')').wrapAll('<div class="clearfix row"></div>');
			}
		}
	}
	if ($(".tagged-news").exists()) {
		var projects = $(".tagged-news .news-item");
		for (i=1; i<=projects.length; i++) {
			if (i % 3 == 0) {
				$('.news-item:eq('+(i-3)+'), .news-item:eq('+(i-2)+'), .news-item:eq('+(i-1)+')').wrapAll('<div class="clearfix row"></div>');
			}
		}
	}
	
	if ($(".service").exists()) {
		var services = $(".service");
		for (i=1; i<=services.length; i++) {
			if (i % 5 == 0) {
				$('.service:eq('+(i-5)+'), .service:eq('+(i-4)+'), .service:eq('+(i-3)+'), .service:eq('+(i-2)+'), .service:eq('+(i-1)+')').wrapAll('<div class="clearfix row"></div>');
			}
		}
	}
	
	$(".related-last .row:last").css("border-bottom", "none");
	
	// wrap each three projects in a div
	if ($("#project-archive").exists() && $(".tagged-projects").length == 0) {
		var projects = $("#project-archive .work-project");
		for (i=1; i<=projects.length; i++) {
			if (i % 3 == 0) {
				$('.work-project:eq('+(i-3)+'), .work-project:eq('+(i-2)+'), .work-project:eq('+(i-1)+') ').wrapAll('<div class="clearfix"></div>');
			}
		}
	}
	
	// Initialise our video and make our flash fallback available if needed
	document.createElement('video'); // Shiv for IE
	$('video#showreel').video();
	$('#work-video video').video({'swapInPoster':false});
	
	// home page
	$("#featured-sub div.project").each(function() {
		$("h3, p", this).wrapAll("<div class='home-project-info'></div>");
	});

	if ($("li#n-contacts").hasClass("selected")) {
		var map = new GMap2(document.getElementById("google-map"));
		
		// http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=SE1+2AX&sll=37.0625,-95.677068&sspn=43.578243,93.076172&ie=UTF8&hq=&hnear=London+SE1+2AX,+United+Kingdom&z=16
		var airlock = [51.50235, -0.07];
		
		map.setCenter(new GLatLng(airlock[0], airlock[1]), 15);
		map.addControl(new GLargeMapControl());
		
		var icon = new GIcon();
		icon.image = "/assets/images/airlock-marker.png";
		icon.shadow = "/assets/images/airlock-marker-shadow.png";
		icon.iconSize = new GSize(27, 43);
		icon.shadowSize = new GSize(49, 43);
		icon.iconAnchor = new GPoint(13.0, 21.0);
	
		map.addOverlay(new GMarker(new GLatLng(airlock[0], airlock[1]), {icon: icon}));
		
	}
	
	if ($(".news-item").exists()) {		
		$('.content').each(function(){		
			$('.news-content', this).equalHeights();		
		});		
	}
	
	$("div.work-project, #featured-main, div.project, div.news-item").each(function() {		
		
		var apply = true;
		if ($(this).hasClass("news-item") && $("a.more", this).length == 0) {
			apply = false;
		}
		
		if (apply) {
			var a = "";
			if ($(this).is("a")) {
				a = $(this);
			} else {
				a = $("a:first", this);
			}

			var href = a.attr("href");

			a.append("<span class='border'></span>");

			span = a.find("span.border");
			span.css("opacity", 0);

			var hitarea = $(this);
			//hitarea.css("cursor", "pointer");

			hitarea.hover(function() {
				$("span.border", this).css("opacity", 0.2);
				//$(this).addClass("hover");
			}, function() {
				$("span.border", this).css("opacity", 0);
				//$(this).removeClass("hover");
			});
		}
	});
	
	$(".airlock-email").html(airlock_email());
	
	$("a[rel='external']").click(function(e) {
		e.preventDefault();
		var href = $(this).attr("href");
		window.open(href);
	});
	
	$("body#error-404 pre").each(function() {
		var html = $(this).html();
		
		html = html.replace(/A/g,"<span>A</span>");
		html = html.replace(/I/g,"<span>I</span>");
		html = html.replace(/R/g,"<span>R</span>");
		html = html.replace(/L/g,"<span>L</span>");
		html = html.replace(/O/g,"<span>O</span>");
		html = html.replace(/C/g,"<span>C</span>");
		html = html.replace(/K/g,"<span>K</span>");		
		html = html.replace(/\)/g,"<span class='orange'>)</span>");
		html = html.replace(/\(/g,"<span class='orange'>(</span>");		
		$(this).html(html);
		
		if ($("span.orange", this).length < 10) {
			$("span.orange", this).remove();
		}
	})
	
});

function airlock_email() {
	return "<a href='&#109;&#97;&#105;&#108;&#116;&#x6f;&#58;&#104;&#x65;&#108;&#x6c;&#111;&#x40;&#x61;&#x69;&#x72;&#x6c;&#x6f;&#99;&#x6b;&#46;&#99;&#111;&#x6d;'>&#104;&#x65;&#108;&#x6c;&#111;&#x40;&#x61;&#x69;&#x72;&#x6c;&#x6f;&#99;&#x6b;&#46;&#99;&#111;&#x6d;</a>";
};

jQuery.fn.exists = function() {
	if (jQuery(this).length > 0) {
		return true;
	} else {
		return false;
	}
};

jQuery.fn.video = function(){
	var cfg = {'swapInPoster': true};
	if(typeof(arguments[0]) != 'undefined') jQuery.extend(cfg, arguments[0]);
	
	return this.each(function(){
		var $video = $(this),
			video = this,
			$parent = $video.parent(); // Use the parent to measure and replace our VIDEO since IE has a hard time handling new elements
			
		if(!$video.canPlayVideo()){ // Browser claims not to support VIDEO
			var container = $('<div></div>').insertAfter($parent),
				so = new SWFObject('/assets/swf/videoPlayer.swf', 'flash_object', $parent.width(), $parent.height(), '8', '#000000');
			
			$parent.remove();
			so.addVariable('configURL', $video.attr('data-config-url'));
			so.addParam('allowFullScreen', 'true');
			so.write(container.get(0));
		}else{
			// Replace our video with the placeholder content, onclick swap the VIDEO back
			if(cfg.swapInPoster && typeof(screen) != 'undefined' && screen.width > 640){ // Ignore for mobiles
				$video.find('a').click(function(evt){
					evt.preventDefault();
					$(this).after($video).remove();
					video.play();
				}).insertAfter($video).end().remove();
			}
		}
	});
};

/**
 * Returns true when the VIDEO element can play one of it's available sources
 */
jQuery.fn.canPlayVideo = function() {
	var video = this.filter('video').get(0);
	
	if(!video || !video.canPlayType){ // Browser doesn't support VIDEO
		return false;
	}
	
	var $video = $(video),
		sources = $video.find('source'),
		l = sources.length;
		
	for(var i = 0; i < l; i++){
		if(video.canPlayType(sources.eq(i).attr('type'))){
			return true; // Browser claims it can play one of our sources
		}
	}
	
	return false;
};

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}