/* JavaScript : koninklijkhuis.nl */
/* versie: 1.2.2 - 07/11/2007 */
/* auteur: Peter-Paul Koch - eend.nl */
/* origineel: Mark Dibbets - fabrique.nl */
  
  
  
var W3CDOM = document.createElement && document.getElementsByTagName;

window.onload = function(){
	if (!W3CDOM) return;
	active();
	navFocus();
	reposLastMenuItems();
//	if (browser.winIE)
//		document.body.appendChild(backgroundElement);
	initMovie();
	initZoom();
	initBlockLinks();
	initPopups();
//	initRSSFeeds();
}

  
  // browser sniffer
	function Browser(){
	  this.iE = navigator.appName.toLowerCase().indexOf('microsoft') != -1 ? 1 : 0;
	  this.mac =  navigator.userAgent.toLowerCase().indexOf('mac') != -1 ? 1 : 0;
	  this.win = navigator.userAgent.toLowerCase().indexOf('windows') != -1 ? 1 : 0;
	  this.safari =  navigator.userAgent.toLowerCase().indexOf('safari') != -1 ? 1 : 0;
	  this.mozilla = navigator.appName.toLowerCase().indexOf('netscape') != -1 && !this.safari ? 1 : 0;
	  this.winIE = this.iE && this.win ? 1 : 0;
	  this.macIE = this.iE && this.mac ? 1 : 0;
	}
	var browser = new Browser();
	
  
	// framebuster
	if (window!= top)top.location.href=location.href;


  
  /* changes classNames for browsers which don's support 
  *  the 'hover' pseudo-element for list-items.
  *  Also the navFocus function needs this one
  */
  var activeMenu;
/*
var backgroundElement = document.createElement('iframe');
backgroundElement.frameBorder = 0;
backgroundElement.style.position = 'absolute';
backgroundElement.style.zIndex = '0';
backgroundElement.style.overflow = 'hidden';
backgroundElement.style.display = 'none';
*/

var movieZichtbaarTimeout = null;

active = function() {
//	if (!browser.winIE) return;
	var nav = document.getElementById("nav");

 // Eend - 071106 - Added GovVid implementation to selection of movies to hide on foldout of main menu.
	var movie = document.getElementById('movie') || document.getElementById('kh_govvid_movie');
	if (!nav) return;
	var e = nav.childNodes;
	for (var i=0; i<e.length; i++) {
		if (e[i].nodeName != 'LI') continue;
		
		//mouseover
		e[i].onmouseover = function () {
			if (movie) movie.style.visibility = 'hidden';
			if (movieZichtbaarTimeout) clearTimeout(movieZichtbaarTimeout);
		}
		
		
		e[i].onmouseenter=function() {
//			if (activeMenu)activeMenu.onmouseleave();
			activeMenu = this;
			var foldOut = this.getElementsByTagName('ul')[0];
			if (!foldOut) return;
/*			var bgWidth = foldOut.offsetWidth;
			var bgHeight = foldOut.offsetHeight;
			var bgPos = findPos(foldOut);
			backgroundElement.style.width = bgWidth + 'px';
			backgroundElement.style.height = bgHeight + 'px';
			backgroundElement.style.left = bgPos[0] + 'px';
			backgroundElement.style.top = bgPos[1] + 'px';
			backgroundElement.style.display = 'block'; */
			this.className+=" active";
		}
		//mouseout
		e[i].onmouseleave=function() {
			this.className=this.className.replace(/active/g, '')
/*			backgroundElement.style.display = 'none'; */
		}
		e[i].onmouseout = function () {
			if (movie) {
				movieZichtbaarTimeout = setTimeout(function () {
					movieZichtbaar(movie)
				},250);
			}
		}

	}
}


