(function($)
{
	var jqAgSlider = function(slider, options)
	{
		var self = this;
		
		this.opt = options;
		this.slider = slider;
		this.query_children_content = $("div.slider-content", this.slider).children("div.ag_il_image");
		this.slider_height = this.getHeight();
		this.max_children = this.getNbrChildren();
		this.index = 0;
		this.indexA = 0;
		this.busy = false;
		
		this.slider.mouseenter(function(){self.stopTimer();})
		this.slider.mouseleave(function(){self.startTimer();})
		
		this.buildSpotsAndContent();
		this.startTimer();
	};
	
	jqAgSlider.prototype.getHeight = function()
	{
		var self = this;
		
		return parseInt($(this.slider).css("height")) + parseInt($(this.slider).css("marginTop")) - 1;
	};
	
	jqAgSlider.prototype.getNbrChildren = function()
	{
		var self = this;
		
		return this.query_children_content.length;
	};
	
	jqAgSlider.prototype.buildSpotsAndContent = function()
	{
		var self = this;
		
		var count = 0;
		var html = "";
		var selected = "";
		
		for(count; count < this.max_children; count++) {
			if (count == 0) selected = "selected";
			else selected = "";
			
			html += '<a href="javascript:void(0)" id="' + count + '" class="index-slider-spot ' + selected + '"></a>';
			this.query_children_content.eq(count).attr("id", count);
		}
		
		$("div.index-slider-picto").append(html);
		$("div.index-slider-picto a.index-slider-spot").each(function() {
			$(this).click(function() {
				if(!self.busy) {
					self.animate($(this).attr("id"));
				}
			});
		});
		
		this.query_children_content.css("opacity", 0);
		this.query_children_content.eq(0).css({"opacity": 1, zIndex: 1});
	};
	
	jqAgSlider.prototype.startTimer = function()
	{
		var self = this; 
		
		this.timer = setInterval(function() { self.animate() }, self.opt.time);
	};
	
	jqAgSlider.prototype.stopTimer = function()
	{
		var self = this;
		
		clearInterval(this.timer);
	};
	
	jqAgSlider.prototype.animate = function(id)
	{
		var self = this;
		
		this.busy = true;
		
		if (id != undefined) {
			this.index = id;
			id = undefined;
		} else {
			this.index = this.setIndex();
		}
		
		var container = $("div.slider-content", this.slider);
		var selected = container.children("div#" + self.index + "");
		var links = $("div.index-slider-picto a", this.slider);
		
		container.append(selected);
		links.css('background-position', '0 0');
		
		selected.animate(
			{opacity: 1},
			self.opt.speed,
			self.opt.easein,
			function() {
				self.updateSlider();
				self.busy = false;
				if (id != undefined) self.startTimer();
				links.eq(self.index).css('background-position', '0 -29px');
			}
		);
	};
	
	jqAgSlider.prototype.updateSlider = function()
	{
		var self = this;
		
		$("div.index-slider div.index-slider-picto a").removeClass("selected");
		this.query_children_content.removeClass("selected");
		
		this.query_children_content.not(self.query_children_content.eq(self.index)).css({opacity: 0, zIndex: 0});
		
		$("div.index-slider div.index-slider-picto a").eq(self.index).addClass("selected");
		this.query_children_content.eq(self.index).addClass("selected");
	};
	
	jqAgSlider.prototype.moveDivContent = function()
	{
		var self = this;
		
		var first = $("div.slider-content", this.slider).children("div.ag_il_image:first");
		var last = $("div.slider-content", this.slider).children("div.ag_il_image:last");

		first.insertAfter(last);

		this.query_children_content = $("div.slider-content", this.slider).children("div.ag_il_image");
		//this.index = 0;
		
	};
	
	jqAgSlider.prototype.setIndex = function()
	{
		if(this.index == this.max_children - 1) return 0;
		else return this.index += 1;
	};
	
	$.fn.agSlider = function(options)
	{
		if(this.length < 0) return this;
		var options = $.extend($.fn.agSlider.defaults, options);
		new jqAgSlider(this, options);
		
		return this;
	};
	
	$.fn.agSlider.defaults = {
		speed: "1000",
		easein: "linear",
		time: 4000
	};
})(jQuery);