// check for layer/resolution and setWidth/SetLayout
// JPU: Be gentle when changing the indenting, this is too complex for me :-/
function setLayout() {
    if (document.getElementById('menuCoulisseAria') == null) {
        // collection
        if (document.getElementById('leftCoulisse') != null) {
            if (screen.width > 800) {
                document.getElementById('switchlayout').style.width = '650px';
                document.getElementById('switchlayout').style.left = '300px';
            } else {
                document.getElementById('switchlayout').style.left = '140px';
                document.getElementById('collectionMenu').style.width = '30px';
                document.getElementById('framework').style.width = '750px';
                document.getElementById('switchlayout').style.width = '610px';
           document.getElementById('leftCoulisse').style.left = '60px';
                if (document.getElementById('rightCoulisse') != null) 
                    document.getElementById('rightCoulisse').style.left = '750px';
            }
            if (document.getElementById('rightCoulisse') != null)
                document.getElementById('contentDivPad').style.padding = '20px 110px 20px 20px';
        } else {
            // collection no visuals
            if (screen.width > 800) {
                document.getElementById('switchlayout').style.width = '730px';
                document.getElementById('switchlayout').style.left = '220px';
            } else {
                document.getElementById('switchlayout').style.left = '60px';
                document.getElementById('collectionMenu').style.width = '30px';
                document.getElementById('framework').style.width = '750px';
                document.getElementById('switchlayout').style.width = '690px';
                if (document.getElementById('rightCoulisse') != null)
                    document.getElementById('rightCoulisse').style.left = '750px';
            }
            
            if (document.getElementById('rightCoulisse') != null)
                document.getElementById('contentDivPad').style.padding = '20px 110px 20px 20px';
        }
    } else {
        // aria
    if (screen.width > 800) {
            document.getElementById('switchlayout').style.width = '810px';
        if (document.getElementById('contentDivside') != null)
                document.getElementById('contentDivmain').style.width = '590px';
        } else {
      document.getElementById('framework').style.width = '750px';
      document.getElementById('switchlayout').style.width = '610px';
      if (document.getElementById('rightCoulisse') != null)
                document.getElementById('rightCoulisse').style.left = '750px';
      if (document.getElementById('contentDivside') != null)
      document.getElementById('contentDivmain').style.width = '570px';
      if (document.getElementById('contentDivside') != null)
                document.getElementById('contentDivside').style.display = 'none';
                /*document.getElementById('contentDivmain').style.width = '570px';
      if (document.getElementById('contentDivside') != null)
                document.getElementById('contentDivside').style.left = '414px';*/
    }
    document.getElementById('switchlayout').style.left = '140px';
        if (document.getElementById('leftCoulisse')) {
        document.getElementById('leftCoulisse').style.left = '60px';
        }
    if (document.getElementById('rightCoulisse') != null)
            document.getElementById('contentDivPad').style.padding = '20px 110px 20px 20px';
  }
}

//set min-height safari
var Browser = function(){
  this.ie = navigator.appName.toLowerCase().indexOf('microsoft') != -1 ? 1 : 0;
  this.mac =  navigator.userAgent.toLowerCase().indexOf('mac') != -1 ? 1 : 0;
  this.win = !this.mac ? 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.mac ? 1: 0;
  this.macIE = this.ie && this.mac ? 1: 0;
  this.safariVersion = function (){
    if (!this.safari) return false;
    var ua = navigator.userAgent.toLowerCase();
    var indexNum = ua.indexOf('safari/') + 7;
    var vNum = ua.substring(indexNum,indexNum+3);
    return parseInt(vNum,10);
  }
}
var browser = new Browser();
function SetHeight(){
  if (browser.safari){
    document.getElementById('contentDivPad').style.display = "table-cell";
    document.getElementById('contentDivPad').style.height = "556px";
  }
}
//window.onload = function() {SetHeight();}



function showArrow(){
  document.getElementById('png').style.display = 'block';
}
function hideArrow(){
  document.getElementById('png').style.display = 'none';
}

//read more 
function zie_ook(id){
  var e = document.getElementById(id).style
  e.display=(e.display=="block") ? "none" : "block"
  window.scrollTo(0,100000);
}

