// prototype functie uit tools, herhalen voor externe sites
String.prototype.endsWith = function(str) {return (this.match(str+"$")==str)}

var logoThemes = ["dgroen","groen","geel","dgeel","oranje","rood","drood","paars","blauw","dblauw","wit","zwart"]; // themes
// alternate
// logoThemes = ["a","b","c","d","e","f","g","h","i","j","white","black"];

function getLogoClass(theme) {
  for (x in logoThemes) {
    if (theme == logoThemes[x]) {
      return theme;
    }
  }
  // random color, excluding white and black
  return logoThemes[Math.floor(Math.random()*(logoThemes.length-2))];
}

function zetCSS() {
  var domain = null;
  if (document) domain = document.domain;
  if (domain == null || (!domain.endsWith('schooltv.nl') && !domain.endsWith('jeugd.ntr.nl'))) {
     domain = "educatie.ntr.nl";
  }
  document.write('<link href="http://' + domain + '/shared/css/balk/balk.css" rel="stylesheet" type="text/css">');
  document.write('<!--[if lt IE 7]>');
  document.write('<link rel="stylesheet" href="http://' + domain + '/shared/css/balk/balk_ie.css" type="text/css" media="screen" />');
  document.write('<![endif]-->');
}

function poMenu() {
  document.write('<script type="text/javascript" src="http://assets.cn.omroep.nl/javascripts/npo-explore.js"></script>');
  document.write('<div class="balkonderdeel npo"><div id="npo_button_container" class="npo_button_lol" ><a href="http://www.omroep.nl/">Ontdek Omroep.nl</a></div></div>');
}

function poMenuZappelin() {
  document.write('<div class="balkonderdeel npo zappelin"><div id="zappelin_button_container" class="zappelin_button_lol" ><a href="http://www.zappelin.nl" onclick="window.open(this.href,\'Zappelin\'); return false;"><span>Ontdek Zappelin.nl</span></a></div></div>');
}

function poMenuZapp() {
  document.write('<script type="text/javascript" src="http://balk.omroep.nl/js/zapp-balk.js"></script>');
  document.write('<div class="balkonderdeel npo zapp"><div id="zapp_button_container" class="zapp_button_lol" ><a href="http://www.zapp.nl/">Ontdek Zapp.nl</a></div></div>');
}

function thirdParty(thirdparty, thirdpartyurl) {
  if (thirdparty != null) {
    document.write('<div class="balkonderdeel thirdparty"><a href="' + thirdpartyurl + '" target="+blank">' + thirdparty + '</a></div>');
  }
}

function groupLink(kind) {
  if (kind != null && kind.indexOf('*00') > 0) {
    document.write('<div class="balkonderdeel jeugd"><a href="' + jeugdurl + '" target="+blank"><span>' + jeugdtitel+ '</span></a></div>');
  } else {
    for (i=7; i>=1; i-=2) {
      if (kind != null && kind.indexOf('*' + i) > 0) {
        var classname = i + '' + (i+1);
        var folder = i + '-' + (i+1);
        document.write('<div class="balkonderdeel group' + classname + '"><a href="http://www.schooltv.nl/' + folder + '" target="+blank"><span>groep ' + folder + '</span></a></div>');
      }
    }
  }
}

function startBalk() {
  document.write('<div id="balk">');
  document.write('<div id="logo_container">');
}

function endBalk () {
   document.write('</div>');
   document.write('</div>');
}

function startLogo() {
  document.write('<div id="logo_merk">');
}

function logo(theme, brand, url, suffix) {
  if (url == null) {
    url = 'http://www.ntr.nl';
  }
  if (brand == null) {
    brand = 'NTR';
  }
  document.write('<a href="' + url + '" class="' + getLogoClass(theme) + ' ' + brand.toLowerCase() + '"><span>' + brand + '</span></a>');
/*
  if (suffix != null) {
    document.write('<a href="' + url + '" class="suffix"><span>' + suffix + '</span></a>');
  }
*/
}

function endLogo() {
  document.write('</div>');
}

function maakBalkNPO(theme, brand, url, suffix, kind, thirdparty, thirdpartyurl) {  // Maak de balk:
  zetCSS();
  startBalk();
  startLogo();
  logo(theme, brand, url, suffix);
  endLogo();
  poMenu();
  groupLink(kind);
  thirdParty(thirdparty, thirdpartyurl);
  endBalk();  
}

function maakBalkZapp(theme, brand, url, suffix, kind, thirdparty, thirdpartyurl) {  // Maak de balk:
  zetCSS();
  startBalk();
  startLogo();
  logo(theme, brand, url, suffix);
  endLogo();
  poMenuZapp();
  groupLink(kind);
  thirdParty(thirdparty, thirdpartyurl);
  endBalk();  
}

function maakBalkZappelin(theme, brand, url, suffix, kind, thirdparty, thirdpartyurl) {  // Maak de balk:
  zetCSS();
  startBalk();
  startLogo();
  logo(theme, brand, url, suffix);
  endLogo();
  poMenuZappelin();
  groupLink(kind);
  thirdParty(thirdparty, thirdpartyurl);
  endBalk();  
}

function maakLogo(theme, brand, url, suffix) {
  if (theme != 'none') {
    var sep = theme.lastIndexOf('_zappelin');
    if (sep > 0) {
      logo(theme.substring(0,sep), brand, url, suffix);
    } else {
      sep = theme.lastIndexOf('_zapp');
      if (sep > 0) {
        logo(theme.substring(0,sep), brand, url, suffix);
      } else {
        logo(theme, brand, url, suffix);
      }
    }
  }
}

function startsWith(str1,str2) {
  return (str1.match("^"+str2)==str2);
}

function maakBalk(theme, brand, url, suffix, kind, thirdparty, thirdpartyurl) {	// Maak de balk:
  if (theme != 'none') {
    var sep = theme.lastIndexOf('_zappelin');
    if (sep > 0) {
      kind = 'zappelin' + kind;
      theme = theme.substring(0,sep);
    } else {
      sep = theme.lastIndexOf('_zapp');
      if (sep > 0) {
        kind = 'zapp' + kind;
        theme = theme.substring(0,sep);
      }
    }
  }
  if (kind && startsWith(kind,'zappelin')) {
    maakBalkZappelin(theme, brand, url, suffix, kind, thirdparty, thirdpartyurl);
  } else if (kind && startsWith(kind,'zapp')) {
    maakBalkZapp(theme, brand, url, suffix, kind, thirdparty, thirdpartyurl);
  } else {
    maakBalkNPO(theme, brand, url, suffix, kind, thirdparty, thirdpartyurl);
  }
}

