var loc;

$(function() {
	
	//Open external links in a new tab
	$("a[rel=external]").attr('target', '_blank');
	
	//Remove IE6 image flicker
	try {document.execCommand('BackgroundImageCache', false, true);} catch(err) {}
	
	//Set country code
	try {loc = google.loader.ClientLocation.address.country_code;}
	catch(err) {loc = 'UK'}

	initHoverNav();
	if ($('#pictures').length)		initCarousel();
	if ($('#thumbs').length)		initCaseStudies();
	if ($('#offices').length)		initOffices();
	if ($('#staff').length)			initStaff();
	//if ($('#contact').length)		initContact();
});

/*
=====================
Hover Nav
=====================
*/
function initHoverNav() {
	$('#breadcrumb .home').bind('mouseenter', function(){
		$('#breadcrumb').css('visibility', 'hidden');
		$('#hoverNav').show();
	});
	$('#hoverNav').bind('mouseleave', function(){
		$('#hoverNav').hide();
		$('#breadcrumb').css('visibility', 'visible');
	});
}

/*
=====================
Carousel
=====================
*/
var count, zIndex = 1, imagesLoadedCount = 0, delay = 3500, isRotating = false;

function initCarousel() {
	$('#carouselNav').css('width', $('#carouselDisplay img').length * 15 + 'px');
	$('#carouselDisplay a').click(function(){
		isRotating = false;
	});
	$('#carouselDisplay img').each(function(){
		$('#carouselNav').append('<a href="' + this.src + '"></a>');
	});
	$($('#carouselNav a')[0]).addClass('active');
	$('#carouselNav a').click(function(e){
		isRotating = false;
		e.preventDefault();
		var index = $('#carouselNav a').index(this);
		$('#carouselNav a').removeClass('active');
		this.className = 'active';
		$('#carouselDisplay li').hide();
		$($('#carouselDisplay li')[index]).show();
	});
	count = $('#carouselDisplay li').length;
	$('#carouselDisplay li:first').css("opacity", "1");
	$('#carouselDisplay li img').each(function() {
		if (this.complete) imagesLoadedCount++;
		else {
			$(this).bind('load', function(){imagesLoadedCount++});
		}
	});
	initRotation();
}

function initRotation() {
	isRotating = true;
	if (imagesLoadedCount != count) setTimeout("initRotation()", 500);
	else {
		$('#carouselDisplay li:not(li:first)').css("display", "none").css("opacity", "1");
		setTimeout("rotate(1)", delay);
	}
}
function rotate(i) {
	if (isRotating) {
		if (i == count) i = 0;
		$($('#carouselDisplay li')[i]).css("display", "none").css("z-index", zIndex).fadeIn(1200);
		setTimeout("$('#carouselNav a').removeClass('active');$($('#carouselNav a')[" + i + "]).addClass('active');", 600);
		zIndex++;
		setTimeout("rotate(" + (i+1) + ")", delay);
	}
}

/*
=====================
Staff contact list
=====================
*/
function initStaff() {
	if (loc == "IE") {
		$('#staff ul').hide();
		$($('#staff ul')[1]).show();
	}
	else if (loc == "AE") {
		$('#staff ul').hide();
		$($('#staff ul')[2]).show();
	}
}

/*
=====================
Case Studies
=====================
*/
function initCaseStudies() {
	var activeTitle = $('.bcCaseStudy').html();
	var activeClassApplied = false;
	$('#thumbs a').each(function(){
		if (this.title == activeTitle && !activeClassApplied) {
			$(this).parent().addClass('active');
			activeClassApplied = true;
		}
	});
	$('#thumbs a').click(function(e){
		e.preventDefault();
		$('#thumbs h2').html(this.title);
		$('.bcCaseStudy').html(this.title);
		Cufon.refresh('#thumbs h2');
		$('#thumbs li').removeClass('active');
		$(this).parent().addClass('active');
		var fileName = $(this).attr('href');
		$('#bannerWrapper').fadeOut(400, function() {
			$('#bannerWrapper').html('');
			if (fileName.match("swf$") == "swf") {
				$('#bannerWrapper').html('<div id="bannerFlash"></div>');
				swfobject.embedSWF(fileName, "bannerFlash", "950", "260", "9.0.0", "", {}, {wmode:"transparent"}, {});
				$('#bannerWrapper').fadeIn();
			}
			else {
				$('#bannerWrapper').show();
				$('#bannerWrapper').html('<img id="bannerImage" src="' + fileName + '" alt="" />');
				$('#bannerImage').hide().load(function(){
					$(this).fadeIn();
				});
			}
		});
	});
}

/*
=====================
Contact page
=====================
*/
function initContact() {
	$('#contactNav a').click(function(e){
		e.preventDefault();
		var index = $('#contactNav a').index(this);
		$('#contact div').css("visibility", "hidden");
		$($('#contact div')[index]).css("visibility", "visible");
	});
	if (loc == "IE") {
		$('#contact div').css("visibility", "hidden");
		$($('#contact div')[1]).css("visibility", "visible");
	}
	else if (loc == "AE") {
		$('#contact div').css("visibility", "hidden");
		$($('#contact div')[2]).css("visibility", "visible");
	}
}

function initOffices() {
	$('#offices a').click(function(e){
		e.preventDefault();
		var index = $('#offices a').index(this);
		$('#bannerWrapper').fadeOut(400, function() {
			loadOffice(index);
		});
	});
	if (window.location.href.indexOf('?') == -1) {
		if (loc == "IE") loadOffice(1);
		if (loc == "AE") loadOffice(2);
	}
}

function loadOffice(index) {
	$('#map span').removeClass();
	$('#staff ul').hide();
	$($('#staff ul')[index]).show();
	var location = $('#offices a')[index].title;
	var fileName = $($('#offices a')[index]).attr('href');
	$('#pin' + location).addClass('active');
	$('.bcLocation').html(location);
	$('#bannerWrapper').html('');
	if (fileName.match("swf$") == "swf") {
		$('#bannerWrapper').html('<div id="bannerFlash"></div>');
		swfobject.embedSWF(fileName, "bannerFlash", "950", "260", "9.0.0", "", {}, {wmode:"transparent"}, {});
		$('#bannerWrapper').fadeIn();
	}
	else {
		$('#bannerWrapper').show();
		$('#bannerWrapper').html('<img id="bannerImage" src="' + fileName + '" alt="" />');
		$('#bannerImage').hide().load(function(){
			$(this).fadeIn();
		});
	}
}
