/* helper function to load multiple functions, courtesy Simon Wilson */
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function popup(url,w,h,scroll)
{
  var l = (screen.width-w)/2;
  var t = (screen.height-h)/2;

  window.open(url,'_blank','resizable=yes,location=no,menubar=no,scrollbars='+scroll+',status=no,toolbar=no,fullscreen=no,dependent=no,width='+w+',height='+h+',left='+l+',top='+t);
}

//get basepath
var site_base = "";
if(a=document.getElementsByTagName("link")[0])
{
  site_base = a.href.substr(0,a.href.indexOf('/resources'));
}


function toggle(obj)
{
  if($(obj))
  {
    var slideEffect = $(obj).effect('height', {duration: 400, transition: Fx.Transitions.Quint.easeInOut});
    if($(obj).offsetHeight>0)
    {
      slideEffect.start($(obj).scrollHeight,0);
    }
    else
    {
      slideEffect.start(0,$(obj).scrollHeight);
    }
  }
}

//toolbox
addLoadEvent(function()
{
  if($('toolbox'))
  {
    $$('#toolbox li').each(function(el)
    {
      el.onmouseover = function()
      {
        this.style.backgroundPosition='-11px 0px';
      }
      el.onmouseout = function()
      {
        if(title!=this.id)
        {
          this.style.backgroundPosition='0px 0px';
        }
      }
      el.onclick = function()
      {
        $(title).style.backgroundPosition='0px 0px';
        title=this.id;

        setActiveStyleSheet(this.id);
        Cookie.set('fontstyle', this.id, {duration:365, path:"/"});
      }
    });
    $('toolboxtoggle').onclick = function(el)
    {
      var slideInOut = $('toolbox').effect('top', {duration: 400, transition: Fx.Transitions.Quint.easeInOut});
      if($('toolbox').getStyle('top').toInt()<-1)
      {
        slideInOut.start(-42, 0);
        (function(){
          //this.setHTML('Sluit de toolbox');
          this.setStyle('backgroundPosition', '0px 2px');
        }).delay(400,this);


      }
      else
      {
        slideInOut.start(0, -42);
        (function(){
          //this.setHTML('Open de toolbox');
          this.setStyle('backgroundPosition', '0px -21px');
        }).delay(400,this);
      }
    }
  }
});

//google analytics
addLoadEvent(function()
{
  $$('a.extlnk').each(function(el)
  {
    el.onclick = function()
    {
      urchinTracker('/external/'+this.rel);
    }
  });
});

//dossierfeeds
addLoadEvent(function()
{
  $$('#dossierlist input').each(function(el)
  {
    el.onclick = function()
    {
      dossierfeed();
    }
  });
});

function dossierfeed()
{
  var dossiers = new Array();
  $$('#dossierlist input').each(function(el)
  {
    if(el.checked)
    {
      dossiers[dossiers.length] = el.value;
    }
  });
  $('dossierfeed').href=site_base+'/rss/dossiers/'+dossiers.join('/');
}

function reloadValidationImage(key)
{
  if(document.getElementById('validationimage_'+key))
  {
    var now = new Date();
    newSrc = document.getElementById('validationimage_'+key).src
    newSrc = (newSrc.indexOf("?")>0)? newSrc.substr(0,newSrc.indexOf("?")) : newSrc;
    newSrc+= "?"+ now.getTime();
    document.getElementById('validationimage_'+key).src = newSrc;
  }
}


/***********************************************/
function setActiveStyleSheet(title)
{
  var i, a, main;
  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) 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 'font_normal';
}

window.onunload = function(e)
{
  var title = getActiveStyleSheet();
  Cookie.set('fontstyle', title, {duration:365, path:"/"});
}

var cookie = Cookie.get('fontstyle');
var title = cookie ? cookie : 'font_normal';
setActiveStyleSheet(title);
