// Helper DHTML library
// Scroll Device
// Code: Andrey Potekhin
// (c) Taitl

// Prerequisites
if (!Debug)
	alert('Assertion fault in scrolldevice.js: Debug object undefined. Please include debug.js.')

if (!is)
	alert('Assertion fault in scrolldevice.js: "is" object undefined.')

if (scrollDevice)
	alert('Assertion fault in scrolldevice.js: scrollDevice object already defined. Please check includes.')

// ScrollDevice

function ScrollDevice(nameOfObj, beginScrolling, endScrolling, onetime)
{
	Debug.assert(nameOfObj, 'ScrollDevice(): You have to specify the name object (same as the name of variable on the left side of "new".')
	
	this.obj = nameOfObj
	
	if(beginScrolling)
		this.onBeginScrolling=beginScrolling
	else
		this.onBeginScrolling=function(){}
		
	if(endScrolling)
		this.onEndScrolling=endScrolling
	else
		this.onEndScrolling=function(){}
	
	if(onetime)
		this.oneTime = onetime
	else
		this.oneTime=0		
	
	//var ScrollDevice = new ScrollDevice()
	this.oldOffset = null
	this.scrollVelocity = null

	this.mac = navigator.appVersion.indexOf("Mac") != -1

	this.windowWidth = function()
	{
		return is.ns? window.innerWidth-16 : 
			(this.mac ? document.body.scrollWidth-20 : document.body.offsetWidth-20)
	}
	this.scrollWidth = function()
	{
		return is.ns? document.width : 
			(this.mac ? document.body.offsetWidth-20 : document.body.scrollWidth-20)
	}
	this.windowXOffset = function()
	{
		return is.ns? window.pageXOffset : document.body.scrollLeft
	}
	this.windowScroll = WindowScroll
	this.windowScrollRel = function(x, dx, delay){
		this.windowScroll(this.windowXOffset() + x, dx, delay)		
	}
}

function WindowScroll(x, dx, delay)
{
	mainOffset = this.windowXOffset()
	movingRight = x > mainOffset
		
	if (this.oldOffset == null)
	{
		this.oldOffset = x
		velocity = dx/(Math.max(delay, 1))
		ddx = (2*velocity)/delay
		scrollVelocity = 0
	}
		
	scrollVelocity = dx
		
	if (movingRight){
		mainOffset += scrollVelocity//dx
		if (mainOffset > x)
			mainOffset = x
	}
	else{
		mainOffset -= scrollVelocity//dx
		if (mainOffset < x)
			mainOffset = x
	}
		
	scroll(mainOffset, 0)

	if (movingRight && mainOffset < x && mainOffset < this.scrollWidth() - this.windowWidth()
			|| !movingRight && mainOffset > x){
		setTimeout(this.obj + ".windowScroll(" + x + "," + dx + "," + delay + ")", delay)
	}
	else{
		eval(this.onEndScrolling)
		oldOffset = null
	}
}	

var scrollDevice = new ScrollDevice('scrollDevice')
