/*
main.js v0.1 flipability
written by scottdarby@redweb.com
*/

$('html').addClass('js');

$(function(){

	function flipThis(el){

		if (el != false)
	
		$(el)
			.find('img')
			.animate(
				{
					width: 0
				}, 
				300,
				function(){
					$(this)
						.parent()
						.css(
							{
								zIndex: 0
							}
						)
						.siblings()
						.css(
							{
								zIndex: 1
							}
						)
						.children()
						.animate(
							{
								width: 225
							},
							300,
							function(){
								
								$last = $(this).parent();
								
							}
						)
				}
			)
			.parent()
			.siblings()
			.find('img')
			.animate(
				{
					width: 0
				},300
			)
	}

	$last = false;

	$('span').not('.top').find('img').css('width','0');
	
	$('.flip-link span.top').click(function(){
	
		if ($('img').is(':animated')){

			return false;
		
		}

		flipThis($last);
		
		flipThis(this);
		
	});
	
	$('#stay-informed').click(function(){
		$(this).fadeOut('slow', function(){
			$(this).next().fadeIn('slow');
		})
	});
	
	
    function validateform() {
        var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
        var validationsummary ='';
        if ($('#name').val() == '') {
            validationsummary += 'Please enter your name'+'\n';	        
        }
        if ($('#email').val() == '') {
            validationsummary += 'Please enter your email address'+ '\n';
        }
        else if (!emailPattern.test($('#email').val())) {
            validationsummary += 'Please enter a valid email address' + '\n';
        }
        if ($('#organisation').val() == '') {
            validationsummary += 'Please enter the name of your organisation';
        }
        if (validationsummary != '') {
            return validationsummary;
        }
    }

    $('#submit').click(function(){

        if (typeof(validateform()) == 'undefined'){
            return true;
        } else {
            alert(validateform());
            return false;
        }
    });
    
    var url = document.location.href;
    
    if (url.search(/submit=true/) != -1){
    
        $('#thanks').show();
    
    }

});

$(window).load(function(){

	$('#promote span.top').click();

})