
var scrollLayers = null;
var numScrollLayers = 0;

function registerScrollLayer(scrollLayerName, clipLayerName, upButtonName, downButtonName, leftButtonName, rightButtonName) 
{
	var index;
	var layerExists = false;
	
	if (scrollLayers == null) {
		scrollLayers = new Array();
		numScrollLayers = 0;
	}
     	 	 
	scrollLayer = new Object();
	scrollLayer.name = scrollLayerName;
	scrollLayer.clipLayerName   = clipLayerName;
	scrollLayer.leftButtonName  = leftButtonName;
	scrollLayer.rightButtonName = rightButtonName;
	scrollLayer.upButtonName    = upButtonName;
	scrollLayer.downButtonName  = downButtonName;
	scrollLayer.stopFlag  = false;
	
	scrollLayer.clipLeft = getIntDIVProp(clipLayerName,'left');
	scrollLayer.clipWidth     = getIntDIVProp(clipLayerName,'width');
	scrollLayer.clipTop       = getIntDIVProp(clipLayerName,'top');
	scrollLayer.clipHeight = getIntDIVProp(clipLayerName,'height');
	scrollLayer.left    = getIntDIVProp(scrollLayerName,'left');
	scrollLayer.width   = getIntDIVProp(scrollLayerName,'width');
	scrollLayer.top     = getIntDIVProp(scrollLayerName,'top');
	scrollLayer.height  = getIntDIVProp(scrollLayerName,'height');
	if (scrollLayer.width == 0) {
		scrollLayer.width = 1000000;
	}
	if (scrollLayer.height == 0) {
		scrollLayer.height = 1000000;
	}
/*
	alert(scrollLayer.name + ":" + scrollLayer.clipLayerName + ":" 
	+ scrollLayer.clipLeft + ":"+ scrollLayer.clipTop + ":"
	+ scrollLayer.clipWidth + ":"+ scrollLayer.clipHeight + ":"
	+ scrollLayer.left + ":"+ scrollLayer.top + ":"
	+ scrollLayer.width + ":"+ scrollLayer.height);
*/
	for(index = 0; index < numScrollLayers; index++) {
		if (scrollLayers[index].name == scrollLayerName) {
			scrollLayers[index] = scrollLayer;
			layerExists = true;
			break;
		}
	}
	
	if (layerExists == false) {
		scrollLayers[numScrollLayers] = scrollLayer;
		numScrollLayers = numScrollLayers + 1;
	}
	
	return scrollLayer;
}

function resetScrollLayer(scrollLayerName)
{
	scrollLayer = getScrollLayer(scrollLayerName);
	if (scrollLayer) {
		setDIVProp(scrollLayerName,'left',scrollLayer.clipLeft);
		scrollLayer.left = scrollLayer.clipLeft;
		clipLayer(scrollLayerName);
	}
}

function presetScrollPosition()
{
   index = document.URL.lastIndexOf("#");
   if (index >= 0) {
		scrollIndex = document.URL.indexOf("_",index+1);
		if (scrollIndex >= 0) {
			scrollLayerName = document.URL.substring(index+1, scrollIndex);
			scrollDirIndex = document.URL.indexOf("_",scrollIndex+1);
			if (scrollDirIndex >= 0) {
				scrollDirName = document.URL.substring(scrollIndex+1, scrollDirIndex);
				scrollDirValueString = document.URL.substring(scrollDirIndex+1, document.URL.length);
				scrollDirValue = parseInt(scrollDirValueString); 
				scrollLayer = getScrollLayer(scrollLayerName);
				if (scrollLayer) {
					setDIVProp(scrollLayerName,scrollDirName,scrollDirValue);
					if (scrollDirName == "left") {
						scrollLayer.left = scrollDirValue;
					}
					if (scrollDirName == "top") {
						scrollLayer.top = scrollDirValue;
					}
					clipLayer(scrollLayerName);
				}
			}
		}
	}
}

function getScrollLayer(scrollLayerName)
{
	scrollLayer = null;
	for(index = 0; index < numScrollLayers; index++) {
		if (scrollLayers[index].name == scrollLayerName) {
			 scrollLayer = scrollLayers[index];
			 break;	
		}
	}
	
	return scrollLayer;
}

