$(document).ready(function(){

	// Vars
	
	linkIndex = 1;
	
	// Functions
	
	function fadeOut(object,time,callback) {
		object.stop().animate({
			opacity:0
		}, time, "easeInOutExpo", function(){
			if (callback == "hide") { $(this).hide(); }
			if (callback == "fadeIn") { fadeIn($(this),1000); }
		});
	}
	
	function fadeIn(object,time) {
		object.stop().show().animate({
			opacity:1
		}, time, "easeInOutExpo");
	}
	
	
	function slideSwitchr(index,time,callback){
		clearTimeout(timer);
		$("#slider_navigation li").removeClass("current");
		$("#slider_navigation li:eq("+(index-1)+")").addClass("current");
		$("#slider .content").animate({opacity:0},time,"easeInOutExpo", function(){
			$(".slides").hide();
			$("#slide"+index).show();
			$(this).animate({opacity:1},time,"easeInOutExpo");
			if (callback == "autorun") {
				timer = setTimeout(slideShow,5000);
			}
		});
	}
	
	function slideShow() {
		if (linkIndex == $(".slides").length) {
			linkIndex = 0;
		}
		linkIndex=linkIndex+1;
		slideSwitchr(linkIndex,1000,"autorun");
	}
	
	// Subnav Dropdown		
	
	$(".subnav").css("opacity","0");
	$("#header .navigation li a:not(.subnav a)").hover(function(){
		if ($("#header .subnav:visible").length > 0) {
			fadeOut($(".subnav"),500,"hide");
		}
		fadeIn($(this).parent().find(".subnav"),500);
	});
	$("#header .navigation .subnav").hover(function(){}, function(){
		fadeOut($(this),500,"hide");
	});
	$(".cat_navigation a").click(function(){
		fadeIn($("#cat_subnav"),500);
		return false;
	});
	$("#cat_subnav").hover(function(){}, function(){
		fadeOut($(this),500);
	});
	
	// Support links
	
	$("#support_links a").click(function(){
		if ($("#header .subnav:visible").length > 0) {
			fadeOut($(".subnav"),500);
		}
		if ($(this).parent().find(".subnav:visible").length > 0) {
			fadeOut($(".subnav"),500,"hide");
			return false;
		}
		fadeIn($(this).parent().find(".subnav"),500,"hide");
	});
	
	// Slides switching
	
	$("#slider_navigation a").click(function(){
		$("#slider").stop();
		linkIndex = $(this).parent().index();
		linkIndex = linkIndex+1;
		slideSwitchr(linkIndex,500,"autorun");
		return false;
	});
	
	// Engaging slideShow function
	
	if ($("#slider").length > 0) {
		timer = setTimeout(slideShow, 8000);
	}
	
		/* Tooltip effect for links 
			$('.tooltip-enabled a').tipsy({gravity: 's'});
		*/
	
	// Message form validation and submission
	/*
	$("#message_submit").click(function(){
		currentForm = $(this).parent().parent().parent().find("form");
		dataString = currentForm.serialize() + '&message=' + $("#message").val();
		if (currentForm.find("#name").val() == '') {
			$("#alert").text("Please fill in your name").css({opacity:0}).show().animate({
				opacity:1
			}, 250, "easeInOutExpo");
			return false;
		}
		if (currentForm.find("#email").val() == '') {
			$("#alert").text("Please fill in your email").css({opacity:0}).show().animate({
				opacity:1
			}, 250, "easeInOutExpo");
			return false;
		}
		if (currentForm.find("#message").val() == '') {
			$("#alert").text("Please write your message").css({opacity:0}).show().animate({
				opacity:1
			}, 250, "easeInOutExpo");
			return false;
		}
		$.ajax ({
			type:"POST",
			url:"/php/sendmail.php",
			data: dataString,
			success: function() {
				currentForm.animate({
					opacity:0
				},500,function(){
					$(this).hide();
					$("#success_message").css({opacity:0}).show().animate({
						opacity:1
					},500)});
			}
		});
		return false;
	});
	*/
	
});



function is_email(email){
	var result = email.search(/^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z]{2,3})+$/);
	if(result > -1){ return true; } else { return false; }
}
function valid_form(){

	if(document.getElementById('nom').value == "" || document.getElementById('email').value == "" || document.getElementById('message').value == "" || document.getElementById('user_code').value == ""){
		alert("Le nom, l'email et le message sont obligatoires (ainsi que le cryptogramme visuel).");
		return false;
	}
	if(!is_email(document.getElementById('email').value)){
		alert("Attention, votre email ne semble pas valide");
		return false;
	}
	return true;
}
