/* 

	made by 4rn0 -- content managed websites (c) 2005 - www.4rn0.nl 
	tednoten.com -- main JavaScript file -- last revision: 14122005
	this file has been stripped from excess whitespace and comments

*/


/* Initialize behaviour on page or DOM load
----------------------------------------------- */
var aboutSubpages, workSubpages, nextColor = 'foo';

if(document.addEventListener){document.addEventListener('DOMContentLoaded',init,null);}
/*@cc_on @*/ /*@if (@_win32) document.write("<script defer src=../js/init.js><"+"/script>"); /*@end @*/   
window.onload=init;

function init() {

	// quit if this function has already been called
	if (arguments.callee.done) {
		return;
	}
	
	// flag this function so we don't do the same thing twice
	arguments.callee.done = true;
	
	// define .getElementById for non W3C DOM compliant browsers
	if (typeof(document.getElementById) == 'undefined') {
		document.getElementById = function(id) {
			return document.all[id];
		};
	}

	var anchors = document.getElementsByTagName('A');
	for (var i = 0; i < anchors.length; i++) {
		
		if (anchors[i].getAttribute('rel') == 'external') {
			anchors[i].setAttribute('target', '_blank');
		}
		
		if (anchors[i].className == 'year') {
			anchors[i].onclick = toggleCollapse;
		}

		if (anchors[i].className == 'confirm') {
			anchors[i].onclick = function() {
				if (!confirm('Weet je zeker dat je dit wilt doen?')) {
					return false;	
				}
			};
		}
		
		if (anchors[i].getAttribute('id') == 'work' && anchors[i].className == 'breadcrumb') {
			document.getElementById('nav_work').className = 'visible';
		}
		
		if (anchors[i].getAttribute('id') == 'about' && anchors[i].className == 'breadcrumb') {
			document.getElementById('nav_about').className = 'visible';
		}
		
		if (anchors[i].className == 'toggleNextUncle') {
			
			cleanWhiteSpace(anchors[i].parentNode.parentNode);
			setInnerHTML(anchors[i], '<span>[+]</span> ' + getInnerHTML(anchors[i]));
			
			var uncle = anchors[i].parentNode.nextSibling;
			uncle.style.display = 'none';
			
			anchors[i].onclick = function() {
				var uncle = this.parentNode.nextSibling;
				uncle.style.display = (uncle.style.display == 'none') ? 'block' : 'none';
				setInnerHTML(this, '<span>[' + (uncle.style.display == 'none' ? '+' : '-') + ']</span>' + getInnerHTML(this).substr(16));

			};
		}
		
	}
	
	// about
	if (document.getElementById('nav_about')) {
		document.getElementById('about').onmouseover = function() {
			if (document.getElementById('navigation').className != 'nav_work') {
				hideMenu('nav_work');
			}
			clearTimeout(aboutSubpages);
			document.getElementById('nav_about').className = 'visible';
		};
		
		document.getElementById('about').onmouseout = function() {
			if (document.getElementById('navigation').className != 'nav_about') {
				aboutSubpages = setTimeout('hideMenu(\'nav_about\')', 3000);
			}
		};
		
		var anchors = document.getElementById('nav_about').getElementsByTagName('A');
		for (var i = 0; i < anchors.length; i++) {
			anchors[i].onmouseout = function() {
				if (document.getElementById('navigation').className != 'nav_about') {
					aboutSubpages = setTimeout('hideMenu(\'nav_about\')', 3000);
				}
			};
			anchors[i].onmouseover = function() {
				clearTimeout(aboutSubpages);
			};
		}
	}
	
	// work
	if (document.getElementById('nav_work')) {
		document.getElementById('work').onmouseover = function() {
			if (document.getElementById('navigation').className != 'nav_about') {
				hideMenu('nav_about');
			}
			clearTimeout(workSubpages);
			document.getElementById('nav_work').className = 'visible';
		};
		
		document.getElementById('work').onmouseout = function() {
			if (document.getElementById('navigation').className != 'nav_work') {
				workSubpages = setTimeout('hideMenu(\'nav_work\')', 3000);
			}
		};
		
		var anchors = document.getElementById('nav_work').getElementsByTagName('A');
		for (var i = 0; i < anchors.length; i++) {
			anchors[i].onmouseout = function() {
				if (document.getElementById('navigation').className != 'nav_work') {
					workSubpages = setTimeout('hideMenu(\'nav_work\')', 3000);
				}
			};
			anchors[i].onmouseover = function() {
				clearTimeout(workSubpages);
			};
		}
	}
	
	if (document.getElementById('featuredNavigation') || document.getElementById('workNavigation') || document.getElementById('publicationsNavigation')) {
		var anchors = (document.getElementById('featuredNavigation')) ? document.getElementById('featuredNavigation').getElementsByTagName('A') : (document.getElementById('workNavigation') ? document.getElementById('workNavigation').getElementsByTagName('A') : document.getElementById('publicationsNavigation').getElementsByTagName('A'));
		for (var i = 0; i < anchors.length; i++) {
			anchors[i].onmouseover = function() {
				getColor();
				this.parentNode.style.backgroundColor = nextColor;
				
				if (document.getElementById('featuredWork')) {
					var nextSibling = this.parentNode.nextSibling;
					while (nextSibling.nodeType == 3) {
						nextSibling = nextSibling.nextSibling;
					}
					document.getElementById('featuredWork').setAttribute('src', nextSibling.childNodes[0].getAttribute('src'));
				}
					

			};
			anchors[i].onmouseout = function() {
				if (this.parentNode.className != 'breadcrumb') {
					this.parentNode.style.backgroundColor = '#FFFFFF';
				}
			};
		}
	}
	
	if (document.getElementById('publicationsSubNavigation')) {
		
		var anchors = document.getElementById('publicationsSubNavigation').getElementsByTagName('A');
		for (var i = 0; i < anchors.length; i++) {
			anchors[i].onclick = function() {

				var src_image = getNextSibling(this.parentNode, 'DD').childNodes[0];
				var dst_image = document.getElementById('featuredPublication');
				dst_image.setAttribute('src', src_image.getAttribute('src'));
				dst_image.setAttribute('alt', src_image.getAttribute('alt'));
				dst_image.parentNode.setAttribute('href', dst_image.parentNode.getAttribute('href').substring(0, dst_image.parentNode.getAttribute('href').length-2) + this.getAttribute('id').substring(6) + '/');

				removeBreadCrumbs(this.parentNode.parentNode);
				this.className = 'breadcrumb';
				this.blur();
				return false;
			};
		}
		
	}
	
	if (document.getElementById('project')) {
		
		var anchors = document.getElementById('project').getElementsByTagName('A');
		for (var i = 0; i < anchors.length; i++) {
			anchors[i].onclick = function() {
				removeBreadCrumbs(document.getElementById('project'));

				if (this.href.indexOf('swf') != -1) {
					
					if (!document.getElementById('VideoPlayback')) {
						var objFlashContainer = document.createElement('EMBED');
						objFlashContainer.setAttribute('id', 'VideoPlayback');
						objFlashContainer.setAttribute('type', 'application/x-shockwave-flash');
						objFlashContainer.setAttribute('allowScriptAccess', 'sameDomain');
						objFlashContainer.setAttribute('quality', 'best');
						objFlashContainer.setAttribute('wmode', 'opaque');
						objFlashContainer.setAttribute('FlashVars', 'autoPlay=true');
					}
					
					else {
						var objFlashContainer = document.getElementById('VideoPlayback');
					}
					
					objFlashContainer.style.display = 'block';
					objFlashContainer.setAttribute('src', this.href);

					document.getElementById('container').insertBefore(objFlashContainer, document.getElementById('workImage'));
					
				}
				
				else {
					
					if (document.getElementById('VideoPlayback')) {
						document.getElementById('VideoPlayback').style.display = 'none';
					}
					
					document.getElementById('workImage').src = this.href;
					
				}
				
				this.className = 'breadcrumb';
				this.blur();
				return false;
			};
		}
		
	}
	
	if (document.getElementById('gallery')) {

		var images = document.getElementById('gallery').getElementsByTagName('IMG');
		for (var i = 0; i < images.length; i++) {
			if (document.all && navigator.userAgent.indexOf('Opera') < 0) {
				images[i].style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + images[i].currentStyle.backgroundImage.substring(5,(images[i].currentStyle.backgroundImage.length)-2) + '\', sizingMethod=\'image\')';
				images[i].style.backgroundImage = 'none';
			}
			images[i].onclick = function() {
				
				var dd = document.getElementById('gallery').getElementsByTagName('DD');
				for (var j = 0; j < dd.length; j++) {
					dd[j].style.visibility = 'hidden';
				}
				
				var nextSibling = this.parentNode.nextSibling;
				while (nextSibling.nodeType == 3) {
					nextSibling = nextSibling.nextSibling;
				}
				
				nextSibling.style.visibility = 'visible';
				
			};
		}

		//if (!(document.all && navigator.userAgent.indexOf('Opera') < 0)) {
			document.getElementById('gallery').style.width = parseInt((document.getElementById('gallery').getElementsByTagName('IMG').length * 350) + 300) + 'px';
		//}
	
	}
	
	
	if (document.getElementById('piggy')) {	
		ScrollMarquee();
	}	


}

