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");

//   // Make console.log available.
//   if( window.console == null ) { window.console = { log: function(text) {} }; }

  $(document).ready( function()
  {
    // ---------------------------
    // Flash
    // Embed as soon as possible

    var objectAttr = { align: 'center' };
    var params = { allowscriptaccess: 'samedomain'
                 , allowfullScreen:   'false'
                 , quality:           'high'
                 , scale:             'noscale'
                 //, salign:            't'            // must be below scale
                 , wmode:             'transparent'  // not supported in Linux
                 , bgcolor:           '#ffffff'
                 };

    // Flash at home page.
    var homevideo = document.getElementById('homevideo');
    if( homevideo != null )
    {
      swfobject.embedSWF( "/resources/swf/tio_video_home.swf", 'homevideo', '237', '188', '6.0.0'
                        , '/resources/swf/expressInstall.swf', objectAttr, params
                        );
    }

    // Flash intro home page.
    var visual = document.getElementById('visual');
    if( visual != null )
    {
      swfobject.embedSWF( "/resources/swf/FlashBlockTio.swf", 'visual', '489', '187', '6.0.0'
                        , '/resources/swf/expressInstall.swf', objectAttr, params
                        );
    }


    // ---------------------------
    // Link rewrites

    // Geef links met rel="external" een target="_blank" attribuut.
    $(document.links).filter('[href][rel=external]').attr("target", "_blank");


    // ---------------------------
    // Home page - study switch

    var selectHBO = function()
    {
      var dl = $('#detail');
      var hboitems = dl.find('.HBO');
      var mboitems = dl.find('.MBO');
      if(dl.length > 0)
      {
        hboitems.removeClass("off");
        mboitems.addClass("off");
        
        $('#selectmbo span')[0].className = 'off';
        $('#selecthbo span')[0].className = 'on';

      }
      return false;  // block default link href processing.
    };

    // Activeer de HBO studies als er op HBO wordt geklikt
    $('#selecthbo').click( selectHBO );


    // Activeer de MBO studies als er op HBO wordt geklikt
    $('#selectmbo')
    .click( function()
    {
      var dl = $('#detail');
      var hboitems = dl.find('.HBO');
      var mboitems = dl.find('.MBO');
      hboitems.addClass("off");
      mboitems.removeClass("off");

      $('#selectmbo span')[0].className = 'on';
      $('#selecthbo span')[0].className = 'off';

      return false;  // block default link href processing.
 
    } );

    selectHBO();


    // ---------------------------
    // Thickbox CSS 

    // Load thickbox CSS only when needed.
    if( window.tb_init != null )
    {
      $('head').append( '<link href="/resources/css/thickbox.css" rel="stylesheet">' );
    }


    // ---------------------------
    // CSS workarrounds:

    // It's impossible to make the filter items the same
    // width in CSS, if the container width is not set.
    //
    // The width can only be read when the element is visible,
    // so it's applied to the mouseover call.

    // Using the '>' char improves the loading performance a lot.
    //var dropLists = $("#filterSelectionbar > ul > li.drop");  // doesn't work in IE5.5, jQuery 1.2.6 bug.
    var dropLists = $("#filterSelectionbar").children("ul").children("li.drop");

    if( dropLists.length > 0 )
    {
      var correctMenuWidth = function()
      {
        // Elements only get a width when they are visible, so run this script at hover.
        var ul  = $(this).children("ul");
        var lis = ul.children("li");

        // Find the largest element
        var largest = 0;
        for( var i = 0; i < lis.length; i++ )
        {
          var width = $( lis[i] ).width();
          if( width > largest )
          {
            largest = width;
          }
        }

        // Apply to all
        if( largest > 0 )
        {
          lis.width( largest );

          // Give IE6 the required kick to scale up 'a' block elements.
          // JavaScript version of the CSS expression * html .. { height:1px; }
          if( jQuery.browser.msie && jQuery.browser.version < 7 )
          {
            lis.children("a").height("1px");
          }
        }
      };

      // Apply a small timeout; MSIE is slow to update the DOM tree so the width remains known.
      // This could apply to other browsers too, so use the setTimeout() here by default.
      //dropLists.mouseover( correctMenuWidth );   // doesn't work in MSIE
      dropLists.mouseover( function() { var li = this; setTimeout( function() { correctMenuWidth.apply(li) }, 0 ) } );
    }


    // ---------------------------
    // Internet Explorer 6 bug workarrounds:

    // Some CSS parts can't be applied in any way because IE6
    // either lacks the support, or is buggy at certain points.
    if( jQuery.browser.msie && jQuery.browser.version < 7 )
    {
      // Define actions only once
      var addHover    = function(e) { $(this).addClass("hover"); };
      var removeHover = function(e) { $(this).removeClass("hover"); };

      // Apply to all elements needed:
      dropLists.hover( addHover, removeHover );
    }

  } );
}