//droplist
function startList() {
if (document.all&&document.getElementById) {
  navRoot = document.getElementById("droplist");
  for (i=0; i<navRoot.childNodes.length; i++) {
    node = navRoot.childNodes[i];
  if (node.nodeName=="LI") {
    node.onmouseover=function() {
    this.className+=" over";}
    node.onmouseout=function() {
    var out = true;
      if(window.event)  {
        var element = window.event.toElement;
        while(element)  {
          out &=  element !=  this;
          element = element.parentNode;}
        }
      if(out) {
        this.className=document.getElementById('sublevel').className.replace(" over", " ");}
        }
      }
    }
  }
}
  
// 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)
  }
  
//swap images
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){
      o.onmouseover = function (){swapImage(this);}
      o.onmouseout = o.onmouseover;
      prelImgs[p] = new Image ();
      prelImgs[p].src= d[i].src.substring(0,o.src.length-8)+'_over.gif';
      p++;
    }
  }
}

function swapImage(o){
  var s = o.src;
  o.src = s.indexOf('_over') == -1 ?
  s.substring(0,s.length-8)+'_over.gif' :
  s.substring(0,s.length-9)+'_out.gif';
}


if (window.HTMLElement) {
  HTMLElement.prototype.contains = function(el)
  {
    var tempEl = el;
    while (tempEl!=null) {
      if (tempEl == this) return true;
      tempEl = tempEl.parentNode;
    }
    return false;
  };
}

function isInside(parentEl, evt) {
  if (window.HTMLElement) {
    // mozilla and alike
    return parentEl.contains(evt.relatedTarget);
  } else {
    return parentEl.contains(evt.toElement);
  }
}

var popupHandle;
function popup(pagina) {
  window.name = "eerstevenster";
  var width = 800;
  var height = 600;
  //
  // fullscreen if screenX=800
  if(navigator.appName == "Microsoft Internet Explorer") {
    screenX = window.screen.availWidth;
  }else{
    screenX = window.outerWidth
  }
  if (screenX==800) {
    var properties = "width=800,height=600,toolbar=no,status=no,resizable=no,scrollbars=no,fullscreen=yes";
  } else {
    var properties = "width=800,height=600,toolbar=no,status=no,resizable=no,scrollbars=no";
  }
  var leftprop, topprop, screenX, screenY, cursorX, cursorY, padAmt;
  if(navigator.appName == "Microsoft Internet Explorer") {
    screenY = document.body.offsetHeight;
    screenX = window.screen.availWidth;
  }else{
    screenY = window.outerHeight
    screenX = window.outerWidth
  }
  leftvar = Math.round((screenX - width) / 2);
  rightvar = Math.round((screenY - height) / 2);
  if(navigator.appName == "Microsoft Internet Explorer") {
    leftprop = leftvar;
    topprop = rightvar;
  }else {
    leftprop = (leftvar - pageXOffset);
    topprop = (rightvar - pageYOffset);
  }
  properties = properties + ",left=" + leftprop;
  properties = properties + ",top=" + topprop;
  popupHandle = open( pagina, "carteOrange", properties);
  setTimeout('popupHandle.focus();',250);

}



// mdi, nov 2005
function getEl(el){
  return document.getElementById(el);
}

// thanx Laurens (laurens@vd.oever.nl)
Function.prototype.closure = function(obj){
  if (!window.__objs){
    window.__objs = [];
    window.__funs = [];
  }
  var fun = this;
  var objId = obj.__objId;
  if (!objId)
    __objs[objId = obj.__objId = __objs.length] = obj;
  var funId = fun.__funId;
  if (!funId)
    __funs[funId = fun.__funId = __funs.length] = fun;
  if (!obj.__closures)
    obj.__closures = [];
  var closure = obj.__closures[funId];
  if (closure)
    return closure;
  obj = null;
  fun = null;
  return __objs[objId].__closures[funId] = function (){
    return __funs[funId].apply(__objs[objId], arguments);
  };
};

function addEvent(obj, evType, fn, useCapture){
  if (obj.addEventListener){
    obj.addEventListener(evType, fn, useCapture);
    return true;
  }
  else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, function() { fn.apply(obj); });
    return r;
  }
  else {
    alert("Handler could not be attached");
  }
}

function eventOnPartOfElement(e,element,to){
  var t;
  if (window.event){
    t = to ? window.event.toElement : window.event.srcElement;
  }
  else if (e){
    t = to ? e.relatedTarget : e.target;
  } 
  else {
    return false;
  }
  if (t == null){
    return false;
  }
  while (t.tagName != 'BODY'){
    if (t == element){
      return true;
    }
    t = t.parentNode;
  }
  return false;
}

