function setActiveStyleSheet(title) {
  var i, a, main;
  // document.getElementById('debug').innerHTML = "set: " + title;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title){
          //document.getElementById('debug').innerHTML = a.getAttribute("title");
          a.disabled = false;
      }
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

function isValidStyle(title){
    if (title == 'default' || title == 'groot' || title == 'extrabig'){
        return true;
    }else{    
        return false;
    }
}

/*
addLoadEvent(function(e) {
  var cookie = readCookie("nbs-style");
  var title  = cookie ? cookie : getPreferredStyleSheet();
  if (!isValidStyle(title)){
    // alert('invalid style ' + title);
    title = 'default';
  }
  setActiveStyleSheet(title);
}
    );
*/

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  if (title == null){
      title = 'default';
  }
  createCookie("nbs-style", title, 365);
}
    

// globals
var  sidebar_bg_height = 160;
var  instretchsite     = 0;

function nbs_stretchsite(){
    instretchsite++;
    if (instretchsite == 1){
        _nbs_stretchsite();
    }
    instretchsite--;
}

function _nbs_stretchsite(){

    try{
        
        // fill right sidebar to get footer below content
        var dim_content;
        var dim_inner;
        var dim_sidebar;
        
        // window
        windowHeight = window.innerHeight || (window.document.documentElement.clientHeight || window.document.body.clientHeight);
        windowHeight2 = windowHeight - 40 - 160; // minus header, top_bar

        // contentblocks
        var is_homepage = false;
        if ($('content_narrow')) { // homepage?
            dim_content = $('content_narrow').getDimensions();
            dim_inner   = $('content_innersidebar_wrapper').getDimensions();
            is_homepage = true;

        } else {
            dim_content = $('content_wrapper').getDimensions();
            dim_inner   = null;
        }

        contentHeight = dim_content.height;
        contentHeight2 = contentHeight + ( sidebar_bg_height / 2);
        
        dim_sidebar   = $('sidebar').getDimensions();
        if (dim_inner && dim_sidebar && dim_inner.height > dim_sidebar.height){
            dim_sidebar = dim_inner;
        }
        sidebarHeight = dim_sidebar.height;

        // nav
        dim_nav       = $('navigation').getDimensions();
        navHeight     = dim_nav.height - 1; // -1 voor de border

        // get max
        maxHeight = contentHeight;
        if (sidebarHeight > maxHeight){
            maxHeight = sidebarHeight;
        }
        if (contentHeight2 > maxHeight){
            maxHeight = contentHeight2;
        }
        if (navHeight > maxHeight){
            maxHeight = navHeight;
        }
        if (windowHeight2 > maxHeight){
            maxHeight = windowHeight2;
        }
       
        debug('window: ' + windowHeight + ' nav: ' + navHeight + ' content: ' + contentHeight + ' sidebar: ' + sidebarHeight + ' max: ' + maxHeight);
       
        // eerst rechts daarna links, anders onstaat er wat witruimte met IE, ????

        // resize right
        if ((sidebarHeight - sidebar_bg_height) < maxHeight){
            $('sidebar_filler').style.height = (maxHeight - sidebarHeight)  + 'px'; 
        }

        // resize nav
        if (navHeight < maxHeight) {
            $('navigation_filler').style.height = (maxHeight - navHeight) +  'px';
        }

        // set footer border
        // remove border zodat het leuk aansluit
        $('navigation_banners').style.borderBottom = 'none';
        $('footer').style.borderTop = '1px solid #d1dced'

    }catch(e){
        debug(e);
    }
}


function init_style(){
  try{
        // set stylesheet
        var cookie = readCookie("nbs-style");
        var title  = cookie ? cookie : getPreferredStyleSheet();
        if (title == null){
            title = 'default';
        }
        // setActiveStyleSheet(title);
        setActiveStyleSheet(title);
        debug('style: ' + title);
    } catch(e){
        debug(e);
    }
}

function nbs_init(){
    
    init_style();
  
    try{
        nbs_stretchsite();
        debug('nbs_init');
    }catch(e){
        debug(e);
    }
}

addLoadEvent(nbs_init);
// onresize = nbs_stretchsite;

// zodra deze js geladen is gelijk een change style, zodat het niet
// te veel verspringt
init_style(); 

