var logoTimer;
var logoObject;
var logoCount;
var logoCurrent = 2;

$(document).ready(function(){
	initClientLogos();
	initPeople();
	initMapArea();
	initGoogleMap();
	initCaseStudies();
	initHomeVideo();
	$('.work_thumbnail').fancybox({
		'overlayOpacity': 0.8
	});
});

function initHomeVideo() {
	if ($('#homeVid').length > 0) {
		$('#homeVid').attr('src', $('#homeVid').attr('src').replace('raining-men','raining-men-play')).wrap('<a href="/video" id="homeVidFancy"></a>')
		$('#homeVidFancy').fancybox({
			'frameWidth': 500,
			'frameHeight': 300,
			'overlayOpacity': 0.8,
			'padding': 20,
			'hideOnContentClick': false
		});
	}
}

function initClientLogos() {
	$('.client-logo img').mouseover(function() {
		logoObject = this;
		if ($(logoObject).width() != 141) {
			logoCount = Math.floor($(logoObject).width() / 141) - 1;
			$(logoObject).css('left', -151);
			logoTimer = setInterval(function() {
				$(logoObject).css('left', -logoCurrent * 151);
				logoCurrent++;
				if (logoCurrent > logoCount) logoCurrent = 1;
			}, 500);
		}
	}).mouseout(function() {
		clearInterval(logoTimer);
		logoCurrent = 2;
		$(logoObject).css({
			'left': '0'
		});
	});
	$('.client-logo a[href]').each(function() {
		$(this).removeAttr('title').attr('href', $(this).attr('href').replace(/client/, 'client_popup'));
	}).fancybox({
		'frameWidth': 700,
		'frameHeight': 500,
		'overlayOpacity': 0.8
	});
	$('#clients-filter')
		.append('<ul><li><a href="/all" class="current">Show all</a></li><li><a href="/financial">Financial Services</a></li><li><a href="/retail">Retail &amp; Home Shopping</a></li><li><a href="/travel">Travel</a></li><li><a href="/b2b">B2B</a></li><li><a href="/leisure">Leisure</a></li><li><a href="/utilities">Utilities</a></li></ul>')
		.find('ul a').click(function(event) {
			$('#clients-filter li a').removeClass('current');
			$(this).addClass('current');
			if (this.pathname.replace(/\//,'') != 'all') {
				$('#client-logos div').stop().fadeTo('normal', 0.25);
				$('#client-logos div *').stop().fadeTo('normal', 0.25);
				$('#client-logos div.' + this.pathname.replace(/\//,'.')).stop().fadeTo('normal', 1);
				$('#client-logos div.' + this.pathname.replace(/\//,'.') + ' *').stop().fadeTo('normal', 1);
			} else {
				$('#client-logos div').stop().fadeTo('normal', 1);
				$('#client-logos div *').stop().fadeTo('normal', 1);
			}
			event.preventDefault();
		});
}

function initPeople() {
	$('#people-images a').each(function() {
		$(this).removeAttr('title').attr('href', $(this).attr('href').replace(/profile/, 'profile_popup'));
	}).fancybox({
		'frameWidth': 700,
		'frameHeight': 440,
		'overlayOpacity': 0.8,
		'padding': 20
	});
}

function initGoogleMap() {
	if ($('#map').length > 0) $('<script src="http://media.theblackhole.co.uk/js/gmap.js" type="text/javascript" charset="utf-8"></script>').appendTo('body');
}

function initMapArea() {
	$('.cycle_description').hide();
	$('#planning_cycle area').attr('title', function() {
		return $($(this).attr('href')).html();
	}).qtip({
		position: {
			corner: {
				target: 'center'
			},
			adjust: {
				screen: true
			}
		},
		style: {
			border: {
				width: 3,
				radius: 5
			},
			name: 'dark',
			tip: {
				corner: 'topLeft',
				color: '#333'
			},
			width: {
				max: 300
			}
		}
	})
}

function initCaseStudies() {
	$(".case-study .details").hide();
	$(".case-study .headings h4").addClass("toggle").click(function() {
		$(".case-study .details").hide();
		$(this).parent().siblings('.details').toggle();
	});
	$('<p class="close">Close</p>').insertAfter('.talk-to-us').click(function(){
		$(this).parent().toggle();
	});
	$(".case-study:nth-child(3n+1) .results").addClass("orange");
	$(".case-study:nth-child(3n+2) .results").addClass("purple");
}