/* $Id: common.js,v 1.1 2006/05/11 23:24:18 hoss Exp hoss $ */

/* El http://www.alistapart.com/stories/alternate */
function setActiveStyleSheet(title) {
   var i, a, main;
   for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
     if(a.getAttribute("rel").indexOf("style") != -1
        && a.getAttribute("title")) {
       a.disabled = true;
       if(a.getAttribute("title") == title) a.disabled = false;
     }
   }
}


function addEvent(obj, evType, fn) { 
  if (obj.addEventListener) { 
    obj.addEventListener(evType, fn, true); 
    return true; 
  }
  else if (obj.attachEvent) { 
    var r = obj.attachEvent("on"+evType, fn); 
    return r; 
  }
  else { 
    return false; 
  } 
}

/* Por kasxi retposxtadresojn de spamrobotoj.  Sxangxos:
 * <span class="retposxtadreso">user cxe example punkto com</span> al
 * <a href="mailto:user@example.com">
 */

function riparuRetposxtadresojn() {
  var spanoj = document.getElementsByTagName('span');
  for (var i = 0; i < spanoj.length; i++) {
    var klaso = spanoj[i].getAttribute('class');
    if (klaso == null) { /* MSIE knows 'className', not 'class' */
      klaso = spanoj[i].getAttribute('className');
    }
    if (klaso == 'retposxtadreso' || klaso == 'emailAddress') {
      var teksto = spanoj[i].firstChild;
      var adreso = teksto.data.replace(/\s+(cxe|che|Äe|at)\s+/i, '@');
      adreso = adreso.replace(/\s+(punkto?|dot)\s+/i, '.');
      adreso = adreso.replace(/\s*/g, '');
      var ankro = document.createElement('a');
      ankro.appendChild(document.createTextNode(adreso));
      ankro.setAttribute('href', 'mailto:' + adreso);
      spanoj[i].removeChild(teksto);
      spanoj[i].appendChild(ankro);
    }
  }
}

/* Sxangxu href-atributojn en ligiloj, kiuj kondukas nin al aliaj pagxaroj.
 * Cxar la sxangxoj nur okazas dum musklakon, la nova valuto ne
 * montrigxas en la statusindikilo dum mussxvebo.
 */

function flaguElligilojn() {
  var ankroj = document.getElementsByTagName('a');
  var re1 = /^http:/;
  var re2 = new RegExp(window.location.hostname);
  for (var i = 0; i < ankroj.length; i++) {
    var a = ankroj[i];
    var url = a.getAttribute('href');
    if (re1.test(url) && !re2.test(url)) {
      var title = a.getAttribute('title') || '';
      a.setAttribute('title', title + (title == '' ? '' : ' ') + '(offsite)');
      a.onclick = function() {
        window.open('/cgi/go/' + this.getAttribute('href'), "_self");
        return(false);
      }
    }
  }
}


/* 
 * Rewrite anchors with classes of the form "popup-<width>x<height>" with
 * window.open() for their onClick attribute, so that the requested content
 * appears in a new window with dimensions width x height.
 *
 * Useful for launching video clips in a separate window.  Passes 
 * XHTML Strict muster, and degrades gracefully when JavaScript support
 * is absent.  (In such cases, the clip simply opens in the current window.)
 *
 * Author: Hoss Firooznia <hoss@lodestone.org>
 *
 */

/* automatically rewrite wxh links */
function sxangxuSxprucLigilojn() {
  var re = /popup-([0-9]+)x([0-9]+)/;
  var ankroj = document.getElementsByTagName("a");
  for (var i = 0; i < ankroj.length; i++) {
    var a = ankroj[i];
    var c = (a.getAttribute('class') || a.getAttribute('className'));
    if (re.test(c)) {
      a.onclick = function() {
        var ary = 
          re.exec(this.getAttribute('class') || this.getAttribute('className'));
        var width = ary[1];
        var height = ary[2];
        window.open(this.getAttribute('href'), '_blank', +
          'toolbar=0,location=0,directories=0,status=0,' +
          'channelmode=0,dependent=0,fullscreen=0,menubar=0,' +
          'scrollbars=1,resizable=1,width=' + width + ',height=' + 
          (Number(height) + 15));
        return(false);
      }
    }
  }
}

/* De: http://validweb.nl/artikelen/javascript/better-zebra-tables */
function striu() {
  var tables = document.getElementsByTagName("table");  

  for(var x=0;x!=tables.length;x++){
    var table = tables[x];
    if (! table) { return; }
    
    var tbodies = table.getElementsByTagName("tbody");
    
    for (var h = 0; h < tbodies.length; h++) {
      var even = true;
      var trs = tbodies[h].getElementsByTagName("tr");
      
      for (var i = 0; i < trs.length; i++) {
        if(even)
          trs[i].className += " even";
        even = !even;
      }
    }
  }
}

addEvent(window, "load", flaguElligilojn);
addEvent(window, "load", sxangxuSxprucLigilojn);
addEvent(window, "load", riparuRetposxtadresojn);
addEvent(window, "load", striu);
