var speed = 10;	// dinstance devider
var intervalTime = 10;
var type = "horizontal";
var scrollerId = "slider";
var isScrolling = false;
var scrollLeftPosition = 0;
var scrollTopPositionOfThumb = 0;
var scrollTopPositionOfLarge = 0;

function getScrollDiv()
{
	return document.getElementById(scrollerId);
}

function isObj(obj)
{
	if(obj) return true;
	return false;
}

/* Top functions */

function getTop(obj)
{
	return obj.offsetTop;
}

function getOffsetTop(obj)
{
	if(isObj(obj))
	{
		return obj.offsetTop;
	}
	
	return 0;
}

function getScrollHeight()
{
	obj = getScrollDiv();
	
	if(isObj(obj))
	{
		return obj.scrollHeight;
	}
	
	return 0;
}

function getScrollTop()
{
	obj = getScrollDiv();
	
	if(isObj(obj))
	{
		obj.scrollTop;
	}
	
	return 0;
}

function scrollToY(y)
{
	scrollDiv = getScrollDiv();
	
	if(isObj(scrollDiv))
	{
		scrollDiv.scrollTop = y;
	}
}

function getPageHeight()
{
	return window.innerHeight || document.documentElement.clientHeight;
}

/* Left functions */

function getLeft(obj)
{
	return obj.offsetLeft;
}

function debugInfo(obj)
{
	if(isObj(obj))
	{
		var result = " ";
		for (curProperty in obj) 
		{
			result += curProperty + " = ";
		}
		
		alert(result);
	}
}

function getOffsetLeft(obj)
{
	var iReturn = 0;
	
	if(isObj(obj))
	{	
		iReturn = obj.offsetLeft
		
		if (obj.offsetParent) 
			while (obj = obj.offsetParent) 
			{
				iReturn += obj.offsetLeft
			}

		var pageWidth = getPageWidth();
		var scrollWidth = getScrollWidth();
		
		if(iReturn > (scrollWidth - pageWidth )) iReturn = (scrollWidth - (pageWidth - 5));

		if(iReturn < 15) return 0;
		return iReturn - 7; // because the shadow
	}
	
	return 0;
}

function getScrollWidth()
{
	obj = getScrollDiv();
	
	if(isObj(obj))
	{
		return obj.scrollWidth;
	}
	
	return 0;
}

function getScrollWidthById(id)
{
	usedObj = document.getElementById(id);
	
	if(isObj(usedObj))
	{
		return usedObj.scrollWidth;
	}
	
	return 0;
}

function getScrollHeightById(id)
{
	usedObj = document.getElementById(id);
	
	if(isObj(usedObj))
	{
		return usedObj.scrollHeight;
	}
	
	return 0;
}

function getScrollLeft()
{
	return scrollLeftPosition;
}

function scrollObjectToX(scrollObjectId, x)
{
	scrollObject = document.getElementById(scrollObjectId);
	
	if(scrollObject)
	{
		scrollObject.scrollLeft = x;
	}
}

function scrollObjectToY(scrollObjectId, y)
{
	scrollObject = document.getElementById(scrollObjectId);
	
	if(scrollObject)
	{
		scrollObject.scrollTop = y;
	}
}

function scrollToX(x)
{
	if(isScrolling == true)
	{
		scrollDiv = getScrollDiv();
		
		if(isObj(scrollDiv))
		{
			scrollLeftPosition = x;
			scrollDiv.scrollLeft = x;
		}
	}
}

function getPageWidth()
{
	return window.innerWidth || document.body.clientWidth || document.documentElement.clientWidth;
}

function enableScrolling()
{
	if(isScrolling == false)
	{
		isScrolling = true;
		showLockPanel();
	}
}

function disableScrolling()
{
	if(isScrolling == true)
	{
		isScrolling = false;
		hideLockPanel();
	}
}

function getStartValue()
{
	textObject = document.getElementById('textDiv');
	
	if(textObject)
	{
		var ool = getOffsetLeft(textObject);	// the offset van het object
		
		if(ool > 0)
		{
			return ool - 143; // initial value
		}
	}
	
	return 0;
}

/* end functions */

function scrollToLeft(id)
{
	if(isScrolling == false)
	{
		fotoObject = document.getElementById(id);
	
		if(fotoObject)
		{
			var pw = getPageWidth();		// width pagina
			var sw = getScrollWidth();		// width scroll box
			var sl = getScrollLeft();		// huidige positie van de scroll box
			if(id == "textDiv" || id == "0")
				var ool = getStartValue();				// the offset van het object
			else
				var ool = getOffsetLeft(fotoObject);	// the offset van het object
			
			if(isScrolling == false)
			{
				//alert(pw + "  " + sw  + "  " + sl  + "  " + ool);
				
				enableScrolling();
			
				if(ool == sl) 
				{	// stop scrolling
					//alert("scrolling done 000");
					disableScrolling();
				}
				else 
				{
					slide(id, sl);	// start scrolling
				}
			}
		}
	}
	/*else
	{
		alert("is already scrolling");
	}*/
}

