var SlideNClick = new Activa.Class({
	fx: null,
	container: null,
	slider: null,
	clone: null,
	clone_fx: null,
	kill: false,
	moving1: false,
	moving2: false,
	timer2: null,
	timer3: null,
	transition: false,
	auto_scroll: false,
	fps: 40,
	options: {
	}, 
	
	init: function init(container, slider, options) {
		this.container = did(container);
		this.slider = did(slider);
		this.setOptions(options);
		this.clone = this.slider.cloneNode(true);
		this.clone.id = 'slider_clone';
		this.container.appendChild(this.clone, this.options);
		this.fx = new Fx.Move(this.slider);
		this.clone_fx = new Fx.Move(this.clone, this.options);
		this.width = getStyle(this.slider, 'width');
		this.container_width = getStyle(this.container, 'width');
		
		this.clone.style.left = -this.width+'px';
	},
	setOptions: function setOptions(options) {
		options = options || {};
		for ( var k in this.options ) {
			this.options[k] = (k in options) ? options[k] : this.options[k];
		}
	},
	calcDur: function calc_duration(move, speed) {
		/*
		var duration = 100;
		return duration;
		*/
		return 100;
	},
	startScroll: function start_scroll(move) {
		this.auto_scroll = move;
		this.move(move);
	},
	move: function move(move, mouse) {
		/*
		if ( this.moving1 || this.moving2 ) {
			return;
		}
		*/
		if ( this.timer3 ) { 
			clearTimeout(this.timer3);
		}
		if ( mouse ) {
			this.kill = false;
			clearTimeout(this.timer2);
		}
		this.fx.options.duration = this.calcDur(move);
		this.clone_fx.options.duration = this.calcDur(move);
		var cur_x = getStyle(this.slider, 'left');
		this.fx.options.onComplete = function() {
			this.moving1 = false;
			this.moveOffScreen(this.slider, this.clone);
		}.bind(this);
		this.moving1 = true;
		this.fx.start(cur_x + move);
		
		var cur_x = getStyle(this.clone, 'left');
		this.clone_fx.options.onComplete = function() {
			this.moving2 = false;
			this.moveOffScreen(this.clone, this.slider);
			if ( !this.kill ) {
				this.move(move);
			}
		}.bind(this);
		this.moving2 = true;
		this.clone_fx.start(cur_x + move);
	},
	stop: function stop() {
		if ( this.timer2 ) {
			clearTimeout(this.timer2);
		}
		this.fx.stop();
		this.clone_fx.stop();
		this.kill = true;
		this.moving1 = false;
		this.moving2 = false;
		this.timer2 = setTimeout(function() {
			this.kill = false;
		}.bind(this), 1000);
		if ( this.auto_scroll ) {
			this.kill = false;
			this.move(this.auto_scroll);
		}
	},
	moveOffScreen: function move_off_screen(obj, ref) {
		var cur_x = getStyle(obj, 'left');
		// right offset
		var offset = (this.container_width + cur_x) - this.container_width;
		//console.log(obj.id, offset);
		if ( offset > this.width ) {
			var left = getStyle(ref, 'left');
			obj.style.left = (left-this.width)+'px';
		} else if ( offset < 0-this.width ) {
			var left = getStyle(ref, 'left');
			//console.log(obj.style.left);
			obj.style.left = (left+this.width)+'px';
		}
	},
	getOffsetWidth: function(obj) {
		var offset = getStyle(obj, 'left');
		return offset + this.width;
	}
});
