// copyright 2006-2011 by Marc De Bruyne for www.descute.be

var dsURL = "http://www.descute.be/";
var dsIndex = "intro-nl.";
var dsPopFile = "image-popup.htm";
var undefined;

function dsCheck()
{
	var dsCurURL = self.location.href;

	if (self.location.protocol != "file:")
	{
		if (self != top)
		{
			top.location = self.location;
		}

		if (dsCurURL.indexOf(window.dsURL + window.dsIndex) == 0)
		{
			self.location.replace(window.dsURL);
		}
	}
	else
	{
		window.dsURL = dsCurURL.substr(0, dsCurURL.indexOf(".be/") + 4);
		var dsElems = document.getElementsByTagName("A");
		var dsCount = dsElems.length;

		while (dsCount--)
		{
			if (dsElems[dsCount].href.indexOf("file:") == 0)
			{
				dsElems[dsCount].href = dsElems[dsCount].href.replace(".htm", ".php")
			}
		}
	}
}

function dsSearchEvents(dsParentId)
{
	var dsParent;
	var dsElems;
	var dsForm;
	var dsButton;
	var dsInput;
	var dsCount = 0;

	if ((dsParent = document.getElementById(dsParentId))
	&& (dsElems = dsParent.getElementsByTagName("FORM")) && dsElems.length && (dsForm = dsElems[0])
	&& (dsElems = dsParent.getElementsByTagName("IMG")) && dsElems.length && (dsButton = dsElems[0])
	&& (dsElems = dsParent.getElementsByTagName("INPUT")) && dsElems.length && (dsInput = dsElems[0]))
	{
		while (dsInput.type != "text" && dsCount++ < dsElems.length)
		{
			dsInput = dsElems[dsCount];
		}

		dsInput.onfocus = dsSearchFocus;
		dsInput.onblur = dsSearchBlur;
		dsForm.onsubmit = dsSearchSubmit(dsInput);
		dsButton.onclick = dsSearchSubmit(dsInput);
	}
}

function dsSearchFocus()
{
	if (this.value == "Zoeken..." || this.value == "Rechercher...")
	{
		this.value = "";
	}

	this.focus();
	this.parentNode.parentNode.style.borderStyle="solid";
}

function dsSearchBlur()
{
	this.parentNode.parentNode.style.borderStyle="dotted";
}

function dsSearchSubmit(dsInput)	// uses closure to avoid relying on persistence of variable
{
	return function()
	{
		dsSearchFocus.call(dsInput);

		if (dsInput.value)
		{
			dsInput.parentNode.submit();
			return(true);
		}

		return(false);
	}
}

function dsClickEvents(dsParentId)
{
	var dsParent;
	var dsCount;
	var dsLinks;

	if ((dsParent = document.getElementById(dsParentId)) && (dsCount = dsParent.childNodes.length))
	{
		while (dsCount--)
		{
			if (dsParent.childNodes[dsCount].nodeType == 1
			&& (dsLinks = dsParent.childNodes[dsCount].getElementsByTagName("A")) && dsLinks.length)
			{
				dsParent.childNodes[dsCount].onclick = (function(dsLoc) {return function() {window.location = dsLoc;}})(dsLinks[0].href);
			}
		}
	}
}

function dsSpinElems(dsParentId)
{
	var dsParent;
	var dsCount;

	if ((dsParent = document.getElementById(dsParentId)) && (dsCount = dsParent.childNodes.length))
	{
		while (dsCount--)
		{
			if (dsParent.childNodes[dsCount].nodeType != 1)
			{
				dsParent.removeChild(dsParent.childNodes[dsCount]);
			}		
		}

		dsCount = Math.round(dsParent.childNodes.length * Math.random() - 0.5);

		while (dsCount--)
		{
			dsParent.appendChild(dsParent.firstChild);
		}
	}
}

