/*  (c) 2011 Steven Albarracin  www.stevenalbarracin.com
 *
 *  Distributable under the terms of an MIT-style license.
 *--------------------------------------------------------------------------*/

if(!SA) SA = {};
SA._ShowCase  = Class.create({
	initialize:function(delay,name,options,showControls){
		Element.addMethods(SA);
		if(showControls) this.showControls = true;
		this.controlLink = [];
		this.width = ( options.width || 640);
		this.height = ( options.height || 480);
		this.container = ( name || "showCase");
		this.slider = this.container + "Slider";
		this.controls = this.container + "ControlWrap";
		this.slideLength =  $$("#"+this.slider+" li").size();
		this.prefix = "sc_";
		this.isSliding = false;
		this.curIndex = 0;
		this.duration = 0.5;
		this.delayInSeconds = (delay || 4) ;
		this.setShowCaseSliderWidth();
		this.setStyles();
		if(this.showControls) this.setControls();
		this.apID = 0;
		this.startAutoSlide();
	},
	setStyles:function(){
		var div = $(this.container).setStyle({width:this.width+'px',height:this.height+'px',overflow:'hidden',position:'relative'});
		var ul = $(this.slider).setStyle({margin:0,padding:0,listStyle:'none'}).childElements().each(function(li){li.setStyle({float:'left',width:this.width+'px'});}.bind(this));
		var images = $$("#"+this.slider + " img").each(function(img){img.setStyle({width:this.width+'px'});}.bind(this));
	},
	setShowCaseSliderWidth:function(){
		var w = this.slideLength * this.width;
		$(this.slider).setStyle({width:w+"px"});
	},
	startAutoSlide:function() {
		this.apID = setTimeout(function(){this.autoSlide(this.curIndex);}.bind(this), this.delayInSeconds * 1000);
	},
	slide:function(moveToId){
		if(!this.isSliding){
			this.isSliding = true;
			var oldId = this.curIndex;
			this.curIndex = moveToId;
			if(this.curIndex != oldId && this.showControls){
				$("control_"+oldId).setStyle({backgroundColor:'#fff',color:'#777'}); //.removeClassName("active");
				$("control_"+moveToId).setStyle({backgroundColor:'#666',color:'#fff'}); //.addClassName("active");
			}
			var x = $(this.prefix+moveToId).positionedOffset().left;		
			new Effect.Move(this.slider,{ x: -x, y:0, mode:'absolute', duration: this.duration, afterFinish:this.finishedSliding() });
		}
	},
	autoSlide:function(moveToId){
		(this.curIndex == (this.slideLength-1) ) ?moveToId = 0:moveToId++ ;
		this.slide(moveToId);
		this.apID = setTimeout(function(){this.autoSlide(moveToId);}.bind(this), this.delayInSeconds * 1000);
	},
	finishedSliding:function(){
		this.isSliding = false;
	},
	stopAutoSlide:function(){
		clearInterval(this.apID);
	},
	setControls:function(){
		var controlWrap = SA.createElement("div",{id:this.controls});
		controlWrap.setStyle({position:'absolute',bottom:'20px',right:'0px',color:'#777',padding:'6px 20px',fontSize:'10px'});
		var links = $$("#"+this.slider+" li").collect(function(l,i){
			return '<span id="control_'+i+'" >'+(i+1)+'</span>';
		}).join(' ');
		controlWrap.update(links);
		$(this.container).insert(controlWrap);
		this.setControlMouseEvents();
	},
	setControlMouseEvents:function(){
		var ths = this;
		$$("#"+this.controls +" span").each(function(link,i){
			link.setStyle({display:'block',float:'left',padding:'3px 6px',backgroundColor:'#fff',marginRight:'2px',border:'2px solid #000',lineHeight:'100%'});
			if(this.curIndex == i) link.setStyle({backgroundColor:'#666',color:'#fff'}); //.addClassName("active");
			link.setCursorAsClickable();
			link.on("click",function(){
				ths.stopAutoSlide();
				if(ths.showControls) ths.slide(link.id.replace("control_",""));
			})
		}.bind(this));
		
	}
});

SA.ShowCase  = function(delay,name,width,showControls){
	return new SA._ShowCase(delay,name,width,showControls);
}
