// Hover Behaviour for Nav etc.
var HoverBehavior = Class.create({
  initialize: function() {
    // $A(document.styleSheets).each( function(stylesheet) {
    //   $A(stylesheet.rules).each( function(rule) {
    //     if( rule.selectorText.match(/:hover/i) ) {
    //       stylesheet.addRule( rule.selectorText.replace(/:hover/ig, '.hover'), rule.style.cssText );
    //     }
    //   });
    // });
    $A(arguments).each( function(arg) {
      $$(arg).each( function(tag) {
        Event.observe(tag, 'mouseenter', function() { Element.addClassName(tag, 'hover'); }, true);
        Event.observe(tag, 'mouseleave', function() { Element.removeClassName(tag, 'hover'); }, true);
      });
    });
  }
})

var InputDefaultValue = Class.create({	
  initialize: function(field){
    // Ensure container exists
    if (!$(field)) { return false; }

    this.field = $(field);
    this.defaultValue = $F(this.field);

    // Clear field on click
    Event.observe(this.field, "click", function(){
      if ($F(this.field)==this.defaultValue) { 
        $(this.field).clear(); 
      }
      $(this.field).activate();
    }.bind(this));

    // Restore field's default text when empty
    Event.observe(this.field, "blur", function(){
      if (!$(this.field).present()) { 
        $(this.field).setValue(this.defaultValue); 
      }
    }.bind(this));
  }
});




var CNQ = Class.create();
CNQ.prototype = {
  
  slideshow: function(){
    if ($('masthead')){
      new Slides({
        delay             : 8000,
        container         : '#masthead',
        slides_container  : 'ul.slides',
        before_transition : function(current_slide, next_slide){ Cufon.replace('#'+ next_slide.id +' .overlay') }
      })      
    }
  },
  
  external_links: function(){
    var filter = new RegExp($A(arguments).join('|'))    
    $$('a[href^=http]').each(function(a){

      if (a.href.match(filter)) {
        // Force link to be internal
      } else {
        // Force link to be external
        a.observe('click', function(e){
          var new_window = window.open(a.href,'_blank')
          new_window.focus()
          e.stop()
        })
      }
    })
  },
  
  // Allow for media elements to be linked inline a text block 
  // <a class="inline_media" rel="media1">some inline text</a>
  inline_media: function(){
    $$('.inline_media').each(function(a){    
      var mediaelement = $(a.readAttribute('rel'))
      if (!mediaelement) { return }

      var new_link = '<a href="#{href}" title="#{title}">#{text}</a>'.interpolate({ 
        href: mediaelement.down('a').readAttribute('href'), 
        title: mediaelement.down('a').readAttribute('title'),
        text: a.innerHTML})

      a.replace(new_link)
      mediaelement.hide()
    })    
  }, 

  nav_parent_class: function(){
    $$("#nav ul.nav-list > li > a").each(function(a){
      if (a.next(0)) { a.addClassName('parent'); }
    });
  },  

  email_forms: function(){
    $$('a.email_form').each(function(a){
      a.observe('click',function(e){
        e.stop();
        window.open(a.href,'_blank','width=573,height=510');
      })
    })	  
  },

  search_box: function(){
    new InputDefaultValue('term');	  
  },
  
  initialize : function(){
    this.slideshow();
    this.external_links('cnrl.com');
    this.inline_media();
    this.nav_parent_class();
    this.email_forms();
    this.search_box();    
  }
};


// Global DOM onload
document.observe("dom:loaded", function() {

  // Let the CSS know JS is enabled
  $$('body').first().addClassName('js') 
  
  // Mark if we're in surf-to-edit
  if (location.href.match(/nterchange/g)) { 
    $$('body').first().addClassName('nterchange') 
    $$('body').first().insert('<div id="nterchange"></div>')
  }

  var cnq = new CNQ();
});


Event.observe(window, 'load', function(){
  
  // (don't enable if we're in surf-to-edit mode)  
  if (!$('nterchange')){
    addLinkerEvents() // addLinkerEvents-ga.js
  }
  
  // IE FIXES
  if (Prototype.Browser.IE) {
    // Set some classnames as an alternative hook to CSS 3 selectors
    $$('#nav > ul > li > a').invoke('addClassName','nav_anchor')
    $$('#side li.active > a').invoke('addClassName','active_anchor')
    $$('#nav li:last-child').each( function(tag) { tag.addClassName('last-child'); });
    new HoverBehavior('#nav li'); // keep at the end!
  }
});









// $$('body').first().insert({top:DetectMediaType()})
// function DetectMediaType() {  
//     
//     var media_types = [      
//       // {media:'all',        selector:'body div#detect_media_type', rule:'border-width:0'},
//       // {media:'all',        selector:'body div#detect_media_type', rule:'padding:0'},
//       // {media:'all',        selector:'body div#detect_media_type', rule:'margin:0'},      
//       {media:'print',      selector:'#detect_media_type', rule:'margin-top:1px'},
//       {media:'embossed',   selector:'#detect_media_type', rule:'margin-right:1px'},
//       {media:'braille',    selector:'#detect_media_type', rule:'margin-bottom:1px'},
//       {media:'aural',      selector:'#detect_media_type', rule:'margin-left:1px'},
//       {media:'tty',        selector:'#detect_media_type', rule:'border-top:1px'},
//       {media:'screen',     selector:'#detect_media_type', rule:'padding-top:1px'},
//       {media:'projection', selector:'#detect_media_type', rule:'padding-right:1px'},
//       {media:'handheld',   selector:'#detect_media_type', rule:'padding-bottom:1px'},
//       {media:'tv',         selector:'#detect_media_type', rule:'padding-left:1px'}]
// 
//     media_types.each(function(test){
//       var cssNode = document.createElement('style');
//       cssNode.type = 'text/css'; cssNode.rel = 'stylesheet';
//       cssNode.media = test.media;
//       $$('head').first().appendChild(cssNode);
//       var styleSheet = $A(document.styleSheets).last();
//       if (styleSheet.addRule) { 
//         styleSheet.addRule(test.selector,test.rule);
//       } else {
//         styleSheet.insertRule(test.selector+'{'+test.rule+'}',0);
//       }
//     });
//     
//     $$('body').first().insert({bottom:'<div id="detect_media_type">&nbsp;</div>'})
//     
//     var detected_media_type = 'unknown';
//     
//     detected_media_type = (parseInt($('detect_media_type').getStyle('margin-top')))     ? 'print'       : detected_media_type;
//     detected_media_type = (parseInt($('detect_media_type').getStyle('margin-right')))   ? 'embossed'    : detected_media_type;
//     detected_media_type = (parseInt($('detect_media_type').getStyle('margin-bottom')))  ? 'braille'     : detected_media_type;
//     detected_media_type = (parseInt($('detect_media_type').getStyle('margin-left')))    ? 'aural'       : detected_media_type;
//     detected_media_type = (parseInt($('detect_media_type').getStyle('border-top')))     ? 'tty'         : detected_media_type;
//     detected_media_type = (parseInt($('detect_media_type').getStyle('padding-top')))    ? 'screen'      : detected_media_type;
//     detected_media_type = (parseInt($('detect_media_type').getStyle('padding-right')))  ? 'projection'  : detected_media_type;
//     detected_media_type = (parseInt($('detect_media_type').getStyle('padding-bottom'))) ? 'handheld'    : detected_media_type;
//     detected_media_type = (parseInt($('detect_media_type').getStyle('padding-left')))   ? 'tv'          : detected_media_type;
// 
//     return detected_media_type;
// }
