// Windows, IE 5.5 / 5.01 / 6, Firefox 1.0.1, Opera
// Mac, Safari 1.2.4 / Netscape 7.2 / Firefox 1.0.2
//
// IE6	document.documentElement.scrollTop
// IE5+	document.body.scrollTop
// Saf	window.pageYOffset
// NN	window.pageYOffset
// FF	window.pageYOffset

var topAlert1 = 0; // variables to interpret scrollTop or pageYOffset are interpreted problematically, if they stay 0 the scroll won't stop, topAlert is set to 1 if the scrollTop stays 0
var alertCounter1 = 0;
var alertCounterCheck1 = 0;
var topAlert2 = 0;
var alertCounter2 = 0;
var alertCounterCheck2 = 0;
var topAlert3 = 0;
var alertCounter3 = 0;
var alertCounterCheck3 = 0;
function scrollPage() {
	if (document.valuesform.json.value == 1) {
		var scrollDelay = 6; // > 0
		// Safari, Navigator, Firefox
		if (!topAlert1 && document.getElementById && window.scrollBy && setTimeout && clearTimeout && document.getElementById(scrollElement).offsetTop != null && window.pageYOffset != null) {
			// checks if scroll is moving
			if (alertCounterCheck1 < 3) {
				alertCounterCheck1++;
				if (window.pageYOffset == 0) {
					alertCounter1++;
				}
				if (alertCounterCheck1 > 1 && alertCounter1 == alertCounterCheck1) {
					topAlert1 = 1;
				}
			}
			if (window.pageYOffset < document.getElementById(scrollElement).offsetTop) {
				window.scrollBy(0,Math.ceil((document.getElementById(scrollElement).offsetTop - window.pageYOffset) / scrollDelay));
				scrollGo = setTimeout('scrollPage()',16);
				if (window.pageYOffset == document.getElementById(scrollElement).offsetTop) {
					clearTimeout(scrollGo);
					document.valuesform.scrollingon.value = 0;
					if (document.valuesform.toload.value != 0) {
						loadProject(document.valuesform.toload.value);
						document.valuesform.toload.value = 0;
					}
				}
			} else if (window.pageYOffset > document.getElementById(scrollElement).offsetTop) {
				window.scrollBy(0,Math.floor((document.getElementById(scrollElement).offsetTop - window.pageYOffset) / scrollDelay));
				scrollGo = setTimeout('scrollPage()',16);
				if (window.pageYOffset == document.getElementById(scrollElement).offsetTop) {
					clearTimeout(scrollGo);
					document.valuesform.scrollingon.value = 0;
					if (document.valuesform.toload.value != 0) {
						loadProject(document.valuesform.toload.value);
						document.valuesform.toload.value = 0;
					}
				}
			} else if (document.valuesform.toload.value != 0) {
				loadProject(document.valuesform.toload.value);
				document.valuesform.toload.value = 0;
			}
		// IE6
		} else if (!topAlert2 && document.getElementById && window.scrollBy && setTimeout && clearTimeout && document.getElementById(scrollElement).offsetTop != null && document.documentElement.scrollTop != null) {
			// checks if scroll is moving
			if (alertCounterCheck2 < 3) {
				alertCounterCheck2++;
				if (document.documentElement.scrollTop == 0) {
					alertCounter2++;
				}
				if (alertCounterCheck2 > 1 && alertCounter2 == alertCounterCheck2) {
					topAlert2 = 1;
				}
			}
			if (document.documentElement.scrollTop < document.getElementById(scrollElement).offsetTop) {
				window.scrollBy(0,Math.ceil((document.getElementById(scrollElement).offsetTop - document.documentElement.scrollTop) / scrollDelay));
				scrollGo = setTimeout('scrollPage()',16);
				if (document.documentElement.scrollTop == document.getElementById(scrollElement).offsetTop) {
					clearTimeout(scrollGo);
					document.valuesform.scrollingon.value = 0;
					if (document.valuesform.toload.value != 0) {
						loadProject(document.valuesform.toload.value);
						document.valuesform.toload.value = 0;
					}
				}
			} else if (document.documentElement.scrollTop > document.getElementById(scrollElement).offsetTop) {
				window.scrollBy(0,Math.floor((document.getElementById(scrollElement).offsetTop - document.documentElement.scrollTop) / scrollDelay));
				scrollGo = setTimeout('scrollPage()',16);
				if (document.documentElement.scrollTop == document.getElementById(scrollElement).offsetTop) {
					clearTimeout(scrollGo);
					document.valuesform.scrollingon.value = 0;
					if (document.valuesform.toload.value != 0) {
						loadProject(document.valuesform.toload.value);
						document.valuesform.toload.value = 0;
					}
				}
			} else if (document.valuesform.toload.value != 0) {
				loadProject(document.valuesform.toload.value);
				document.valuesform.toload.value = 0;
			}
		// IE6-
		} else if (!topAlert3 && document.getElementById && window.scrollBy && setTimeout && clearTimeout && document.getElementById(scrollElement).offsetTop != null && document.body.scrollTop != null) {
			// checks if scroll is moving
			if (alertCounterCheck3 < 3) {
				alertCounterCheck3++;
				if (document.body.scrollTop == 0) {
					alertCounter3++;
				}
				if (alertCounterCheck3 > 1 && alertCounter3 == alertCounterCheck3) {
					topAlert3 = 1;
				}
			}
			if (document.body.scrollTop < document.getElementById(scrollElement).offsetTop) {
				window.scrollBy(0,Math.ceil((document.getElementById(scrollElement).offsetTop - document.body.scrollTop) / scrollDelay));
				scrollGo = setTimeout('scrollPage()',16);
				if (document.body.scrollTop == document.getElementById(scrollElement).offsetTop) {
					clearTimeout(scrollGo);
					document.valuesform.scrollingon.value = 0;
					if (document.valuesform.toload.value != 0) {
						loadProject(document.valuesform.toload.value);
						document.valuesform.toload.value = 0;
					}
				}
			} else if (document.body.scrollTop > document.getElementById(scrollElement).offsetTop) {
				window.scrollBy(0,Math.floor((document.getElementById(scrollElement).offsetTop - document.body.scrollTop) / scrollDelay));
				scrollGo = setTimeout('scrollPage()',16);
				if (document.body.scrollTop == document.getElementById(scrollElement).offsetTop) {
					clearTimeout(scrollGo);
					document.valuesform.scrollingon.value = 0;
					if (document.valuesform.toload.value != 0) {
						loadProject(document.valuesform.toload.value);
						document.valuesform.toload.value = 0;
					}
				}
			} else if (document.valuesform.toload.value != 0) {
				loadProject(document.valuesform.toload.value);
				document.valuesform.toload.value = 0;
			}
		} else {
			document.valuesform.json.value = 0;
		}
	}
}

