$(document).ready(function() {

	$.preloadImages("img/ico/accept.png", "img/ico/ajax-loader.gif","img/ico/exclamation.png");

	$('#s3slider').s3Slider({
		timeOut: 3000
	});

	$('#menu').accordion({
		icons: {
			'header': 'inactive',
			'headerSelected': 'active'
		},
		'event': 'click',
		'autoHeight': false
	});

	$("input[type=text], textarea").focus(function () {
		$(this).css('background-color', '#628db3');
	});		 
	$("input[type=text], textarea").blur(function () {
		$(this).css('background-color', '');
	});

	$('#contacto').ajaxForm({
			target:        '#result',   // target element(s) to be updated with server response 
			beforeSubmit:  function(){
				var sucesso = true;
				var msg = '';
				var nomeValue = $('#nome').fieldValue(); 
				var emailValue = $('#email').fieldValue();
				var mensagemValue = $('#mensagem').fieldValue();
				var msg = '<img src="img/ico/exclamation.png" alt="" /> ';
				$('#contacto input , #contacto textarea').attr('disabled','disabled');
				if (!nomeValue[0]) {
					sucesso = false;
					msg += 'O nome &eacute; obrigat&oacute;rio.';
					focusField = $('#nome');
				}
				else if (!validate(emailValue[0])) {
					sucesso = false;
					msg += 'Coloque um e-mail v&aacute;lido.';
					focusField = $('#email');
				}
				else if (!mensagemValue[0]) {
					sucesso = false;
					msg += 'A mensagem &eacute; obrigat&oacute;ria.';
					focusField = $('#mensagem');
				}
				if (!sucesso) {
					//alert(msg);
					$('#result').fadeIn('slow').html(msg).animate({opacity: 1.0}, 1000).fadeOut('slow',function(){
						$('#contacto input , #contacto textarea').removeAttr("disabled");
						focusField.focus();
					});
					return false;
				}
				else {
					$('#result').show().html('<img src="img/ico/ajax-loader.gif" alt="" /> A submeter pedido...');
				}
			},  // pre-submit callback 
			success:       function(){
				$('#result').animate({opacity: 1.0}, 3000).fadeOut('slow',function(){
					$('#contacto input , #contacto textarea').removeAttr("disabled");
					$('#captcha').attr('src','include/securimage/securimage_show.php?'+parseInt(Math.random()*9999999))
				});
			},  // post-submit callback 

			url:       'ajax-contacto.php',         // override for form's 'action' attribute 
			//type:      type        // 'get' or 'post', override for form's 'method' attribute 
			//dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
			clearForm: true        // clear all form fields after successful submit 
			//resetForm: false        // reset the form after successful submit 

			// $.ajax options can be used here too, for example: 
			//timeout:   3000 
	});

});

function validate(address) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   return reg.test(address);
}