function initScroll()
{
	textDivObject = document.getElementById('textDiv');
	
	if(textDivObject)
	{
		scrollToLeft('textDiv');
	}
	else
	{
		hideLockPanel();
	}
}

function slide(id,sl)
{
	if(isScrolling == true)
	{
		fotoObject = document.getElementById(id);
	
		if(fotoObject)
		{
			var pw = getPageWidth();		// width pagina
			var sw = getScrollWidth();		// width scroll box
			//var sl = getScrollLeft();		// huidige positie van de scroll box
			if(id == "textDiv"  || id == "0")
				var ool = getStartValue();				// the offset van het object
			else
				var ool = getOffsetLeft(fotoObject);	// the offset van het object
			
			if(ool == sl) 
			{	// stop scrolling
				//alert("scrolling done");
				disableScrolling();
			}
			else
			{
				if(ool > sl)	
				{	// scroll to the left
					if(sw-ool>pw)
						sl+=Math.ceil((ool-sl)/speed)
					else
						sl+=Math.ceil((ool-sl-(sw-ool))/speed)
				}
				else
				{
					sl = Math.ceil(sl+(ool-sl)/speed) - 1;
				}
			}
			//alert(ool +  " " + sl);
			
			if(isScrolling == true)
			{
				scrollToX(sl);
				setTimeout("slide('"+id+"',"+sl+")", intervalTime);
			}
		}
		else 
		{
			disableScrolling();
		}
	}
}

/* hide functies for thumb panel */
function hideLockPanel()
{
	lockObj = document.getElementById("lockPanel");
	
	if(lockObj)
	{
		lockObj.style.visibility = 'hidden';
	}
}

function showLockPanel()
{
	lockObj = document.getElementById("lockPanel");
	
	if(lockObj)
	{
		lockObj.style.visibility = 'visible';
	}
}

function slideLarge(toPosition)
{
	if(isScrolling == false)
	{
		if(scrollTopPositionOfLarge != toPosition)
		{
			var oldScrollHeight = scrollTopPositionOfLarge;
			scrollTopPositionOfLarge = toPosition;
			enableScrolling();
			slideY("largeSlidePanel",oldScrollHeight, toPosition);
		}
	}
}

function slideThumb(type)
{
	var pk = 162;
	var heightOfPanel = 162;
	var scrollHeight = getScrollHeightById("thumbSlidePanel");
	var oldScrollHeight = scrollTopPositionOfThumb;
	
	if(type == "top")
	{
		if((scrollTopPositionOfThumb - pk) < 0)
		{
			scrollTopPositionOfThumb = 0;
		}
		else
		{
			scrollTopPositionOfThumb = scrollTopPositionOfThumb - pk;
		}
	}
	else
	{
		if((oldScrollHeight + pk) > (scrollHeight - heightOfPanel))
		{
			scrollTopPositionOfThumb = scrollHeight - heightOfPanel;
		}
		else
		{
			scrollTopPositionOfThumb = scrollTopPositionOfThumb + pk;
		}
	}
		
	if(isScrolling == false)
	{
		enableScrolling();
		slideY("thumbSlidePanel",oldScrollHeight, scrollTopPositionOfThumb);
	}
}

function slideX(actualX, toX)
{
	if(isScrolling == true)
	{
		if(toX == actualX) 
		{	// stop scrolling
			disableScrolling();
		}
		else
		{
			//472 400
			if(toX > actualX)	
			{	// scroll to the left
				//if(scrollWidth-toX>pw)
					actualX+=Math.ceil((toX-actualX)/speed)
				//else
				//	actualX+=Math.ceil((toX-actualX-(scrollWidth-toX))/speed)
			}
			else
			{
				actualX = Math.ceil(actualX+(toX-actualX)/speed) - 1;
			}
		}
		
		if(isScrolling == true)
		{
			scrollObjectToX("thumbSlidePanel",actualX);
			setTimeout("slideX("+actualX+","+toX+")", intervalTime);		
		}
	}
}

function slideY(objectId, actualY, toY)
{
	if(isScrolling == true)
	{
		if(toY == actualY) 
		{	// stop scrolling
			disableScrolling();
		}
		else
		{
			if(toY > actualY)	
			{	// scroll to down
				actualY+=Math.ceil((toY-actualY)/speed)
			}
			else
			{	// scroll up
				//actual = actual - (Math.ceil((actualY-toY)/speed) - 1)
				actualY = Math.ceil(actualY+(toY-actualY)/speed) - 1;
			}
		}
		
		if(isScrolling == true)
		{
			scrollObjectToY(objectId,actualY);
			setTimeout("slideY('"+objectId+"',"+actualY+","+toY+")", intervalTime);	
		}
	}
}
