JABA.trainline = {
    Ticker : new Class({
		isPlay : false,
		
		setOptions: function(options) {
			this.options = Object.extend({
				autoscroll:true,
				speed: 1500,
				delay: 5000,
				direction: 'vertical',
				onComplete: Class.empty,
				onStart: Class.empty
			}, options || {});
		},
		initialize: function(el,options){

			this.setOptions(options);
			this.el = $(el);
			this.items = this.el.getElements('li');

			this.current = -1;
			this.fx = new Fx.Morph(this.el,{duration:350,onComplete:function() {
				var removedElement = this.el.getElements('.slideitmoo_element')[0].dispose();
				this.el.adopt( removedElement ).set({
					'styles':{
						'margin-left':null
					}
				});
				this.isPlay = false;
			}.bind(this)});

			this.fxPrev = new Fx.Morph(this.el,{duration:350,onComplete:function() {
				this.el.getElements('.slideitmoo_element')[0].set({
					'styles':{
						'margin-left' : null
					}
				});
				this.el.set({
					'styles':{
						'margin-left': null
					}
				});
				this.isPlay = false;
			}.bind(this)});
            if( this.options.autoscroll ){
	        	this.next();
			}
		},

		next: function() { 

            if( !this.isPlay ) {
				this.isPlay = true;
				this.current++;
				if (this.current >= this.items.length) {
					this.current = 0;
				}
				var size = this.items[this.current].getSize();
				this.fx.start({
					'margin-left': -size.x
				});
				if ( this.options.autoscroll ) { 

					mytimer = this.next.bind(this).delay(this.options.delay+this.options.speed);
				}                  
			}
		},
		prev: function() {
            if( !this.isPlay ) {
				this.isPlay = true;			
				this.current--;
				if (this.current <= 0) {
					this.current = this.items.length - 1;
				}			
				var size = this.el.getElements('.slideitmoo_element')[this.items.length-1].getSize();
				var removedElement = this.el.getElements('.slideitmoo_element')[this.items.length-1].dispose();

				var newElement = removedElement.inject(this.el,'top');
				newElement.set({
					'styles':{
						'margin-left': -size.x
					}
				});
				this.fxPrev.start({
					'margin-left': size.x
				});
        	}
		},

		pause: function() {
			$clear(mytimer);
			mytimer = null;
		},
		resume: function() {
			if (mytimer == null) {
				this.next();
			}
		}
	})
           
}

JABA.addInitialiser( function(){ 
		var Div = $('trainline');
		Div.set({
			'html' : '<img src="'+DIR_STATIC_ROOT+'/images/loader.gif" >'
		});
		JsHttpRequest.query(
		 	'http://www.social.jaba.ru/include/ajax/train_line.php',
			{},
			function(result, errors) { 
				if (!result) {
		 			this.msgErrorBox.alert('Error','Please try again later');           
				} else {
					if (result.bStateError) {
						this.msgErrorBox.alert('Error','ololo');           
					} else {
						Div.set('html',  result.sText);
						if( result.sText != '' ){
							var newline = new JABA.trainline.Ticker('trainimagelist');
						}
					}      
				}
			},
			true
		);

});
