// JavaScript Document
function disableForm() {
	$('input#nome').attr("disabled", "disabled");
	$('input#email').attr("disabled", "disabled");
	$('input#testo').attr("disabled", "disabled");
	$('input#preventivo').attr("disabled", "disabled");
	$('input#idea').attr("disabled", "disabled");
	$('input#problemi').attr("disabled", "disabled");	
}

function enableForm() {
	$('input#nome').attr("disabled", "false");
	$('input#email').attr("disabled", "false");
	$('input#testo').attr("disabled", "false");
	$('input#preventivo').attr("disabled", "false");
	$('input#idea').attr("disabled", "false");
	$('input#problemi').attr("disabled", "false");	
}

$(document).ready(function(){
	

	$('#alien_imagelink').css("background","url('wp-content/themes/isayblog_theme/images/icons/alien_sprite.png')  -52px 0px no-repeat");
	
	
	$('.refresh_form').click(function() {
		$('div#failure_box').hide();
		$('div#success_box').hide();
		$('div#sub_form_box').fadeIn('slow');
		return false;
	});
	
	$('#alien_imagelink').click(function() {
		$(this).css("background-position","-52px 0px");
		$('#car_imagelink').css("background-position","0px 0px");
		$('#spaceship_imagelink').css("background-position","0px 0px");
		
		$('h4#raggiungere_textheader_auto').hide();
		$('p#raggiungere_text_auto').hide();
		
		$('h4#raggiungere_textheader_alien').hide();
		$('p#raggiungere_text_alien').hide();
		
		$('h4#raggiungere_textheader_piedi').fadeIn('fast');
		$('p#raggiungere_text_piedi').fadeIn('fast');
		
		$('div#alien_map').hide();
		return false;
	});
	
	$('#alien_imagelink').mouseover(function() {
		$(this).css("background-position","-52px 0px");
		$('#car_imagelink').css("background-position","0px 0px");
		$('#spaceship_imagelink').css("background-position","0px 0px");
		
	});

	$('#car_imagelink').click(function() {
		
		$(this).css("background-position","-116px 0px");
		$('#alien_imagelink').css("background-position","0px 0px");
		$('#spaceship_imagelink').css("background-position","0px 0px");
		
		$('h4#raggiungere_textheader_alien').hide();
		$('p#raggiungere_text_alien').hide();
		
		$('h4#raggiungere_textheader_piedi').hide();
		$('p#raggiungere_text_piedi').hide();
		
		$('h4#raggiungere_textheader_auto').fadeIn('fast');
		$('p#raggiungere_text_auto').fadeIn('fast');
		
		$('div#alien_map').hide();
		return false;
	});
	
	$('#car_imagelink').mouseover(function() {
		
		$(this).css("background-position","-116px 0px");
		$('#alien_imagelink').css("background-position","0px 0px");
		$('#spaceship_imagelink').css("background-position","0px 0px");
	});
	
	$('#spaceship_imagelink').click(function() {
		
		$(this).css("background-position","-73px 0px");
		$('#alien_imagelink').css("background-position","0px 0px");
		$('#car_imagelink').css("background-position","0px 0px");
		
		$('h4#raggiungere_textheader_piedi').hide();
		$('p#raggiungere_text_piedi').hide();
		
		$('h4#raggiungere_textheader_auto').hide();
		$('p#raggiungere_text_auto').hide();
		
		$('h4#raggiungere_textheader_alien').fadeIn('fast');
		$('p#raggiungere_text_alien').fadeIn('fast');
		$('div#alien_map').show();
		return false;
	});
	
	$('#spaceship_imagelink').mouseover(function() {
		
		$(this).css("background-position","-73px 0px");
		$('#alien_imagelink').css("background-position","0px 0px");
		$('#car_imagelink').css("background-position","0px 0px");
	});

	
	$('a#postrecenti_link').click(function() {
		$('#rec_comments').hide();
		$('#tagcloud').hide();
		
		$('ul#newsmix_header').css('background-position', '0px 0px');
		$(this).css('color', '#FFFFFF');
		$('a#commenti_recenti_link').css('color', '#999999');
		$('a#tagcloud_link').css('color', '#999999');
		
		$('#recent_posts').show();
		return false;
	});
	
	$('a#commenti_recenti_link').click(function() {
		$('#tagcloud').hide();
		$('#recent_posts').hide();
		
		$('ul#newsmix_header').css('background-position', '0px -44px');
		$(this).css('color', '#FFFFFF');
		$('a#postrecenti_link').css('color', '#999999');
		$('a#tagcloud_link').css('color', '#999999');
		
		$('#rec_comments').show();
		return false;
	});
	
	$('a#tagcloud_link').click(function() {
		$('#recent_posts').hide();
		$('#rec_comments').hide();
		
		$('ul#newsmix_header').css('background-position', '0px -88px');
		$(this).css('color', '#FFFFFF');
		$('a#postrecenti_link').css('color', '#999999');
		$('a#commenti_recenti_link').css('color', '#999999');
		
		$('#tagcloud').show();
		return false;
	});
	
	
	$('form#contatti_form input[type=text], form#contatti_form textarea').focus(function(){ 
		if($(this).val() == $(this).attr('defaultValue'))
		{
		  $(this).val('');
		}
	});
  
	$('form#contatti_form input[type=text, form#contatti_form textarea]').blur(function(){
		if($(this).val() == '')
		{
		  $(this).val($(this).attr('defaultValue'));
		} 
	});
	
	
	
	// Contact Form Handler.
	$('form#contatti_form').submit(function() {
		
		// Regex Rules.
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		// Alert on empty fields.
		if( ($('#nome').val() == "") || ($('#email').val() == "") || ($('#testo').val() == "")) {
			alert("Attenzione! Devi compilare tutti i campi del form.");
			return false;
		} else if(!emailReg.test($('#email').val())){ alert('Attenzione! Indirizzo email non valido.'); } else {
			
			if(!jcap()) {} else {
			// Display Success Message.
			$('div#sub_form_box').fadeOut('slow', function() {
				// Animation complete.
				
				// Display Ajax Loader
				$('div#loading_box').fadeIn('slow', function() {
				// Animation complete.
					// Ajax call to mail php mail class.
					$.ajax({
						type: "POST",
						url: '../includes/php/mail.php',
						data: $('form#contatti_form').serialize(),
						dataType: "html",
						success: function(data) {
							if (data == 1) {
								$('div#loading_box').fadeOut('slow', function() {
									$('div#success_box').fadeIn('slow');
								});
							}
							else
							{
								$('div#loading_box').fadeOut('slow', function() {
									$('div#failure_box').fadeIn('slow');							  
								});
							}
						}
					});
				});
			});
		}}
		
		// Prevent reload.
		return false;
	});
});
