$(document).ready(
	function(){
		$.colorbox.init();
		$('.gallery-item a').colorbox();
		$('.calendar a[href*=#]').addClass('empty');
		$('#BlackBoard li:first').addClass('first');
		$('#rotator ul li:first').addClass('show');
		
		
	$(function() {
		$( ".slider" ).accordion({
			collapsible: true,
			active: 2 
		});
	});
					
							
$('.clearSearch').each(function() {
    var default_value = this.value;
    $(this).focus(function() {
        if(this.value == default_value) {
            this.value = '';
            $(this).css({'color':'#000', 'font-style':'normal'});
        }
    });
    $(this).blur(function() {
        if(this.value == '') {
            $(this).css({'color':'#666', 'font-style':'italic'});
            this.value = default_value;
        }
    });
});

});




function theRotator() {
	//Set the opacity of all images to 0
	$('#rotator ul li').css({opacity: 0.0});
	
	//Get the first image and display it (gets set to full opacity)
	$('#rotator ul li:first').css({opacity: 1.0});
		
	//Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
	
	setInterval('rotate()',6000);
	
}

function rotate() {	
	//Get the first image
	var current = ($('#rotator ul li.show')?  $('#rotator ul li.show') : $('#rotator ul li:first'));

    if ( current.length == 0 ) current = $('#rotator ul li:first');

	//Get next image, when it reaches the end, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('#rotator ul li:first') :current.next()) : $('#rotator ul li:first'));
	
	//Un-comment the 3 lines below to get the images in random order
	
	//var sibs = current.siblings();
        //var rndNum = Math.floor(Math.random() * sibs.length );
        //var next = $( sibs[ rndNum ] );
			

	//Set the fade in effect for the next image, the show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 1000);

	//Hide the current image
	current.animate({opacity: 0.0}, 1000)
	.removeClass('show');
	
};



$(document).ready(function() {		
	//Load the slideshow
	theRotator();
	$('#rotator').fadeIn(1000);
    $('#rotator ul li').fadeIn(1000); // tweek for IE

	$("#email-signup").submit(function(){
		var inputs = $("#email-signup input");
		var error = false;
		
		$.each(inputs, function(index, value) {
			if ($(value).val() == "") {
				error = true;
			}
		})

		if (error == true) {
			alert("Please fill out all of the form fields");
			return false;
		}

		var emailReg = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/;
		if(!emailReg.test($("#email-email-signup").val())) {
			alert("Incorrect email – please check and resubmit");
			return false;			
			// error = true;
		}

		$.post("form_submit.php", $("#email-signup").serialize(), function(data) {
		})

		var url = "http://listerarms.co.uk/newsletter/";
		url += "?email=" + $("#email-email-signup").val() + "&firstname=" + $("#firstname-email-signup").val() + "&lastname=" + $("#lastname-email-signup").val() + "&hide-form=true";

		window.location = url;

		return false;
	});

	$("#contact-form").submit(function(){
		var inputs = $("#contact-form input[type=text].wpcf7-validates-as-required");
		var error = false;
		
		$.each(inputs, function(index, value) {
			if ($(value).val() == "") {
				error = true;
			}
		})

		if (error == true) {
			alert("Please fill out all of the form fields");
			return false;
		}

		$.post("form_submit.php", $("#contact-form").serialize(), function(data) {
		})	

		window.location = "http://listerarms.co.uk/thanks/";		

		return false;
	});		        
});

