var appname = navigator.appName.toLowerCase();
var browserIE = (appname == "microsoft internet explorer");
var browserNN = !browserIE;
var browserVersion = navigator.appVersion.toLowerCase();
var browserOS = (browserVersion.indexOf("win") != -1)?"win":"mac";

// array push and pop support for IE50 and others
if (!Array.prototype.push)
  Array.prototype.push = function(el)
  {
    this[this.length] = el;
  };

if (!Array.prototype.pop)
  Array.prototype.pop = function()
  {
    var el = this[this.length-1];
    this.length--;
    return el;
  };

var NOCLOSE = false;
var popups =
{
};
var iframetop = null;
var iframemiddle = null;
var iframebottom = null;

var theTop = window;
if (theTop.opener && theTop.opener.popups)
  theTop = window.opener;
for (; theTop.parent != theTop && theTop.popups; theTop = theTop.parent);

function load()
{
  try
  {
    if (loader) loader();
  }
  catch(e)
  {
  }
  iframetop = theTop.document.getElementById("iframetop");
  iframemiddle = theTop.document.getElementById("iframemiddle");
  iframebottom = theTop.document.getElementById("iframebottom");
}
function fadeLogo(hide)
{
  var logo = document.getElementById('nbelogo');
  if (!logo) return;
  logo.style.display = hide? "none" : "";
  setTimeout("try { fadeLogo(" + !hide + "); } catch(e) {}", 5000);
}

function hideLightBox() {
    // if the current lightbox is not a photo AND lightBoxContentLeft is visible (i.e. if the photolightBox didn't link from another lightBox)
    if (document.getElementById("lightBoxPhoto").style.display == 'block' && document.getElementById("lightBoxContentLeft").style.display == 'block') {
        // set z-index of lightBoxBg back to 100
        document.getElementById("lightBoxBg").style.zIndex = '100';
        document.getElementById("lightBoxPhoto").style.display = 'none';
    } else {
        // hide lightboxIframes
        document.getElementById("lightBoxBg").style.display = 'none';
        document.getElementById("lightBoxCross").style.display = 'none';
        document.getElementById("lightBoxPhoto").style.display = 'none';
        document.getElementById("lightBoxPijl").style.display = 'none';
        document.getElementById("lightBoxContentLeft").style.display = 'none';
        document.getElementById("lightBoxContentRight").style.display = 'none';
        // empty lightboxIframes
        document.getElementById("lightBoxContentRight").src = '/empty.html';
        document.getElementById("lightBoxContentLeft").src = '/empty.html';
        // show flash
        document.getElementById("iframemiddle").style.visibility = 'visible';
        
        // set photoIframe back to default
        document.getElementById("lightBoxPhoto").src = '/empty.html';
        document.getElementById("lightBoxPhoto").style.height = "1px";
        document.getElementById("lightBoxPhoto").style.width = "1px";
        contentLeft = 0;
    }
}

