nubedo = function() {
	var 
		maxLoop = 5,
		loop = 0;
	return {
		init : function() {
			if ($('#heading h3'))
				this.animateHeading($('#heading h3:first'));
		
			if ($("#quickcontact").length)
				this.vForm('#quickcontact');
				
			if ($("#contact").length)
				this.vForm('#contact');
				
			if ($("#evaluation").length)
				this.vForm('#evaluation');
				
			if ($("#rfp").length)
				this.vForm('#rfp');
			
			if ($(".overlay").length) {
				$("li[rel]").overlay({
					close: ".close, .overlay img"	
				});
			}
		},
		animateHeading : function(el) {
			el.css('right','-150px' );
			el.css('opacity','0.0' );
			
			el.animate({
				right: '10',
				opacity: '1'
			}, 1500, function() {
				if (loop < maxLoop) {
					$(this).animate({
						right: '160',
						opacity: '0'
					}, 1500, function() {
					
						$(this).removeClass('active');
						
						if ($(this).next('#heading h3').length) {
							var next = $(this).next();
						}else{
							var next = $('#heading h3:first');
							loop+=1;
						}
						nubedo.animateHeading(next);
					
					});
				}
			});
		},
		vForm : function(theID) {
		
			$(theID).validator().submit(function(e) {
				var form = $(this);
				var page = this.action
				$.getJSON(page +'?'+ form.serialize(), function(json) {
				
					// everything is ok. (server returned true)
					if (json.success === true)  {
					
						form.html(json.thankyou);
					
					// server-side validation failed. use invalidate() to show errors
					} else {
					
						form.data("validator").invalidate(json);
					}
				});
				
				e.preventDefault();
			
			});
			
		}
	};
}();

$(document).ready(function() { nubedo.init(); });