$(function(){
	
	// get in touch section
	$('#map').initMap();
	
	// global
	$('#logo,#nav,#knowledge,#brand,#people,#home,#approaches>li>p.more').navScroll();
	$('.nav_sub').showPanels();
	
	// home section
	$('#fundamentals').fundamentalPanels();
	
	// about section
	$('div.staff').loadProfile().showName();
	
	// who we work with section
	$('#logos').gallerify();
	
	// questionnaire popup
	$('#insights>.button>a').colorbox({
		iframe: true,
		opacity: .7,
		innerWidth: 636,
		innerHeight: 500,
		scrolling: false
	});
	// Video popup
	$('#video_popout>a').colorbox({
		onOpen: function() {
			_gaq.push(['_trackPageview', '/opening-homepage-video']);
		},
		iframe: true,
		opacity: .7,
		scrolling: false,
		innerWidth: 640, 
		innerHeight: 360
	});
	

});


$.fn.navScroll = function(){
	return this.each(function(){
		$('a',this).click(function(){
			var id = this.href.split('#')[1];
			var target = $('#'+id);
			
			if (target.is('.section')) { target.find('.nav_sub a:first').click(); }

			if (target.is('.panel')) {
				target = target.closest('.section');
				target.find('.nav_sub a[href$='+id+']').click();
				id = target.attr('id');
			}
			
			$('html,body').animate({
				scrollTop: target.offset().top
			},{
				duration: 500,
				complete: function(){ window.location.hash = id; }
			});
			return false;
		});
	});
}

$.fn.showPanels = function(){
	return this.each(function(){
		var panels = $(this).parent().find('.panel');
		panels.not(':first').addClass('hidden');
		
		$('a',this).click(function(){
			$(this).parent().addClass('on').siblings().removeClass('on');
			panels.addClass('hidden');
			$('#'+this.href.split('#')[1]).removeClass('hidden');
			return false;
		});

	});
}

$.fn.fundamentalPanels = function(){
	return this.each(function(){
		var items = $(this).children('div');
		$('#nav_fundamentals>li>a').click(function(){
			items.filter(':visible').hide();
			$('#'+this.href.split('#')[1]).show();
			$(this).parent().removeClass('off').siblings().addClass('off');
			return false;
		});
	});
}

$.fn.showName = function(){
	return this.each(function(){
		if (!$(this).parent().is('#gallery')) {
			$(this).delegate('a','hover',function(){
				var li = $(this).parent();
				li.is('.over') ? li.removeClass('over') : li.addClass('over');
			});
		}
	});
}

$.fn.loadProfile = function(){
	return this.each(function(){
		
		// staff on our values page
		if ($(this).parents('#our_values').length) {
			var our_people_link = $(this).closest('.section').find('.nav_sub a').eq(1);
			$('a',this).click(function(){
				var link = $(this);
				var i = link.closest('ul').children().index(link.parent());
				our_people_link.click();
				$('#gallery').find('li').eq(i).click();
				return false;
			});
		}
		
		// staff on management landing page
		if ($(this).parent().is('#gallery')) {
			var gallery = $(this).parent();
			var profiles = gallery.next().hide();
			var nav = gallery.closest('.section').children('.nav_sub').find('a');
			$('li',this).click(function(){
				gallery.hide();
				profiles.show();
				var link = $(this);
				var i = link.parent().children().index(link);
				profiles.find('.staff a').eq(i).click();
				nav.one('click',function(){
					gallery.show();
					profiles.hide();
				});
				return false;
			});
		}
		
		// staff on profile pages
		else {
			var profile = $(this).closest('.content').find('.profile');
			var our_values = $('#our_values');

			$('a',this).click(function(){
				profile.empty().append('<img class="loader" src="/img/loader.gif" alt="Loading…" />');
				$.get(this.href,function(data){
					data = $(data);
					profile.replaceWith(data);
					profile = data;
				});
				$(this).parent().addClass('on').siblings().removeClass('on');
				return false;
			});
		}
		
	});
}


$.fn.gallerify = function(){
	return this.each(function(){
		var SCROLL_SPEED = 500;
		var container = $(this).css('overflow','hidden');
		var slides = $('#slides');
		var ul = $('>ul',slides);
		var scrollAmount = 800;
		var items = $('>li',ul);
		var itemsWidth = items.outerWidth(true) * items.length;
		
		items.clone().appendTo(ul);
		
		var slide = function(e){
			if(!slides.is(':animated')) {
				var currentScroll = slides.scrollLeft();

				if (e.data.direction == 'next') {
					slides.animate({scrollLeft: currentScroll+scrollAmount},{duration: SCROLL_SPEED, complete:function(){
						if (slides.scrollLeft()>=itemsWidth) slides.scrollLeft(slides.scrollLeft()-itemsWidth);
					}});
				} else {
					slides.animate({scrollLeft: currentScroll-scrollAmount},{duration: SCROLL_SPEED, complete:function(){
						if (slides.scrollLeft()<=0) slides.scrollLeft(itemsWidth);
					}});
				}
			}
		}
		
		// initialise slideshow
		slides.css({
			height: items.outerHeight(),
			overflow: 'hidden'
		});
		ul.css({width: itemsWidth * 2 +'px'});
		
		$('<button id="button_prev"><img src="/img/button_prev.png" alt="Previous" /></button>').bind('click',{direction:'prev'},slide).appendTo(container);
		$('<button id="button_next"><img src="/img/button_next.png" alt="Next" /></button>').bind('click',{direction:'next'},slide).appendTo(container);

	});
}


$.fn.initMap = function(){
	return this.each(function(){
		var latlng = new google.maps.LatLng(51.446933002079206,-0.3440362215042114);
		var myOptions = {
			zoom: 16,
			center: latlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		var pin = '/img/pin.png';
		var map = new google.maps.Map(this, myOptions);
		var marker = new google.maps.Marker({
			position: latlng, 
			map: map,
			icon: pin
		});

	});
}
