function ieHover(node) {
  if (node) {
    node.each(function(e) {
      e.observe('mouseenter', function(event) { this.addClassName('over'); });
      e.observe('mouseleave', function(event) { this.removeClassName('over'); });
    });
  }
}
		
Event.observe(window,'load', function() {
  //var modal;

  // add hover to ie6
  if (Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 6) {
   ieHover($$('.quicklinks .parent'));
	 ieHover($$('.quicklinks .qlSubscribe'));
	 ieHover($$('#nav > li'));
	 ieHover($$('#aside li.shareShare'));
	}

  // print this page
  if ($$('.share li a.print')) {
    $$('.share li a.print').each(function(e) {
      e.observe('click', function(event) {
        window.print();
        Event.stop(event);
      });
    });
  }

});

// Email protection
function u(a){var b=[],i=ac=c=c1=c2=0;while(i<a.length){c=a.charCodeAt(i);if(c<128){b[ac++]=String.fromCharCode(c);i++}else if((c>191)&&(c<224)){c2=a.charCodeAt(i+1);b[ac++]=String.fromCharCode(((c&31)<<6)|(c2&63));i+=2}else{c2=a.charCodeAt(i+1);c3=a.charCodeAt(i+2);b[ac++]=String.fromCharCode(((c&15)<<12)|((c2&63)<<6)|(c3&63));i+=3}}return b.join('')}function d(a){var b="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";var c,o2,o3,h1,h2,h3,h4,bits,i=ac=0,dec="",tmp_arr=[];do{h1=b.indexOf(a.charAt(i++));h2=b.indexOf(a.charAt(i++));h3=b.indexOf(a.charAt(i++));h4=b.indexOf(a.charAt(i++));bits=h1<<18|h2<<12|h3<<6|h4;c=bits>>16&0xff;o2=bits>>8&0xff;o3=bits&0xff;if(h3==64){tmp_arr[ac++]=String.fromCharCode(c)}else if(h4==64){tmp_arr[ac++]=String.fromCharCode(c,o2)}else{tmp_arr[ac++]=String.fromCharCode(c,o2,o3)}}while(i<a.length);dec=tmp_arr.join('');dec=u(dec);return dec}function e(a){a=a.replace('--','==');a=a.replace('-','=');a=d(r(a));window.location=r('znvygb')+':'+a}function ri(){var a=new Array();var s="abcdefghijklmnopqrstuvwxyz";for(i=0;i<s.length;i++)a[s.charAt(i)]=s.charAt((i+13)%26);for(i=0;i<s.length;i++)a[s.charAt(i).toUpperCase()]=s.charAt((i+13)%26).toUpperCase();return a}function r(a){if(typeof rmap=='undefined')rmap=ri();s="";for(i=0;i<a.length;i++){var b=a.charAt(i);s+=(b>='A'&&b<='Z'||b>='a'&&b<='z'?rmap[b]:b)}return s}

// Order form interactions.
Event.observe(window, 'load', function () {
  if ($('publication-table'))
  {
    var row = $$('.publication-row')[0].cloneNode(true),
      table = $('publication-table'),
      update = function () {
        $$('.publication-remove-row').each(Element[($$('.publication-row').length <= 1 ? 'hide' : 'show')]);
      };
    
    table.observe('click', function (event) {
      var target;
      
      if (target = event.findElement('.publication-add-row'))
      {
        event.stop();
        var clone = row.cloneNode(true);
        clone.down('option').selected = true;
        clone.down('input').setValue('1');
        Element.insert(target, {'before': clone});
      }
      else if (event.findElement('.publication-remove-row'))
      {
        event.stop();
        event.findElement('tr').remove();
      }
      
      update();
    });
    
    update();
  }
});

// Subscribe form interactions.
Event.observe(window, 'load', function () {
  if ($('subscribe_interest')) {
    $('subscribe_interest').observe('change', function () {
      if (this.getValue() == 4) {
        $$('.field_ref_interest_other').invoke('show');
      } else {
        $$('.field_ref_interest_other').invoke('hide');
      }
    });
  
    if ($('subscribe_interest').getValue() != 4) {
      $$('.field_ref_interest_other').invoke('hide');
    }
  }
});

// Handling default values for subscribe form.
Event.observe(window, 'load', function () {
  $$('#qlSubscribeForm .input_text').each(function (input) {
    var label = input.readAttribute('title');
    
    input
      .observe('focus', function () {
        if (input.getValue() == label) {
          input.setValue('').addClassName('active');
        }
      })
      .observe('blur', function () {
        if (input.getValue() == '') {
          input.setValue(label).removeClassName('active');
        }
      });
    
    if (input.getValue() == '') {
      input.setValue(label);
    }
  });
  
  $('qlSubscribeForm').observe('submit', function (event) {
    this
      .select('input')
        .findAll(function(input) { return input.getValue() == input.getAttribute('title'); })
          .invoke('setValue', '');
  });
});

// Flyout navigation items with delay. This not only adds a delay on the 
// mouseout, it also delays the next mouseover if there is already a flyout
// visible.
Event.observe(window, 'load', function () {
  var over,      // The current visible flyout li.
      next_over, // The next flyout to display.
      timeout;   // The timeout id for until the current flyout dissappears.
  
  $$('#nav .parent').each(function (element) {
    element
      .observe('mouseover', function () {
        // Flyout already visible, register for next.
        if (over && over != this) {
          next_over = this;
        }
        // Otherwise display this flyout.
        else {
          window.clearTimeout(timeout);          
          if (over != this) {
            over = this;
            over.addClassName('over');
          }
        }
      })
      .observe('mouseout', function () {
        // Deregister for next flyout.
        if (next_over == this) {
          next_over = null;
        }
        // Otherwise set a timeout to hide the flyout.
        else {
          timeout = window.setTimeout(function () {
            over.removeClassName('over');
            // Display the next flyout.
            if (next_over) {
              over = next_over;
              over.addClassName('over');
              next_over = null;
            }
            // Otherwise just
            else {
              over = null;
            }
          }, 230);
        }
      });
  });
});

