/**
 * @author julien
 */
Slideshow={
	_timeOut:null,
	_nbSlides:null,
	_currentSlide:0,
	_duration:5,
	init:function() {
		//console.log("init slideshow");
		this._buildBtns();
		
		this._containerWidth=753;
	},
	_buildBtns:function() {
		var _childs=$$("#slideshow_home .slides > div");
		this._nbSlides=_childs.length;
		if (this._nbSlides>1) {
			var _ul=new Element("ul",{"id":"slideshow_nav"});
			
			for (var i=0;i<this._nbSlides;i++) {
				var _class="";
				if (i==0) _class="active";
				var _li=new Element("li",{"class":_class});
				_li.update(i+1);
				_li.observe("click",this._slide.bind(this,i))
				_ul.appendChild(_li);
			}
			$("slideshow_home").appendChild(_ul);
			
			this.start();
		}
	},
	start:function() {
		//console.log("start slideshow");
		this._pauseBind=this.pause.bind(this);
		$("slideshow_home").observe("mouseover",this._pauseBind);
		this._timeOut=setInterval(this._slide.bind(this,"auto"),1000*this._duration);
	},
	_slide:function(index) {
		//console.log("slide "+index);
		var _move;
		var _old=this._currentSlide+0;
		if (index=="auto") {
			if (this._currentSlide < this._nbSlides - 1) {
				_move = 1;
				this._currentSlide++;
			} else {
				_move = -this._nbSlides + 1;
				this._currentSlide = 0;
			}
		} else {
			if (index!=this._currentSlide) {
				_move=index-this._currentSlide;
				this._currentSlide = index;
			}
		}
		if (_move) {
			new Effect.Move($("slideshow_home").down(".slides"),{x:-_move*this._containerWidth,y:0,mode:'relative',duration:0.5})
			$("slideshow_nav").down("li",_old).removeClassName("active");
			$("slideshow_nav").down("li",this._currentSlide).addClassName("active");
		}
	},
	pause:function() {
		$("slideshow_home").stopObserving();
		clearInterval(this._timeOut);
		$("slideshow_home").observe("mouseout",this._checkStart.bind(this));
	},
	_checkStart:function(event) {
		if (event.pointerY()>$("slideshow_home").positionedOffset()[1]&&event.pointerY()<$("slideshow_home").positionedOffset()[1]+$("slideshow_home").getHeight()&&event.pointerX()>$("slideshow_home").positionedOffset()[0]&&event.pointerX()<$("slideshow_home").positionedOffset()[0]+$("slideshow_home").getWidth()) {
		} else {
			this.start();
		}
	}

}

document.observe("dom:loaded", function() {
	if (Prototype.Browser.Opera) {
		function initOpera(){
			Slideshow.init();
		}
		initOpera.defer();
	} else {
		Slideshow.init();
	}
});

