// initialise jQuery-based stuff
$(function(){

	// Service tabs
	var offset = ($("nav.services").hasClass("big")) ? -134 : -79;	
	
	$("nav.services div").each(function(i) {
		// Rollovers
		$container = $(this).parent();
		//var offset = ($container.hasClass("big")) ? -134 : -79;
		$(this).hover(
			function() {
				$container.css("background-position", "0 " + (offset * (i + 1)) + "px");
			}, function() {
				resetBackgroundPosition(offset);			
			}
		);	
		// Allow the whole of the services tabs to be clickable	
		$(this).click(
			function() {
				document.location = $(this).children("a").attr("href");
			}
		);
	});
	
	resetBackgroundPosition(offset);
	
	// Rollover tooltips for client logos
	$("article.client-logo").hover(
		function() {
			$(this).children("div")
				.fadeTo(0, 0)
				.show()
				.fadeTo(0, 0.78);
		},
		function() {
			$(this).children("div")
				.fadeOut("fast");	
		}
	);

	// Make the whole client logo clickable using the href from the link inside it	
	$("article.client-logo div").click(
		function() {
			document.location = $(this).children("a").attr("href");		
		}
	);

	// Kill the right margin on every third client logo
	$("article.client-logo:nth-child(3n+1)").css({
		"margin": "0 0 30px 0"
	});
	
	// Hack full height for columns	
	var contentHeight = $("#content").height() - 117;
	// The section argument to $() sets context for the selector
	$("section", "body:not(.home)").css({
		"height": contentHeight
	});
	
});

$(window).load(function(){  
	// Slideshow for the homepage
	// Requires jquery.cycle plugin
	$("#puzzle div.slide").cycle({ 
		fx:        "fade", 
		speed:     1000, 
        timeoutFn: rand
	});	
});  

function resetBackgroundPosition(offset) {
	$("nav.services").css("background-position", "0 0");
	$(".public-affairs nav.services").css("background-position", "0 " + (offset * 1) + "px");
	$(".planning-communications nav.services").css("background-position", "0 " + (offset * 2) + "px");
	$(".public-relations nav.services").css("background-position", "0 " + (offset * 3) + "px");	
}

// Global variable, sorry
// This is needed so there's less delay on the first run of the slides
var slideIteration = 0;
var low = 200;
var high = 8000;
var initialSlides = 5;
function rand() {
	var delay = Math.floor((Math.random() * high) + low);
	if (slideIteration < initialSlides) {
		slideIteration++;
	} else {
		low = 6000;
		high = 11000;		
	}
	return delay;
}