function previousImage() {
	if (document.valuesform.json.value == 1) {
		if (document.valuesform.scrollingon.value == 0) {
			document.valuesform.scrollingon.value = 1;
			goImage = document.valuesform.imagecurrent.value;
			goImage--;
			if (goImage == 0) {
				goImage = document.valuesform.imagetotal.value;
				goImage++;
				goImage--;
			}
			document.valuesform.imagecurrent.value = goImage;
			scrollImage();
		}
	} else {
		loadProject(1);
	}
}

function nextImage() {
	if (document.valuesform.json.value == 1) {
		if (document.valuesform.scrollingon.value == 0) {
			document.valuesform.scrollingon.value = 1;
			goImage = document.valuesform.imagecurrent.value;
			goImage++;
			if (goImage > document.valuesform.imagetotal.value) {
				goImage = 1;
				goImage++;
				goImage--;
			}
			document.valuesform.imagecurrent.value = goImage;
			scrollImage();
		}
	} else {
		loadProject(1);
	}
}

var topAlertIm1 = 0; // variables to interpret scrollTop or pageYOffset are interpreted problematically, if they stay 0 the scroll won't stop, topAlert is set to 1 if the scrollTop stays 0
var alertCounterIm1 = 0;
var alertCounterCheckIm1 = 0;
var topAlertIm2 = 0;
var alertCounterIm2 = 0;
var alertCounterCheckIm2 = 0;
var topAlertIm3 = 0;
var alertCounterIm3 = 0;
var alertCounterCheckIm3 = 0;
function scrollImage() {
	if (document.valuesform.json.value == 1) {
		if (document.valuesform.textloading.value == 0 && document.valuesform.imageloading.value == 0) {
			var scrollDelay = 4; // > 0
			// Safari, Navigator, Firefox
			if (!topAlertIm1 && document.getElementById && window.scrollBy && setTimeout && clearTimeout && window.frames[0].document.images[goImage - 1].offsetTop >= 0 && window.frames[0].window.pageYOffset != null) {
				// checks if scroll is moving
				if (alertCounterCheckIm1 < 3) {
					alertCounterCheckIm1++;
					if (window.frames[0].window.pageYOffset == 0) {
						alertCounterIm1++;
					}
					if (alertCounterCheckIm1 > 1 && alertCounterIm1 == alertCounterCheckIm1) {
						topAlertIm1 = 1;
					}
				}
				if (window.frames[0].window.pageYOffset < window.frames[0].document.images[goImage - 1].offsetTop) {
					window.frames[0].window.scrollBy(0,Math.ceil((window.frames[0].document.images[goImage - 1].offsetTop - window.frames[0].window.pageYOffset) / scrollDelay));
					scrollGo = setTimeout('scrollImage()',16);
					if (window.frames[0].window.pageYOffset == window.frames[0].document.images[goImage - 1].offsetTop) {
						clearTimeout(scrollGo);
						document.valuesform.scrollingon.value = 0;
						threeMoment();
					}
				} else if (window.frames[0].window.pageYOffset > window.frames[0].document.images[goImage - 1].offsetTop) {
					window.frames[0].window.scrollBy(0,Math.floor((window.frames[0].document.images[goImage - 1].offsetTop - window.frames[0].window.pageYOffset) / scrollDelay));
					scrollGo = setTimeout('scrollImage()',16);
					if (window.frames[0].window.pageYOffset == window.frames[0].document.images[goImage - 1].offsetTop) {
						clearTimeout(scrollGo);
						document.valuesform.scrollingon.value = 0;
						threeMoment();
					}
				}
			// IE6
			} else if (!topAlertIm2 && document.getElementById && window.scrollBy && setTimeout && clearTimeout && window.frames[0].document.images[goImage - 1].offsetTop != null && window.frames[0].document.documentElement.scrollTop != null) {
				// checks if scroll is moving
				if (alertCounterCheckIm2 < 3) {
					alertCounterCheckIm2++;
					if (window.frames[0].document.documentElement.scrollTop == 0) {
						alertCounterIm2++;
					}
					if (alertCounterCheckIm2 > 1 && alertCounterIm2 == alertCounterCheckIm2) {
						topAlertIm2 = 1;
					}
				}
				if (window.frames[0].document.documentElement.scrollTop < window.frames[0].document.images[goImage - 1].offsetTop) {
					window.frames[0].window.scrollBy(0,Math.ceil((window.frames[0].document.images[goImage - 1].offsetTop - window.frames[0].document.documentElement.scrollTop) / scrollDelay));
					scrollGo = setTimeout('scrollImage()',16);
					if (window.frames[0].document.documentElement.scrollTop == window.frames[0].document.images[goImage - 1].offsetTop) {
						clearTimeout(scrollGo);
						document.valuesform.scrollingon.value = 0;
						threeMoment();
					}
				} else if (window.frames[0].document.documentElement.scrollTop > window.frames[0].document.images[goImage - 1].offsetTop) {
					window.frames[0].window.scrollBy(0,Math.floor((window.frames[0].document.images[goImage - 1].offsetTop - window.frames[0].document.documentElement.scrollTop) / scrollDelay));
					scrollGo = setTimeout('scrollImage()',16);
					if (window.frames[0].document.documentElement.scrollTop == window.frames[0].document.images[goImage - 1].offsetTop) {
						clearTimeout(scrollGo);
						document.valuesform.scrollingon.value = 0;
						threeMoment();
					}
				}
			// IE6-
			} else if (!topAlertIm3 && document.getElementById && window.scrollBy && setTimeout && clearTimeout && window.frames[0].document.images[goImage - 1].offsetTop != null && window.frames[0].document.body.scrollTop != null) {
				// checks if scroll is moving
				if (alertCounterCheckIm3 < 3) {
					alertCounterCheckIm3++;
					if (window.frames[0].document.body.scrollTop == 0) {
						alertCounterIm3++;
					}
					if (alertCounterCheckIm3 > 1 && alertCounterIm3 == alertCounterCheckIm3) {
						topAlertIm3 = 1;
					}
				}
				if (window.frames[0].document.body.scrollTop < window.frames[0].document.images[goImage - 1].offsetTop) {
					window.frames[0].window.scrollBy(0,Math.ceil((window.frames[0].document.images[goImage - 1].offsetTop - window.frames[0].document.body.scrollTop) / scrollDelay));
					scrollGo = setTimeout('scrollImage()',16);
					if (window.frames[0].document.body.scrollTop == window.frames[0].document.images[goImage - 1].offsetTop) {
						clearTimeout(scrollGo);
						document.valuesform.scrollingon.value = 0;
						threeMoment();
					}
				} else if (window.frames[0].document.body.scrollTop > window.frames[0].document.images[goImage - 1].offsetTop) {
					window.frames[0].window.scrollBy(0,Math.floor((window.frames[0].document.images[goImage - 1].offsetTop - window.frames[0].document.body.scrollTop) / scrollDelay));
					scrollGo = setTimeout('scrollImage()',16);
					if (window.frames[0].document.body.scrollTop == window.frames[0].document.images[goImage - 1].offsetTop) {
						clearTimeout(scrollGo);
						document.valuesform.scrollingon.value = 0;
						threeMoment();
					}
				}
			} else {
				document.valuesform.json.value = 0;
			}
		}
	}
}

