// To not screw up our beautiful global namespace, here's our fluffy object literal
FGH = {

 // Adds a stylesheet to the document to let the entire world know JS is enabled, hooray \o/
 addStylesheet: function(name) {
  $('head').append('<link rel="stylesheet" type="text/css" media="all" href="/Style%20Library/FghBank/static/css/' + name + '.css" id="css-' + name + '">');
  return this;
 },

 // Adds sliding 'tooltips' to the main menu items, using the title attribute set on the anchors
 setupNavigation: function() {
  // If we're on the homepage, the items should be animated up. On subpages they go down.
  var weAreOnTheHomepage = $('body').hasClass('homepage');
  // Loop through all menu items
  $('#navigation li a').each(function() {
   // Get the anchor and the anchor title
   var anchor = $(this), title = anchor.attr('title');
   // If <a title=""> was specified and it's not empty
   if (title) {
    // Set the title to the empty string to hide the default browser tooltip and append a new element to the anchors' parent (li)
    anchor.attr('title', '').parent().append('<p class="closed">' + title + '</p>');
    if (weAreOnTheHomepage) {
     // Add animations to the mouseover and mouseout events, using 'hoverIntent'
     anchor.hoverIntent({interval: 0, timeout: 300, over: function() {
      $(this).next().dequeue().animate({height: '7.69em', top: '-100px', opacity: 1});
     }, out: function() {
      $(this).next().dequeue().animate({height: '0', top: '0', opacity: 0.72});
     }});
    } else {
     // Add animations to the mouseover and mouseout events, using 'hoverIntent'
     anchor.hoverIntent({interval: 0, timeout: 300, over: function() {
      $(this).next().dequeue().animate({height: '100px'});
     }, out: function() {
      $(this).next().dequeue().animate({height: '0', paddingTop: '4px'});
     }});
    }
   }
  });
  return this;
 },

 // Adds a resize event to the window
 addResizeHandler: function() {
  $(window).resize(function() {
   FGH.checkSize();
  });
  return this;
 },

 // Is set to true if the lower resolution stylesheet is added to the DOM
 lowResCSSAdded: false,

 // The resize handler
 checkSize: function() {
  // If the current width is too small for our default design 
  if ($(window).width() < 1000) {
   // If the lower resolution stylesheet is already loaded, don't load it again, but enable it
   if (FGH.lowResCSSAdded) {
    $('html').addClass('lower-res');
    document.getElementById('css-lower-res').disabled = false;
   } else {
    FGH.addStylesheet('lower-res');
    $('html').addClass('lower-res');
    FGH.lowResCSSAdded = true;
   }
  } else {
   // If the lower resolution stylesheet is already loaded, disable it
   if (FGH.lowResCSSAdded) {
    $('html').removeClass('lower-res');
    document.getElementById('css-lower-res').disabled = true;
   }
  }
  return this;
 },

 addExternalWindows: function() {
  //$('a[rel="external"]').addClass('external').attr('target', '_blank').append(' <span>(nieuw scherm)</span>');
  return this;
 },

 // Is set to true of a help layer has been opened, so multiple help messages can't be opened at the same time
 helpLayerOpen: false,

 // The help layers transform <p class="help">[Text]</p> inside forms to '?' help buttons next to the labels */
 addFormHelpLayers: function() {
  $('#primary form p.help').each(function() {
   $(this).hide();
   var text = $(this).text();
   var help = $('<a href="#" title="Help">?</a>').click(function() {
    if (FGH.helpLayerOpen) {
     $(this).parents('form').find('div.help').remove();
     FGH.helpLayerOpen = false;
    }
    $('<div class="help">' + text + '</div>').hide().appendTo($(this).parents('form')).css('top', $(this).offset().top - ($(this).parents('form').offset().top) - 5).show();
    FGH.helpLayerOpen = true;
    return false;
   });
   var next = $(this).next();
   if (next.is('p')) {
    next.find('label:first').prepend(help);
   } else if (next.is('fieldset')) {
    next.find('legend:first span').prepend(help);
   }
  });
  return this;
 },

 // To simulate :hover or IE6
 addHoverOnInputs: function() {
  $('form input:submit').hover(function() {
   $(this).addClass('hover');
  }, function() {
   $(this).removeClass('hover');
  });
 }

};

/* As soon as the DOM is loaded */
if (typeof($) != 'undefined') {
 $(function() {
  document.getElementsByTagName('html')[0].className = 'js-enabled';
  FGH.checkSize().addStylesheet('default-js').setupNavigation().addResizeHandler().addExternalWindows().addFormHelpLayers();
  if ($.browser.msie && $.browser.version == 6) {
   FGH.addHoverOnInputs();
  }
 });
}