$(function() {
	fbsites = new FBSites();
	fbsites.init();
	
/*
	$('#features').jCarouselLite({
		btnNext: '#next_feature',
		btnPrev: '#prev_feature',
		auto: 10000,
		speed: 400,
		circular: false
	});
*/
	
	$('#contact_us_form').submit(function() {
		$.post('/send_contact_form.php', $('#contact_us_form').serialize(), function(data) {
			thumbs = (data.result) ? 'up' : 'down';
			$('.your_next_project').addClass('thumbs_'+thumbs);
			$('#contact_us_form').fadeOut('normal', function() {
				$('.your_next_project h3').html(data.heading);
				
				if (data.result)
				{
					var google_conversion_id = 1063521543;
					var google_conversion_language = "en_GB";
					var google_conversion_format = "3";
					var google_conversion_color = "ffffff";
					var google_conversion_label = "_jHmCPeCSRCHmpD7Aw";
					var google_conversion_value = 0;
					if (1.0) {
					  google_conversion_value = 1.0;
					}
					$.getScript('http://www.googleadservices.com/pagead/conversion.js');
				}
				
			});
			if (!data.result)
			{
				setTimeout(function() {
					$('#contact_us_form').fadeIn();
					$('.your_next_project h3').html('We&rsquo;d love to hear about your next project! (in 5 mins!)');
					$('.your_next_project').removeClass('thumbs_down');
				}, 5000);
			}
			
		}, 'json');
		return false;
	});
	
	var interval = '';
	
	updateCounter();
	setInterval(updateCounter, 30000);
	
	$('#pump').click(function() {
		updateCounter();
	});
	
	function updateCounter()
	{
		$.getJSON('/counter.php', function(data) {
			// Number of digits here (7)
			diff = 7 - data.length;

			while (diff > 0)
			{
				data = '0' + data;
				diff--;
			}
			
			// add a new digit, incrementing the numbers
			setCountTo(1, data.charAt(0));
			setCountTo(2, data.charAt(1));
			setCountTo(3, data.charAt(2));
			setCountTo(4, data.charAt(3));
			setCountTo(5, data.charAt(4));
			setCountTo(6, data.charAt(5));
			setCountTo(7, data.charAt(6));
		});
	}	
	
	function setCountTo(span_id, number)
	{
		el = $('#count_'+span_id);
		current = (el.data('count') == undefined) ? 0 : el.data('count');
		diff = Math.abs(current - number - 10);
		diff = (diff > 10) ? diff - 10 : diff;
		if (diff > 0 && diff != 10)
		{
			increment(span_id, diff);
		}
		
		el.data('count', number);
	}
	
	function increment(span_id, flips)
	{
		// Get current position of CSS bg
		var current_pos = {};
		current_pos = $('#count_'+span_id).data('bg-position');
		
		if (current_pos == undefined)
		{
			current_pos = 0;
		}

		// height of the number sprite
		if (current_pos >= 813)
		{
			current_pos = 0;
		}
						
		// do flip
		interval[span_id] = setInterval(flip, 50);
		var count = 0;
		var flips_number = flips;
		function flip()
		{
			if (count == (3 * flips_number))
			{
				clearInterval(interval[span_id]);
			}
			else
			{
				// 27.33333 is based on number sprite image height / (10 * 3)
				// 3 is the number of frames per number, and 10 is the number of digits ;)
				current_pos = (current_pos*1) + 27.1;
				$('#count_'+span_id).data('bg-position', current_pos);
				$('#count_'+span_id).css('background-position', '0 -'+current_pos+'px');
				
				count++;
			}
		}				
	}

});
