$.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(){
	/**
	Cheat way of setting the height of the sidebar so it matches the variable height of the content
	area throughout the site! DOESN'T ALWAYS WORK!
	**/
	//var body_height = $(".copy_container").height();
	//$(".sidebar").css({"height":body_height});
	
	var maskH = $(document).height();
	var maskW = $(document).width();
			
	$("#brochure-mask").css({'height':maskH,'width':maskW, 'opacity':'0.69'});
			
});


$(function () {
	
	//Main nav superfish
	$actives = $("#nav_main .active");

	$("#nav_main").superfish({
		dropShadows: false,
		autoArrows: false,
		pathLevels: 3,
		onInit: function () { $actives.addClass("active");},
		pathClass: 'active'
	});
	
	
	$('#image_slider ul').innerfade({
			speed: 'slow',
			timeout: 4000,
			type: 'sequence',
			containerheight: '507px'
	});
	
	
	
	//home page carousel controls
	//$next = $('.prev');
  //$prev = $('.next');
  
  
  
  //all of this is commented out as this is what makes it slide automatically!
  
  
  //var startDelay = true;
  
  function startCarousel()
  {
  	$(".carousel").carousel($next, $prev);
  	//startDelay = false;
  }
  
	//setTimeout(startCarousel, 4000);
	//startCarousel();
	
	
	// - for the moment whilst on 1 image stop the rotating effect!!
	/*
  var isStopped = false;
  function next()
  {
  	if(!isStopped)
    {
    	$prev.trigger("click");
    }
    setTimeout(next, 5000);
  }
  next();
  */
  /*  
  $("#image_slider").hover (function () {
  	isStopped = true;
  	if(!startDelay){
   		$prev.stop().animate({'bottom':0}, 600);
   		$next.stop().animate({'bottom':0}, 600);
   	}
   }, function () {
     isStopped = false;
     if(!startDelay){
     	$prev.stop().animate({'bottom':-25}, 600);
   	 	$next.stop().animate({'bottom':-25}, 600);
   	}
   });
  
  $next.hover(function () {
  	isStopped = true;
   }, function () {
     isStopped = false;
  });
   
  $prev.hover(function () {
  	isStopped = true;
   }, function () {
     isStopped = false;
  });
   
  $next.click(function() {
  	isStopped = false;
  });
  $prev.click(function() {
  	isStopped = false;
  });
	*/




/*********************************
			ETHICORE BUTTONS 
*********************************/

	$("#tactical a").addClass('niceanimation').append('<div class="animationhelper"></div>');
	$("#tactical a").each(function () {
		$$this = $(this);
		$.data($$this, 'animating', false);
		$(this).find (".animationhelper").css("background-image", $(this).css("background-image"));
	}).hover (function () 
		{
				
			if ($.data($$this, 'animating')===false)
			{
				$.data($$this, 'animating', true);
				$(this).find(".animationhelper").fadeOut('fast', function () {$.data($$this, 'animating', false)});
			}
		}, function () 
		{
			
			$(this).find(".animationhelper").fadeIn('slow');
		}); // end $(.tactical-box).each
			
		$(".tactical a").mousedown (function () {
			$(this).addClass("active");
		}).mouseup (function () {
			$(this).removeClass("active");
		})



  
});

/**********************************************
						PROJECT GALLERY PAGES
**********************************************/

$(document).ready(function() {
	//initialise the content area of the page by hiding the text and showing image 1 and more info buttons and nav
	$("#mask").css({opacity: 0}).show();	
	$("#story_container").css({opacity: 0}).show();	
	$("#close").css({opacity: 0}).hide();	
	
	//initialise jScrollPane...
	$("#main_story").jScrollPane({dragMaxHeight: 50, dragMinHeight: 50});
	
}); //end of document ready function!