// ---------------------------
// Configure sIFR
// (image flash replacement)
//
// Set titles with Avenir Black font.

if( window.sIFR && window.jQuery )
{
  var modifyCss = function(css, contentNode, selector )
    {
      // Read color, sifr doesn't do this properly
      var jqNode   = $(contentNode);
      var color    = colorToHex( jqNode.css("color") );
      var fontSize = jqNode.css("font-size");

      // Provide adjusted (clean) CSS.
      var rootCSS  = "color:" + color + "; font-weight:bold; text-transform:uppercase;";  //font-size:" + fontSize + ";";
      var linkCSS  = "color:" + color + "; text-decoration:none;";
      var hoverCSS = "color:#fff; background-color:" + color + "; text-decoration:none;";

      if( jqNode.attr("id") == "opendagenTitle" )
      {
        linkCSS += "text-decoration:none;";
      }

      var newCSS = ".sIFR-root{" + rootCSS + "} a{" + linkCSS + "} a:hover{" + hoverCSS + "}";
      return newCSS;
    };

  // Define default properties
  var avenirblack = { src: '/resources/swf/sifr_avenir_black.swf'
                    , ratios: [9,1.16,16,1.09,24,1.06,37,1.04,74,1.02,1.01]
                    , wmode: "opaque"
                    , modifyCss: modifyCss
                    };

  // Speed up loading in Firefox/Safari, checks for .sIRF-dummy element.
  sIFR.useStyleCheck = true;
  sIFR.forceTextTransform = true;

  // Replace titles
  // text-transform is handled by sIFR, is not supported by flash.
  sIFR.activate( avenirblack );
  sIFR.replace(avenirblack, { selector: '#page h1.title',     tuneHeight: -6, css: ".sIFR-root{text-transform:uppercase;}" } );
  sIFR.replace(avenirblack, { selector: '#contents h1.title', tuneHeight: -6, css: ".sIFR-root{text-transform:uppercase;}" } );
  sIFR.replace(avenirblack, { selector: '#contents h2.title', tuneHeight: -6, css: ".sIFR-root{text-transform:uppercase;}" } );
  sIFR.replace(avenirblack, { selector: '#contentpage h2',    tuneHeight: -6, css: ".sIFR-root{text-transform:uppercase;}" } );
  sIFR.replace(avenirblack, { selector: '#itempage h2',       tuneHeight: -6, css: ".sIFR-root{text-transform:uppercase;}" } );
  sIFR.replace(avenirblack, { selector: '#studies dt.title',  tuneHeight: -6, css: ".sIFR-root{text-transform:uppercase;}" } );


  function colorToHex( rgb )
  {
    if( rgb.match( /^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/ ) )
    {
      return "#" + toHex( RegExp.$1 ) + toHex( RegExp.$2 ) + toHex( RegExp.$3 );
    }

    return rgb;
  }

  function toHex( num )
  {
    return "0123456789ABCDEF".charAt( ( num & 0xF0 ) >> 4 )
         + "0123456789ABCDEF".charAt( num & 0x0F )
  }
}

