$(document).ready(function(){
    $("div.carousel").carousel({
		loop: true,
		dispItems: 1,
		//pagination: true,
		//autoSlide: true,
		effect: "fade"
	});
	
	$.post('ajax.php',{ op: "xml" }, function(xml) {
		var text_html="";
		var i=0;
		$(xml).find('item').each(function(){
			if(i<6){
				if(i%2==0){
					text_html+='<div class="rss_item float_left">';
				}
				else{
					text_html+='<div class="rss_item float_right">';
				}
				text_html+='<div class="rss_title"><a href="'+$(this).find('link').text()+'">';
				text_html+=$(this).find('title').text();
				text_html+='</a></div>';
				text_html+='<div class="rss_date">';
				text_html+=$(this).find('pubDate').text();
				text_html+='</div>';
				text_html+='</div>';
				i++;
			}
		});
		$('#rss_content').html(text_html);
	}, 'xml');
	
	$('.submit').click(function(){
		var params={ 
			op: "contact",
			name: $('#name_input').val(),
			mail: $('#mail_input').val(),
			telf: $('#telf_input').val(),
			comment: $('#comment_area').val()
		};
		
		var validar_result=validar_form(params);
		
		if(validar_result==true){
			$.post('ajax.php',params, function(data) {
				$('#contact_message').show().removeClass('input_ko').addClass('input_ok').text(data);
			}, 'text');
		}
		
		return false;
	});
	
	$("img[rel]").overlay({mask: '#000'});
	$("a[rel]").overlay({mask: '#000'});
	
	/*******
            
	*** Anchor Slider by Cedric Dugas ***
	*** Http://www.position-absolute.com ***
	
	You can use and modify this script for any project you want, but please leave this comment as credit.
	
	Never have an anchor jumping your content, slide it.
	
	Just add the class anchor at your <a> tag and it will slide, user with no javascript will
	still go to destination with the normal html anchor
	
	Don't forget to put an id to your anchor !
	
	*****/
	
	anchor = {
		init : function() {
			$("a.anchorLink").click(function () {
				elementClick = $(this).attr("href");
				destination = $(elementClick).offset().top;
				$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, 1100 );
				return false;
			});
		}
	}
	
	anchor.init();
});

function validar_form(params){
	$('#name_input, #mail_input, #telf_input, #comment_area, #contact_message').removeClass('input_ko');
	$('#contact_message').hide();
	
	if($.trim(params.name)==''){
		$('#name_input, #contact_message').addClass('input_ko');
		$('#contact_message').show().text('El campo de nombre no pueden estar vacio.');
		return false;
	}
	
	if($.trim(params.mail)==''){
		$('#mail_input, #contact_message').addClass('input_ko');
		$('#contact_message').show().text('El campo de mail no pueden estar vacio.');
		return false;
	}
	
	if(!validarEmail(params.mail)){
		$('#mail_input, #contact_message').addClass('input_ko');
		$('#contact_message').show().text('El mail introducido es incorrecto.');
		return false;
	}
	
	if($.trim(params.telf)==''){
		$('#telf_input, #contact_message').addClass('input_ko');
		$('#contact_message').show().text('El campo de telefono no pueden estar vacio.');
		return false;
	}
	
	if($.trim(params.comment)==''){
		$('#comment_area, #contact_message').addClass('input_ko');
		$('#contact_message').show().text('El campo de comentarios no pueden estar vacio.');
		return false;
	}
	
	return true;
}

function validarEmail(valor) {
	if (valor.match(/[^@]+@[^\.]+\..+/)){
		return true;
	} 
	else{
		return false;
	}
}
