$(function() {
	var ac = 'active';				// Active class
	
	/**
	 * Home page slideshow
	 */
	var to = null;					// Timeout object (used to clear timeout on mouseover)
	var sd = 2500;					// Slideshow delay
	var hs = $('#home-slideshow');	// Slideshow container
	var hsi = hs.find('.images');	// Slideshow images
	var hsl = hs.find('.links');	// Slideshow links
	
	// Whenever the mouse is moved over an <li>
	hsl.find('li').mousemove(function() {
		// Remove the active class from everything
		hsl.find('li').removeClass(ac);
		hsi.find('img').removeClass(ac);
		
		// Find the <img> associated with this <li>
		var li = $(this);
		var i = li.find('a').attr('data-image-id');
		var img = $(i);
		
		// Add the active class to this <li> and <img>
		li.addClass(ac);
		img.addClass(ac);
		
		// Reset timeout, then mouseover next <li> in sd milliseconds
		clearTimeout(to);
		to = setTimeout(function() {
			var a = hsl.find('.' + ac);
			var n = a.next('li').size() > 0 ? a.next('li') : hsl.find('li:first');
			n.mousemove();
		}, sd);
	});
	
	hsl.find('li:first').mousemove();
	
	/**
	 * Customer feedback
	 */
	$('#customer-feedback .feedbacks').cycle({
		timeout: 6000,
		speed: 1500,
		fx: 'scrollUp',
		cleartype: true,
		cleartypeNoBg: true
	});
	
	/**
	 * Coverage Fancybox
	 */
	$('#home-slideshow .links .last a').fancybox({
		width: 800,
		height: 600,
		autoscale: false,
		transitionIn: 'none',
		transitionOut: 'none',
		type: 'iframe',
		centerOnScroll: true,
		margin: -1000,
		padding: 0
	});
	
	/**
	 * Coverage map highlights
	 */
	var a = null;
	var b = null;
	$('*[data-partner]').hover(function() {
		a = $(this);
		b = $(a.attr('data-partner'));
		a.addClass(ac);
		b.addClass(ac);		
	}, function () {
		a.removeClass(ac);
		b.removeClass(ac);
	});
	
	/**
	 * Contact form Fancybox
	 */
	$('#offices .email-us a').fancybox({
		width: 490,
		height: 600,
		autoscale: false,
		transitionIn: 'none',
		transitionOut: 'none',
		type: 'iframe',
		centerOnScroll: true,
		margin: -1000,
		padding: 0
	});
	
	$('#contact .close').click(function(e) {
		parent.$.fancybox.close();
		e.preventDefault();
	});
	
	$('#offices .map a').fancybox({
		width: 600,
		height: 490,
		autoscale: false,
		transitionIn: 'none',
		transitionOut: 'none',
		type: 'iframe',
		centerOnScroll: true,
		margin: -1000,
		padding: 0
	});
	
	$('#contact .close').click(function(e) {
		parent.$.fancybox.close();
		e.preventDefault();
	});
	
	/**
	 * Contact page accordion
	 
	$('#offices').accordion({
		header: '.heading',
		collapsible: true,
		active: false
	}).find('.heading').css({cursor: 'pointer'});
	
	*/
	
	// This code opens the section that is targeted in the URL fragment
	$(window.location.hash).find('.heading').click();
});
