//$(document).ready(function(){

	var calendarOffset = 0;
	
	$('.tooltip').tipsy({gravity: 's', fade: 'true', html: 'true', opacity: 1});
	
	$('#q1').addClass('active');
	
	$('.calendar-right a').click(function(){
		calendarOffset++;
		$('.calendar-loading').show();
		$('.calendar-wrap').load("http://www.kenteventcentre.co.uk/wp-content/themes/kec/inc/calendar.php?offset="+calendarOffset, function(){
			$('.tooltip').tipsy({gravity: 's', fade: 'true', html: 'true', opacity: 1});
			$('.calendar-loading').fadeOut('fast');
		});
		return false;
	});
	
	$('.calendar-left a').click(function(){
		calendarOffset--;
		$('.calendar-loading').show();
		$('.calendar-wrap').load("http://www.kenteventcentre.co.uk/wp-content/themes/kec/inc/calendar.php?offset="+calendarOffset, function(){
			$('.tooltip').tipsy({gravity: 's', fade: 'true', html: 'true', opacity: 1});
			$('.calendar-loading').fadeOut('fast');
		});
		return false;
	});
	
	for(a=2; a<100; a++){
		
		if($('#a'+a).length!=0){
			$('#a'+a).hide();
		} else {
			break;
		}
		
	}
	
	for(b=2; b<100; b++){
		
		if($('.carousel li.'+b).length!=0){
			$('.carousel li.'+b).hide();
		} else {
			break;
		}
		
	}
	
	var carouselItems = 0;
	var carouselCurrent = 1;
	
	$('.carousel li').each(function(){
		carouselItems++;
	});
	
	var carouselButtonWidth = 929/carouselItems;
	
	for(c=1; c<=carouselItems; c++){
		$('.carousel .controls').append('<div class="'+c+'" style="width:'+carouselButtonWidth+'px;height:8px;float:left" onClick="showCarousel('+c+')"></div>');
	}
	
	$('.carousel .controls .1').addClass('active');
	
	var run = setInterval('carouselNext()', 6000);
	
	$('.carousel').hover(function() {
		clearInterval(run);
	}, function() {
		run = setInterval('carouselNext()', 6000);
	}
);

	$('#detling').click(function() {
		$('.lightbox').fadeIn();
		$('.lightboxcont').append('<iframe src="https://www.google.com/calendar/embed?showTitle=0&amp;showNav=0&amp;showPrint=0&amp;mode=WEEK&amp;height=600&amp;wkst=1&amp;bgcolor=%23FFFFFF&amp;src=vaa39r4o89rk1p8po6qmihio74%40group.calendar.google.com&amp;color=%23691426&amp;ctz=Europe%2FLondon" style=" border-width:0 " width="800" height="600" frameborder="0" scrolling="no"></iframe>');
	});
	$('#thurnham').click(function() {
		$('.lightbox').fadeIn();
		$('.lightboxcont').append('<iframe src="https://www.google.com/calendar/embed?showTitle=0&amp;showNav=0&amp;showPrint=0&amp;mode=WEEK&amp;height=600&amp;wkst=1&amp;bgcolor=%23ffffff&amp;src=h99rhhd6p3ip07s3h51k7ag2l8%40group.calendar.google.com&amp;color=%23856508&amp;ctz=Europe%2FLondon" style=" border-width:0 " width="800" height="600" frameborder="0" scrolling="no"></iframe>');
	});
	$('#astor').click(function() {
		$('.lightbox').fadeIn();
		$('.lightboxcont').append('<iframe src="https://www.google.com/calendar/embed?showTitle=0&amp;showNav=0&amp;showPrint=0&amp;mode=WEEK&amp;height=600&amp;wkst=1&amp;bgcolor=%23ffffff&amp;src=vdn1jran61s2sibe2meiirkg9c%40group.calendar.google.com&amp;color=%23125A12&amp;ctz=Europe%2FLondon" style=" border-width:0 " width="800" height="600" frameborder="0" scrolling="no"></iframe>');
	});
	$('#stockbury').click(function() {
		$('.lightbox').fadeIn();
		$('.lightboxcont').append('<iframe src="https://www.google.com/calendar/embed?showTitle=0&amp;showNav=0&amp;showPrint=0&amp;mode=WEEK&amp;height=600&amp;wkst=1&amp;bgcolor=%23ffffff&amp;src=l81gtrl7kbc0e2t71p16aile34%40group.calendar.google.com&amp;color=%238C500B&amp;ctz=Europe%2FLondon" style=" border-width:0 " width="800" height="600" frameborder="0" scrolling="no"></iframe>');
	});
	$('#boxley').click(function() {
		$('.lightbox').fadeIn();
		$('.lightboxcont').append('<iframe src="https://www.google.com/calendar/embed?showTitle=0&amp;showNav=0&amp;showPrint=0&amp;mode=WEEK&amp;height=600&amp;wkst=1&amp;bgcolor=%23ffffff&amp;src=3tofen9mu5qmsgcfukrifla928%40group.calendar.google.com&amp;color=%2342104A&amp;ctz=Europe%2FLondon" style=" border-width:0 " width="800" height="600" frameborder="0" scrolling="no"></iframe>');
	});
	$('#bredhurst').click(function() {
		$('.lightbox').fadeIn();
		$('.lightboxcont').append('<iframe src="https://www.google.com/calendar/embed?showTitle=0&amp;showNav=0&amp;showPrint=0&amp;mode=WEEK&amp;height=600&amp;wkst=1&amp;bgcolor=%23ffffff&amp;src=u7l6s0v3cp8vbeht3qecjmps60%40group.calendar.google.com&amp;color=%236B3304&amp;ctz=Europe%2FLondon" style=" border-width:0 " width="800" height="600" frameborder="0" scrolling="no"></iframe>');
	});
	$('#bearsted').click(function() {
		$('.lightbox').fadeIn();
		$('.lightboxcont').append('<iframe src="https://www.google.com/calendar/embed?showTitle=0&amp;showNav=0&amp;showPrint=0&amp;mode=WEEK&amp;height=600&amp;wkst=1&amp;bgcolor=%23ffffff&amp;src=s1o950gf992quvgfp1trb025cc%40group.calendar.google.com&amp;color=%232F6309&amp;ctz=Europe%2FLondon" style=" border-width:0 " width="800" height="600" frameborder="0" scrolling="no"></iframe>');
	});
	
	$(document).ready(function() {
		$('.lbclose').click(function() {
			$('.lightbox').fadeOut();
		});
		
		$('ul.gallery li a').click(function() {
			$('div.gallery-current').find('img').attr("src", $(this).attr("href"));
			$(this).addClass('current');
			return false;
		});
		
		$('div.scroll-up').click(function() {
			if($(this).parent().find('div.slide ul').css('margin-top')!='0px' && !($(this).parent().find('ul').is(':animated')))
			{
				$(this).parent().find('ul').animate({
					marginTop: '+=400px'
				});
			}
		});
		
		//$('ul.gallery li a:eq(0)').click();
		
		var ulHeight = parseInt($('div.slide ul').height());
		var slideHeight = parseInt($('div.slide').height());
		var slideAmount = Math.ceil(ulHeight/slideHeight);
		var slideCalc = (slideHeight*(slideAmount-2))-slideHeight;
		
		$('div.scroll-down').click(function() {
			//alert(slideCalc);
			if(parseInt($(this).parent().find('div.slide ul').css('margin-top').replace('px',''))>=slideCalc)
			{
				$(this).parent().find('ul').animate({
					marginTop: '-=400px'
				});
			}
		});
		
		$('ul.gallery li:nth-child(3n+1)').css('margin-left', '0px');
		
		$('form.validate').validate();
	});
	
