// --------------------------------------------------------------------
// --------------------------------------------------------------------
jQuery.jQueryRandom = 0;
jQuery.extend(jQuery.expr[":"],
{
    random: function(a, i, m, r) {
        if (i == 0) {
            jQuery.jQueryRandom = Math.floor(Math.random() * r.length);
        };
        return i == jQuery.jQueryRandom;
    }
});
// --------------------------------------------------------------------
// --------------------------------------------------------------------

$(document).ready(function() {
	
	$('p.quote').each(function() {
		ajaxQuote($(this));
		$(this).css('opacity', 0.1);
	});
	setInterval('randQuote()', 8000);
	setInterval('randFade()', 6000);
	
	$('div#menu > a').each(function() {
		$(this).attr('title', $(this).text());
	});
	$('div#menu > a').click(function() {
		var o = $('div#main'),
			a = $(this).attr('class');
		$.get('inc/'+a+'.htm', function(data) {
			o.fadeOut('slow', function() {
				o.html(data);
				o.fadeIn('slow', function() {
					if(a == 'pictures') {
						$('div#featured').orbit({
							animation: 'fade',
							animationSpeed: 800,
							advanceSpeed: 6000,
							startClockOnMouseOut: true,
							startClockOnMouseOutAfter: 3000,
							directionalNav: true,
							captions: true,
							captionAnimationSpeed: 800,
							timer: true
						});
					} else if(a = 'contact') {
						$('form#form-contact').submit(function() {
							var	name = $('input#form-name'),
								mail = $('input#form-mail'),
								mess = $('textarea#form-mess');
								
							var valid = true;
							valid = valid && check_length(name, "Nom", 3, 45);
							valid = valid && check_length(mail, "E-mail", 6, 80);
							valid = valid && check_length(mess, "Message", 3, 512);
							valid = valid && check_regexp(mail, /^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$/, "Format : email@domaine.com");
							
							if(!valid) return false;
							
							$.ajax({
								type:	'POST',
								url:	'contact.php',
								data:	'name='+name.val()+'&mail='+mail.val()+'&mess='+mess.val(),
								success: function(msg) { alert(msg); }
							});
							return false;
						});
					}
				});
			});
		});
	});
	
});

function ajaxQuote(o) {
	$.ajax({
		type:	'POST',
		url:	'data/quotes.php',
		success: function(data) {
			o.fadeOut(500, function() {
				o.html('« '+data+' »');
				o.fadeIn(500);
			});
		}
	});
}

function randQuote() {
	ajaxQuote($('p.quote:random'));
}

function randFade() {
	var o = $('p.quote:random');
	quoteBlur(o, 'in');
	o.animate({ opacity: 1 }, 3000);
	$('p.quote').each(function(i) {
		if(o.attr('id') != $(this).attr('id')) {
			$(this).animate({ opacity: 0.1 }, 3000, function() {
				quoteBlur($(this), 'out');
			});
			
		}
	});
}

function quoteBlur(o, n) {
	if(n == 'in') o.css('text-shadow', '1px 1px 1px #000');
	else o.css('text-shadow', '0 0 10px #fff');
}

function check_length(o, n, min, max) {
	if(o.val().length > max || o.val().length < min) {
		alert('La longueur du champ '+n+' doit être comprise entre '+min+' et '+max+' !');
		return false;
	}
	return true;
}

function check_regexp(o, regexp, n) {
	if(!(regexp.test(o.val()))) {
		alert(n);
		return false;
	}
	return true;
}
