/*
*  scroll.js
* ----------------------------------------------------------------------------
*  Copyright © 1995-2003 Intesys S.r.l. Verona (I). All rights reserved.
* ----------------------------------------------------------------------------
*  Data e versione:
*	06/02/2003 - 1.1
* ----------------------------------------------------------------------------
*  Descrizione:
*	Libreria per la gestione di layer scrollabili.
* ----------------------------------------------------------------------------
*/

var scrollLayers = []
var currentScrollLayer
var SCROLLTIMER = false


function getLayer(name, path) {
	if (document.getElementById) return document.getElementById(name)
	else if (document.all) return document.all[name]
	else if (document.layers) return path ? path.layers[name] : document.layers[name]
}


// Metodi di "layer" definiti dalla funzione "initLayer(layer)"
function getLayerWidth() {
	if (document.layers) return this.clip.width
	else if (typeof this.offsetWidth != 'undefined') return this.offsetWidth
	else return this.style.pixelWidth
}


function getLayerHeight() {
	if (document.layers) return this.clip.height
	else if (typeof this.offsetHeight != 'undefined') return this.offsetHeight
	else return this.style.pixelHeight
}


function getLeftPos() {
	return document.layers ? this.left : this.offsetLeft
}


function getTopPos() {
	return document.layers ? this.top : this.offsetTop
}


function setLeftPos(left) {
	if (document.layers) this.left = left
	else this.style.left = left
}


function setTopPos(top) {
	if (document.layers) this.top = top
	else this.style.top = top
}


function initLayer(layer) {
	layer.getLeftPos = getLeftPos
	layer.setLeftPos = setLeftPos
	layer.getLayerWidth = getLayerWidth
	layer.getTopPos = getTopPos
	layer.setTopPos = setTopPos
	layer.getLayerHeight = getLayerHeight
}


function setCurrentScrollLayer(name) {
	currentScrollLayer = scrollLayers[name]
}


function initHorizontalScrolling(name, boxName, horizontalSpeed, endLeftScrollFunction, endRightScrollFunction) {
	var parentBox = getLayer(boxName)
	currentScrollLayer = getLayer(name, parentBox.document)
	initLayer(parentBox)
	initLayer(currentScrollLayer)
	scrollLayers[name] = currentScrollLayer
	
	var maxScroll = currentScrollLayer.getLayerWidth() - parentBox.getLayerWidth()
	currentScrollLayer.leftStop = maxScroll > 0 ? -maxScroll : 0
	currentScrollLayer.rightStop = 0
	currentScrollLayer.horizontalPos = 0
	currentScrollLayer.horizontalSpeed = horizontalSpeed || 4
	currentScrollLayer.endLeftScrollFunction = endLeftScrollFunction
	currentScrollLayer.endRightScrollFunction = endRightScrollFunction
}


function initVerticalScrolling(name, boxName, verticalSpeed, endTopScrollFunction, endBottomScrollFunction) {
	var parentBox = getLayer(boxName)
	currentScrollLayer = getLayer(name, parentBox.document)
	initLayer(parentBox)
	initLayer(currentScrollLayer)
	scrollLayers[name] = currentScrollLayer

	var maxScroll = currentScrollLayer.getLayerHeight() - parentBox.getLayerHeight()
	currentScrollLayer.topStop = maxScroll > 0 ? -maxScroll : 0
	currentScrollLayer.bottomStop = 0
	currentScrollLayer.verticalPos = 0
	currentScrollLayer.verticalSpeed = verticalSpeed || 4
	currentScrollLayer.endTopScrollFunction = endTopScrollFunction
	currentScrollLayer.endBottomScrollFunction = endBottomScrollFunction
}


function stopScroll() {
	if (SCROLLTIMER) {
		clearInterval(SCROLLTIMER)
		SCROLLTIMER = false
	}
}


function startHorizontalScroll(move) {
	if (!SCROLLTIMER && typeof(currentScrollLayer) == 'object') {
		var delta = move * currentScrollLayer.horizontalSpeed
		SCROLLTIMER = setInterval('horizontalScroll('+delta+')', 50)
	}
}


function startVerticalScroll(move) {
	if (!SCROLLTIMER && typeof(currentScrollLayer) == 'object') {
		var delta = move * currentScrollLayer.verticalSpeed
		SCROLLTIMER = setInterval('verticalScroll('+delta+')', 50)
	}
}


function horizontalScroll(delta) {
	with (currentScrollLayer) {
		var oldPos = horizontalPos
		horizontalPos += delta
		if (horizontalPos < leftStop) horizontalPos = leftStop
		if (horizontalPos > rightStop) horizontalPos = rightStop
		if (oldPos != horizontalPos) {
			setLeftPos(horizontalPos)
		} else {
			stopScroll()
			if (horizontalPos == leftStop  && endRightScrollFunction) endRightScrollFunction()
			if (horizontalPos == rightStop && endLeftScrollFunction) endLeftScrollFunction()
		}
	}
}

function verticalScroll(delta) {
	with (currentScrollLayer) {
		var oldPos = verticalPos
		verticalPos += delta
		if (verticalPos < topStop) verticalPos = topStop
		if (verticalPos > bottomStop) verticalPos = bottomStop
		if (oldPos != verticalPos) setTopPos(verticalPos)
		else {
			stopScroll()
			if (verticalPos == topStop    && endBottomScrollFunction) endBottomScrollFunction()
			if (verticalPos == bottomStop && endTopScrollFunction) endTopScrollFunction()
		}
	}
}


function horizontalScrollTo(newPosition) {
	if (typeof(currentScrollLayer) != 'object') return false
	clearInterval(SCROLLTIMER)
	with (currentScrollLayer) {
		if (newPosition == 'left')	{
			newPosition = leftStop
		} else if (newPosition == 'right')	{
			newPosition = rightStop
		}
		horizontalPos = newPosition
		setLeftPos(newPosition)
		if (horizontalPos == leftStop  && endRightScrollFunction) endRightScrollFunction()
		if (horizontalPos == rightStop && endLeftScrollFunction) endLeftScrollFunction()
	}
	return false
}


function verticalScrollTo(newPosition) {
	if (typeof(currentScrollLayer) != 'object') return false
	clearInterval(SCROLLTIMER)
	with (currentScrollLayer) {
		if (newPosition == 'top')	{
			newPosition = topStop
		} else if (newPosition == 'bottom')	{
			newPosition = bottomStop
		} 
		verticalPos = newPosition
		setTopPos(newPosition)
		if (verticalPos == topStop    && endBottomScrollFunction) endBottomScrollFunction()
		if (verticalPos == bottomStop && endTopScrollFunction) endTopScrollFunction()
	}
	return false
}
