
window.addEvent('domready', function(){
	
	$$('.list_scroller').each(function(list_scroller){
		
		if(!list_scroller.get('id'))
			return;
		
		var url, props = '';
		
		if(list_scroller.get('src'))
			url = list_scroller.get('src');
		else
			url = list_scroller.get('id').replace(/^(last|related)_([a-z]+)/, '/$2/$1');
			
		var props = '';
		
		list_scroller.getElements('input[type=hidden]').each(function(input){
			if(props != '')
				props += '&';
			
			props += input.get('name') + '=' + input.get('value');
		});
		
		if(props != '')
			url += ( (url.test(/\?/)) ? '&' : '?' ) + props;
		
		if(!url || url == '')
			return;
		
		new listScroller({
			'container': list_scroller,
			'url': url
		});
		
	})
	
});


var listScroller = new Class({

	Implements: [Chain, Events, Options],

	options: {
		'elements': false,
		'in_col': 3,
		'create_arrows': true,
		'container': false,
		'col_class': false,
		'cols_in_wrap': 1,
		'duration': 400,
		'transition': Fx.Transitions.Quad.easeInOut,
		'url': null,
		'elementFn': null
	},

	initialize: function(options){
		this.setOptions(options);
		
		this.elements = this.options.elements;
		this.container = $(this.options.container);
		
		this.pagenum = 1;
		this.cols_len = 0;
		
		var $this = this;
		
		
		if(this.options.url){
			this.new_els = new Array();
			
			this.request = new Request.HTML({
				'url': this.options.url,
				'method': 'get',
				//'async': false,
				'onRequest': function(){
					this.arrows_container.addClass('loading');
				}.bind(this),
				'onComplete': function(){
					this.arrows_container.removeClass('loading');
				}.bind(this),
				'onSuccess': function(responseTree, responseElements, responseHTML){
					
					if( responseElements.length <= 0 ){
						this.right.addClass('disabled');
						return;
					}
					
					this.createCol().set('html', responseHTML);
					
					if(this.curr)
						this.next = this.curr.getNext();
						
					this.right.removeClass('disabled');
					
				}.bind(this)
			});
			
			this.getAjax = true;
		}
		
		
		// создаем стрелочки
		if(this.options.create_arrows)
			this.createArrows();
		
		this.cols_in_wrap = (this.options.cols_in_wrap.toInt() <= 0) ? 1 : this.options.cols_in_wrap.toInt();
		
		
		// обертываем в нужные теги 
		this.cols_len = 1;
		this.wrapper = this.container.getElement('.wrapper');
		
		this.cols = new Element('div', {
			'class':'wrapcols',
			'styles':{
				'width': '200%'
			}

		})
		
		new Element('div', {
			'class':'wrapcol',
			'html': this.wrapper.get('html'),
			'styles':{
				'width': '50%'
			}
		}).inject( this.cols );
		
		this.wrapper.empty();
		
		this.cols.inject( this.wrapper );
		
		
		
		// объявляем текущий предыдущий и следующий слайд
		this.curr = this.cols.getFirst().setStyle('display', 'block');
		this.previous = this.curr.getPrevious();
		this.next = this.curr.getNext();
		
		if(this.next)
			this.next.setStyle('display', 'none');
		else
			this.right.addClass('disabled');
		
		if(this.previous)
			this.previous.setStyle('display', 'none');
		else
			this.left.addClass('disabled');
		
		//объявляем скролл
		var scroll = new Fx.Scroll(this.wrapper, {
			'wait': false,
			'duration': this.options.duration,
			'transition': this.options.transition,
			'wheelStops': false,
			'onStart': function(){
				this.busy = true;
			},
			'onComplete': function(){
				if(this.next)
					this.next.setStyle('display', 'none');
				if(this.previous)
					this.previous.setStyle('display', 'none');
				
				this.scroll.set(0, 0);
				
				this.scroll.busy = false;
			}.bind(this)
		});
		
		this.scroll = scroll;
		
		
		
		// если контент первой колонки, то узнаем его
		if(this.curr.get('html') == '' && this.getAjax){
			
			
			new Request.HTML({
				'url': this.options.url,
				'method': 'get',
				//'async': false,
				'onRequest': function(){
					this.curr.addClass('loading');
				}.bind(this),
				'onComplete': function(){
					this.curr.removeClass('loading');
				}.bind(this),
				'onSuccess': function(responseTree, responseElements, responseHTML){
					
					this.curr.set('html', responseHTML);
					this.request.send('p='+ (this.cols_len + 1) );
					
					
				}.bind(this)
			}).send();
		}
		else if(this.getAjax){
			this.request.send('p='+ (this.cols_len + 1) );
		}
		
		window.addEvent('resize', function() {
			if(this.curr)
				scroll.toElement( this.curr );
		}.bindWithEvent(this));
		
	},
	
	
	
	createCols: function(injecttop){
		var newcols_len = (this.elements.length / this.options.in_col).ceil();
		
		if(!this.cols){
			this.cols = new Element('div', {
				'class':'wrapcols',
				'styles':{
					'width': '200%'
				}
			}).inject( this.wrapper );
		}

		this.cols_len += newcols_len;
		
		var col;
		
		var i=1;
		for (i=1;i<=newcols_len;i++){
			col = this.createCol();
			
			var z=1;
			for (z=1;z<=this.options.in_col;z++){
				if(this.elements.length > 0)
					this.elements.shift().inject( col, ((injecttop) ? 'top' : 'bottom') );
				else
					break;
			}
		}
		
	},
	
	createCol: function(){
		this.cols_len++;
		return new Element('div', {
			'class':'wrapcol',
			'styles':{
				'width': '50%',
				'display': 'none'
			}
		}).inject( this.cols );
	},

	createArrows: function(){
		this.arrows_container = new Element('div', {
			'class':'list_arrows'
		}).inject(this.container, 'top');
		
		this.left = new Element('img', {
			'class':'larr dis',
			'src':'/imgs/blank.gif'
		}).inject( this.arrows_container );
		this.right = new Element('img', {
			'class':'rarr',
			'src':'/imgs/blank.gif'
		}).inject( this.arrows_container );
		
		this.addArrowsEvents();
	},
	
	addArrowsEvents: function(){
		
		this.left.addEvent('click', function(e, this_btn) {
			e.stop();
			
			if(this_btn.hasClass('disabled') || !this.previous || this.scroll.busy) return;
			
			
			if(this.right.hasClass('disabled'))
				this.right.removeClass('disabled');
				
			this.next = this.curr;
			this.curr = this.previous.setStyle('display', 'block');
			this.previous = this.curr.getPrevious();
			var pos = this.next.getPosition(this.container);
			this.scroll.set(pos.x, pos.y);
        
			this.scroll.toElement( this.curr );
        
			if(!$chk(this.previous))
				this_btn.addClass('disabled');
        
			this.pagenum--;
        
		}.bindWithEvent(this, this.left));
		
		
		this.right.addEvent('click', function(e, this_btn) {
			e.stop();
			
			
			
			if( this_btn.hasClass('disabled') || !this.next || this.scroll.busy) return;
			
			if(this.left.hasClass('disabled'))
				this.left.removeClass('disabled');
			
			this.previous = this.curr;
			this.curr = this.next.setStyle('display', 'block');
			this.next = this.curr.getNext();
			
			this.scroll.toElement( this.curr );
			
			var dis = false;
			
			if(!$chk(this.next) ){
				this_btn.addClass('disabled');
				dis = true;
			}
			
			this.pagenum++;
			
			if(this.getAjax && dis )
				this.request.send('p='+ (this.cols_len + 1) );
        	
		}.bindWithEvent(this, this.right));
		
	}	
	
});