//});

function showInfo(i) {

	$('.info').slideUp('fast');
	
	if($('.info'+i).is(':visible')){} else { $('.info'+i).slideDown('fast'); }
	
}

function goToByScroll(id){
	$('html,body').animate({scrollTop: $("#"+id).offset().top},'slow');
}

function showFaq(i){
	
	$('.question a').removeClass('active');
	
	$('#q'+i).addClass('active');
	
	for(a=1; a<100; a++){
		
		if($('#a'+a).length!=0){
			$('#a'+a).fadeOut('fast');
		} else {
			break;
		}
	}
	
	$('#a'+i).delay(200).fadeIn('fast');
	
}

function showCarousel(i){
	
	$('.carousel .controls div').removeClass('active');
	$('.carousel .items li').fadeOut('slow');
	$('.carousel .controls .'+i).addClass('active');
	$('.carousel .items li.'+i).fadeIn('slow');
	carouselCurrent = i;
	
}

function carouselNext(){
	
	if(carouselCurrent<carouselItems){
		carouselCurrent++;
	} else {
		carouselCurrent = 1;
	}
	
	$('.carousel .controls .'+carouselCurrent).click();
	
}





/***** Slider *****/

var slideCount = 0;
var slideWidth;
var slideHeight;
var slideCountNum;
var timeout;

