
jQuery(document).ready(function(){


	/*
	  * FORM ***
	*/
	
	
	$('.required').each(function(){
		$(this).blur(function(){
			if($(this).val() == ''){
				if(!$(this).prev().hasClass('error')){
					$(this).before('<div class="error">This field cannot be left blank</div>');
				}
			}else{
				if($(this).prev().hasClass('error')){
					$(this).prev().remove();
				}
			}
		})
	})
	
	$('input.email').each(function(){
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		$(this).blur(function(){
			if($(this).val() == ''){
				if(!$(this).prev().hasClass('error')){
					$(this).before('<div class="error">This field cannot be left blank</div>');
				}	
			}else{
				if(!emailReg.test($(this).val())){
					if($(this).prev().hasClass('error')){
						$(this).prev().remove();
					}
					$(this).after('<div class="error">You must enter a valid email address</div>');
				}else{
					if($(this).prev().hasClass('error')){
						$(this).prevx().remove();
					}
				}
			}
		})
	})
	
	$(".validate").bind("submit", function() {
		
		//add loading image
		$(this).before("<p id='loading'><img src='/images/loading.gif' /> Sending....</p>")
		
		form = '#' + $(this).attr('id');
		var valid = true;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		$(form + ' .error').remove();
		$(form + ' .required').each(function(){
			if($(this).val() == ''){
				valid = false;
				$(this).before('<div class="error">This field cannot be left blank</div>');
			}
		})
        
		$(form + ' input.email').each(function(){
			if($(this).val() == ''){
				valid = false;
				$(this).after('<div class="error">This field cannot be left blank</div>');
			}else{
				if(!emailReg.test($(this).val())){
					valid = false;
					$(this).before('<div class="error">You must enter a valid email address</div>');
				}
			}
		})
        
		if(valid){
			$('#flash-message-container').remove();
			var dataString = $(this).serialize();
			//alert (dataString);return false;
			$.ajax({
				type: "POST",
				url: $(this).attr('action'),
				data: dataString,
				success: function(msg) {
						$(form).before("<div id='flash-message-container' class='success'>Thank you, we have received your enquiry and we will contact you shortly.</div>")
						.fadeIn('slow');
						$('#loading').remove();
						}
					});
			return false;
		}else{
			$('#loading').remove();
			$(form).before("<div id='flash-message-container' class='error'>Sorry there was a problem with your form, Please review the errors and resubmit</div>");
			return false;
		}
		return false;
	});
	/*
	  * Top links ***
	*/
	// setup popup balloons (add contact / add task)
    $('.has-popupballoon').click(function(){
        // close all open popup balloons
        $('.popupballoon').fadeOut();
        $(this).next().fadeIn();
        return false;
    });

    $('.popupballoon .close').click(function(){
        $(this).parents('.popupballoon').fadeOut();
    });

	/*
	  * SLIDES ***
	*/

	if (jQuery("#featured img").length > 1){
		jQuery("#featured_button_prev, #featured_button_next").css({display:"block"});
		jQuery("#featured").cycle({
			fx: 'fade',
    		next: '#featured_button_next', 
    		prev: '#featured_button_prev',
    		timeout: 5000
    	});
    	jQuery("#featured").css({zIndex:0});
    	jQuery("#featured_button_prev").css({zIndex:1});
		jQuery("#featured_button_next").css({zIndex:2});
	}
	
	jQuery("#slide").cycle({
		fx: 'fade',
    	next: '#slide_button_next', 
    	prev: '#slide_button_prev',
    	timeout: 9000
    });
    
   	jQuery("#gallery").css({zIndex:0});
   	if (jQuery("#gallery img").length > 1){
    	jQuery("#gallery").cycle({
			fx: 'fade',
   			timeout: 5000
    	});
    }

	
	jQuery("#tweets").getTwitter({
		userName: "iconbarcelona",
		numTweets: 5,
		loaderText: "Loading tweets...",
		showTimestamp: true
	});
	
});
