var Scroller = {}

Scroller.init = function(doTrigger) {
	this.content = document.getElementById('content');
	this.bar = document.getElementById('bar');
	this.handle = document.getElementById('handle');
	this.width = document.getElementById('pos').offsetLeft;
	this.screenWidth = document.getElementById('screenWidth').offsetLeft;
	this.visibleWidth = this.width - this.screenWidth;
	this.pos = 0;
	this.timeoutId = -1;
	this.doTrigger = doTrigger;
	
	this.handleWidth = this.handle.offsetWidth;
	this.handleOffset = this.handleWidth / 2;
	this.targetWidth = 110;
	this.scrollWidth = this.screenWidth - ((this.handleWidth + this.targetWidth) / 2); 
	if (this.width > this.bar.offsetWidth) {
		this.handle.style.visibility = 'visible';
		document.getElementById('target').src = document.getElementById('target').src.replace('nothing', 'target_h');
		this.bar.onmousedown = this.slideTo;
		this.handle.onmousedown = this.startDrag;
		this.handle.ondragstart = function() { return false; };
	} else {
		document.getElementById('bar').style.cursor = 'default';
	}
	if (!this.handle.style.visibility) this.doTrigger = 0;
	
	if (this.doTrigger) {
		this.targetX = 100 + parseInt(0.8 * Math.random() * (this.scrollWidth - 100), 10);
		with (Scroller) {
			var offset = targetX;
			Scroller.pos += offset;
			handle.style.left = Scroller.pos;
			moveContent();
			reset();
		}
	}
}

Scroller.reset = function() {
	with (Scroller) {
		targetX = 0; 
		var startTime = 1500 + 250 * parseInt(10 * Math.random(), 10);
		timeoutId = setTimeout('Scroller.slide()', startTime);
	}
}

Scroller.slideTo = function(e) {
	clearTimeout(Scroller.timeoutId);
	if (!e) e = window.event;
	var mouseX = e.clientX ? e.clientX : e.layerX;
	with (Scroller) {
		targetX = mouseX - handleOffset;
		if (targetX > scrollWidth) targetX = scrollWidth;
		if (targetX < 0) targetX = 0;
		Scroller.targetX = targetX;
		slide();
	}
}

Scroller.slide = function() {
	clearTimeout(Scroller.timeoutId);
	with (Scroller) {
		var offset = targetX - pos;
		if (offset != 0) {
			if (Math.abs(offset) >= 0.1) offset *= 0.1; 
			Scroller.pos += offset;
			handle.style.left = Scroller.pos;
			moveContent();
			timeoutId = setTimeout('Scroller.slide()', 40);
		}	
	}
}

Scroller.moveContent = function() {
	with (Scroller) {
		var contentX = parseInt(-1 * (pos / scrollWidth) * visibleWidth);
		document.getElementById('content').style.left = contentX;
	}
}

Scroller.startDrag = function(e) {
	clearTimeout(Scroller.timeoutId);
	if (!e) e = window.event;
	with (Scroller) {
		var ex = e.pageX ? e.pageX : e.x;
		dragLastPos = ex;
		dragStartOffset = ex - parseInt(handle.style.left);
		dragActive = true;
		window.document.onmousemove = doDrag;
		window.document.onmouseup = stopDrag;
	}
	return false;
}

Scroller.doDrag = function(e) {
	if (!e) e = window.event;
	var s  = Scroller;
	var target = e.pageX ? e.pageX : e.x;
	var offset = target - s.dragLastPos;
	var newPos = parseInt(Scroller.handle.style.left) + offset;
	
	if (newPos > s.scrollWidth + s.handleWidth / 2) {
		s.dragLastPos = s.scrollWidth;
	} else if (newPos < s.handleWidth / 2) {
		s.dragLastPos = 0;
	} else {
		s.dragLastPos = target - s.handleWidth / 2;
	}
	
	s.pos = s.dragLastPos;
	s.handle.style.left = s.pos;
	s.moveContent();
	return false;
}

Scroller.stopDrag = function() {
	Scroller.dragActive = false;
	Scroller.pos = parseInt(Scroller.handle.style.left);
	window.document.onmousemove = null;
	window.document.onmouseup   = null;
}
