$.fn.mediaSlider = function(option) {
	var defaults = {
		prevId : 'prev_media',
		prevText : 'Précédent',
		nextId : 'next_media',
		nextText : 'Suivant',
		nbAff : 4,
		numeric : true,
		numericControlId : 'media_control',
		numericId : 'media',
		selfId : 'medias'
	}
	var options = $.extend(defaults, option);
	this.each(
		function()
		{
			var self = $("#" + options.selfId);
			self.items = $("li", self);
			self.items.not(":lt(" + options.nbAff + ")").hide().end();
			self.currentitem = 0;
			self.options = options;
			var html = '<a id="' + options.prevId + '" href=\"javascript:void(0);\"><span>' + options.prevText + '</span></a>';
			html += '<a id="' + options.nextId + '" href=\"javascript:void(0);\"><span>' + options.nextText + '</span></a>';
			if (options.numeric) {
				html += '<ol id="' + options.numericControlId + '"></ol>';
			}
			$(this).append(html);
			if (options.numeric) {
				for ( var i = 0; i < self.items.size(); i++) {
					if (i < options.nbAff)
						$(document.createElement("li")).attr('id', options.numericId + (i + 1)).attr('class', 'current').html(i + 1).appendTo($("#" + options.numericControlId));
					else
					$(document.createElement("li")).attr('id', options.numericId + (i + 1)).html(i + 1).appendTo($("#" + options.numericControlId));
				};
			}
			$("#" + options.nextId).click(function() {
				doTick("next");
			});
			$("#" + options.prevId).click(function() {
				doTick("prev");
			});
			
			var doTick = function(dir)
			{
				$.mediaslider(self, dir);
			}
		}
	)
}
$.mediaslider = function(el, dir)
{
	if (dir == "next")
	{
		$(el.items[el.currentitem]).hide("slow",
			function()
			{
				$(this).appendTo("#" + el.options.selfId);
				el.currentitem = ++el.currentitem % (el.items.size());
				$(el.items[(el.currentitem+(el.options.nbAff-1)) % el.items.size()]).show();
				if(el.options.numeric)
				{
					first_aff = el.currentitem;
					if (first_aff == 0)
						first_aff = el.items.size();
					$("#" + el.options.numericId + first_aff).toggleClass("current");
					last_aff = (el.currentitem+el.options.nbAff) % el.items.size();
					if (last_aff == 0)
						last_aff = el.items.size();
					$("#" + el.options.numericId + last_aff).toggleClass("current");
				}
			}
		);
	} else {
		$(el.items[(el.currentitem+(el.options.nbAff-1)) % el.items.size()]).hide(0,
			function()
			{
				el.currentitem = --el.currentitem % (el.items.size());
				if (el.currentitem < 0)
					el.currentitem = el.items.size() - 1;
				$(el.items[el.currentitem]).prependTo("#" + el.options.selfId).show("slow");
				if(el.options.numeric)
				{
					first_aff = el.currentitem + 1;
					$("#" + el.options.numericId + first_aff).toggleClass("current");
					last_aff = (first_aff+el.options.nbAff) % el.items.size();
					if (last_aff == 0)
						last_aff = el.items.size();
					$("#" + el.options.numericId + last_aff).toggleClass("current");
				}
			}
		);
	}
}
