var SlideShow_Direct_Left={'scroll':'scrollLeft','pos':'left','size':'scrollWidth','fix_size':'clientHeight','fix':'height'};
var SlideShow_Direct_Top={'scroll':'scrollTop','pos':'top','size':'height','fix_size':'clientWidth','fix':'width'};

function SlideShow(ContainerID, Datasource, Direct, Callback, AddClick){
	this.ContainerID=ContainerID;
	this.Datasource=Datasource;
	
	this._scroll=Direct['scroll'];
	this._pos=Direct['pos'];
	this._size=Direct['size'];
	this._fix=Direct['fix'];
	this._fix_size=Direct['fix_size'];
	this.AddClick=AddClick;
	this.Callback=Callback;
	this.Speed=[8,2];
	this.Items=[];
	this.pos=0;
	this.fix=0;
	
	this.i=0;
}

SlideShow.prototype.setSpeed= function(Move, Interval){
	this.Speed=[Move,Interval];
}

SlideShow.prototype.Init= function(){
	this.Container=DOM(this.ContainerID);
	this.Container.style.position='relative';
	this.Container.style.overflow='hidden';

	this.pos=0;
	this.i=0;
	var Items=[]
	for (i=0; i<this.Container.childNodes.length; i++){
		if (this.Container.childNodes[i].tagName){Items[Items.length]=this.Container.childNodes[i]}
	}	
	this.AddItems(Items);
	//this.Move(1);
	
}


SlideShow.prototype.ClickFactory= function(i){
	var self=this;
	return function(event){
		
		self.Move(i);
		DOM_Stop(event);
	}
}

SlideShow.prototype.Move= function(i){	
	//if (this.Repeat) return false;//
	
	//if (this.Callback) this.Callback.Select(this.Items[i]);
	//Вычисляем позицию:
	//ScrollValue=parseInt(this.Items[i].style[this._pos])+this.Items[i][this._size]/2-this.Container[this._size]/2;	
	//if (ScrollValue<0) ScrollValue=0;
	//if (ScrollValue+this.Container[this._size]>=this.pos) {
	//	ScrollValue=this.pos-this.Container[this._size];
	//}
	//if (i>0){
	//	if (i>1&&(i==this.Items.length-1)){
	//		
	//		ScrollValue=parseInt(this.Items[i-2].style[this._pos]);
			
	//	}
	//	else {
	//		ScrollValue=parseInt(this.Items[i-1].style[this._pos]);
	//	}
	//}
	//else {
	//	ScrollValue=0;
	//}
	
	//Запоминаем:
	//this.i=i;
}

SlideShow.prototype.setScrollValue= function(ScrollValue){
	//Выставляем:
	if (this.Repeat) return;	
	if (ScrollValue>=this.Container[this._size]||ScrollValue<0){
		
		return;
	}
	if (this.Container[this._scroll]!=ScrollValue){
		if (this.Speed[0]){
			this.ScrollValue=ScrollValue;
			this.Repeat=setInterval(this.MoveFactory(), this.Speed[1]);
		}
		else {
			this.Container[this._scroll]=ScrollValue;
		}
	}
}

SlideShow.prototype.Fwd= function(){
	if (this.Repeat) return;//locked
	
	if (this.Items.length>this.i+3){		
		this.i=this.i+1;
		
		this.setScrollValue(parseInt(this.Items[this.i].style[this._pos]));
	}
	else {		
		this.LoadMore();
	}
	
}

SlideShow.prototype.Back= function(){
	if (this.Repeat) return;//locked
	if (this.i>0){
		this.i=this.i-1;
		this.setScrollValue(parseInt(this.Items[this.i].style[this._pos]));
	}
	else return true;
}

SlideShow.prototype._Move= function(){
	delta=this.ScrollValue-this.Container[this._scroll];
	
	if (this.Container[this._scroll]+this.Speed[0]>=this.Container[this._size]){
		clearInterval(this.Repeat);
		this.Repeat=0;
		return;
	}
	if (delta>0){
		this.Container[this._scroll]+=this.Speed[0];
	}
	else {
		this.Container[this._scroll]-=this.Speed[0];
	}
	if ((this.ScrollValue-this.Container[this._scroll])/delta<=0){
		//Дошли до точки:
		this.Container[this._scroll]=this.ScrollValue;
		clearInterval(this.Repeat);
		this.Repeat=0;
	}
}

SlideShow.prototype.AddItems=function(Items){
	var i=0;	
	for (i=0; i<Items.length; i++){
		if (Items[i].parentNode!=this.Container){	
			this.Container.appendChild(Items[i]);
		}
		if (parseInt(Items[i][this._fix_size])>this.fix) this.fix=parseInt(Items[i][this._fix_size]);
		Items[i].style.position='absolute';
		Items[i].style[this._pos]=this.pos+'px';				
		Items[i].style.float='left';			
		attachClass(Items[i],'canvas');
		attachClass(Items[i],'scrollable');
		this.pos=this.pos+Items[i][this._size];	
		this.Items[this.Items.length]=Items[i];
	}
	this.Container.style[this._fix]=this.fix+'px';
}

SlideShow.prototype.AddItems_Factory=function(){
	var self=this;
	return function(Items){
		self.AddItems(Items);
		if (Items.length) self.Fwd();	
		else self.Datasource=null;
	}
}

SlideShow.prototype.MoveFactory= function(){
	var self=this;
	return function(){self._Move();}
}

SlideShow.prototype.LoadMore= function(){
	if (this.Datasource){
		this.Datasource(this.Items.length, this.AddItems_Factory());
	}
}

SlideShow.prototype.NextFactory= function(){
	var self=this;
	
}