$(function() {
  	
	//projects page controls...
	$p_next = $("#next");
	$p_prev = $("#prev");
	$p_close = $("#close");
	$p_read_story = $("#read_story");
	
	//starting margin
	var galPos = 0;
	var maxMove = $("#image_gallery ul li").size() - 1;
	var prev_hid = true;
	var next_hid = false;
	maxMove = maxMove * -810;
	
	
	$p_next.click(function () {
		if(galPos!=maxMove){
			if(prev_hid)
				$p_prev.show();
							
			galPos = galPos - 810;
			$("#image_gallery ul").stop().animate({'marginLeft': galPos},1000);
		}
		if(galPos==maxMove){
			$(this).hide();
			next_hid = true;
		}
	});
	
	$p_prev.click(function () {
		if(galPos!=0){
			if(next_hid)
				$p_next.show();
				
			galPos = galPos + 810;
			$("#image_gallery ul").stop().animate({'marginLeft': galPos},1000);
		}
		if(galPos==0){
			$(this).hide();
			prev_hid = true;
		}
	});	
		
	$p_read_story.click(function () {
		
		$(this).animate({opacity:0},300).hide();
		$p_next.css({'z-index':0});
		$p_prev.css({'z-index':0});
		$p_close.show();
						
		$("#mask").stop().animate({opacity:0.69, height:507},1000, 
			function(){
				$("#story_container").animate({opacity:1},500,		
					function(){						
						$p_close.animate({opacity:1}, 600);
						$p_next.animate({opacity:0}, 600);
						$p_prev.animate({opacity:0}, 600);
					});
				});
		});

	$p_close.click(function() {
		$(this).animate({opacity:0},300).hide();
		$p_close.animate({opacity:0}, 400);
		$("#story_container").animate({opacity:0},500,
			function(){
				$("#mask").stop().animate({opacity:0, height:0},500, 
					function(){
						$p_read_story.animate({opacity:1},500).show();
						$p_next.animate({opacity:1}, 600);
						$p_prev.animate({opacity:1}, 600);
				});
			});
			
		});
});

/**************************************
			jQuery for Contact Map
**************************************/

$(document).ready( function () {
	$("#contact-mask").css({'opacity':0.69});
	$("a.office-link span").css({'top':15});
	
});

$(function () {
	
	$("a.office-link").hover( 
		function () {
			$("a.office-link").css({'z-index':'1'});
    	$(this).css({'z-index':'9999'});
			
			$(this).children("span").stop().animate({top:8},200).show();
		},
		function () {
			$(this).children("span").css({'top':'20px'}).hide();
			$(this).css({'z-index':'1'});
		});
	
	
	$("a.office-link").click( function () {
		$(this).children("span").css({'top':'15px'}).hide();
		var o_link = $(this).attr("id");
		
		if(o_link!='scotland'&&o_link!='thame'&&o_link!='america'){
			var office = "?send=1&office=";
			office += $(this).attr("id");
			$("form#contact-us").attr("action", office);
		
			$("#contact-mask").css({'opacity':0}).show();
			$("#contact-mask").stop().animate({opacity:0.69},500, function(){			
				$("#contact-form-container").stop().animate({opacity:1},400).show();
			});
		}
	});
	
	
	$("a#scotland").click(function() {
		$("div.head-offices").hide();
		$("div#scotland-office").show();
	});
	
	$("a#thame").click(function() {
		$("div.head-offices").hide();
		$("div#thame-office").show();
	});
	
	$("a#america").click(function() {
		$("div.head-offices").hide();
		$("div#usa-office").show();
	});
	
	$("#contact-form a#contact-form-close").click( function () {
		$("#contact-mask").css({opacity:0}).hide();
		$("#contact-form-container").hide();
	});
	
	$("#contact-mask").click( function () {
		$(this).css({opacity:0}).hide();
		$("#contact-form-container").hide();
	});
			
}); 


/*********************************************
					CONTACT US FORM VALIDATION
*********************************************/

