$(document).ready(function() {
  
  var menuSlide = function(direction, display) {
	return function() {
	  var self = this;
	  var ul = $("ul", this);
	  if( ul.css("display") == display && !self["block" + direction] ) {
		self["block" + direction] = true;
		ul["slide" + direction]("fast", function() {
		  self["block" + direction] = false;
		});
	  }
	};
  }
  
  $("li.uitvouwbaar").hover(menuSlide("Down", "none"), menuSlide("Up", "block"));
  
  $('<a href="#" class="styleswitcher" rel="big" id="bigger">A+</a><a href="#" class="styleswitcher" rel="normal" id="normal">A-</a>').insertAfter('div#breadcrumbs a#print');
  $('a#print').click(function(){
    window.print();
    return false;
  });

  $('a.external').click(function(){
    window.open($(this).attr("href"));
    return false;
  });
  
  $('.styleswitcher').click(function(){
    switchStyle(this.getAttribute("rel"));
    $(this).fadeTo("normal", 0.3);
    if(this.getAttribute('id') == 'bigger') {
      $('.styleswitcher#normal').fadeTo("normal", 1);
    } else {
      $('.styleswitcher#bigger').fadeTo("normal", 1);
    }
    return false;
  });
  
  var c = readCookie('style');
  if (c) switchStyle(c);
  
  if (c == 'big'){
    $('.styleswitcher#bigger').css("opacity", 0.3);
  } else if (c == "normal") {
    $('.styleswitcher#normal').css("opacity", 0.3);
  }
  
});

  function hidemenu(){
    $("li.uitvouwbaar>ul").hide();
  }
  
  function switchStyle(styleName){
   
      $('link[@rel=alternate stylesheet][@title]').each(function(i){
        this.disabled = true;
        if (this.getAttribute('title') == styleName) this.disabled = false;
      });
      createCookie('style', styleName, 365);
    
  } 

 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 var 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 eraseCookie(name) {
    createCookie(name,"",-1);
  }
    