//Change the below values to suit you

var slideContainer = 'div.testimonial-slider';
var slideInterval = 7000;
var speed = 500;
var easing = 'easeInOutCubic';
var controlsContainer = 'div.controls';
var prevButton = 'div.prev';
var nextButton = 'div.next';

$(function() {

	var slider = $(slideContainer);
	
	//Gets width and height of slider div
	
	slideWidth = 401;
	
	//Counts children and positions them
	
	slider.find('div.text p').each(function() {
		slideCount++;
	});
	
	if(slideCount>2)
	{
		slideCount = 0;
		slider.find('div.text p').each(function() {
			slideCount++;
			$(this).css('left', slideWidth*(slideCount-2));
		});
	}
	else if(slideCount<3)
	{
		slideCount = 0;
		slider.find('div.text p').each(function() {
			slideCount++;
			$(this).css('left', slideWidth*(slideCount-1));
		});
	}
	
	slideCountNum = slideCount-slideCount+1;
	
	//Show controls if there's more than one slide
	
	if(slideCount>1)
		slider.find('div.prev, div.next').show();
		
	//Set the variable timeout to start the sliding
	
	timeout = setTimeout('slideGallery()', slideInterval);
	
	//Clear timeout on hover on slider div
	
	slider.hover(function() {
		clearTimeout(timeout);
	}, function() {
		timeout = setTimeout('slideGallery()', slideInterval);
	});
	
	//Call the previous/next functions on click of controls
	
	slider.find(prevButton).click(function() {
		slideGalleryPrev();
	});
	
	slider.find(nextButton).click(function() {
		slideGalleryNext();
	});
	
});

//Automatic sliding

function slideGallery() {

	var slider = $(slideContainer);
	
	if(slideCount>1)
	{
		slider.find('div.text p').each(function() {
			$(this).animate({ left: '-='+slideWidth }, speed, easing, function() {
				if($(this).css('left')==-slideWidth*(slideCount-1)+'px')
					$(this).css('left', slideWidth*(slideCountNum)+'px');
			});
		});
	}
	
	timeout = setTimeout('slideGallery()', slideInterval);
	
}

//Previous button function

function slideGalleryPrev() {

	var slider = $(slideContainer);
	
	if(slideCount>1)
	{
		slider.find('div.text p').each(function() {
			if(!$(this).is(':animated'))
			{
				$(this).animate({ left: '+='+slideWidth }, speed, easing, function() {
					if($(this).css('left')==slideWidth*(slideCount-1)+'px')
						$(this).css('left', -slideWidth*(slideCountNum)+'px');
				});
			}
		});
	}
	
}

//Next button function

function slideGalleryNext() {

	var slider = $(slideContainer);
	
	if(slideCount>1)
	{
		slider.find('div.text p').each(function() {
			if(!$(this).is(':animated'))
			{
				$(this).animate({ left: '-='+slideWidth }, speed, easing, function() {
					if($(this).css('left')==-slideWidth*(slideCount-1)+'px')
						$(this).css('left', slideWidth*(slideCountNum)+'px');
				});
			}
		});
	}
	
}





/***** Pull Twitter @mentions *****/

var tweetCount = 0;
var templateURL = 'http://kenteventcentre.co.uk/wp-content/themes/kec';

$(function() {
	var username = 'Kenteventcentre';
	$.getJSON('http://search.twitter.com/search.json?q=%40'+username+'&callback=?', function(response) {
		$(response.results).each(function(i, result) {
			tweetCount++;
			if(tweetCount<4)
			{
				var html = '<div class="grid_4 tweet">'+
					'<div class="padding">'+
					'<a href="http://twitter.com/'+result.from_user+'" target="_blank"><b>'+result.from_user+'</b>:</a> '+
					result.text+' '+
					'<a href="http://twitter.com/'+result.from_user+'/status/'+result.id_str+'" class="posted-on" target="_blank">'+result.from_user+' <span class="timeago" title="'+result.created_at+'">'+result.created_at+'</span></a>'+
					'</div><img src="'+templateURL+'/timthumb.php?src=http://img.tweetimag.es/i/'+result.from_user+'&amp;w=18&amp;h=18&amp;zc=1" alt="" /></div>';
				$('div.twitter-feed').append(html);
				$('span.timeago').timeago();
			}
		});
	});
});





/***** Downloads *****/

$(function() {

	$('div.downloads div.row div.download:last').css('margin-right', 0);

});