var contentLeft=0;
function popup(href, id, rebuild, concertid) {
    // resize lightBoxContentLeft to browserHeight
    var newHeight = document.body.clientHeight - 140;
    document.getElementById('lightBoxContentLeft').style.height = newHeight + "px";

    // hide flash
    document.getElementById("iframemiddle").style.visibility = 'hidden';
    
    // show all lightbox related elements
    document.getElementById("lightBoxBg").style.display = 'block';
    document.getElementById("lightBoxPijl").style.display = 'block';
    document.getElementById("lightBoxContentLeft").style.display = 'block';
    document.getElementById("lightBoxContentRight").style.display = 'block';
    document.getElementById("lightBoxCross").style.display = 'block';

    
    // load and show iframes
    var popupname = href.replace(/http\:\/\//g, '').replace(/^[^\/]*(\/.*)/, '$1').replace(/\//g, '').toLowerCase();
    
    switch (popupname) {
        case "nieuws":
            //document.getElementById("lightBoxContentLeft").src = '/nieuws?id=' + id + '&buildframes=false'; //wordt in de file niews.xsl getriggerd
            document.getElementById("lightBoxContentRight").src = '/nieuws?buildframes=false';
            contentLeft = 1;
            break;
        case "gastenboek":
            document.getElementById("lightBoxContentLeft").src = '/gastenboek/?buildframes=false'
            document.getElementById("lightBoxContentRight").src = '/reageer.asp?upd=' + new Date().getTime();
            contentLeft = 1;
            break;
        case "watisjongnbe":
            document.getElementById("lightBoxContentLeft").src = '/watisjongnbe/?buildframes=false';
            document.getElementById("lightBoxContentRight").style.display = 'none';
            contentLeft = 1;
            break;
        case "contact":
            document.getElementById("lightBoxContentLeft").src = '/contact/?buildframes=false';
            document.getElementById("lightBoxContentRight").style.display = 'none';
            contentLeft = 1;
            break;
            case "compovandedag":
            document.getElementById("lightBoxContentLeft").src = '/opwegnaar/composities?id=' + id + '&concertid=' + concertid + '&iframe=true&buildframes=false&cvdd=true&frameless=true';
            document.getElementById("lightBoxContentRight").style.display = 'none';
            contentLeft = 1;
            break;
        case "watisjongnbe?id=colofononly":
            document.getElementById("lightBoxContentLeft").src = '/watisjongnbe/?id=colofononly&buildframes=false';
            document.getElementById("lightBoxContentRight").style.display = 'none';
            contentLeft = 1;
            break;           
        case "stemmee":
            document.getElementById("lightBoxContentLeft").src = '/stemmee';
            document.getElementById("lightBoxContentRight").style.display = 'none';
            contentLeft = 1;
            break;           
        default:
            if (popupname.indexOf('playlist') != -1) {
                if (contentLeft == 0) {
                    document.getElementById("lightBoxContentRight").style.display = 'none';
                    document.getElementById("lightBoxContentLeft").style.display = 'none';
                } else {
                    document.getElementById("lightBoxBg").style.zIndex = '200';
                }
                document.getElementById("lightBoxPijl").style.display = 'none';
                document.getElementById("lightBoxPhoto").src = href + ((href.indexOf('?') == -1) ? '?' : '&') + 'buildframes=false';
                document.getElementById("lightBoxPhoto").style.display = 'block';
            }
        }
/*

  
  //try
  //{
    if (!rebuild) 
      var rebuild = [true, true, true];
    var theleft = 200;
    var thetop = 50;
    var thewidth = 375;
    var theheight = 500;
    var popupname = href.replace(/http\:\/\//g,'').replace(/^[^\/]*(\/.*)/,'$1').replace(/\//g,'').toLowerCase();
    var resizable = (href.indexOf('playlist')!=-1)? "yes" : "no";
    var popupLoc1 = (popupname=='stemmee')? '/popupkroontje.html' : '/popuppijltje.html';
    var popupLoc2 = href + ((href.indexOf('?') == -1)? '?' : '&') + 'buildframes=false';
    var popupLoc3 = '';
    switch(popupname)
    {
      case "nieuws":
          popupLoc3 = popupLoc2;
          popupLoc2 = id? '/nieuws?id='+id+'&buildframes=false' : '';
          rebuild[0] = id? false : true;
          rebuild[2] = id? false : true;
          rebuild = [true, true, true];
          break;
      case "compovandedag":
          popupLoc2 = '/opwegnaar/composities?id=' + id + '&concertid=' + concertid + '&iframe=true&buildframes=false&cvdd=true';
          thewidth = 474;
          break;
      case "gastenboek":
        popupLoc3 = '/reageer.asp?upd=' + new Date().getTime();
        break;
      default:
          if (popupname.indexOf('overcomponeren')!=-1)
          {
            theleft = 100;
            thetop = 100;
            thewidth = 747;
            theheight = 106;
            rebuild[0] = false;
            rebuild[2] = false;
          }
          if (popupname.indexOf('agenda')!=-1)
          {
            theleft = 100;
            thetop = 100;
            thewidth = 747;
            theheight = 132;
            rebuild[0] = false;
            rebuild[2] = false;
          }
          if (popupname.indexOf('playlist')!=-1)
          {
            thewidth = 640;
            theTop.popups['popup4'] = window.open(popupLoc2, 'popup4', 'width='+thewidth+',height='+theheight+',left='+(theleft+30)+',top='+(thetop+20)+',menubar=no,scrollbars=yes,scroll=yes,resizable=yes,status=no,toolbar=no');
            rebuild = [false, false, false];
          }
      break;
    }
    theTop.NOCLOSE = true;
    for (var popup in theTop.popups)
    {
      try
      {
        if (!theTop.popups[popup].document) delete theTop.popups[popup];
      }
      catch(e)
      {
        delete theTop.popups[popup];
      }
    }
    if (rebuild[0])
    {
      if (theTop.popups['popup1'])
      {
        if (popupLoc1 != '') theTop.popups['popup1'].document.location.href = popupLoc1;
        else closepopup(theTop.popups['popup1']);
      }
      //popuppijltje
      else if (popupLoc1 != '') theTop.popups['popup1'] = window.open(popupLoc1, 'popup1', 'width=130,height=130,left='+(theleft-152)+',top='+thetop+',menubar=no,scrollbars=no,resizable=no,status=no,toolbar=no');
    }
    if (rebuild[1])
    {
      if (theTop.popups['popup2']) closepopup(theTop.popups['popup2']);
      if (popupLoc2 != '') theTop.popups['popup2'] = window.open(popupLoc2, 'popup2', 'width='+thewidth+',height='+theheight+',left='+theleft+',top='+thetop+',menubar=no,scrollbars=no,resizable='+resizable+',status=no,toolbar=no');
    }
    if (rebuild[2])
    {
      if (theTop.popups['popup3']) closepopup(theTop.popups['popup3']);
      if (popupLoc3 != '') theTop.popups['popup3'] = window.open(popupLoc3, 'popup3', 'width=210,height=360,left='+(theleft+397)+',top='+thetop+',menubar=no,scrollbars=no,resizable=no,status=no,toolbar=no');
    }
    if ((popupLoc2 != '') && (browserOS=='mac')) theTop.popups['popup2'] = window.open(popupLoc2, 'popup2', 'width='+thewidth+',height='+theheight+',left='+theleft+',top='+thetop+',menubar=no,scrollbars=no,resizable='+resizable+',status=no,toolbar=no');
    if (theTop.popups["popup1"]) theTop.popups["popup1"].focus();
    if (theTop.popups["popup2"]) theTop.popups["popup2"].focus();
    if (theTop.popups["popup3"]) theTop.popups["popup3"].focus();
    if (theTop.popups["popup4"]) theTop.popups["popup4"].focus();
    theTop.NOCLOSE = false;
  //}
  //catch(e)
  //{
  //}
 */
}

function openNewsItem(id)
{
  if (!id)
      return;
  /*ron:*/parent.document.getElementById("lightBoxContentLeft").src = '/nieuws/?id=' + id;
  /*var url = "/nieuws?id=" + id;
  var theleft = 200;
  var thetop = 50;
  var thewidth = 375;
  var theheight = 500;
  theTop.popups['popup2'] = window.open(url, 'popup2', 'width='+thewidth+',height='+theheight+',left='+theleft+',top='+thetop+',menubar=no,scrollbars=no,resizable=no,status=no,toolbar=no');*/
}

function closepopup(thepopup)
{
  try
  {
    for (var popup in theTop.popups)
    {
      if (thepopup == theTop.popups[popup])
      {
        theTop.popups[popup].unload = function(){}
        theTop.popups[popup].close();
        delete theTop.popups[popup];
      }
    }
  }
  catch(e)
  {
  }
}
function closepopups()
{
  if (theTop.NOCLOSE) return;
  try
  {
    for (var popup in theTop.popups)
    {
      theTop.popups[popup].unload = function(){}
      theTop.popups[popup].close();      
      delete theTop.popups[popup];
    }
  }
  catch(e)
  {
  }
}
function gotosubhome(href)
{
  try
  {
    if (iframebottom)
      iframebottom.setAttribute("src", href);  
  } catch(e)
  {
    return true;
  }
}
function gotocontent(href, id, tab)
{
  try
  {
    if (theTop.iframebottom)
      theTop.iframebottom.setAttribute("src", href + "?id=" + id + "&tab=" + tab);  
  } catch(e)
  {
    return true;
  }

  if (href=='/agenda') href="/opwegnaar";
  if ((href=='/opwegnaar') || (href=='/overcomponeren') || (href=='/hethalfuur') || (href=='/linkslezenluisteren'))
  {
    var tophref = (href=='/linkslezenluisteren')? "/overcomponeren" : href;
    iframetop.src = "/logo?buildframes=false&orihref=" + tophref + "&showup=true";
    if (iframemiddle.style.display=='none')
    {
      iframebottom.src = href + '?id='+id+'&buildframes=false' + (tab? '&tab=' + tab : '&tab=0');
    }
    else
    {
      iframetop.src = "/logo?buildframes=false&orihref=" + href + "&showup=true";
      iframemiddle.src = "/empty.html";
      if (browserNN || browserOS=='mac')
      {
        var h = iframebottom.offsetHeight;
        iframemiddle.style.display = "none";
        iframebottom.src = href + '?id='+id+'&buildframes=false' + (tab? '&tab=' + tab : '&tab=0');
        if (browserOS=='mac')
        {
          iframebottom.style.height ="auto";
          iframebottom.style.height = (h-90) + "px";
        }
        else
        {
          iframebottom.style.height ="auto";
          iframebottom.style.height = (h + 130) +"px"
        }
        if (href!='/opwegnaar')
        {
          iframebottom.style.height = "100%";
        }
      }
      else
      {
        animator.start(
        {
          element:iframemiddle,
          attribute:"height",
          target:1,
          duration:800,
          endCode:'iframemiddle.style.display="none";iframebottom.src="'+href+'?id='+id+'&buildframes=false' + (tab? '&tab=' + tab : '&tab=0') + '";'
        }
        );
      }
    }
  }
  return false;
}
function openIntrotab (tab)
{
  document.location.href="default.asp?buildframes=false&introtab="+tab;
}
function doIntrotabMouseover(tab)
{
  for (var i=0; i<20; i++)
  {
    var elem=document.getElementById ("intro_arrow"+i);
    if (elem) elem.style.visibility="hidden";
  }
  document.getElementById ("intro_arrow"+tab).style.visibility="visible";
}
var visitor =
{
  setFlashColor: function(theColor) {
    for (var i = 0; i < theTop.frames.length; i++) {
      var theFrame = theTop.frames[i];
      if (theFrame.setFlashColor) theFrame.setFlashColor(theColor);
    }
  }
,
  setFlashSound: function(how) {
    for (var i = 0; i < theTop.frames.length; i++) {
      var theFrame = theTop.frames[i];
      if (theFrame.setMusic)
        theFrame.setMusic(how);
    }
  }
,
  getParentWithId: function(el, id) {
    while (el.id != id && el.parentNode) el = el.parentNode;
    return (el.id == id) ? el : false;
  }
,
  doPlay: function(el, id, isVideo) {
    var player = document.getElementById("player" + id);
    var placeholder = document.getElementById("placeholder" + id);

    switch (player.PlayState) {
      case 0:
        this.setFlashSound(1);
        if ((isVideo) && (placeholder)) placeholder.style.visibility = "hidden";
        player.style.visibility = "visible";
      case 1:
        this.setFlashSound(0);
        player.play();
        el.src = "/_img/pause.gif";
        break;
      case 2:
        player.pause();
        el.src = "/_img/play.gif";
        break;
    }
  }
,
  doStop: function(el, id, isVideo) {
    var player = document.getElementById("player" + id);
    var playbutton = document.getElementById("playbutton" + id);
    var placeholder = document.getElementById("placeholder" + id);
    player.stop();
    this.setFlashSound(1);
    playbutton.src = "/_img/play.gif";
    player.style.visibility = "hidden";
    if ((isVideo) && (placeholder)) placeholder.style.visibility = "visible";
  }
,
  isEmail: function(emailaddress) {
    var array = emailaddress.match(/\b[\w\.\-]+\@\w+[\w\.\-]*\.\w{2,3}/g);
    return array ? (array.length == 1) : false;
  }
,
  prevote: function(nr) {
    document.getElementById("rater").className = "rating" + nr;
    document.getElementById("rating").value = nr;
  }
,
  vote: function(id, cid) {
    this.wc('c_' + id + '_' + cid, '1');
  }
,
  react: function(title, reaction, name) {
    if ((browserOS == 'mac') && (browser = 'IE')) {
      theTop.NOCLOSE = true;
      theTop.setTimeout("popup('/gastenboek');", 200);
      document.getElementById("theform").submit();
      return;
    }
    if (!title || !reaction || !name) {
      alert("Niet alle velden zijn ingevuld!");
      return false;
    }
    var xml = this.initXML('<react />');
    var t = xml.createElement('title');
    t.appendChild(t.ownerDocument.createTextNode(title));
    var c = xml.createElement('content');
    c.appendChild(c.ownerDocument.createTextNode(reaction));
    var n = xml.createElement('name');
    n.appendChild(n.ownerDocument.createTextNode(name));
    xml.documentElement.appendChild(t);
    xml.documentElement.appendChild(c);
    xml.documentElement.appendChild(n);
    this.send(xml);
    popup('/gastenboek');
  }
,
  subscribe: function(name, emailaddress) {
    return;
    if (!this.isEmail(emailaddress)) return alert("Het email adres '" + emailaddress + "' is geen valide emailadres.");
    if (!name) return alert("Niet alle velden zijn ingevuld!");
    if ((browserOS == 'mac') && (browser = 'IE')) {
      //theTop.NOCLOSE = true;
      //document.getElementById("theform").submit();
      return;
    }
    var xml = this.initXML('<subscribe />');
    var f = xml.createElement('firstname');
    f.appendChild(f.ownerDocument.createTextNode(name));
    var l = xml.createElement('lastname');
    l.appendChild(l.ownerDocument.createTextNode(name));
    var e = xml.createElement('email');
    e.appendChild(e.ownerDocument.createTextNode(emailaddress));
    xml.documentElement.appendChild(f);
    xml.documentElement.appendChild(l);
    xml.documentElement.appendChild(e);
    this.send(xml);
    popup('/watisjongnbe');
  }
,
  initXML: function(xmlString) {
    var xml = browserIE ? new ActiveXObject("Microsoft.XMLDOM") : document.implementation.createDocument("text/xml", "", null);
    xml.async = false;
    xml.loadXML(xmlString);
    return xml;
  }
,
  send: function(xml, stay) {
    try {
      var xmlhttp = browserIE ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
      xmlhttp.open("POST", "/visitor.asp", false);
      xmlhttp.send(xml);
      if (xmlhttp.responseText) {
        alert(xmlhttp.responseText);
      }
      else {
        if (!stay) location.reload();
      }
    }
    catch (e) {
    }
  }
,
  rc: function(theName, theDefault) {
    try {
      var theC = document.cookie + "|;";
      var p = theC.indexOf("Q42=");
      if (p == -1) return theDefault;
      theC = theC.substring(p, theC.indexOf("|;", p)) + "|";
      var pos = theC.indexOf(theName + ":");
      if (pos == -1) return theDefault;
      theValue = unescape(theC.substring(pos + theName.length + 1, theC.indexOf("|", pos)));
      if (theValue == "") return theDefault;
      else return theValue;
    }
    catch (e) {
      return theDefault;
    }
  }
,
  wc: function(theName, theValue) {
    var theC = document.cookie;
    if (theC != "") var theContent = theC.substring(theC.indexOf("=") + 2, theC.length - 1);
    else var theContent = "";
    var cArray = theContent.split("|");
    var nvs = new Object();
    for (var i = 0; i < cArray.length; i++) {
      if (cArray[i].substring(0, cArray[i].indexOf(":")) != "")
        nvs[cArray[i].substring(0, cArray[i].indexOf(":"))] = cArray[i].substring(cArray[i].indexOf(":") + 1, cArray[i].length);
    }
    nvs[theName] = theValue;
    var s = "";
    for (var n in nvs) s += "|" + n + ":" + nvs[n];
    var nextYear = new Date();
    nextYear.setFullYear(nextYear.getFullYear() + 1);
    var c = "Q42=" + s + "|; expires=" + nextYear.toGMTString() + "; path=/;";
    document.cookie = "Q42=";
    document.cookie = c;
  }
}
function objlength(obj)
{
  var count=0;
  for (var o in obj) count++;
  return count;
}
var horizontalScrollerTOH = null;
var scrollSpeed = 650;
function globalMouseUp()
{
  clearHorizontalScroller();  
}
function clearHorizontalScroller()
{
  clearTimeout(horizontalScrollerTOH);
  scrollSpeed = 800;
}

function getElementsByTagNameAttributeValue(ancestorEl, tagName, attrName, attrValue)
{
  if (!ancestorEl)
    return;

  if (typeof(attrValue) == 'undefined')
    attrValue = null;

  var els = [];
  var a = ancestorEl.getElementsByTagName(tagName.toUpperCase());

  for (var i=0; i<a.length; i++)
  {
    var el = a[i];    
    switch(attrName)
    {
      case "class":
      case "className":
        var val = el.className;
        break;
      default:
        var val = el.getAttribute(attrName);    
    }        
    if (val != null && (attrValue == null || val.indexOf(attrValue) != -1))
      els.push(el);
  }
  
  return els;
}

function getParentElementByTagNameAttributeValue(el, tagName, attrName, attrValue)
{
  if (typeof(attrValue) == 'undefined')
    attrValue = null;

  tagName = tagName.toLowerCase();

  while (el && el.getAttribute && el.parentNode)
  {
    if (el.tagName && (el.tagName.toLowerCase() == tagName))
    {
      switch(attrName)
      {
        case "class":
        case "className":
          var val = el.className;
          break;
        default:
          var val = el.getAttribute(attrName);    
      }        
      if (val != null && (attrValue == null || val.indexOf(attrValue) != -1))
        return el;
    }
    el = el.parentNode;
  }

  return null;
}
