/*

	mintAjax slideshow extension
	
*/

mint.ext.slideshow = {};

mint.ext.slideshow.Create = function(container) {
	var w = {
		c : $(container),
		list : $(container).down("ul"),
		items : [],
		activeItem : 0,
		itemWidth : null,
		listWidth : null,
		running : false,
		slideSteps : 20,
		slideDuration : 500,
		autoCount : 0,
		autoTimer : null,
		autoWait : 1500,
		navNext : null,
		navPrev : null,
		navAuto : null,
		navStop : null,
		navCounter : null,
		navCounterTotal : true,
		OnNext : function() {},
		OnPrev : function() {},
		OnAuto : function() {},
		OnStop : function() {},
		OnUpdateCounter : function() {},
		
		SetNav : function(next, prev, auto, stop, counter) {
			if(next) this.SetNext(next);
			if(prev) this.SetPrev(prev);
			if(auto) this.SetAuto(auto);
			if(stop) this.SetStop(stop);
			if(counter) this.SetCounter(counter);
		},
		
		SetNext : function(next) {
			$D(next).slideshow = this;
			AddEvent($D(next), "click", function() {this.slideshow.OnNext(); this.slideshow.Next()});
		},
		
		SetPrev : function(prev) {
			$D(prev).slideshow = this;
			AddEvent($D(prev), "click", function() {this.slideshow.OnPrev(); this.slideshow.Prev()});
		},
		
		SetAuto : function(auto) {
			$D(auto).slideshow = this;
			AddEvent($D(auto), "click", function() {this.slideshow.OnAuto(); this.slideshow.Auto()});
		},
		
		SetStop : function(stop) {
			$D(stop).slideshow = this;
			AddEvent($D(stop), "click", function() {this.slideshow.OnStop(); this.slideshow.Stop()});
		},
		
		SetCounter : function(counter) {
			this.navCounter = $D(counter);
			this.UpdateCounter();
		},
		
		Next : function() {
			if(!this.running && parseInt(GetStyle(this.list, "left"))-this.itemWidth > -this.listWidth) {
				var end = parseInt(GetStyle(this.list, "left"))-this.itemWidth;
				mint.fx.Style(this.list, "left", null, end, this.slideSteps, this.slideDuration, null, this.AnimationDone);
				this.running = true;
				this.activeItem++;
			}
		},
		
		Prev : function() {
			if(!this.running && parseInt(GetStyle(this.list, "left"))+this.itemWidth <= 0) {
				var end = parseInt(GetStyle(this.list, "left"))+this.itemWidth;
				mint.fx.Style(this.list, "left", null, end, this.slideSteps, this.slideDuration, null, this.AnimationDone);
				this.running = true;
				this.activeItem--;
			}
		},
		
		Auto : function() {
			if(!this.running) {
				var t = this;
				this.running = true;
				setTimeout(function() {t.AutoNext(t.list)}, this.autoWait);
			}
		},
		
		AutoNext : function(obj) {
			var w = obj.slideshow;
			
			if(w.running && w.activeItem < w.items.length-1) {
				var end = parseInt(GetStyle(w.list, "left"))-w.itemWidth;
				mint.fx.Style(w.list, "left", null, end, w.slideSteps, w.slideDuration, null, function(obj) {w.autoTimer = setTimeout(function() {obj.slideshow.AutoNext(obj)}, obj.slideshow.autoWait)});
				w.autoCount++;
				w.activeItem++;
				w.UpdateCounter();
			}
			else {
				w.running = false;
				w.autoTimer = null;
			}
		},
		
		Stop : function() {
			if(this.autoTimer && this.running) {
				clearTimeout(this.autoTimer);
				this.running = false;
				this.autoTimer = null;
			}
		},
		
		AnimationDone : function(obj) {
			var w = obj.slideshow;
			
			w.running = false;
			w.UpdateCounter();
		},
		
		UpdateCounter : function() {
			if(this.navCounter) {
				this.navCounter.innerHTML = this.navCounterTotal ? (this.activeItem+1)+"/"+this.items.length : this.activeItem;
				this.OnUpdateCounter(this.activeItem+1);
			}
		},
		
		GoTo : function(index) {
			if(!this.running && this.activeItem+1 != index) {
				var end = this.itemWidth*(index-1);
				mint.fx.Style(this.list, "left", null, -end, this.slideSteps, this.slideDuration, null, this.AnimationDone);
				this.activeItem = index-1;
				this.running = true;
			}
		},
		
		GoToBegin : function() {
			this.GoTo(1);
		},
		
		GoToEnd : function() {
			this.GoTo(this.items.length);
		},
		
		SetItemWidth : function(width) {
			this.itemWidth = width;
			this.listWidth = this.items.length*width;
			SetWidth(this.list, this.listWidth);
		}
	}
	
	var nli = w.list.down("li");
	
	while(nli) {
		w.items.push(nli);
		nli = $(nli).next("li");
	}
	
	w.list.slideshow = w;
	w.itemWidth = GetWidth(w.items[0]);
	w.listWidth = w.itemWidth*w.items.length;

	if(GetStyle(w.c, "position") != "absolute" && GetStyle(w.c, "position") != "relative") w.c.style.position = "relative";
	
	w.c.style.overflow = "hidden";
	
	w.list.style.position = "relative";
	w.list.style.left = "0px";
	
	SetWidth(w.list, w.listWidth);
	
	return w;
}