// init flash headers
var hGCalHeight = 12;
var hGCalId = '';
  var prefix = '/';
  //var prefix = '';
  hGPath = prefix + '_swf/hg.swf';
  addSwfHeader({swfFont:prefix + '_swf/documenta_bold_rijks.swf',tagName:'h1',parentId:'info',size:22,color:'585B4B',paddingBottom:1}); 
  addSwfHeader({swfFont:prefix + '_swf/documenta_bold_rijks.swf',tagName:'h1',parentId:'bodyAria',size:22,color:'B6BDB7',paddingBottom:1});
  addSwfHeader({swfFont:prefix + '_swf/documenta_bold_rijks.swf',tagName:'h1',parentId:'nieuws',size:22,color:'35585C',paddingBottom:1}); 
  addSwfHeader({swfFont:prefix + '_swf/documenta_bold_rijks.swf',tagName:'h1',parentId:'tentoonstelling',size:22,color:'35585C',paddingBottom:1});
  addSwfHeader({swfFont:prefix + '_swf/documenta_bold_rijks.swf',tagName:'h1',parentId:'WhiteContent',size:22,color:'FFFFFF',paddingBottom:1}); 
  addSwfHeader({swfFont:prefix + '_swf/documenta_bold_rijks.swf',tagName:'h1',parentId:'vrienden',size:22,color:'475F3B',paddingBottom:1}); 
  addSwfHeader({swfFont:prefix + '_swf/documenta_bold_rijks.swf',tagName:'h1',parentId:'geschiedenis',size:22,color:'212930',paddingBottom:1}); 
  addSwfHeader({swfFont:prefix + '_swf/documenta_bold_rijks.swf',tagName:'h1',parentId:'contentDiv',size:22,color:'5C5538',paddingBottom:1});
  addSwfHeader({swfFont:prefix + '_swf/documenta_bold_rijks_sidecaps.swf',tagName:'h2',parentId:'homeCoulisse',rotate:270,size:13,paddingTop:5,paddingLeft:245,paddingBottom:5,color:'FFFFFF',upperCase:true,bGColor:'030303',bGColorAlpha:20});
  addSwfHeader({swfFont:prefix + '_swf/documenta_bold_rijks_sidecaps.swf',tagName:'h3',parentId:'rightCoulisse',rotate:90,size:13,paddingTop:4,paddingLeft:50,paddingRight:20,paddingBottom:5,color:'FFFFFF',upperCase:true,bGColor:'030303',bGColorAlpha:20});
  addSwfHeader({swfFont:prefix + '_swf/documenta_bold_rijks.swf',tagName:'h3',parentId:'subhomeCoulisse',size:32,color:'FFFFFF',paddingBottom:1}); 
  hGStart();
  addEvent (window,'load',windowLoader);
  function windowLoader(){
    hGLoader();
    addEvent(document.body,'click',coulisseCloseAll);
  }
  
  
  // 17 november 2005 Added by Mark D. en Matthijs Kl.
    function showContentFlash(){
      return;
      var list = document.getElementsByTagName('object');
      for (var i = 0; i < list.length; i++){
        var o = list[i];
        if (o.parentNode.className == 'flashWrapper'){
          o.parentNode.style.display = 'block';
          o.style.width = '100px';
          o.style.display = 'block !important';
          o.style.height = '50px';
        }
      }
    }
    
  function openFile(file){
		location.href = (file);
	}
  

 // set scrolltop, mdi
function setScrollTop(y){
	//can't get proper discriminating object detection for safari
	if (/WebKit/i.test(navigator.userAgent)){
		document.body.scrollTop = y;
	}
	else if (document.documentElement && 
    typeof document.documentElement.scrollTop != "undefined"){
		document.documentElement.scrollTop = y;
	}
};
  
  
  // flashObject 1.3, Mark Dibbets, Fabrique, update mei 2006
    
// getElement.. wrapper 
function getEl(el){
  return document.getElementById(el);
};

// setClassName
function setClassName(el,className){
  if (!className){
    return;
  }
  if (el.getAttribute('className') != null){
    el.setAttribute('class',className);
  }
  else {
    el.setAttribute('class',className);
  }
};