function ScrollMarquee() {
	
	if (document.getElementById('piggy')) {	
		window.setTimeout('ScrollMarquee()', 200);
	
		var msg = document.getElementById('piggy').value;
		document.getElementById('piggy').value = msg.substring(1) + msg.substring(0,1);
	}
	
}


function hideMenu(id) {
	if (document.getElementById(id)) {
		document.getElementById(id).className = ''; 
	}
}

function getColor() {

	var colors = new Array('#00CCFF', '#0099FF', '#0066FF', '#0099FF', '#00CCFF', '#00FFFF');
	nextColor = (nextColor.length == 7) ? nextColor : colors[0];

	for (var i = 0; i < colors.length; i++) {
		if (colors[i] == nextColor) {
			nextColor = (i == 5) ? colors[0] : colors[(i+1)];
			break;
		}
	}

}

function toggleCollapse() {
	var nextSibling = this.nextSibling;
	while (nextSibling.nodeName != 'UL') {
		nextSibling = nextSibling.nextSibling;
	}

	var display = nextSibling.style.display;
	nextSibling.style.display = (display == 'none' || display == '' && nextSibling.className != 'visible') ? 'block' : 'none';
	return false;
}

function getNextSibling(element, type) {
	var nextSibling = element.nextSibling;
	while (nextSibling.nodeName != type) {
		nextSibling = nextSibling.nextSibling;
	}
	return nextSibling;
}

function removeBreadCrumbs(parent) {
	var anchors = parent.getElementsByTagName('A');
	for (var i = 0; i < anchors.length; i++) {
		anchors[i].className = '';
	}
}

function cleanWhiteSpace(element) {
	for (var i = 0; i < element.childNodes.length; i++) {
		var node = element.childNodes[i];
		if (node.nodeType == 3 && !/\S/.test(node.nodeValue)) {
			node.parentNode.removeChild(node);
		}
	}
}

function setInnerHTML(element, toValue) {
	if (typeof(element.innerHTML) != 'undefined') {
		element.innerHTML = toValue;
	}
	else {
		var range = document.createRange();
		rangthis.selectNodeContents(element);
		rangthis.deleteContents();
		element.appendChild(rangthis.createContextualFragment(toValue));
	}
}

function getInnerHTML(element) {
	if (typeof(element.innerHTML) != 'undefined') {
		return element.innerHTML;
	}
	else {
		var returnStr = '';
		for (var i = 0; i < element.childNodes.length; i++)
			returnStr += getOuterHTML(element.childNodes[i]);

		return returnStr;
	}
}