// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function hrdecode(str) {
  str = $A(str.match(/../g)).reverse().collect(function(s) {
          return '%' + s.substring(1, 2) + s.substr(0, 1);
        }).join('');
  return decodeURIComponent(str);
}

function revive(c, addr, label) {
  var e = $('addr' + c), title;
  addr = hrdecode(addr);
  label = hrdecode(label);
  title = e.innerHTML.replace(/@\.\.\./, label);
  Element.update(e, '<a href="mailto:' + addr + '">' + title + '</a>');
}

function searchInputs() {
  return $$('input[type=checkbox][name]');
}
function searchAll() {
  var i = $('idxall');
  if(i) {
    if(i.checked) searchInputs().each(function(e) { e.checked = false; });
    i.checked = true;
    searchFix();
  }
}
function searchSel() {
  var i = $('idxall');
  if(i) {
    var sel = searchInputs().find(function(e) { return e.checked; });
    i.checked = !sel;
    searchFix();
  }
}
function searchFix() {
  var i = $('idxall');
  if(i) {
    var c = i.checked;
    searchInputs().each(function(e) {
      e.parentNode.style.color = c ? 'gray' : 'black';
    });
  }
}

function scrollToWithoutToolbar(e) {
  var pos = Position.cumulativeOffset(e);
  pos[1] -= Element.getDimensions('toolbar').height;
  window.scrollTo(Position.deltaX, pos[1]);
}

function scrollToWithoutToolbarOnClick(e) {
  var elt = Event.element(e), a, m;
  a = elt.getAttribute('href');
  if(a && (m = a.match(/#(.*)/)) && (elt = $(m[1]))) {
    scrollToWithoutToolbar(elt);
    Event.stop(e);
  }
}

var toolbarHeight;
function adjustToolbarSize() {
  var height = Element.getDimensions('toolbar').height;
  if(toolbarHeight != height) {
    toolbarHeight = height;
    Element.setStyle('content', { paddingTop: height + 'px' });
  }
}

var searchQueries = [
  'q', 'as_q', 'as_epq', 'as_oq',      // Google, etc.
  'p',                                 // Yahoo
  'query', 'wfq',                      // AOL, etc.
  'search',                            // Excite, Netscape
  'general',                           // Go2net
  'MT',                                // MSN, goo
  'qt',                                // Infoseek
  'key',                               // Looksmart
  'qkw', 'searchText',                 // webcrawler, infospace
  'Keywords',                          // overture
  'ask',                               // ask
  'qr',                                // northernlight, nlsearch
];
function hilightSearchKeyword() {
  if(!document.referrer || $('query_content')) return;
  if(/\bdisableKeywordHilight=/.test(document.cookie)) return;
  
  var qs = document.referrer.split(/\?/, 2);
  if(qs[1]) {
    var re = new RegExp("^(" + searchQueries.join("|") + ")=(.*)");
    var keywords = $A(qs[1].split(/[;&]/)).collect(function(q) {
                     var m = q.match(re);
                     return m ? decodeURIComponent(m[2].replace(/\+/g, ' ')) : null;
                   }).compact();
    if(keywords.length > 0) {
      doHilightSearchKeyword(keywords);
    }
  }
}

function doHilightSearchKeyword(keywords) {
  keywords = keywords.collect(function(q) {
               var elts = q.match(/("[^"]+"|[^"\s　])+/g);
               return elts.collect(function(e) {
                        return e.replace(/"/g, '');
                      }).select(function(e) {
                        return e.search(/^([a-z]+:|([!&\|]+|[\(\)]|and|or|not)$)/i);
                      }).collect(function(e) {
                        return e.replace(/^[\+\!]/, '').
                                 replace(/([\\\^\$\*\+\?\.\(\)\{\}\|\[\]])/g, '\\$1');
                      });
             }).flatten();
  doHilightSearchKeywordElements($('content').childNodes,
    new RegExp("(" + keywords.join('|') + ")", "ig"));
}

function doHilightSearchKeywordElements(elements, re) {
  $A(elements).each(function(elt) {
    if(elt.nodeType == 3) {
      var m, str = elt.nodeValue, result, lastIndex;
      lastIndex = re.lastIndex = 0;
      result = document.createElement('span');
      while(m = re.exec(str)) {
        var hi = document.createElement('span');
        hi.className = 'shilight';
        hi.appendChild(document.createTextNode(m[0]));
        if(m.index > lastIndex)
          result.appendChild(document.createTextNode(str.substring(lastIndex, m.index)));
        result.appendChild(hi);
        lastIndex = re.lastIndex;
      }
      if(lastIndex > 0) {
        if(lastIndex < str.length)
          result.appendChild(document.createTextNode(str.substring(lastIndex)));
        elt.parentNode.replaceChild(result, elt);
      }
    } else {
      doHilightSearchKeywordElements(elt.childNodes, re);
    }
  });
}

Event.observe(window, 'load', function() {
  $$('a').select(function(e) {
    var a = e.getAttribute('href');
    return a && a.match(/#/);
  }).each(function(e) {
    Event.observe(e, 'click',
      scrollToWithoutToolbarOnClick.bindAsEventListener(this));
  });
  if(location.hash) {
    var m = location.hash.replace(/^#/, '');
    if(m = $(m)) scrollToWithoutToolbar(m);
  }
  searchFix();
  adjustToolbarSize();
  hilightSearchKeyword();
});
Event.observe(window, 'resize', adjustToolbarSize);

function ieInstallToolbarFixer() {
  ieVersion = navigator.appVersion.match(/\bMSIE (\d)/)[1];
  if(ieVersion <= 6) {
    $('toolbar').style.position = 'absolute';
    Event.observe(window, 'scroll', ieFixedToolbar, false);
  }
  $$('#toolbar input', '#toolbar button').each(function(e) {
    e.ieTarget = true;
    e.origBorderColor = Element.getStyle(e, 'borderColor');
    Event.observe(e, 'mouseover', ieHoverOn, false);
    Event.observe(e, 'mouseout', ieHoverOff, false);
    Event.observe(e, 'focus', ieHoverFocus, false);
    Event.observe(e, 'blur', ieHoverBlur, false);
  });
}
function ieFixedToolbar() {
  var s = $('toolbar').style;
  Position.prepare();
  s.left = Position.deltaX;
  s.top = Position.deltaY;
}
function ieHoverOn() {
  var e = window.event.srcElement;
  if(e.ieTarget) {
    e.style.borderColor = '#008';
  }
}
function ieHoverFocus() {
  var e = window.event.srcElement;
  e.focused = true;
  ieHoverOn();
}
function ieHoverOff() {
  var e = window.event.srcElement;
  if(e.ieTarget && !e.focused) {
    e.style.borderColor = e.origBorderColor;
  }
}
function ieHoverBlur() {
  var e = window.event.srcElement;
  e.focused = false;
  ieHoverOff();
}

if(navigator.appVersion.match(/\bMSIE\b/)) {
  Event.observe(window, 'load', ieInstallToolbarFixer, false);
}
