var ByMarquee = new Class({
	
	Implements: Options,
	
	options: {
		mode: 'horizontal',
		delay: 4000,
		duration: 2000,
		playButtonId: 'marquee_play_button',
		playLabel: '[]',
		stopLabel: '>'
	},
	
	initialize: function(container, options){
		
		if(!$(container))
			return;
		
		this.setOptions(options);

		this.container = $(container);
		this.marquee_play_button = $(this.options.playButtonId);

		this.dimensions = this.container.getStyles('width', 'height');
		this.elements = this.container.getChildren();
		
		this.total_elements = this.elements.length;
		
		if(this.total_elements < 2)
			return;
			
		this.current_element = 0;
		
		this.container.adopt(this.elements[0]);

		this.start();
	},
	
	startStop: function(){
		if(this.interval_id)
			this.stop();
		else
			this.start();
	},
	
	start: function(){
		if(!this.interval_id)
		{
			this.interval_id = this.moveForward.periodical(this.options.delay, this, true);
			if(this.marquee_play_button)
				this.marquee_play_button.innerHTML = this.options.playLabel;
		}
	},
	
	stop: function(){
		$clear(this.interval_id);
		this.interval_id = null;
		if(this.marquee_play_button)
				this.marquee_play_button.innerHTML = this.options.stopLabel;
	},
	
	moveForward: function(keep_interval){
		
		if(!keep_interval)
			this.stop();
		
		var current_element = this.elements[this.current_element];
		this.current_element++;
		
		if(this.current_element == this.total_elements)
			this.current_element = 0;
			
		var next_element = this.elements[this.current_element];
		current_element.setStyle('margin-left', 0);
		next_element.setStyle('margin-left', this.dimensions.width.toInt());
		this.container.adopt(next_element);
		
		current_element.get('tween', {
			property: 'margin-left',
			duration: this.options.duration
		}).start(-this.dimensions.width.toInt());
		
		next_element.get('tween', {
			property: 'margin-left',
			duration: this.options.duration
		}).start(0);
	},
	
	moveBackward: function(keep_interval){
		
		if(!keep_interval)
			this.stop();
		
		var current_element = this.elements[this.current_element];
		this.current_element--;

		if(this.current_element < 0)
			this.current_element = this.total_elements - 1;

		var prev_element = this.elements[this.current_element];

		current_element.setStyle('margin-left', 0);
		prev_element.setStyle('margin-left', -this.dimensions.width.toInt());
		this.container.adopt(prev_element);
		
		current_element.get('tween', {
			property: 'margin-left',
			duration: this.options.duration
		}).start(this.dimensions.width.toInt());
		
		prev_element.get('tween', {
			property: 'margin-left',
			duration: this.options.duration
		}).start(0);
	}
});