function clipLayer(scrollLayerName)
{
	scrollLayer = getScrollLayer(scrollLayerName);

	if (scrollLayer) {
		clipLeft     = scrollLayer.clipLeft;
		clipRight     = scrollLayer.clipLeft + scrollLayer.clipWidth - 1;
		clipTop = scrollLayer.clipTop;
		clipBottom = scrollLayer.clipTop + scrollLayer.clipHeight - 1;
	
		scrollLeft     = scrollLayer.left;
		scrollRight     = scrollLayer.left + scrollLayer.width - 1;
		scrollTop = scrollLayer.top;
		scrollBottom = scrollLayer.top + scrollLayer.height - 1;
	
		clipRect = 'rect(' + (clipTop - scrollTop) + 'px ' + (clipRight - scrollLeft) + 
		  'px ' + (clipBottom - scrollTop) + 'px ' + (clipLeft - scrollLeft) + 'px)';
		
		setDIVProp(scrollLayerName,'clip',clipRect);
		setDIVProp(scrollLayerName,'clip.left',(clipLeft - scrollLeft));
		setDIVProp(scrollLayerName,'clip.width',(clipRight - clipLeft+1));
		setDIVProp(scrollLayerName,'clip.top',(clipTop - scrollTop));
		setDIVProp(scrollLayerName,'clip.height',(clipBottom - clipTop+1));
	}
}

// vertical scroll funcs

function setVerticalScrollButtonVisibility(scrollLayerName)
{
	scrollLayer = getScrollLayer(scrollLayerName);

	if (scrollLayer) {
		clipTop = scrollLayer.clipTop;
		clipBottom = clipTop +  + scrollLayer.clipHeight - 1;
		scrollTop     = scrollLayer.top;
		scrollBottom  = scrollLayer.top + scrollLayer.height - 1;
	
		MM_showHideLayers(scrollLayer.upButtonName,'','hide');
		MM_showHideLayers(scrollLayer.downButtonName,'','hide');
		if (scrollBottom > clipBottom) {
			MM_showHideLayers(scrollLayer.downButtonName,'','show');
		}
		if (scrollTop < clipTop) {
			MM_showHideLayers(scrollLayer.upButtonName,'','show');
		}
	}
}

function scrollUp(scrollLayerName, increment, incrementAcceleration, incrementLimit)
{
	scrollLayer = getScrollLayer(scrollLayerName);

	if (scrollLayer) {
		clipTop = scrollLayer.clipTop;
		clipBottom = clipTop +  + scrollLayer.clipHeight - 1;
		scrollTop     = scrollLayer.top;
		scrollBottom  = scrollLayer.top + scrollLayer.height - 1;
	
		canScroll = false;
	
		actualIncrement = increment;
		if (increment > incrementLimit && incrementLimit > 0) {
			actualIncrement = incrementLimit;
			incrementLimit = 0;
		}
	
		if (scrollLayer.stopFlag == false) {
			if (scrollBottom > clipBottom) {
				scrollTop -= actualIncrement;
				if (scrollBottom < clipBottom) {
					scrollTop = clipBottom - 1 - scrollBottom;
				}
				scrollLayer.top = scrollTop;
				//setDIVProp(scrollLayerName,'top', scrollTop+'px');
				setDIVProp(scrollLayerName,'top', scrollTop);
				clipLayer(scrollLayerName);
				
				//alert("scrollTop: " + scrollTop);
				canScroll = true;
			}   
		}
		
		if (canScroll == true) {
			if (incrementLimit == -1) {
				setTimeout('scrollUp(\'' + scrollLayerName + '\', ' + (increment+incrementAcceleration) + ', ' + incrementAcceleration + ', 0)', 50);
			} else if (incrementLimit > 0) {
				setTimeout('scrollUp(\'' + scrollLayerName + '\', ' + (increment+incrementAcceleration) + ', ' + incrementAcceleration + ', ' + (incrementLimit-increment) + ')', 50);
			} else {
				scrollLayer.stopFlag = false;
				setVerticalScrollButtonVisibility(scrollLayerName);
			}
		} else {
			scrollLayer.stopFlag = false;
			setVerticalScrollButtonVisibility(scrollLayerName);
		}
    }
}