// add stylesheet
var styleSheetFromScript = false;
function addStyle(selector,properties){
  if (document.styleSheets) {
    if (!styleSheetFromScript){
      styleSheetFromScript = document.createElement('style');
      styleSheetFromScript.setAttribute('type','text/css');
      document.getElementsByTagName('HEAD')[0].appendChild(styleSheetFromScript);
    }
    var lastSheet = document.styleSheets[document.styleSheets.length - 1];
    if(lastSheet && typeof lastSheet.addRule == 'object'){
      lastSheet.addRule(selector, properties);
    }
    else {
      styleSheetFromScript.appendChild(document.createTextNode(selector + ' { ' + properties + ' }'));
    }
 }
};
  
// check if the proper version is installed
function hasMinFlashVersion(versionNumber){
	if (window.ActiveXObject){
		try{
			var flash = new ActiveXObject("ShockwaveFlash.ShockwaveFlash." + versionNumber);
			return 1;
		}
		catch(e){
			return 0;
		}
	}
	else {
		if(navigator.plugins.length){
			for (var i=0; i < navigator.plugins.length; i++){
				var pluginIdent = navigator.plugins[i].description.split(" ");
				if(pluginIdent[0] == "Shockwave" && pluginIdent[1] == "Flash"){
					var versionArray = pluginIdent[2].split(".");
					return versionArray[0] >= versionNumber;
				}
			} 	
		}
	}
	return 0;
};

// Class FlashObject
flashObjects = [];
function FlashObject(oArg){

  this.id = oArg.id;
  this.parentId = oArg.parentId;      
  this.uri = oArg.uri;
  this.width = oArg.width;
  this.height = oArg.height;
  this.hasRequiredVersion = hasMinFlashVersion(oArg.requiredVersion);
  this.className = oArg.className ? oArg.className : '';
  this.params = oArg.params;
  this.noFocus = oArg.noFocus;
  if (this.hasRequiredVersion){
    addStyle('#' + oArg.parentId + ' .alternateContent','display:none !important;');
  }
  
  // create the object
  this.create = function(){
    if ((!this.hasRequiredVersion) || (!getEl(this.parentId))){
      return;
    }
    var obj = document.createElement('object');
    obj.setAttribute('type','application/x-shockwave-flash');
    if (this.id){
      obj.setAttribute('id',this.id);
    }
    obj.setAttribute('data',this.uri);
    obj.setAttribute('src',this.uri);
    obj.setAttribute('width',this.width);
    obj.setAttribute('height',this.height);
    if (this.noFocus){
      obj.setAttribute('tabIndex',-1);
    }
    setClassName(obj,this.className);
    for (var i in this.params){
      var param = this.params[i];
      var paramEl = document.createElement('param');
      paramEl.setAttribute('name',i);
      paramEl.setAttribute('value',this.params[i]);
      obj.appendChild(paramEl);
      obj.setAttribute(i,this.params[i]);
    }
    getEl(this.parentId).appendChild(obj);
    try {
      obj.loadMovie(0,this.uri);
    }
    catch (e){
    }
  }
  
};

// make a flash object
function createFlashObject(oArg){
  flashObjects[flashObjects.length] = new FlashObject(oArg);
};

// create all the flash objects (onload)
function createFlashObjects(){
  for (var i in flashObjects){
    flashObjects[i].create();
  }
};

addEvent(window,'load',createFlashObjects);

/*
* static Class DipBox
* v1.2, mdi, 15 oct 2007
* sort of scalable Lightbox for specials and '.swf links' 
*/
  
