// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
$(function() {
  
  // BEHAVIOUR: Layout compatibility adjustments
  // If the menu is larger than the content, resize the content to the
  // same size as the menu.
  var content = $('div.content');
  var menus = $('ul#supplementaryMenu')
  
  if(content != null && menus != null) {
    if(content.height() < menus.height()) {
      content.height(menus.height());
    }
  }

  // BEHAVIOUR: "All" checkboxes
  // If you include a checkbox with an id of "all"
  // on a page, it can be used to check other checkboxes
  // as a group.
  // If you have multiple groups, you can use the "related"
  // attribute to select all checkboxes of the same name.
  var check_all = $('input[@type=checkbox]#all');
  if(check_all != null) {
    check_all.click(function() {
      var selector = 'input[@type=checkbox]';
      
      if(this.getAttribute('related') != null) {
        selector = selector + '[@name="' + this.getAttribute('related') + '"]';
      }
      
      $(selector).attr('checked', this.checked);
    });
  }
  
  // BEHAVIOUR: Flash Notifications
  jQuery.highlightFade.defaults.speed = 1000;
  
  notifications = {
    '.notice': '#8888ee',
    '.warning': '#ff0000'
  }
  
  for(var selector in notifications) {
    jQuery(selector).each(function(notification) {
      if(this.getAttribute('highlighted') == null && this.style.display != 'none') {
        $(this).highlightFade(notifications[selector]);
        this.setAttribute('highlighted', true);
      }
    });
  }
  
  // BEHAVIOUR: Fix CSS image resizing for IE
/*
  $('div.content.portraitSmaller img').each(function() {
    if(document.all) {
      this.style.filter = 'progid:DXImageTransform.Microsoft.Matrix(FilterType="bilinear",M11=0.75,M22=0.75,SizingMethod="auto expand");';
    } else {
      $(this).height(360);
    }
  });
*/
  
  // BEHAVIOUR: Insert Shy-breaks
  // Give an element a class of "shy", and it's child TextNodes will have shy-breaks
  // inserted.  
  var SHY_NODE = (navigator.product && navigator.product == 'Gecko') ? '<wbr/>' : '&shy;';
  
  function insertShyBreaks(element) {
    if(element.getAttribute('bashful')) return;
    
    var children_are_text_nodes = true;
    for(var i = 0; i < element.childNodes.length; i++) {
      if(element.childNodes[i].nodeType != 3) {
        children_are_text_nodes = false;
        break;
      }
    }
    
    if(children_are_text_nodes) {
      element.innerHTML = element.innerHTML.replace(/[^\s]{10,}/g, function(match) {
        return match.replace(/[^\s]{10}/g, function(part) { return part + SHY_NODE; });
      });
    }
    
    element.setAttribute('bashful', true);
  }
  
  jQuery('.shy').each(function() {
    insertShyBreaks(this);
  });
});