function scrollDown(scrollLayerName, increment, incrementAcceleration, incrementLimit)
{
	scrollLayer = getScrollLayer(scrollLayerName);

	if (scrollLayer) {
		clipTop = scrollLayer.clipTop;
		scrollTop = scrollLayer.top;

		canScroll = false;
	
		actualIncrement = increment;
		if (increment > incrementLimit && incrementLimit > 0) {
			actualIncrement = incrementLimit;
			incrementLimit = 0;
		}
		if (scrollLayer.stopFlag == false) {
			if (scrollTop < clipTop) {
				scrollTop += actualIncrement;
				if (scrollTop > clipTop) {
					scrollTop = clipTop;
				}
				scrollLayer.top = scrollTop;
				//setDIVProp(scrollLayerName,'top', scrollTop+'px');
				setDIVProp(scrollLayerName,'top', scrollTop);
				clipLayer(scrollLayerName);
	
				canScroll = true;
			}   
		}
		
		if (canScroll == true) {
			if (incrementLimit == -1) {
				setTimeout('scrollDown(\'' + scrollLayerName + '\', ' + (increment+incrementAcceleration) + ', ' + incrementAcceleration + ', 0)', 50);
			} else if (incrementLimit > 0) {
				setTimeout('scrollDown(\'' + scrollLayerName + '\', ' + (increment+incrementAcceleration) + ', ' + incrementAcceleration + ', ' + (incrementLimit-increment) + ')', 50);
			} else {
				scrollLayer.stopFlag = false;
				setVerticalScrollButtonVisibility(scrollLayerName);
			}
		} else {
			scrollLayer.stopFlag = false;
			setVerticalScrollButtonVisibility(scrollLayerName);
		}
	}
}

function startScrollUp(scrollLayerName, startIncrement, incrementAcceleration, incrementLimit)
{
	scrollLayer = getScrollLayer(scrollLayerName);

	if (scrollLayer) {
		scrollLayer.stopFlag = false;
	}
	setTimeout('scrollUp(\'' + scrollLayerName + '\', ' + startIncrement+ ', ' + incrementAcceleration + ', ' + incrementLimit + ')', 50);
}

function startScrollDown(scrollLayerName, startIncrement, incrementAcceleration, incrementLimit)
{
	scrollLayer = getScrollLayer(scrollLayerName);

	if (scrollLayer) {
		scrollLayer.stopFlag = false;
	}
	setTimeout('scrollDown(\'' + scrollLayerName + '\', ' + startIncrement+ ', ' + incrementAcceleration + ', ' + incrementLimit + ')', 50);
}



// horizontal scroll funcs
function setHorizontalScrollButtonVisibility(scrollLayerName)
{
	scrollLayer = getScrollLayer(scrollLayerName);

	if (scrollLayer) {
		clipLeft = scrollLayer.clipLeft;
		clipRight = clipLeft +  + scrollLayer.clipWidth - 1;
		scrollLeft     = scrollLayer.left;
		scrollRight  = scrollLayer.left + scrollLayer.width - 1;
	
		MM_showHideLayers(scrollLayer.leftButtonName,'','hide');
		MM_showHideLayers(scrollLayer.rightButtonName,'','hide');
		if (scrollRight > clipRight) {
			MM_showHideLayers(scrollLayer.rightButtonName,'','show');
		}
		if (scrollLeft < clipLeft) {
			MM_showHideLayers(scrollLayer.leftButtonName,'','show');
		}
	}
}

