// 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.opera =  navigator.userAgent.toLowerCase().indexOf('opera') != -1 ? 1 : 0;    
  this.mozilla = navigator.appName.toLowerCase().indexOf('netscape') != -1 && !this.safari ? 1 : 0;
  this.winMozilla = this.mozilla && this.win ? 1 : 0;
  this.winIE = this.iE && this.win && !this.opera ? 1 : 0;
  this.macIE = this.iE && this.mac ? 1 : 0;
}
var browser = new Browser();

//incl. .js
document.write('<script type="text/javascript" src="/_js/homepage/library.js"><\/script>');
//document.write('<script type="text/javascript" src="scripts/jquery.js"><\/script>');

// framebuster
if (window!= top)top.location.href=location.href;

// addStyle
function addStyle(selector,properties){
  if (document.styleSheets) {
    var s = document.getElementsByTagName('STYLE');
    if (s.length == 0){
      var sheet = document.createElement('style');
      sheet.setAttribute('type','text/css');
      document.getElementsByTagName('HEAD')[0].appendChild(sheet);}
    if (browser.winIE){
      var lastSheet = document.styleSheets[document.styleSheets.length - 1];
      lastSheet.addRule(selector, properties);}
    else {var lastSheet = s[0];
      lastSheet.appendChild(document.createTextNode(selector + ' { ' + properties + ' }'));}
  	}
};
addStyle('.drempel','display:none;');

// getElementById
function getEl(id){
  return document.getElementById(id);
}

//top nav
var activeMenu;
 active = function(id) {
 var nav = document.getElementById(id);
 var hbg = document.getElementById('content').getElementsByTagName('select');
 if (!nav){return;}
var e = nav.getElementsByTagName("LI");
for (var i=0; i<e.length; i++) {
	e[i].onmouseover=function() {
     if (activeMenu){activeMenu.onmouseout();}
     activeMenu = this;
		this.className+=" active";
		if (browser.iE){
			for (var i = 0; i < hbg.length; i++) {		
			hbg[i].style.visibility = 'hidden';
			}
		}
	}
	e[i].onmouseout=function() {
		this.className=this.className.replace(/active/g, '');
		if (browser.iE){
			for (var i = 0; i < hbg.length; i++) {		
				hbg[i].style.visibility = 'visible';
				}
			}
		}
	}
	navFocus(id,true);
};

navFocus = function(id,mouseOverMenu){
   var subnav = document.getElementById(id);
   if (!subnav){return;}  
   var list = subnav.getElementsByTagName("A");
   for (var i = 0; i < list.length; i++){
     var e = list[i];
     if (e.parentNode.parentNode.id == id){
	  if (mouseOverMenu){
	    e.onfocus = function(){this.parentNode.onmouseover();}
	  }
	  else {
	    e.onfocus = function(){this.parentNode.onclick();}
	  }
     }
   }
 if (list.length == 0){return;}
   var lastSub = list[list.length-1];
   if (lastSub.parentNode.parentNode.id == id){return;}
   lastSub.onblur = function(){
     this.parentNode.parentNode.parentNode.onmouseout();
   }
 };

 //lazy mouse
function createMouseOvers(){
var p = 0, d = document.images;
	prelImgs = new Array();
	for (var i = 0; i < d.length; i++){
		var o = d[i];
		if (o.src.indexOf('_out') != -1){
			var s = o.src, l = s.length, e = s.substring(l-4,l);
			o.outSrc = s;
			o.overSrc = s.substring(0,l-8)+'_over'+e;
			o.onmouseover = function(){this.src = this.overSrc;}
			o.onmouseout = function(){this.src = this.outSrc;}
			prelImgs[p] = new Image();
			prelImgs[p].src = o.overSrc;
			p++;
		}
	}
};

//search toggle value
var setSearch = function(id,key){
  var el = document.getElementById(id);
  if(!el) return false;
  el.setAttribute('key',key);
	el.onfocus = function(){
		if(this.value == this.getAttribute('key'))
      this.value = '';
	}
	el.onblur = function(){
		if(this.value == '')
      this.value = this.getAttribute('key');
	}
};

//navigation small
var setSmallNav = function(id){
  var el = document.getElementById(id);
  if(!el) return false;
	el.onmouseover = function(){
		this.className+=" over";
	}
	el.onmouseout = function(){
      this.className=this.className.replace(/over/g, '');
	}
};
	
