var timeOut = null
var refreshMiliseconds = 3000
var slideshowLang = 'nl'

function Photo(category, src, width, height, thumbSrc, twidth, theight, caption) {
	this.category = category
	this.src = src
	this.width = width
	this.height = height
	this.thumbSrc = thumbSrc
	this.twidth = twidth
	this.theight = theight
	this.caption = caption
}

function showPhotos(cat, showCaption, showDropdown, offAndOn, showSlideshowLink, showPrevNextLink) {
	if (cat == null) {
		var qs = new Querystring()
		cat = qs.get("g")
	}
	
	if (cat == null && PHOTOS.length > 0) {
		cat = PHOTOS[0].category
	}
	
	clearTimeout(timeOut)

	var thumbnails = document.getElementById("thumbnails")
	if (thumbnails == null) {
		alert("Element thumbnails not found")
		return
	}

	var picture = document.getElementById("picture")
	if (picture == null) {
		alert("Element picture not found")
		return
	}

	var caption = document.getElementById("caption")
	if (caption == null && showCaption) {
		alert("Element caption not found")
		return
	}
	
	var dropdown = document.getElementById("dropdown")
	if (dropdown == null && showDropdown) {
		alert("Element dropdown not found")
		return
	}
	
	var prevnextlink = document.getElementById("prevnextlink")
	if (prevnextlink == null && showPrevNextLink) {
		alert("Element prevnextlink not found")
		return
	}

	
	/* The photo to show in the category */
	var first = -1
	
	if (PHOTOS != null) {
		var last = null
		removeChildren(thumbnails)
		removeChildren(dropdown)

		if (showDropdown) {
			var select = document.createElement("select")
			dropdown.appendChild(select)
			select.setAttribute("onchange", "showPhotos(this.options[this.selectedIndex].value, "+ showCaption +", true, "+ offAndOn +", "+ showSlideshowLink +", "+ showPrevNextLink +")")
			select.onchange = function() { showPhotos(this.options[this.selectedIndex].value, showCaption, true, offAndOn, showSlideshowLink, showPrevNextLink) }
		}

		for (var i = 0; i < PHOTOS.length; i++) {
			var photo = PHOTOS[i]

			if (photo != null) {
				if (photo.category == cat) {
					if (first == -1) {
						first = i
					}

					var a = document.createElement("a")
					a.setAttribute("href", "javascript:showPhoto(" + i + ", " + showCaption + ", "+ showPrevNextLink +")")
					a.setAttribute("title", photo.caption)
					
					var img = document.createElement("img")
					img.setAttribute("src", photo.thumbSrc)
					img.setAttribute("width", photo.twidth)
					img.setAttribute("height", photo.theight)
					img.setAttribute("alt", photo.caption)

					
					if (offAndOn) {
						if (i % 2 == 0) {
							img.style.className = "left"
						} else {
							img.style.className = "right"
						}
					} else {
						img.style.className = "left"
					}
					
					a.appendChild(img)
					thumbnails.appendChild(a)
				}
						
				if (photo.category != last && showDropdown) {
					var option = document.createElement("option")
					option.setAttribute("value", photo.category)
					option.appendChild(document.createTextNode(photo.category))

					if (photo.category == cat) {
						option.setAttribute("selected", "selected")
					}

					select.appendChild(option)
					last = photo.category
				}
			}
		}
		if (showSlideshowLink) {
			writeSlideshowLink(first, cat, true, showCaption, showPrevNextLink)
		}
	}

	showPhoto(first, showCaption, showPrevNextLink)
	
	return true
}

function showPhoto(i, showCaption, showPrevNextLink) {
	var photo = PHOTOS[i]
	
	removeChildren(document.getElementById("picture"))
	removeChildren(document.getElementById("caption"))

	var picture = document.getElementById("picture")
	
	img = document.createElement("img")
	img.setAttribute("src", photo.src)
	img.setAttribute("alt", photo.caption)
	img.setAttribute("width", photo.width)
	img.setAttribute("height", photo.height)
	picture.appendChild(img)
	
	if (showPrevNextLink) {
		var cat = photo.category
		var next = -1
		var prev = -1
		var linkstr = ''

		for (var j=(i+1); j<PHOTOS.length; j++) {
			if (PHOTOS[j].category == cat) {
				next = j
				break;
			}
		}

		for (var k=(i-1); k>=0; k--) {
			if (PHOTOS[k].category == cat) {
				prev = k
				break;
			}
		}
		
		if (prev > -1) {
			linkstr += ' <a href="javascript:showPhoto('+ prev +', '+ showCaption +', true)">&#171; ' + (slideshowLang == 'nl' ? 'Vorige foto' : 'Previous photo') + '</a> '
		}
		
		if (next > -1) {
			linkstr += ' <a href="javascript:showPhoto('+ next +', '+ showCaption +', true)">' + (slideshowLang == 'nl' ? 'Volgende foto' : 'Next photo') + ' &#187;</a> '
		}
		
		document.getElementById("prevnextlink").innerHTML = linkstr
	}

	if (showCaption) {
		var caption = document.getElementById("caption")
		caption.appendChild(document.createTextNode(photo.caption))
	}
	
}

function removeChildren(e) {
	if (e) {
		for (var i = e.childNodes.length-1; i >= 0; i--) {
			removeChildren(e.childNodes[i])
			e.removeChild(e.childNodes[i])
		}
	}
}

function slideShow(cat, i, showCaption, start, showPrevNextLink) {
	var length = PHOTOS.length
	if (start) {
		writeSlideshowLink(i, cat, false, showCaption, showPrevNextLink)
		showPhoto(i, showCaption, showPrevNextLink)
		for (var j=i; j<length; j++) {
			if (cat == PHOTOS[j].category && j > i) {
				break
			}
		}
		if (j < length) {
			timeOut = setTimeout("slideShow('"+ cat +"', "+ j +", "+ showCaption +", true, "+ showPrevNextLink +")", refreshMiliseconds)
		} else {
			for (var k=0; k<length; k++) {
				if (cat == PHOTOS[k].category) {
					break
				}
			}
			writeSlideshowLink(k, cat, true, showCaption, showPrevNextLink)
			clearTimeout(timeOut)
		}
	} else {
		writeSlideshowLink(i, cat, true, showCaption, showPrevNextLink)
		clearTimeout(timeOut)
	}
}

function writeSlideshowLink(index, cat, start, showCaption, showPrevNextLink) {
	var el = document.getElementById("slideshowlink")
	if (start) {
		el.innerHTML = ' <a href="javascript:slideShow(\''+ cat +'\', '+ index +', '+ showCaption +', true, '+ showPrevNextLink +');">' + (slideshowLang == 'nl' ? 'Start diashow' : 'Start slideshow') + '</a> '
	} else {
		el.innerHTML = ' <a href="javascript:slideShow(\''+ cat +'\', '+ index +', '+ showCaption +', false, '+ showPrevNextLink +');">' + (slideshowLang == 'nl' ? 'Stop diashow' : 'Stop slideshow') + '</a> '
	}
}

function openisn() {
	var s = 'http://www.panoview.nl/7976/kurhaus2005/ned'
	isn = window.open(s, 'isn', 'width=784,height=527,left=5,top=140,statusbar=yes,scrollbars=no')
	isn.opener = self
}


function openWindow(url) {
	var win = window.open(url)
	win.opener = self
}

function openPopup(img, imwidth, imheight) {
	window.open(img, 'popup', 'width='+(imwidth+16)+',height='+(imheight+16)+',scrollbars=no,resizable=no')
}