var Carrousel = function(pictures, time) {
	this.pictures = pictures;
	this.current = 0;
	this.timeout = time || 3000;
	this.timer = null;
	this.pictures.css("display", "none");
	$(this.pictures[this.current]).css("display", "block");
};

Carrousel.prototype.play = function() {
	if (this.timer) return;
	var carrousel = this;
	this.timer = setInterval(function(){ carrousel.next() }, this.timeout);
};

Carrousel.prototype.pause = function() {
	if (!this.timer) return;	
	clearInterval(this.timer);
};

Carrousel.prototype.next = function() {
	this.pause();
	this.play();
	$(this.pictures[this.current]).css("display", "none");
	this.current++;
	if (this.current >= this.pictures.length) {
		this.pause();
		this.current = this.pictures.length - 1;
	}
	$(this.pictures[this.current]).css("display", "block");
}

Carrousel.prototype.previous = function() {
	this.pause();
	this.play();
	$(this.pictures[this.current]).css("display", "none");
	this.current--;
	if (this.current < 0) {
		this.current = 0;
	}
	$(this.pictures[this.current]).css("display", "block");
}


var myPictures;
$(function(){
	myPictures = new Carrousel($(".picture"));
	myPictures.play();
	$("#previous").click(function() { myPictures.previous(); return false; });
	$("#next").click(function() { myPictures.next(); return false; });
	$("#play").click(function() { myPictures.play(); return false; });
	$("#pause").click(function() { myPictures.pause(); return false; });
});

