$.fn.clickableBlocks = function ()
{
	return this.each (function () {
		var $this = $(this);
		var $anchor = $this.find("a");
		if ($anchor.length)
		{
			$this.css("cursor", "pointer").click (function ()
			{
				location.href = $anchor[0].href;
			});
		}
	});
}

$(document).ready( function () {
	$(".event-container").clickableBlocks();

	$("a.newsletter-link").fancybox({
		'hideOnContentClick': false,
		'padding':2,
		'autoDimensions':false,
		'width':460,
		'height':390
	});

	$("a[href=#booking-form]").fancybox({
		'hideOnContentClick': false,
		'padding': 2,
		'autoDimensions': false,
		'width': 460,
		'height': 320
	});
	
	$("li#menu-item-26 a").fancybox({
		'hideOnContentClick': false,
		'padding':2,
		'autoDimensions':false,
		'width':430,
		'height':280
	});

	$("li#menu-item-27 a").fancybox({
		'hideOnContentClick': false,
		'padding':2,
		'autoDimensions':false,
		'width':360,
		'height':180
	});

	//home page fading banners
	$('ul#banner-fade').innerfade({
		speed: 1000,
		timeout: 3000,
		type: 'sequence',
		containerheight: '315px'
	});

	//footer tactical boxes
	var largest = 0;
	$('.footer-boxes').each( function () {
		if($(this).height()> largest)
			largest = $(this).height();

		$('.footer-boxes').height(largest);
	});

	//IT wizard auto submit
	$("input#sub").hide();

	$(".question input").click( function() {

		$("form#case-wizard").submit();
	});


	//Partners page hovers
	$("article.partner").clickableBlocks();
	$("article.partner").hover ( function () {
		$(this).children("section.over-section").stop().animate({'left':'2px'},200);
	}, function () {
		$(this).children("section.over-section").stop().animate({'left':'211px'},200);
	});


	//Our clients category nav
	/*
	AO: NOT SURE WHETHER TO DO THIS ANYMORE AS IT DOESN'T MAKE MUCH SENSE TO...
	if($("p#sector").length != 0)
	{
		var parent_cat = $("p#sector").attr("class").split(" ");
		$("li."+parent_cat[1]).addClass("current-cat");
		$("li."+parent_cat[0]).children("ul.children").slideDown();
	}

	$("li.current-cat").children("ul.children").slideDown();
	*/
	$("li.current-cat").parent().addClass("current-cat-parent");
	$("ul.current-cat-parent").slideDown();

	$("a.parent-link").each( function() {
		$(this).siblings("ul").addClass("children");
	});

	if(window.location.pathname == "/our-clients/")
		$("a.market-sector").siblings("ul").slideDown();

	$("a.parent-link").click( function (e) {
		e.preventDefault();

		$childList = $(this).siblings("ul");

		if($childList.is(":visible"))
		{
			$childList.slideUp();
		}
		else
		{
			$("ul.children").slideUp();
			$childList.slideDown();
		}
	});

	//Events sub-nav
	if(window.location.pathname == "/about-us/events/")
		$("li#all-events").addClass("current-cat");


/********************************************************************
			MEET THE TEAM JS
********************************************************************/

	/***
		Image gallery scroller!
	***/

	$("#gallery-slider, #controls").addClass("has-js");
	
	$("#controls #prev").hide();
	var $allTeam = $("#gallery-slider article"),
		teamCount = $allTeam.length,
		colCount = Math.floor(teamCount / 3),
		pcRemainder = teamCount % 3,
		galleryWidth = colCount * 183;
	
	if(pcRemainder != 0)
	{
		colCount++;
		galleryWidth = galleryWidth + 183;
	}
	
	
	
	$("#gallery-slider").width(galleryWidth);
	
	$allTeam.each( function(idx) {
		var row = idx % 3,
			col = Math.floor(idx/3),
			boxTop = (row * 23) + (row * 160),
			boxLeft = (col * 23) + (col * 160);  
		
		$(this).css({"position":"absolute", "cursor":"pointer", "top":boxTop, "left":boxLeft});
				
	});
	
	// AO: Now just to make the left and right clicks work!
	var galleryBlocks = Math.floor(colCount / 4),
		extraRows = colCount % 4,
		maxBlocks = galleryBlocks,
		extraMove = extraRows * 183,
		extras = false;
	
	$("#next").click( function (e) {
		e.preventDefault();
		
		if(galleryBlocks > 1)
		{
			// need to work out the generic move amount..,
			$allTeam.stop().animate({"left":"-=732px"},"slow");
			galleryBlocks--;
			$("#prev").show();	

			if(extraMove == 0)
				$(this).hide();
		}
		else if(galleryBlocks == 1 && !extras)
		{
			$allTeam.stop().animate({"left":"-="+extraMove+"px"},"slow");
			extras = true;
			$("#next").hide();
			$("#prev").show();
		}

	});

	$("#prev").click( function (e) {
		e.preventDefault();

		if(extras)
		{
			$allTeam.stop().animate({"left":"+="+extraMove+"px"},"slow");
			extras = false;
			$("#next").show();

			if(galleryBlocks == maxBlocks)
				$("#prev").hide();

		}
		else if(galleryBlocks < maxBlocks)
		{
			$allTeam.stop().animate({"left":"+=732px"},"slow");
			galleryBlocks++;

			if(galleryBlocks == maxBlocks)
				$("#prev").hide();

			if(extraMove == 0)
				$("#next").show();
		}
		
	});
	
	//The team overlay section...
	$("a.team-member").hover( function () {
		var animeHeight = 146 - $(this).children("span").height();

		$(this).children("span").stop().animate({"top":animeHeight},300);
	}, function () {
		$(this).children("span").stop().animate({"top":"160px"},300);
	});

	$("a.team-member").click( function (e) {
		e.preventDefault();
		var $this = $(this),
			$bio = $($this.attr("href")+".bio-container");
		

		$("#team-office").append('<div class="mask"></div>');
		$(".mask").css({"opacity":0}).show();
		$bio.css({"opacity":0}).show();

		$(".mask").stop().animate({"top":0,"left":0,"width":"710px","height":"526px","opacity":0.95},800, function () {
			$bio.stop().animate({"opacity":1},800);
		});
	});

	$("a.bio-close").click( function (e) {
		e.preventDefault();

		$(this).parents(".bio-container").stop().animate({"opacity":0},800, function () {
			$(".mask").stop().animate({"top":"263px","left":"355px","width":"0px","height":"0px","opacity":0},800, function () {
				$(this).remove();
				$(".bio-container").hide();
			});
		});
	});
	
	/*

	//The team overlay section...
	$("a.team-member").hover( function () {
		var animeHeight = 146 - $(this).children("span").height();

		$(this).children("span").stop().animate({"top":animeHeight},300);
	}, function () {
		$(this).children("span").stop().animate({"top":"160px"},300);
	});

	$("a.team-member").click( function (e) {
		e.preventDefault();

		$(this).parent().append('<div class="mask"></div>');
		$(".mask").css({"opacity":0}).show();
		$(this).parent().children(".bio-container").css({"opacity":0}).show();

		$(".mask").stop().animate({"top":0,"left":0,"width":"710px","height":"526px","opacity":0.95},800, function () {
			$(this).parent().children(".bio-container").stop().animate({"opacity":1},800);
		});
	});

	$("a.bio-close").click( function (e) {
		e.preventDefault();

		$(this).parents(".bio-container").stop().animate({"opacity":0},800, function () {
			$(".mask").stop().animate({"top":"263px","left":"355px","width":"0px","height":"0px","opacity":0},800, function () {
				$(this).remove();
				$(".bio-container").hide();
			});
		});
	});
	*/
	
	
	$("a[href^=mailto]").click (function (e) {
		try {
			_gaq.push(['_trackPageview'], $(this).attr("href"));
		} catch (er) {}
	});
});