var DipBox = {

  /*
  * Class variables
  */
  
  requiredFlashVersion:7,
  marginTop:20,
  marginRight:85,
  marginBottom:20,
  marginLeft:85,
  textNodes: {
    en: {
      buttonOpen:'Enlarge this special',
      buttonClose:'Restore this special'
    },
    nl: {
      buttonOpen:'Vergroot deze special',
      buttonClose:'Verklein deze special'
    }
  },
  originalParentNode:null,
  
  /*
  * functions
  */
  
  //setup
  setup:function(){
    DipBox.setupFSLink();
    DipBox.setupFlashURLs();
    addEvent(window,'resize',DipBox.setFullScreenFlashSize);
  },
  
  //setup the link
  setupFSLink:function(){
    if ((!DipBox.flashId) || (!getEl('contentDivhead'))){
      return;
    }
    var button = document.createElement('div');
    var lang = document.getElementsByTagName('html')[0].getAttribute('lang');
    button.appendChild(document.createTextNode(DipBox.textNodes[lang].buttonOpen));
    button.setAttribute('id','dipbox-button-open');
    button.setAttribute('flashId',DipBox.flashId);
    getEl(DipBox.flashId + '_container').appendChild(button);
    addEvent(button,'click',
      function(){
        DipBox.createFSFromObject(getEl(this.getAttribute('flashId')));
      }
    )
  },
  
  //setup links with 'swf' in it to show Fullsceen in DipBox
  setupFlashURLs:function(){
    var list = document.getElementsByTagName('a');
    for (var i = 0; i < list.length; i++){
      var url = list[i].getAttribute('href');
      if (!url || url.indexOf('.swf') == -1){
        continue;
      }
      list[i].setAttribute('url',url);
      list[i].setAttribute('href','javascript:void(0)');
      addEvent(list[i],'click',
        function(){
          DipBox.createFSFromURL(this.getAttribute('url'));
        }
      );
    }
  },
  
  //set flash id from special
  setFlashId:function(id){
    DipBox.flashId = id;
    addStyle('h1','margin-right:160px !important;'); //hackery
  },
  
  // create the FS container with DOM functions
  setupFSContainer:function(){
    
    if (getEl('dipbox')){
      return;
    }
    
    var elContainer = document.createElement('div');
    elContainer.setAttribute('id','dipbox');
    
    var buttonClose = document.createElement('div');
    buttonClose.setAttribute('id','dipbox-button-close');
    var lang = document.getElementsByTagName('html')[0].getAttribute('lang');
    buttonClose.appendChild(document.createTextNode(DipBox.textNodes[lang].buttonClose));
    addEvent(buttonClose,'click',DipBox.closeFullScreen);   
    
    var elInner = document.createElement('div');
    elInner.setAttribute('id','dipbox-inner');
    
    var elBG = document.createElement('div');
    elBG.setAttribute('id','dipbox-bg');
    
    elContainer.appendChild(buttonClose);
    elContainer.appendChild(elInner);
    elContainer.appendChild(elBG);
    document.body.appendChild(elContainer);

  },
  
  //create from url
  createFSFromURL:function(url){
    DipBox.setupFSContainer();
    DipBox.originalParentNode = null;
    var fO = new FlashObject({
      parentId:'dipbox-inner',
      uri:url,
      className:'flash-object',
      requiredVersion:DipBox.requiredFlashVersion
    });
    fO.create();
    DipBox.showFullScreen();
  },
  
  //create from object (DOM element)
  createFSFromObject:function(obj){
    DipBox.setupFSContainer();
    DipBox.originalParentNode = obj.parentNode;
    getEl('dipbox-inner').appendChild(obj);
    DipBox.showFullScreen();
  },
  
  //show full screen (shared function from url and object)
  showFullScreen:function(oArg){
    setScrollTop(0);
    document.getElementsByTagName('html')[0].style.overflow = 'hidden';
    getEl('dipbox').style.display = 'block';
    DipBox.setFullScreenFlashSize();
  },
  
  //close the damn thing
  closeFullScreen:function(){
    var elObj = getEl('dipbox-inner').getElementsByTagName('object')[0];
    if (DipBox.originalParentNode){
      DipBox.originalParentNode.appendChild(elObj);
      elObj.style.height = '';
      elObj.style.width = '';
    }
    else {
      elObj.parentNode.removeChild(elObj);
    }
    getEl('dipbox').style.display = 'none';
    document.getElementsByTagName('html')[0].style.overflow = 'auto';
  },
  
  //set flash size with script: some browsers have problems with style only
  setFullScreenFlashSize:function(){
    var elParent = getEl('dipbox-inner');
    if (!elParent){
      return;
    }
    elObj = elParent.getElementsByTagName('object')[0];
    if (!elObj){
      return;
    }
    elObj.style.width = elParent.offsetWidth - (DipBox.marginLeft + DipBox.marginRight) + 'px';
    elObj.style.height = elParent.offsetHeight - (DipBox.marginTop + DipBox.marginBottom) + 'px';
    elObj.style.left = DipBox.marginLeft + 'px';
    elObj.style.top = DipBox.marginTop + 'px';
  }
  
};

//initialize
addEvent(window,'load', function(){ setTimeout('DipBox.setup()',10) });


//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;
}