var speed 						= 10;	// dinstance devider
var intervalTime 				= 10;
var type 						= "horizontal";
var scrollerId 					= "slider";
var isScrolling 				= false;
var scrollLeftPosition 			= 0;
var scrollLeftPositionOfThumb 	= 0;
var iDiashow					= false;
var iDiashowSpeed				= 3500;
var iTotalPhotos				= 0;
var iCurrentPhoto				= 0;

function getScrollDiv()
{
	return document.getElementById(scrollerId);
}

/* 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));
		
		//log("Pagewidth: " + pageWidth + " scrollWidth: " + scrollWidth + " iReturn: " + iReturn);
		
		return iReturn - getInitialValue();
		//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 getScrollLeft()
{
	return scrollLeftPosition;
}

function scrollObjectToX(scrollObjectId, x)
{
	scrollObject = document.getElementById(scrollObjectId);
	
	if(scrollObject)
	{
		scrollObject.scrollLeft = x;
	}
}

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();
		showSliderImage();
	}
}

function getStartValue()
{
	textObject = document.getElementById('textDiv');
	
	if(textObject)
	{
		var ool = getOffsetLeft(textObject);	// the offset van het object
		
		if(ool > 0)
		{
			return ool - 90; // initial value
		}
	}
	
	return 0;
}

/* end functions */

function scrollSlider(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
				
			//log("ool: " + ool);
			
			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 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
			
			// toon message
			//log("ool = " + ool + " sl " + sl );
			
			ool = Math.round(ool);
			
			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 showSliderImage()
{
	var id_of_object = 'imageHider';
	objectToFade = getObj(id_of_object);
	
	if(isObj(objectToFade))
	{
		new Effect.Opacity(id_of_object, { from: 0.75, to: 0.0, duration: 0.5 });
	}
}

function hideSliderImage()
{
	var id_of_object = 'imageHider';
	objectToFade = getObj(id_of_object);
	
	if(isObj(objectToFade))
	{
		new Effect.Opacity(id_of_object, { from: 0.0, to: 0.75, duration: 0.5 });
	}
}

function initSlider()
{
	ShowPlus();
	
	var id_of_object = '3';
	slideObj = getObj(id_of_object);

	setPlus(id_of_object);
	
	if(isObj(slideObj))
	{
		scrollSlider(id_of_object);
	}
}

function ShowDiaMenu() {MakeVisible('diashowMenu');}
function ShowPlay() {MakeVisible('play');}
function ShowPause() {MakeVisible('pause');}
function HideDiaMenu() {MakeHidden('diashowMenu');}
function HidePlay() {MakeHidden('play');}
function HidePause() {MakeHidden('pause');}
function ShowPlus() {MakeVisible('plus');}

function btn_startShow()
{
	if(iDiashow == false)
	{
		iDiashow = true;
		moveShowTo(iCurrentPhoto);
		ShowPause();
		HidePlay();
	}
}

function btn_pauseShow()
{
	if(iDiashow == true)
	{
		iDiashow = false;
		HidePause();
		ShowPlay();
	}
}

function GetPictureSrc(id)
{
	var sReturn = '';
	imageObj = getObj("img_" + id);
	
	if(imageObj)
	{
		sReturn = imageObj.src;
		
		sReturn = sReturn.replace("large","xl");
	}
	
	return sReturn;
}

function setPlus(id)
{
	linkObj = getObj("a_plus");
	
	if(linkObj)
	{
		linkObj.href = GetPictureSrc(id);
	}
}

function startDiashow(iTotal)
{
	iTotalPhotos = iTotal;
	// Set the diashow flag
	iDiashow = true;
	
	ShowDiaMenu();
	ShowPause();
	
	// parseInt
	var id_of_object = '3';
	slideObj = getObj(id_of_object);

	if(isObj(slideObj))
	{
		scrollSlider(id_of_object);
	}
	
	// Start after 0.5 seconds the sliding
	var functionToCall = function()
	{
		if(iDiashow == true)
		{
			moveShowTo(4);
		}
	};
	
	setTimeout(functionToCall, iDiashowSpeed);
}

function moveShowTo(iShowPhoto)
{	
	// Check the total
	if(iShowPhoto > (iTotalPhotos - 4))
	{
		iShowPhoto = 3;
	}
	
	var id_of_object = '' + iShowPhoto;
	slideObj = getObj(id_of_object);

	if(isObj(slideObj))
	{
		scrollToPhoto('' + iShowPhoto);		
	
		// Add one counter
		iShowPhoto++;
		iCurrentPhoto = iShowPhoto;
		
		// Start after 0.5 seconds the sliding
		var functionToCall = function()
		{
			if(iDiashow == true)
			{
				moveShowTo(iShowPhoto);
			}
		};
		
		setTimeout(functionToCall, iDiashowSpeed);
	}
}

function scrollToPhoto(objectId)
{
	// Show the lock panel
	showLockPanel();
	
	// Fade out the slider
	hideSliderImage();
	
	// Start after 0.5 seconds the sliding
	var functionToCall = function()
	{
		scrollSlider(objectId);
	};
	
	setTimeout(functionToCall, 500);
}

function getInitialValue()
{
	//return 0;
	return (getPageWidth() / 2) - 333;
}

function log(sValue)
{
	addLog(sValue);
}

function addLog(sValue)
{
	logObj = getObj('logger');
	
	if(isObj(logObj))
	{
		logObj.innerHTML += "<br>" + sValue;
	}
}

function loadProjectImages(objectId, url)
{
	iDiashow = false;
	HideDiaMenu();
	HidePlay();
	HidePause();
	
	// Show the lock panel
	showLockPanel();
	
	// Use this callback
	var callback = function()
	{
		scrollToPhoto(objectId);
	}
		
	LoadPage(url,'galleryContent',callback);
	
}

//alert("Page width: " + getPageWidth());
//alert(window.innerWidth);
//alert(self.innerWidth);
//alert("test");
