
 /*######## Слайдер  ###################################################*/
 /* Пример внедрения
 	new multi_items_slider({
		items:$('.slider .slider_item'),
		arrow_left:$('.slider .arrleft'),
		arrow_right:$('.slider .arrright'),
		move_speed:'slow',
		remPosition:false,
		automation:{enabled:true, delay:0,interval:5000}
	});
 
 */
 function multi_items_slider(p){ //1.7 Слайдер сдвигающий и подстраивающий набор элементов
	if (p.items.size()==0) return;
 
	this.error=function(t){alert('multi_items_slider — '+t);}		
	if (!p.arrow_left) this.error('Не передана левая стрелка');
	this.$l=p.arrow_left;
	if (!p.arrow_right) this.error('Не передана правая стрелка');
	this.$r=p.arrow_right;
	
	this.is_width_align=p.width_align!=null?p.width_align:1; //Выстраивать по ширине
	this.move_step=p.move_step!=null?p.move_step:1; //Мультиплексор шага
	this.move_speed=p.move_speed!=null?p.move_speed:'fast'; //Мультиплексор шага
	this.remPosition=p.remPosition!=null?p.remPosition:false; //Мультиплексор шага
	this.animateCallback=p.animateCallback; //Callback function throw animate
	this.auto_width_on_window_resize=p.auto_width_on_window_resize?p.auto_width_on_window_resize:true; //Callback function throw animate
	this.dpars=p; //All init params
	this.screen_number=0; //Текущий экран слайдера

	this.auto_width_on_window_resize = true;
	
	p.automation=p.automation?p.automation:{};
	this.automation={ //Автоматическое перемещение
		enabled:(p.automation.type || p.automation.interval || p.automation.delay || p.automation.enabled)?true:false,
		type:p.automation.type?p.automation.type:'loop',
		interval:p.automation.interval?p.automation.interval:'5000',
		delay:p.automation.delay?p.automation.delay:'0'
	}

	if (!p.items) this.error('Не передан массив элементов');
	this.$items=p.items; //items
	this.$items_size=this.$items.size(); //количество элементов
	
	this.$s=$(p.items.parent().get(0));//slider - то что ездит
	this.$sw=this.$s.width();//Ширина слайдера
	this.$con=$(p.items.parent().get(0)).parent();//Контейнер в котором слайдер
	this.$conw= 1600; //this.$con.width(); //Ширина контейнера
//	this.co=0;
	if (this.remPosition){
		this.co=1*(GetCookie('co')?GetCookie('co'):0); //Current offset
	}else{
		this.co=0
	}

	//this.$s.css({'top':'-'+this.$con.height()+'px'});
	
	this.can_step_right=function(){ //Можно ли переместиться вправо
		return !(this.$sw+this.co-1<this.$conw || !this.$items_size);
	}
	
	this.can_step_left=function(){ //Можно ли переместиться вправо
		return !(this.co+1>0 || !this.$items_size);
	}
	
	this.rd=function(){ //Перерисовать
		if (!this.can_step_right()){this.$r.fadeOut()}else{this.$r.fadeIn()}
		if (!this.can_step_left()){this.$l.fadeOut()}else{this.$l.fadeIn()}
		this.$s.animate({left:this.co},this.move_speed);
		if (this.animateCallback) this.animateCallback(this);
	}	
	
	var _p=this;
	this.get_items=function(){ //items_fill
		var mw=0;		
		var t=_p;
		var rwidth=0;
		_p.$items.each(function(){
			tw=$(this).width();	
			if (tw>mw) mw=tw;
			rwidth+=tw;	
			if (tw<2){_p.error('Один из элементов меньше 2х пикселей');}
		});
		
		if (_p.is_width_align){
			var icnt=Math.floor(_p.$conw/mw);
			var nw=Math.round(_p.$conw/icnt);		
			_p.$items.width(nw);	
			_p.$sw=_p.$conw/icnt*_p.$items_size;
		}else{
			_p.$sw=rwidth;
		}
		/*echo('conw='+_p.$conw);
		echo('mw='+mw);
		echo('icnt='+icnt);
		echo('items_size='+_p.$items_size);*/
		
	}
	
	this.get_items();
	this.rd();
	if (this.$s)
	this.$s.hide().css({'top':'0'}).fadeIn();
		
	this.$l.bind('click',this,function(e){e.data.step_left();});
	this.$r.bind('click',this,function(e){e.data.step_right();});	
	this.$s.bind('click',this,function(e){e.data.onSliderClick();});	
	$('body').bind('click',this,function(e){e.data.onBodyClick();});	

	if (this.automation.enabled){
		var _p=this;
		with (this.automation){
			_p.automation_timeout=setTimeout(function(){
				var direct=true;
				if (_p.can_step_right() || _p.can_step_left()){
					_p.$r.bind('mouseup',function(){if (_p.automation_interval) clearInterval(_p.automation_interval);});
					_p.$l.bind('mouseup',function(){if (_p.automation_interval) clearInterval(_p.automation_interval);});
					_p.automation_interval=setInterval(function(){
						if (!_p.can_step_right() && direct) direct=false;
						if (!_p.can_step_left() && !direct) direct=true;
						
						if (direct)
							_p.$r.click();
						else  
							_p.$l.click();
						
					},interval);					
				}
			
			},delay);		
		}

	}
	
	this.automation_stop=function(){
		this.automation.enabled=false;
		clearInterval(this.automation_interval);
	}

	
	this.onBodyClick=function(){
		if (!this.bodyclickblock){
			if (this.remPosition){
				SetCookie('co','0','','/');
			}
		}
	}
	
	this.onSliderClick=function(){
		SetCookie('co',this.co,'','/');
		this.bodyclickblock=1;
	}
	
	this.step_left=function(){
		var step=Math.floor(this.$conw*this.move_step);
		this.co=(this.co+step<=0)?this.co+step:0;
		this.screen_number--;
		this.rd();
		this.automation_stop();
	}
	
	this.step_right=function(){	
		var step=Math.floor(this.$conw*this.move_step);
		this.co=(this.co-step<this.$conw-this.$sw)?this.$conw-this.$sw:this.co-step;
		this.screen_number++;
		this.rd();
		this.automation_stop();
	}
	
	this.move_to=function(n){ //Скользить к номеру слайда
		//alert('mt '+n+' '+this.screen_number);
		//alert('mt');
		if (n>this.screen_number){ this.step_right(); this.move_to(n);}
		if (n<this.screen_number){ this.step_left(); this.move_to(n);}	 
		this.automation_stop();
		
	}
	
	
	if (this.auto_width_on_window_resize)
		$(window).bind('resize',this,function(o){
				o.data.$conw=o.data.$con.width();
				o.data.$items.width(o.data.$conw);
				o.data.get_items();
				o.data.$s.css({left:0});
				o.data.co=0; 
				o.data.move_to(0);			
		});
	
}


