/**
 * Every anchor that is of class 'external' will be opened in a new window.
 */
window.onload = function () {
  var x = document.getElementsByTagName('a');
  for (var i=0; i < x.length; i++) {
    if (x[i].getAttribute('class') == 'external') {
      x[i].onclick = function () {
        return openNewWindow(this.href)
      }
    }
  }
}

/**
 * Open a new window for external links.
 */
function openNewWindow(url) {
  newwindow = window.open(url, 'dummy');
  if (window.focus) { newwindow.focus() }
  return false;
}

/**
 * Get human readable last modified date of the page.
 * (script from quirksmode.org)
 */
function altLastModified(date) {
  var x = date || new Date(document.lastModified);
  Modif = new Date(x.toGMTString());
  Year = altGetYear(Modif);
  Month = Modif.getMonth();
  Day = Modif.getDate();
  Mod = (Date.UTC(Year, Month, Day, 0, 0, 0)) / 86400000;
  x = new Date();
  today = new Date(x.toGMTString());
  Year2 = altGetYear(today);
  Month2 = today.getMonth();
  Day2 = today.getDate();
  now = (Date.UTC(Year2, Month2, Day2, 0, 0, 0)) / 86400000;
  daysago = now - Mod;
  if (daysago < 0) return '';
  unit = 'jours';
  if (daysago > 730) {
    daysago = Math.floor(daysago / 365);
    unit = 'années';
  }
  else if (daysago > 60) {
    daysago = Math.floor(daysago / 30);
    unit = 'mois';
  }
  else if (daysago > 14) {
    daysago = Math.floor(daysago/7);
    unit = 'semaines'
  }
  var towrite = 'page modifiée ';
  if (daysago == 0) towrite += 'aujourd\'hui';
  else if (daysago == 1) towrite += 'hier';
  else towrite += 'il y a ' + daysago + ' ' + unit;
  return towrite;
}

function altGetYear(theDate) {
  var x = theDate.getYear();
  var y = x % 100;
  y += (y < 38) ? 2000 : 1900;
  return y;
}

/**
 * Set the opacity of an object.
 * @param object obj The object to set opacity on.
 * @param integer value A value from 0 to 10.
 */
function setOpacity(obj, value) {
  obj.style.opacity = value/10;
  obj.style.filter = 'alpha(opacity=' + value*10 + ')';
}

/**
 * Get the current position of an object.
 * @param obj The object to get the position.
 * @return array Return an array with left and top position.
 * (Script from quirksmode.org)
 */ 
function findPos(obj) {
  var curleft = curtop = 0;
  if (obj.offsetParent) {
    curleft = obj.offsetLeft
    curtop = obj.offsetTop
    while (obj = obj.offsetParent) {
      curleft += obj.offsetLeft
      curtop += obj.offsetTop
    }
  }
  return [curleft,curtop];
}