function scrollLeft1(scrollLayerName, increment, incrementAcceleration, incrementLimit)
{
	scrollLayer = getScrollLayer(scrollLayerName);

	if (scrollLayer) {
		clipLeft = scrollLayer.clipLeft;
		clipRight = clipLeft + scrollLayer.clipWidth - 1;
		scrollLeft     = scrollLayer.left;
		scrollRight  = scrollLayer.left + scrollLayer.width - 1;

		actualIncrement = increment;
		if (increment > incrementLimit && incrementLimit > 0) {
			actualIncrement = incrementLimit;
			incrementLimit = 0;
		}
		
		canScroll = false;
		if (scrollLayer.stopFlag == false) {
			if (scrollRight > clipRight) {
				scrollLeft -= actualIncrement;
				if (scrollRight < clipRight) {
					scrollLeft = clipRight - 1 - scrollRight;
				}
				scrollLayer.left = scrollLeft;
				setDIVProp(scrollLayerName,'left', scrollLeft);
				clipLayer(scrollLayerName);
	
				canScroll = true;
			}   
		}
		
		if (canScroll == true) {
			if (incrementLimit == -1) {
				setTimeout('scrollLeft1(\'' + scrollLayerName + '\', ' + (increment+incrementAcceleration) + ', ' + incrementAcceleration + ', -1)', 50);
			} else if (incrementLimit > 0) {
				setTimeout('scrollLeft1(\'' + scrollLayerName + '\', ' + (increment+incrementAcceleration) + ', ' + incrementAcceleration + ', ' + (incrementLimit-increment) + ')', 50);
			} else {
				scrollLayer.stopFlag = false;
				setHorizontalScrollButtonVisibility(scrollLayerName);
				
			}
		} else {
			scrollLayer.stopFlag = false;
			setHorizontalScrollButtonVisibility(scrollLayerName);
		}
    }
}

function scrollRight1(scrollLayerName, increment, incrementAcceleration, incrementLimit)
{
	scrollLayer = getScrollLayer(scrollLayerName);

	if (scrollLayer) {
		clipLeft = scrollLayer.clipLeft;
		scrollLeft     = scrollLayer.left;

		canScroll = false;
	
		actualIncrement = increment;
		if (increment > incrementLimit && incrementLimit > 0) {
			actualIncrement = incrementLimit;
			incrementLimit = 0;
		}
		
		canScroll = false;
		if (scrollLayer.stopFlag == false) {
			if (scrollLeft < clipLeft) {
				scrollLeft += actualIncrement;
				if (scrollLeft > clipLeft) {
					scrollLeft = 0;
				}
				scrollLayer.left = scrollLeft;
				//setDIVProp(scrollLayerName,'left', scrollLeft+'px');
				setDIVProp(scrollLayerName,'left', scrollLeft);
				clipLayer(scrollLayerName);
	
				canScroll = true;
			}   
		}
		
		if (canScroll == true) {
			
			if (incrementLimit == -1) {
				setTimeout('scrollRight1(\'' + scrollLayerName + '\', ' + (increment+incrementAcceleration) + ', ' + incrementAcceleration + ', 0)', 50);
			} else if (incrementLimit > 0) {
				setTimeout('scrollRight1(\'' + scrollLayerName + '\', ' + (increment+incrementAcceleration) + ', ' + incrementAcceleration + ', ' + (incrementLimit-increment) + ')', 50);
			} else {
				scrollLayer.stopFlag = false;
				setHorizontalScrollButtonVisibility(scrollLayerName);
			}
		} else {
			scrollLayer.stopFlag = false;
			setHorizontalScrollButtonVisibility(scrollLayerName);
		}
	}
}

function startScrollLeft(scrollLayerName, startIncrement, incrementAcceleration, incrementLimit)
{
	scrollLayer = getScrollLayer(scrollLayerName);

	if (scrollLayer) {
		scrollLayer.stopFlag = false;
	}
    setTimeout('scrollLeft1(\'' + scrollLayerName + '\', ' + startIncrement+ ', ' + incrementAcceleration + ', ' + incrementLimit + ')', 50);
}

function startScrollRight(scrollLayerName, startIncrement, incrementAcceleration, incrementLimit)
{
	scrollLayer = getScrollLayer(scrollLayerName);

	if (scrollLayer) {
		scrollLayer.stopFlag = false;
	}
     setTimeout('scrollRight1(\'' + scrollLayerName + '\', ' + startIncrement+ ', ' + incrementAcceleration + ', ' + incrementLimit + ')', 50);
}