function movieZichtbaar(movie) {
	if (browser.iE)
		initMovie();
	movie.style.visibility = 'visible';
}
  
  //  makes the menu accessible by keyboard
  navFocus = function(){
    var nav = document.getElementById("nav");
    if (!nav){return;}  
    var list = nav.getElementsByTagName("A");
    for (var i = 0; i < list.length; i++){
      var e = list[i];
      if (e.parentNode.parentNode.id == 'nav'){
        e.onfocus = function(){this.parentNode.onmouseover();}
      }
    }
  }
  
  /* reposition the x-position of the last two menu items 
  *  dropdowns when the fonts are larger then default
  * not very useful in my opinion
  */
  reposLastMenuItems = function(){  
    var footer = document.getElementById('footer'); 
    if (footer && footer.offsetHeight > 38){
      var list = document.getElementById("nav").getElementsByTagName("UL");
      for (var i = 0; i < list.length; i++){
         list[i].className = 'normalMargin';
      }
    }
  }

/* QUICKTIME DETECT */

var QuickTime = {
	supported: false,
	mime: [
		'video/x-m4v',
		'video/quicktime'
	],
	vb: [
		'QuickTimeCheckObject.QuickTimeCheck.1'
	],
	detect: function () {
		if (window.opera) return;
		if (navigator.plugins.length) {
			for (var i=0;i<this.mime.length;i++) {
				if (navigator.mimeTypes[this.mime[i]]) {
					this.supported = true;
					break;
				}
			}
		}
		else {
			for (var i=0;i<this.vb.length;i++) {
				try {
					var testObject = new ActiveXObject(this.vb[i]);
					this.supported = true;
					break;
				}
				catch (e) {}
			}
		}
	}
}
QuickTime.detect();

/* CREEER QUICKTIME MOVIE */

function initMovie() {
	var movieContainer = document.getElementById('movie');
	if (!movieContainer) return;
	if (!QuickTime.supported) return;
	var movieSrc;
	if (!movieContainer.movieSrc) {
		movieSrc = document.getElementById('movieSRC').href;
		movieContainer.movieSrc = movieSrc;
	}
	else
		movieSrc = movieContainer.movieSrc;
	var newEmbed = document.createElement('embed');
	newEmbed.setAttribute('type','video/quicktime');
	newEmbed.setAttribute('src',movieSrc);
	if (window.hotspot) {
		for (var i=0;i<hotspot.length;i++) {
			if (!hotspot[i]) continue;
			newEmbed.setAttribute('hotspot'+i,hotspot[i]);
		}
	}
	newEmbed.setAttribute('autostart','true');
	newEmbed.setAttribute('width','413');
	newEmbed.setAttribute('height','252');
	newEmbed.setAttribute('controller','true');
	newEmbed.setAttribute('kioskmode','true');
	movieContainer.innerHTML = '';
	movieContainer.appendChild(newEmbed);
}

/* FOTO'S ZOOMEN */

var verkleinTekst = 'Verkleinen';
function initZoom() {
	var x = document.getElementsByTagName('div');
	for (var i=0;i<x.length;i++) {
		if (x[i].className.indexOf('zoom') != -1) {
			var link = x[i].getElementsByTagName('a')[0];
			link.normalPhoto = link.getElementsByTagName('img')[0];
			link.onclick = zoomPhoto;
		}
	}
	if (document.documentElement.lang == 'en')
		verkleinTekst = 'Reduce';
}

function zoomPhoto() { // this is de link
	if (!this.zoomedPhoto) {
		this.zoomedPhoto = createImg(this.href);
		this.insertBefore(this.zoomedPhoto,this.normalPhoto);
	}
	this.relatedSpan = this.getElementsByTagName('span')[0];
	this.relatedSpan.originalText = this.relatedSpan.firstChild.nodeValue;
	this.relatedSpan.className = 'zoomout';
	this.relatedSpan.innerHTML = verkleinTekst;
	var zoomPhoto = this.zoomedPhoto;
	this.normalPhoto.style.display = 'none';
	this.zoomedPhoto.style.display = 'block';
	if (!zoomPhoto.width) {
		var breedteMeting = setInterval(function () {
			if (zoomPhoto.width > 0) {
				sizeContent(zoomPhoto);
				clearInterval(breedteMeting);
			}	
		},100);
	}
	else
		sizeContent(zoomPhoto);
	this.onclick = unzoomPhoto;
	return false;
}