$(function () {

$("form#contact-us").submit (function () {
		var fullname = $(this).find ("input[name=fullname]");
		var email = $(this).find("input[name=email]");
		var company = $(this).find("input[name=company]");
		var request = $(this).find("textarea[name=request]");
		if (VALIDATOR.isEmpty (fullname.val()))
		{
			alert ('Please enter your name');
			fullname.focus();
			return false;
		}
		else if ( VALIDATOR.isEmpty (email.val()) || !VALIDATOR.isEmail (email.val()) )
		{
			alert('Please enter your email address');
			email.focus();
			return false;
		}
		else if ( VALIDATOR.isEmpty (company.val()) )
		{
			alert('Please enter your company name');
			company.focus();
			return false;
		}
		else if (VALIDATOR.isEmpty (request.val()) )
		{
			alert('Please type your message into the text box');
			request.focus();
			return false;
		}
		return true;
	});
});

var VALIDATOR = {
	isEmpty: function (str) {
		return str === "";
	},
	isEmail: function (str) {
		var regex = /^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i;
		return regex.test(str);
	}
};					


/*********************************************
					MEET THE TEAM PAGE
*********************************************/

$(function () {
		//can't remeber why this is there!
		//$("#profile-container").addClass("hasJS");
		
		if(location.hash && ($(location.hash).length))
		{
			$(".profile-link[href*="+location.hash+"]").addClass("active");
		}
		else
		{
			$("#clive-m").addClass("active");
		}
		
		$(".profile-link").click (function () {
			
			$(".profile-link").removeClass("active");
			$(this).addClass("active");
					
			$("#biographies .profile").hide();
			$($(this).attr("href")).show();
			
			return false;
		});
		
});
	
	
	
	
$(function () {
	//this is simply an area to initialise functions in very few lines not encapsulating them in their own function section
	
	//apply lightbox to images on the Our clients page
	$("a#met-police").lightBox();
	$("a#wardown").lightBox();
	$("a#wiltshire").lightBox();
	$("a#wartski").lightBox();
	$("a#palais-tokyo").lightBox();
	$("a#brent").lightBox();
	$("a#british").lightBox();
		
	//make the gallery links div entirely clickable on the individual showcases pages
	$("#gallery-link").clickableBlocks();
	
	//show and hide images in the Showcase galleries
	$(".gal-link").click (function () {
			$("#gallery-viewer .image-area").hide();
			$($(this).attr("href")).show();
			return false;
	});
	

});


$(function () {
	
	$("#tactical a#magazine").click( function () {
		$(this).children("span").css({'top':'15px'}).hide();
		var o_link = $(this).attr("id");
		
		if(o_link!='scotland'&&o_link!='thame'){
			var office = "?send=1&office=";
			office += $(this).attr("id");
			$("form#brochure-us").attr("action", office);
		
			
			var maskHeight = $(document).height();
			var maskWidth = $(document).width();
			$("#brochure-mask").css({'opacity':0, 'height':maskHeight, 'width':maskWidth}).show();
	
			$("#brochure-mask").stop().animate({opacity:0.69},500, function(){			
				$("#brochure-form-container").stop().animate({opacity:1},400).show();
			});
		}
	});
	
	$("#brochure-form a#brochure-form-close").click( function () {
		$("#brochure-mask").css({opacity:0}).hide();
		$("#brochure-form-container").hide();
	});
	
	$("#brochure-mask").click( function () {
		$(this).css({opacity:0}).hide();
		$("#brochure-form-container, #magazine-picker").hide();
	});
	
	
	/****
	NEW MAGAZINE JS	
	****/
	
	
	$("#brochure").click( function (e) {
		e.preventDefault();


		var maskHeight = $(document).height();
		var maskWidth = $(document).width();
		$("#brochure-mask").css({'opacity':0, 'height':maskHeight, 'width':maskWidth}).show();

		$("#brochure-mask").stop().animate({opacity:0.69},500, function(){			
			$("#magazine-picker").stop().fadeIn(600);
		});

//		$("#magazine-mask").css({'opacity':0}).show();

//		$("#magazine-mask").stop().animate({"opacity":0.8},500);
//		$("#magazine-picker").stop().fadeIn(500);
	});
	
	
	$("#magazine-picker a#close-button").click( function () {
		$("#brochure-mask").css({opacity:0}).hide();
		$("#magazine-picker").hide();
	});
});
