var validate_email_input = function(text){
	var filter = /[a-zA-Z0-9._-]+@[a-zA-Z0-9]+.[a-z]+(.[a-z]+)?/;
	
	if (!filter.test(text)) {
		return false;
	}
	return true;
}


jQuery(document).ready(function() {
	

	jQuery.get('http://www.robocatapps.com/blog/',function(data){
		var h2 = jQuery(data).find('.first h2');
		var text = h2.text();
		var link = h2.find('a').attr('href');
		jQuery('.blogpost_heading')
		    .attr('href',link)
		    .find('span')
		    .text(text)
		    .parent()
		    .fadeIn(400,function(){
				jQuery('small.latest').fadeIn(200);
			});
	});
	
	jQuery('#vimeo').fancyZoom( {closeOnClick: false});
	
	/* Preperations*/
	jQuery('.bubble').css({ 'opacity' : 0});
	
	/* Animations */
	var catCounter = 0;
	var totalCats = jQuery('#logo a img').size();
	var animateNextCat = function()
	{
		if (catCounter == -1){
			var currentcat = jQuery('#logo a img').eq(totalCats-1);
			var nextcat = jQuery('#logo a img').eq(0);
		} else {
			var currentcat = jQuery('#logo a img').eq(catCounter);
			var nextcat = jQuery('#logo a img').eq(catCounter+1);
		}
				
		nextcat.fadeIn(500, function(){
				currentcat.fadeOut(500);
		});
		catCounter++;
		if (catCounter == totalCats-1) {catCounter = -1}
		
		
	}
	setInterval( function(){ animateNextCat() }, 1200)
	
	
	
	/* Event handlers*/
	
	// the form

	jQuery('#mce-FNAME').bind('blur', function(){
		jQuery(this).removeClass('error');
		var text = jQuery(this).val();
		if (text == "") {
			jQuery(this).val('Name');
		}
	});
	jQuery('#mce-EMAIL').bind('blur', function(){
		jQuery(this).removeClass('error');
		var text = jQuery(this).val();
		if (text == "") {
			jQuery(this).val('Email');
		}
	});
	jQuery('#mce-EMAIL').bind('focus', function(){
		var text = jQuery(this).val();
		if (text == "Email") {
			jQuery(this).val('');
		}
	});
	jQuery('#mce-FNAME').bind('focus', function(){
		var text = jQuery(this).val();
		if (text == "Name") {
			jQuery(this).val('');
		}
	});
	
	jQuery('#mc-embedded-subscribe').bind('click', function(){

		var email_value = jQuery('#mce-EMAIL').val();
		var name_value = jQuery('#mce-FNAME').val();
		var returnVal = true;
		
		if (name_value == "Name" || name_value.length < 1)
		{
			var fields = jQuery('#mce-FNAME');
			jQuery('#mce-FNAME').addClass('error');
			fields.animate({
				'margin-left' : -10
			},100).animate({
				'margin-left' : 10
			},100).animate({
				'margin-left' : 0
			},100);
			returnVal = false;
		}
		if ( !validate_email_input(email_value) ) 
		{
			var emailField = jQuery('#mce-EMAIL');
			var fields = jQuery('#mc-embedded-subscribe, #mce-EMAIL');
			emailField.addClass('error');
			fields.animate({
				'margin-left' : -10
			}, 100).animate({
				'margin-left' : 10
			}, 100).animate({
				'margin-left' : 0
			}, 100);
			returnVal = false;
		}
		
		// all wen't great, lets submit to mailchimp
		if (returnVal) {
			var url = "http://www.robocatapps.com/add_to_mailchimp.php?" + "email=" + email_value +"&name=" + name_value;   
			jQuery('#mechanical_mouse').css({'margin-top' : '10px'});
			jQuery('#form').slideUp(500, function() {
				jQuery('#signup').text("Thank you! we'll keep you posted");
			});
			jQuery.post(url);
		}
		return false;
	});
	
	// the menu
	jQuery('.item').hover(
		function(){
			jQuery(this).find('.bubble').animate( { 'opacity' : 1}, 300);
		},
		function(){
			jQuery(this).find('.bubble').animate( { 'opacity' : 0}, 300);
		});
		
		jQuery('.item').bind('click', function(){
			var div = jQuery(this).attr('href');
			var offset = jQuery(div).offset().top;
			jQuery('html,body').animate({scrollTop: offset}, 500);
			return false
		});
		
});



