
if( window.jQuery )  // Avoid more errors in IE 5.0
{
  // Directly set flag for CSS to indicate JavaScript is enabled.
  // At this point "HTML" is the only accessable element.
  $(document.documentElement).addClass("jsEnabled");


  $(document).ready(

    // ???

    function()
    {
      // get us out of frames
      if (top.location != location) {
        top.location.href = document.location.href ;
      }

      $("#contentboxform .button").hover(
          function(e)
          {
              $(this).css( { background: "url('/resources/img/button.png') 0 103% no-repeat", cursor: "pointer" } );
              $(this).find("input").css( { background: "url('/resources/img/button.png') 100% 103% no-repeat", cursor: "pointer" } );
          },
          function(e)
          {
              $(this).css( { background: "url('/resources/img/button.png') 0 0 no-repeat" } );
              $(this).find("input").css( { background: "url('/resources/img/button.png') 100% 0 no-repeat" } );
          }
      );


      // -------------------------------
      // Fix links

      // Geef links met rel="external" een target="_blank" attribuut.
      $(document.links).filter('[href][rel=external]').each(function() { this.target = '_blank'; });


      // -------------------------------
      // open and close links

      $("#readmore").hide();
                
      $("a.readon").click( function(event)
      {
        $(this).next("#readmore").slideToggle("slow");
        $(this).addClass("active"); 
        event.preventDefault();
        return false;
      });
  
      $("a.close").click( function(event)
      {
        $(this).parent("#readmore").slideToggle("slow");
        $("a.readon").removeClass("active");
        event.preventDefault();
        return false;
      });

      // --------------------------------
      // Form input label flip

      // Activeer 'overlabels' over compacte formulieren zoals het zoekscherm
      $('label.overlabel')
        .each( function()
        {
          // Get field
          var label = $(this);
          var id    = this.htmlFor || this.getAttribute('for');
          var field = null;
          if( ! id || ! (field = document.getElementById( id ) ) ) return;

          // Implement show/hide effect
          $(field).addClass("overlabel-js")                 // for setTimeout() below
                  .focus( function() { label.hide(); } )
                  .blur ( function() { this.value === '' && label.show(); } );

          // Also for mouse click
          label.mousedown( function() { field.focus(); } );
        }
      );

      // Hide overlabels after the form is auto-filled in by the browser.
      setTimeout( function()
      {
        var fields = $("input.overlabel-js");
        var labels = $("label.overlabel");
        fields.each( function() { this.value !== '' && labels.filter("[for=" + this.id + "]").hide(); } );
      }, 50 );


      // -------------------------------
      // Tab events

      $("#homebox .tabbutton").click( function( event )
      {
        var buttonId = this.id;
        var cap;
        if( ( cap = /([0-9]+)$/.exec( buttonId ) ) )
        {
          var activeTab = "tab" + cap[1];
          $("#homebox .phototab:not([id$=" + activeTab + "])").hide();
          $("#homebox .texttab:not([id$=" + activeTab + "])").hide();
          $("#homebox .tabbutton[id!=" + buttonId + "]").removeClass("onpath");
          $(this).addClass("onpath");
          $("#photo" + activeTab + ", #text" + activeTab ).show();
          event.preventDefault();
        }
      } );

      $("#actiondecreasefont a").click( function( event )
      {
        decreaseFontSize();
      } );
      $("#actionincreasefont a").click( function( event )
      {
        increaseFontSize();
      } );

      if (document.getElementById("actiondecreasefont")) {
        var fontsizecookie = readCookie("rotary-fontsize");
        var fontsize = fontsizecookie ? fontsizecookie : '83'
        document.body.style.fontSize=fontsize+"%";
        //alert("set font size: " + fontsize);
      }

      $("#page_toolbox .print a").click( function ( event )
      {
        window.print();
      } );

      // -------------------------------
      // Mega Firefox / IE bug workarround.

      // "One true layout" (-500 margin, +500 padding) breaks hash link navigation.
      // Animate scrolling, thanks to http://www.zachstronaut.com/posts/2009/01/18/jquery-smooth-scroll-bugs.html
      // Much smaller then jQuery.ScrollTo as well.

      // Need to get the scroll element first.
      // Depends on the browser. Either html or body accepts changes to scrollTop.
      var scrollElement = 'html, body';
      $('html, body').each(function () {
          var initScrollTop = $(this).attr('scrollTop');
          $(this).attr('scrollTop', initScrollTop + 1);
          if ($(this).attr('scrollTop') == initScrollTop + 1) {
              scrollElement = this.nodeName.toLowerCase();
              $(this).attr('scrollTop', initScrollTop);  // Firefox 2 reset
              return false;
          }    
      });

      $(document.links).filter("[href*=#]").click( function(event)
        {

//alert("this.hostname: '" + this.hostname + "' location.hostname: '" + location.hostname + "' this.pathname: '" + this.pathname + "' location.pathname: '" + location.pathname + "'");
          if( this.hostname == location.hostname && ( this.pathname == location.pathname || "/" + this.pathname == location.pathname  || this.pathname == (location.pathname+"index.html")) ) // within this page. |
          {
            //alert("match");
            var name = unescape( this.href.substring( this.href.indexOf("#") + 1 ) );
            if (name == "") {
              return;
            }
            var target = $( ( name.indexOf(" ") == -1 ? "#" + name + ", " : "" ) + "a[name=" + name + "]").eq(0);
            if( target.length )
            {
              var targetOffset = target.offset().top;
              $(scrollElement).animate( {scrollTop: targetOffset}, 500 ); //, function() { location.hash = name; });
            }
            else {
            }

            event.preventDefault();
          }
        });
/*

*/
      $(document.links).filter("[href$=.swf]").addClass("wb-video");
      $(document.links).filter("[href*=youtube.com]").addClass("wb-video");

  } );
}

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 setCookie() {
  var fontsize = 83;

  if (document.getElementById('actionincreasefont'))
  {
    fontsize = parseInt(document.body.style.fontSize)
  }

  createCookie("rotary-fontsize", fontsize, 365);
}

function decreaseFontSize() {
  document.body.style.fontSize=(parseInt(document.body.style.fontSize)-5) + "%";
  setCookie();
  return null;
}

function increaseFontSize() {
  document.body.style.fontSize=(parseInt(document.body.style.fontSize)+5) + "%";
  setCookie();
  return null;
}

