/**
 * JS functionality for RWE website, by Plag BV.
 */
RWE =  {
  $: function(id) {return document.getElementById(id)},
  height: function(id) {var o = RWE.$(id); return o.scrollHeight}
}

// Run the following functions onload!
$(function() {
  RWE.resizeLeftMenu();
  //RWE.homepageActions();
  RWE.sitemapColumnsResize();
  RWE.fixForms();
});

/**
 * Resize the left menu to always be as long as the page.
 */
RWE.resizeLeftMenu = function() {
  if (RWE.$('menuLeftContainer') != null) {
    var newHeight = RWE.height('page');
    RWE.$('menuLeftContainer').style.height = newHeight +'px';

    // In case there's a download menu, position it at the bottom
    if(RWE.$('menuDownloads') != null) {
      if (RWE.$('menuDownloads').childNodes[0].innerHTML != '') {
         var topMargin = newHeight - RWE.height('menuDownloads') - 30;
         RWE.$('menuDownloads').style.marginTop = topMargin + 'px';
      }
    }
  }
}

/**
 * Resize the columns for the sitemap page.
 */
RWE.sitemapColumnsResize = function() {
  // only try to resize if we have a sitemap in the page
  if (RWE.$('sitemap') != null) {
    var newHeight = RWE.height('sitemap');

    for(i = 0; i < RWE.$('sitemap').childNodes.length; i++) {
      child = RWE.$('sitemap').childNodes[i];
      if (child.className == 'segment') {
        child.style.height = newHeight +'px';
      }
    }
  }
}

/**
 * Hover function for home page action blocks.
 */
RWE.homepageActions = function() {
  if ($.browser.msie && $.browser.version < 7) {
    $('#actions>div').hover(
      function() { $(this).css('border', 'dashed 1px #7fb2d9') },
      function() { $(this).css('border', 'solid 1px #fff') }
    );
  }
  else {
    $('#actions>div').hover(
      function() { $(this).css('border', 'dashed 1px #7fb2d9') },
      function() { $(this).css('border', 'solid 1px #fff') }
    );
    $('#action div div').hover(
      function() { $(this).fadeTo('fast', 1).addClass('hover') },
      function() { $(this).fadeTo('fast', .5).removeClass('hover') }
    );
  }
}

/**
 * Add content to empty fieldtitles.
 * In mozilla empty form title elements don't get a width, this is a workaround.
 */
RWE.fixForms = function() {
  $('.formRow .field input')
    .focus(function() { $(this).parent().parent().addClass('focused') })
    .blur(function() { $(this).parent().parent().removeClass('focused') });

  if ($.browser.mozilla == true) {
    $('.fieldTitle').each(function() {
      if ($(this).html() == '') $(this).html('&nbsp;');
    });
  }
}