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;
    }
}

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

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

function bpw_stretchsite(){
    instretchsite++;
    if (instretchsite == 1){
        _bpw_stretchsite();
    }
    instretchsite--;
}

function _bpw_stretchsite(){

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


        // content
        try{
            dim_content  = $('content_wrapper').getDimensions();
        }catch(e){}
        try{
            dim_content  = $('content_narrow').getDimensions();

        }catch(e){}

        contentHeight = dim_content.height;
        contentHeight2 = contentHeight + ( sidebar_bg_height / 2);
        
        // right
        try{
            dim_right = $('sidebar').getDimensions();
        }catch(e){} 
        
        try{
            dim_inner = $('content_innersidebar_wrapper').getDimensions();
        }catch(e){
            dim_inner = null;
        }
     
        
        dim_sidebar = dim_right;
        if (dim_inner && dim_sidebar && dim_inner.height > dim_sidebar.height){
            dim_sidebar = dim_inner;
        }
        rightHeight = dim_sidebar.height;

        var is_homepage = false;

        try{
            // de homepage zit net iets anders inelkaar
            tmp = $('content_innersidebar');
            if (tmp){
                is_homepage = true;
                rightHeight = rightHeight + sidebar_bg_height;
                debug('grr');
            }
        }catch(e){
            debug('nie sidebarS');
        }

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

        // get max
        maxHeight = contentHeight;
        if (rightHeight > maxHeight){
            maxHeight = rightHeight;
        }
        if (contentHeight2 > maxHeight){
            maxHeight = contentHeight2;
        }
        if (navHeight > maxHeight){
            maxHeight = navHeight;
        }
        if (windowHeight2 > maxHeight){
            maxHeight = windowHeight2;
        }
       

        debug('window: ' + windowHeight + ' nav: ' + navHeight + ' content: ' + contentHeight + ' right: ' + rightHeight + ' max: ' + maxHeight);

       
        // eerst rechts daarna links, anders onstaat er wat witruimte met IE, ????

        // resize right
        if ((rightHeight - sidebar_bg_height) < maxHeight){
            debug('resizing right');
            if (minHeight < rightHeight) {
                    minHeight = (maxHeight - rightHeight);
                }
            $('sidebar_filler').style.height          = minHeight + 'px'; 
            $('sidebar_filler').style.backgroundColor = '#fffff';

            // @TODO
            try{
            if (is_homepage){
                $('sidebar_filler_filler').style.width  =  '240px';
             //   if (minHeight < (maxHeight - rightHeight)) {
                    minHeight = (maxHeight - rightHeight);
             //   }
                $('sidebar_filler_filler').style.height =  minHeight  + 'px'; 
                $('sidebar_filler_filler').style.backgroundColor = '#E6E6F6';
            }
            }catch(e){}

        }

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

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

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


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

function bpw_init(){
    
    init_style();
  
    try{
        bpw_stretchsite();
        debug('bpw_init');
    }catch(e){
        debug(e);
    }
}

addLoadEvent(bpw_init);
// onresize = bpw_stretchsite;

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