// ==============
// = NEWSLETTER =
// ==============

$(document).ready(function(){
	$("#newsletter").submit(function(){
		if($("#email").val().indexOf("@") == -1) {
			alert("Merci d'indiquer votre adresse e-mail !");
			return false;
		}
		var data = $("#email").serialize();
		$.ajax({
			type: "POST",
			url: "newsletter.php",
			data: data,
			success: function(msg){
				$("#newscontent").html('<p><b>Félicitations !</b> Vous êtes maintenant abonné(e) à notre newsletter. Vous recevrez régulièrement des informations sur nos promotions et nouveautés.</p>');
			}
		});
		return false;
	});
});

// =============
// = SCROLLERS =
// =============

function Scroller(scroller, interval, retain, style, isRandom)
{
	var interval = typeof(interval) != 'undefined' ? interval : 2000;	 //time in milliseconds
	var headline_count = $(scroller + ">li").size();
	var next, current = 0;
	var rotate = 1;
	var height = $(scroller).css("height");
	var width = $(scroller).css("width");
	
	if(retain) {
		$(scroller).hover(function() {
			rotate = 0;
		}, function() {
			rotate = 1;
		});
	}
	
	if(isRandom) {
		current = Math.floor(Math.random()*headline_count);
	}
		
	var curLine = $(scroller + ">li:eq(" + current + ")");
	var otherLines = $(scroller + ">li:not(:eq(" + current + "))");
	
	if(style == "slide_up") {
		curLine.css('top','0px').css('opacity',1);
		otherLines.css('top',height).css('opacity',0).css('display', 'none');
	} else if(style == "slide_left") {
		curLine.css('left','0px').css('opacity',1);
		otherLines.css('left',width).css('opacity',0).css('display', 'none');
	} else { // fade
		curLine.css('left','0px').css('opacity',1);
		otherLines.css('left','0px').css('opacity',0).css('display', 'none');
	}


	setInterval(scroll, interval);

	function scroll() {
		if(rotate == 1) {

			next = (current + 1) % headline_count;
			var curLine = $(scroller + ">li:eq(" + current + ")");
			var nextLine = $(scroller + ">li:eq(" + next + ")");

			nextLine.css('display', 'inline');

			if(style == "slide_up") {
				curLine.animate({top: "-" + height, opacity: 0},"slow",
					function() { $(this).css('top',height).css('display', 'none'); });
				nextLine.animate({top: "0px", opacity: 1},"slow");
			}
			else if(style == "slide_left") {
				curLine.animate({left: "-" + width, opacity: 0},"slow",
					function() { $(this).css('left',width).css('display', 'none'); });
				nextLine.animate({left: "0px", opacity: 1},"slow");
			}
			else { // fade
				curLine.animate({opacity: 0},"slow",
					function() { $(this).css('display', 'none'); });
				nextLine.animate({opacity: 1},"slow");
			}

			current = next;
		}
	}
	
}

$(document).ready(function(){
//	new Scroller("#scrollup", 6000, true, "slide_up", false);
	new Scroller("#plaquette", 5000, false, "fade", true);
});