function previousProject() {
	goProject = document.valuesform.projectcurrent.value;
	goProject--;
	if (goProject < 1) {
		goProject = proj.length;
	}
	loadProject(goProject);
}

function nextProject() {
	goProject = document.valuesform.projectcurrent.value;
	goProject++;
	if (goProject > proj.length) {
		goProject = 1;
	}
	loadProject(goProject);
}

function loadProject(goProject) {
	if (document.valuesform.json.value == 1) {
		oneMoment();
		goProject++;
		goProject--;
		if (goProject <= 0) {
			goProject = 1
		} else if (goProject > document.valuesform.projecttotal.value) {
			goProject = document.valuesform.projecttotal.value;
		}
		if (document.valuesform.projectcurrent.value != goProject || document.valuesform.firstload.value == 1) { //don't load if already loaded, stops loading on first load so firstload is needed
			if (document.valuesform.textloading.value == 0 && document.valuesform.imageloading.value == 0) {
				document.valuesform.textloading.value = 1;
				document.valuesform.imageloading.value = 1;
				document.valuesform.projectcurrent.value = goProject;
				window.frames[1].window.location.href = '\/?text=' + proj[goProject - 1];
				// loading imageframe from textframe seems unreliable somehow
				if (document.valuesform.firstload.value == 1) { // on first relaod only the text frame is reloaded, to resize it correctly
					document.valuesform.imageloading.value = 0;
					document.valuesform.scrollingon.value = 0;
					threeMoment();
				} else {
					window.frames[0].window.location.href = '\/?images=' + proj[goProject - 1];
				}
				if (document.getElementById('projlink' + goProject)) {
					for(n = 1; n <= proj.length; n++) {
						document.getElementById('projlink' + n).className = '';
					}
					document.getElementById('projlink' + goProject).className = 'selected';
				}
			}
		} else {
			threeMoment();
		}
	} else {
		window.location.href = '\/?showproject=' + proj[goProject - 1];
	}
}