// set external links
var setExternalLinks = function(){
var localDomain = (location.href.split('/')[2]);
var localExtensionsInNewWindow = Array('doc','xls','pdf');
var list = document.getElementsByTagName('A');
	for (var i = 0; i < list.length; i++){
	var aEl = list[i];
	var aElHref = aEl.href;
// check for extensions
var extension = (aElHref.substring(aElHref.length - 4,aElHref.length)).toLowerCase();
var foundExtension = false;
	for (var j = 0; j < localExtensionsInNewWindow.length; j++){
	if (extension == '.' + localExtensionsInNewWindow[j]){
		foundExtension = true;
	break;
	}
}
// open in new window if conditions are right
if ((aElHref.split('/')[2] != localDomain && aElHref.indexOf('mailto:') == -1 && aElHref.indexOf('javascript:') == -1 && aElHref.className == 'thickbox') || foundExtension){
		aEl.onclick = function(){
			window.open(this.href);
			return false;
			}
		}
	}
};

//getDocumentHeight
var yWithScroll;
var xWithScroll;
function getPageSizeWithScroll(){     
if (window.innerHeight && window.scrollMaxY) {         
	yWithScroll = window.innerHeight + window.scrollMaxY;         
	xWithScroll = window.innerWidth + window.scrollMaxX;} 
else if (document.body.scrollHeight > document.body.offsetHeight){      
	yWithScroll = document.body.scrollHeight;         
	xWithScroll = document.body.scrollWidth; } 
else {       
	yWithScroll = document.body.offsetHeight;         
	xWithScroll = document.body.offsetWidth; }     
arrayPageSizeWithScroll = new Array(xWithScroll,yWithScroll);     
//alert( 'The height is ' + yWithScroll + ' and the width is ' + xWithScroll );     
return arrayPageSizeWithScroll; 
}; 

//layout case switcher 
var currentLarge = 1;
function checkWindowWidth(){
//if(!document.getElementById(id)) return false;
	var smaller = ((document.documentElement.clientWidth > 0 && document.documentElement.clientWidth < 960) || document.body.clientWidth < 960);
	var elHTML = document.getElementsByTagName('html')[0];
  if (smaller) {
		if (currentLarge == 0){
			return;
		}
		currentLarge = 0;
    if (elHTML){
      elHTML.className = 'small';
    }
	}
	else {
		if (currentLarge == 1){
			return;
		}
		currentLarge = 1;
    if (elHTML){
      elHTML.className = '';
    }
	}
};

//checkWindowWidth();

// center popup script  
var win = null;
function newWindow(mypage,myname,w,h,scroll,resize){
  LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
  TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
  settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable='+resize+''
  win = window.open(mypage,myname,settings)
}


//readspeaker URL
function getReadspeakerURI(lang){
  var loc = location.href;
  var localDomain = 'http://' + loc.split('/')[2];
  loc = loc.replace(localDomain,'http://www.rijksmuseum.nl');
  if (loc.indexOf('?') == -1){ //browsertaalafhankelijke toevoeging
    loc = loc + '%253Flang=' + lang;
    return loc;
  }
  var loc =  loc.replace('?','%253F').replace(/&/g,'%2526');
  return loc;
}

//DOM loader
function init() {
	if (arguments.callee.done) return;
	arguments.callee.done = true;
	checkWindowWidth();
	if (getEl('navigation'))active('nav');
	if (getEl('viewport'))setSmallNav('viewport');
	setSearch('searchbox','zoeken');
	createFlashObjects();
	createMouseOvers();		
	setExternalLinks();
	getPageSizeWithScroll();
	try {document.execCommand('BackgroundImageCache', false, true);} catch(e) {}
};
if (document.addEventListener) {	document.addEventListener("DOMContentLoaded", init, null);}
if (/WebKit/i.test(navigator.userAgent)) { var _timer = setInterval(function() { if (/loaded|complete/.test(document.readyState)) { clearInterval(_timer); init();}}, 10);};
/*@cc_on @*/
	/*@if (@_win32)document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");var script = document.getElementById("__ie_onload");script.onreadystatechange = function() {	if (this.readyState == "complete") { init(); }};
/*@end @*/
window.onload = init
window.onresize = function(){
	checkWindowWidth();
};
