
// Pseudo selectors for IE
$('#nav li:nth-child(5)').addClass('last');
$('div.column:nth-child(4)').addClass('lastcol');
$('#footer li:nth-child(3)').addClass('last');


var tabs = $('#tabs').tabs({
	fx: { width: 'toggle'}
});

if(tabs.length){ 
	tabCycle(tabs);
}

$('#tabs-2 .overlays').cycle({
	speed : 4000
});


// Show/Hide the tooltips on the expertise page

$('.chart div').hover(function() {
	$(this).children('.tooltip').toggle();
});

// IE 7 has a nasty z-index bug, this assigns classes to the parent element
// and the parents siblings to allow the expertise page to display properly

$('.ie7 .chart div').hover(function () {
	$(this).children('.tooltip').toggleClass('hovered');
	$(this).toggleClass('parent');
	$(this).siblings().toggleClass('sibs');
});


// ================
// = Contact Page =
// ================

if($('#content').hasClass('contact')){contact_init();}

function contact_init () {
	
	// ==============
	// = Google Map =
	// ==============
	
	//Map
	var centerPoint = new google.maps.LatLng(51.509397,-0.148702);
	var gmap = new google.maps.Map(document.getElementById('gmap'), {
		zoom: 15,
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		center: centerPoint
	});

	var contentString = 
		'<h3>Carbon</h3>'+
		'<p>29 Farm Street<br />'+
		'London<br />'+
		'W1J 5RL<br />'+
		'United Kingdom</p>'+
		'<p>Tel: +44 (0) 20 3178 2500<br />'+
		'Email: <a class="mail" href="mailto:enquiries@carbonsearch.com">enquiries@carbonsearch.com</a></p>'+
		'<p><a href="http://maps.google.co.uk/maps?f=d&source=s_d&saddr=&daddr=29+Farm+St,+Westminster,+London+W1J+5RL,+UK&hl=en&geocode=CXFXGhwCQsTRFY_4EQMdHrv9_ynjUhecLgV2SDH3-aqVoQMmwA&mra=ls&sll=51.509391,-0.148706&sspn=0.011031,0.017917&g=29+Farm+Street+London+W1J+5RL&ie=UTF8&ll=51.509384,-0.148702&spn=0.022062,0.035834&z=15" target="_blank">Directions to...</a><br />'+
		'<a href="http://maps.google.co.uk/maps?f=d&source=s_d&saddr=29+Farm+St,+Westminster,+London+W1J+5RL,+UK&daddr=&hl=en&geocode=FY_4EQMdHrv9_ynjUhecLgV2SDH3-aqVoQMmwA&mra=prev&sll=51.509391,-0.148706&sspn=0.011031,0.017917&ie=UTF8&ll=51.509384,-0.148702&spn=0.022062,0.035834&z=15" target="_blank">Directions from...</a></p>';
	
	var infowindow = new google.maps.InfoWindow({
	    content: contentString
	});
	
	//Marker
	var marker = new google.maps.Marker({
		map: gmap,
		position: centerPoint
	});
	
	google.maps.event.addListener(marker, 'click', function() {
	  infowindow.open(gmap,marker);
	});
}



// =============
// = Tab cycle =
// =============
function tabCycle (tabNode) {
	
	var cycle = true;
	var current = 0;
	var next = 0;
	var timeoutHandler = null
	
	timeoutHandler = setTimeout(function(){
		
		if(current == 2){
			next = 0;
		}else{
			next = current+1;
		}
		
		tabNode.tabs('select', next);
		
		current = next;
		timeoutHandler = setTimeout(arguments.callee, customDelay())
		
	}, customDelay());
	
	
	
	// Returns the correct delay depending on what tab is shown
	function customDelay () {
		
		//Fist Tab
		if(current==0){
			return 12000;
		}
		
		//Second Tab
		if(current==1){
			return 18000
		}
		
		//Third Tab
		if(current==2){
			return 10000;
		}
	}
	
	
	//Disable the cycle if a tab is clicked
	$("ul.ui-tabs-nav").find("a").click(function(event){
		clearTimeout(timeoutHandler);
	})
}

$(document).ready(function(){

	$('.slider .preload').css('display','block');

});