function dsPopEvents(dsParentId)
{
	var dsParent;
	var dsThumbs;
	var dsCount;

	if (! (dsParent = document.getElementById(dsParentId)) || ! (dsThumbs = dsParent.getElementsByTagName("A")) || ! (dsCount = dsThumbs.length))
	{
		return;
	}

	while (dsCount--)
	{
		dsThumbs[dsCount].onclick = dsImagePop;
	}

	window.onbeforeunload = function() {if (window.dsPopWin && window.dsPopWin.open && ! window.dsPopWin.closed) window.dsPopWin.close();};
}

function dsPrevImage()
{
	var dsParent;
	var dsPrev;

	if (dsParent = window.dsPopLink.parentNode)
	{
		dsPrev = window.dsPopLink.previousSibling || dsParent.lastChild;

		while (dsPrev.nodeName != "A")
		{
			dsPrev = dsPrev.previousSibling || dsParent.lastChild;
		}

		if (dsPrev != window.dsPopLink)
		{
			dsPrev.onclick();
		}
	}

	return(false);
}

function dsNextImage()
{
	var dsParent;
	var dsNext;

	if (dsParent = window.dsPopLink.parentNode)
	{
		dsNext = window.dsPopLink.nextSibling || dsParent.firstChild;

		while (dsNext.nodeName != "A")
		{
			dsNext = dsNext.nextSibling || dsParent.firstChild;
		}

		if (dsNext != window.dsPopLink)
		{
			dsNext.onclick();
		}
	}

	return(false);
}

function dsImagePop()
{
	var dsFeat = "location=no,menubar=no,status=no,toolbar=no,scrollbars=no,width=768,height=576";

	window.dsPopLink = this;

	if (! window.dsPopWin || ! window.dsPopWin.open || window.dsPopWin.closed)
	{
		window.dsPopWin = window.open(window.dsURL + window.dsPopFile, "_blank", dsFeat);

		if (! window.dsPopWin || ! window.dsPopWin.open || window.dsPopWin.closed)
		{
			return (true);
		}
	}
	else if (! window.dsPopWin.dsImage)
	{
		window.dsPopWin.location = window.dsURL + window.dsPopFile;
	}
	else
	{
		window.dsPopWin.dsImageLoad();
	}

	window.dsPopWin.focus();
	return (false);
}

function dsInitPop()		// runs in popup
{
	if (! window.opener || window.opener.closed || ! window.opener.dsPopLink)
	{
		window.close();
	}

	document.getElementById("vorige").onclick = window.opener.dsPrevImage;
	document.getElementById("volgende").onclick = window.opener.dsNextImage;
	window.dsTitle = document.title;
	window.dsImage = new Image();
	window.dsPreLoad = new Image();
	window.dsPreLoad.onload = dsImageShow;
	window.dsPreLoad.onerror = dsImageError;
	dsImageLoad();
}

function dsImageLoad()		// runs in popup
{
	document.title = dsTitle + " - afbeelding wordt geladen...";
	window.dsPreLoad.src = window.opener.dsPopLink.href;
}

function dsImageShow()		// runs in popup
{
	var dsWidth = window.opener.dsPopLink.getAttribute("data-SizeX") || 768;
	var dsHeight = window.opener.dsPopLink.getAttribute("data-SizeY") || 576;

	window.dsImage.src = window.dsPreLoad.src;
	window.dsImage.style.width = dsWidth + "px";
	window.dsImage.style.height = dsHeight + "px";
	window.dsImage.style.left = ((768 - dsWidth) / 2) + "px";
	window.dsImage.style.top = ((576 - dsHeight) / 2) + "px";
	window.dsImage.alt = window.opener.dsPopLink.getAttribute("data-Title") || window.opener.dsPopLink.firstChild.alt;
	document.title = window.dsTitle + " - " + window.dsImage.alt;

	if (! window.dsImage.parentNode)
	{
		document.body.appendChild(window.dsImage);
	}

	window.focus();
}

function dsImageError()		// runs in popup
{
	document.title = dsTitle + " - afbeelding niet beschikbaar";
	document.body.removeChild(window.dsImage);
}