function unzoomPhoto() { // this is de link
	this.normalPhoto.style.display = 'block';
	this.zoomedPhoto.style.display = 'none';	
	this.zoomedPhoto.parentNode.className = '';
	this.parentNode.style.width = '';
	this.relatedSpan.style.marginLeft = '';
	this.onclick = zoomPhoto;
	this.relatedSpan.className = 'zoomin';
	this.relatedSpan.innerHTML = this.relatedSpan.originalText;
	return false;
}

function sizeContent(obj) { // obj is img.zoomPhoto, wiens parentNode de link is
	var fotoBreedte = obj.width;
	var contentVlak = document.getElementById('subcontent');
	var totaleBreedte = contentVlak.offsetWidth;
	var verschil = totaleBreedte - fotoBreedte;
	if (verschil < 180) {
		var breedte = totaleBreedte - 30;
		obj.parentNode.parentNode.style.width = breedte + 'px';
		obj.parentNode.className = 'zoomPhoto';
		obj.parentNode.relatedSpan.style.marginLeft = verschil - 30 + 'px';
	}
}

function createImg(source) {
	var newImage = document.createElement('img');
	newImage.src = source;
	return newImage;
}

/* LINKBLOKKEN VOOR HOMEPAGE */

function initBlockLinks() {
	if (document.body.className.indexOf('home') == -1) return;
	var x = document.getElementsByTagName('div');
	for (var i=0;i<x.length;i++) {
		if (x[i].className == 'zieook' || x[i].parentNode.id == 'block' || x[i].parentNode.id == 'announcement') initOneBlockLink(x[i]);
	}
}

function initOneBlockLink(obj) {
	var link = obj.getElementsByTagName('a')[0];
	var linkHref = link.href;
	obj.style.cursor = 'pointer';
	obj.onclick = function () {
		location.href = linkHref;
	}
	obj.onmouseover = function () {
		link.style.textDecoration = 'underline';
	}
	obj.onmouseout = function () {
		link.style.textDecoration = 'none';	
	}
}

/* POPUPS VOOR PDF-LINKS */

function initPopups() {
	var x = document.getElementsByTagName('a');
	for (var i=0;i<x.length;i++) {
		if (x[i].getAttribute('type') == 'application/pdf')
			x[i].onclick = openPopup;
	}
}

/* RSS FEEDS GENEREREN VOOR ABONNEERPAGINA */

var RSSReadroot,RSSWriteroot;

function initRSSFeeds() {
	RSSReadroot = document.getElementById('rss');
	if (!RSSReadroot) return;
	RSSReadroot.onclick = createRSSFeed;
	RSSWriteroot = document.getElementById('rss-url');
	RSSReadroot.elements[RSSReadroot.elements.length-1].style.display = 'none';
	RSSReadroot.onclick();
}

function createRSSFeed() {
	var x = this.elements;
	var aanvinkFlag = false;
	var RSSString = 'http://www.koninklijkhuis.nl/rss/';
	for (var i=0;i<x.length;i++) {
		if (x[i].type != 'checkbox') continue;
		if (!x[i].checked) continue;
		RSSString += x[i].name + '/';
		aanvinkFlag = true;
	}
	if (!aanvinkFlag) {
		RSSWriteroot.style.display = 'none';
		return;
	}
	else
		RSSWriteroot.style.display = 'block';		
	if (!this.writeroot)
		genereerRSSContainer(RSSString);
	else
		this.writeroot.href = this.writeroot.innerHTML = RSSString;
}

function genereerRSSContainer(linkText) {
	var par = document.createElement('p');
	par.appendChild(document.createTextNode('Uw gegenereerde URL is:'));
	var span = document.createElement('span');
	var link = document.createElement('a');
	link.href = link.innerHTML = linkText;
	span.appendChild(link);
	par.appendChild(span);	
	RSSWriteroot.appendChild(par);
	RSSReadroot.writeroot = link;
}

/* POPUPS OPENEN */

function openPopup() {
	
	/* Deze return weghalen als je de popup wilt openen */
	
	return;
	
	/* hier nog gewenste width en height invullen */
	
	window.open(this.href,'PDFData','width=500,height=400,resizable=yes');
	return false;
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}