function scrollGoToProject(toLoad) {
	toLoad++;
	toLoad--;
	if (toLoad == 0) {
		toLoad = 1;
	}
	scrollElement = document.valuesform.projectsdir.value;
	document.valuesform.toload.value = toLoad;
	scrollPage();
}

function oneMoment() {
	var oneMoment = '<h2 class="menu-item"' + '><a href="#' + '" onClick="return false;' + '">&lt; vorige afbeelding' + '<\/' + 'a> \/ <a href="#' + '" onClick="return false;' + '">volgende afbeelding &gt;<' + '\/a' + '> afbeeldingen laden&hellip;<' + '\/h2' + '>';
	if (document.getElementById) {
		document.getElementById('navimage').innerHTML = oneMoment;
	} else if (document.all) {
		document.all.navimage.innerHTML = oneMoment;
	}
}

function threeMoment() {
var threeMoment = '<h2 class="menu-item"' + '><a href="\/?showproject=' + proj[document.valuesform.projectcurrent.value - 1] + '" onClick="previousImage(' + ');if(document.valuesform.slideshow.value == 1' + '){slideShow(' + ');}if(document.valuesform.json.value == 1' + '){return false;' + '}">&lt; vorige afbeelding' + '<\/' + 'a> \/ <a href="\/?showproject=' + proj[document.valuesform.projectcurrent.value - 1] + '" onClick="nextImage(' + ');if(document.valuesform.slideshow.value == 1' + '){slideShow(' + ');}if(document.valuesform.json.value == 1' + '){return false;' + '}">volgende afbeelding &gt;<' + '\/a' + '> '+ document.valuesform.imagecurrent.value + ' van ' + document.valuesform.imagetotal.value + '<' + '\/h2' + '>';
// before slideshow
//var threeMoment = '<h2 class="menu-item"' + '><a href="\/?showproject=' + proj[document.valuesform.projectcurrent.value - 1] + '" onClick="previousImage(' + ');if(document.valuesform.json.value == 1' + '){return false;' + '}">&lt; vorige afbeelding' + '<\/' + 'a> \/ <a href="\/?showproject=' + proj[document.valuesform.projectcurrent.value - 1] + '" onClick="nextImage(' + ');if(document.valuesform.json.value == 1' + '){return false;' + '}">volgende afbeelding &lt;<' + '\/a' + '> \/ '+ document.valuesform.imagecurrent.value + ' van ' + document.valuesform.imagetotal.value + '<' + '\/h2' + '>';
	if (document.getElementById) {
		document.getElementById('navimage').innerHTML = threeMoment;
	} else if (document.all) {
		document.all.navimage.innerHTML = threeMoment;
	}
}

// slideshow
function sliding() {
	nextImage();
	showTimer = setTimeout("sliding()", 3000);
}

var stopSlideshow = '<h2 class="menu-item"' + '><a href="#" onClick="slideShow(' + ');if(document.valuesform.json.value == 1' + '){return false;' + '}">stop slideshow' + '<\/' + 'a><' + '\/h2' + '>';
var startSlideshow = '<h2 class="menu-item"' + '><a href="#" onClick="slideShow(' + ');if(document.valuesform.json.value == 1' + '){return false;' + '}">start slideshow' + '<\/' + 'a><' + '\/h2' + '>';
function slideShow() {
	if (document.valuesform.slideshow.value == 0) {
		sliding();
		document.valuesform.slideshow.value = 1;
		document.getElementById('slideshowdiv').innerHTML = stopSlideshow;
	} else {
		document.valuesform.slideshow.value = 0;
		clearTimeout(showTimer);
		document.getElementById('slideshowdiv').innerHTML = startSlideshow;
	}
}
