//prevent conflicts by using jQuery first with the $ in the function call!
jQuery(function ($) {

	$.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;
				});
			}
		});
	}
	
	// Set up things... home logo click and remove the noJS classes
	
	$('.service').clickableBlocks();
	$('#logo').clickableBlocks();
	$('.service-link').clickableBlocks();
	$("#meet-the-team-navigation ul").removeClass("noJS");
	
	$("#tactical-slider").jcarousel({
		scroll: 1,
		visible: 1,
		wrap: "circular",
		auto: 3
	});
	
	//Check and set active states on menu when on single pages
	var activeOffice = false;
	//var activeService = false;
	
	$('#menu-offices li').each( function () {
		if($(this).hasClass('current-menu-item'))
		{
			$('#office-photo').attr('class', $(this).attr("id"));
			activeOffice = $(this).attr("id");
		}
	});

	/** no longer using the services logos.
	$('#services-navigation ul li').each( function () {
		if($(this).hasClass('current-menu-item'))
		{
			$('#services-icon').attr('class', $(this).attr("id"));
			activeService = $(this).attr("id");
		}
	});
	**/
	
	// Lightbox, only currently on office map pages

	$("a.lightbox").fancybox({
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic'
	});
	
	// Menu rollovers - icons/photo's change - so far no change back! Default them back sucka!
	
	$('#menu-offices li').hover ( function () {
		$('#office-photo').attr('class', $(this).attr("id"));
	}, function () {
		if(activeOffice)
			$('#office-photo').attr('class', activeOffice);
		else
			$('#office-photo').attr('class', "menu-item-11");
	});

	/** Not using the services icons any longer
	$('#services-navigation ul li').hover ( function () {
		$('#services-icon').attr('class', $(this).attr("id"));
	}, function () {
		if(activeService)
			$('#services-icon').attr('class', activeService);
		else
			$('#services-icon').attr('class', "menu-item-48");
	});
	**/

	// Services questions selection
	
	//$('#answers-list div').css({"opacity":0,"position":"absolute","top":"0","left":"0"}).hide();
	//$('#answers-list div#answer-1').css({"opacity":1}).show();
	
	$('#answers-list div').each ( function() {
		$(this).data("openHeight",$(this).height());	
	});
	
	var visHeight = $('#answers-list div#answer-1').data("openHeight");
	$('#answers-list').height(visHeight + 20).width(470);;
	
	$('#answers-list div').slideUp(1);
	$('#answers-list div#answer-1').slideDown(1);
	
	$('#questions-list ul li a').click ( function (e) {
		e.preventDefault();
		
		var answer = $(this).attr('href');

		if(!$(this).hasClass('active'))
		{
			var cur = $('#questions-list ul li a.active').attr("href");
			$('#questions-list ul li a').removeClass('active');
			$(this).addClass('active');
			
			$("#answers-list").animate({height: 0},"slow");
			
			$("#answers-list div"+cur).slideUp("slow",function () {
				
				var answerHeight = $("#answers-list div"+answer).data("openHeight") + 20;
				$("#answers-list div"+answer).slideDown("slow");
				$("#answers-list").animate({height:answerHeight}, "slow");
				
			});
			
		}
		/**
		var answer = $(this).attr('href');
		if(!$(this).hasClass('active'))
		{
			$('#questions-list ul li a').removeClass('active');
			$(this).addClass('active');

			$('#answers-list div').stop().animate({"opacity":0},500,function () {
				$(this).hide();
				$(answer).stop().show().animate({"opacity":1},500);
			});
		}	
		**/
	});
	
	// Service answers length setter!
	$('#answers-list').removeClass('noJS');
	/*
	var largest = 0;
	$('#answers-list div').each( function () {
		if($(this).height() > largest)
			largest = $(this).height();
	});
	*/
	//$('#answers-list').height(largest + 20).width(470);
	
	
	//service page service links rollover
	$(".service-link").hover ( function () {
		var $this = $(this);
		$this.css({"background-color":"#f3f3f3"});
		if( $this.attr("id") == "service-developer-link")
		{
			$this.find("h2").css({"color":"#699425"});
			$this.find("p,p a").css({"color":"#59636a"});
		}
	}, function () {
		var $this = $(this);
		if( $this.attr("id") == "service-developer-link")
		{
			$this.css({"background-color":"rgb(175,209,0)"}).find("h2,p,p a").css({"color":"#fff"});
		}
		else
		{
			$this.css({"background-color":"#fff"});
		}
	});
	
	// Meet the team filter menu's
	var sOpenHeight = $("#meet-the-team-navigation ul#service-filter").height();
	var oOpenHeight = $("#meet-the-team-navigation ul#office-filter").height();
	$("#meet-the-team-navigation ul").height(16)
	
	$("#meet-the-team-navigation ul#service-filter li.current").click( function () {
		if($(this).parent("ul").hasClass("open"))
			$(this).parent("ul").stop().animate({"height":15},500).removeClass("open");
		else
			$(this).parent("ul").stop().animate({"height":sOpenHeight},500).addClass("open");
	});

	$("#meet-the-team-navigation ul#office-filter li.current").click( function () {
		if($(this).parent("ul").hasClass("open"))
			$(this).parent("ul").stop().animate({"height":15},500).removeClass("open");
		else
			$(this).parent("ul").stop().animate({"height":oOpenHeight},500).addClass("open");
	});


	$(".signup-form form").submit (function (e) {
			if (!validForm($(this).attr("id"))) {

				return false;
			} else {

				return true;	
			}
			
		});


	function validForm (form_id) {

		var inputs = [
				'#'+form_id+'-name',
				'#'+form_id+'-email'
			];
		$(".error").removeClass("error");
		/*
		$.each (inputs, function (i) {
			$(inputs[i]).removeClass("error");
		});
		*/
		$.each (inputs, function (i) {
			if ($(inputs[i]).attr("value") == "") 
			{
				invalid = true;
				$(inputs[i]).parent("p").addClass("error");
			}
			if(inputs[i] == "#commercial-form-email" || inputs[i] == "#inbrief-form-email") 
			{
				//check if attr is numeric!
				var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;  

			   	if( !emailPattern.test( $(inputs[i]).attr("value") ))
			   	{
			   		$(inputs[i]).parent("p").addClass("error");
			   	}
			}
		});
		
		return !$(".signup-form form").find(".error").length;
	}


});

