// Write a link to my email.
function email(link_class, visible_text) {
	var email = 'thibault';
		email += '.deckers@';
		email += 'gmail';
 		email += '.com';
		document.write('<a class="' + link_class + '" href="mailto:'+email+'">' + ((visible_text == undefined)? email : visible_text) + '</a>');
}

// Fade in/out functions.
var opacityStep = 0.1;
var faderInterval = 25;
var delay = 200;

var waitingFadeInHandlers = {};
var faderHandlers = {};

function setOpacity(id, opacity) {
  var element = document.getElementById(id);
  element.style.opacity = opacity;
  element.style.filter = 'alpha(opacity=' + (opacity * 100) + ')';
}

function waitAndFadeIn(group, id) {
  if(typeof waitingFadeInHandlers[group] == 'undefined') {
    waitingFadeInHandlers[group] = {};
  } else {
    cancelWaitingFadeIns(group);
  }
  waitingFadeInHandlers[group][id] = setTimeout("fadeIn('" + group + "','" + id + "')", delay);
}
function cancelWaitingFadeIns(group) {
  var groupHandlers = waitingFadeInHandlers[group];
  for(var id in groupHandlers) {
    clearTimeout(groupHandlers[id]);
  }
}

function fadeIn(group, id) {
  if(typeof faderHandlers[group] == 'undefined') {
    faderHandlers[group] = {};
  } else {
    // Avoid fade-out of the element to fade-in.
    clearInterval(faderHandlers[group][id]);
    delete faderHandlers[group][id];
    // Parallel fade-outs of every fading-in and faded-in elements of the group.
    var groupHandlers = faderHandlers[group];
    for(var i in groupHandlers) {
      setTimeout("fadeOut('" + group + "','" + i + "')", 0);
    }
  }
  // Fade-in of the specified element.
  var element = document.getElementById(id);
  if(element.style.display == "none") {
    setOpacity(id, 0);
    element.style.display = "block";
  }
  setTimeout("giveTimeToFadeOut('" + group + "','" + id + "')", 180);
}
function giveTimeToFadeOut(group, id) {
  faderHandlers[group][id] = setInterval("increaseOpacity('" + group + "','" + id + "')", faderInterval);
}
function increaseOpacity(group, id) {
  var element = document.getElementById(id);
  var opacityAsNumber = +element.style.opacity;
  var newOpacity = Math.round((opacityAsNumber + opacityStep) * 100) / 100;
  setOpacity(id, newOpacity);
  if(newOpacity >= 1) {
    clearInterval(faderHandlers[group][id]);
  }
}

function fadeOut(group, id) {
  clearInterval(faderHandlers[group][id]);
  var element = document.getElementById(id);
  faderHandlers[group][id] = setInterval("decreaseOpacity('" + group + "','" + id + "')", faderInterval);
}
function decreaseOpacity(group, id) {
  var element = document.getElementById(id);
  var opacityAsNumber = +element.style.opacity;
  var newOpacity = Math.round((opacityAsNumber - opacityStep) * 100) / 100;
  setOpacity(id, newOpacity);
  if(newOpacity <= 0) {
    element.style.display = "none";
    clearInterval(faderHandlers[group][id]);
    delete faderHandlers[group][id];
  }
}

// Scrolling functions.
var pageSize = 320;
var slowFactor = 20;
var thumbVerticalInterval = 11;

var scrollHandlers = {};

function showNewerThumbs(id) {
  if(typeof scrollHandlers[id] == 'undefined') {
    var page = document.getElementById(id);
    var newPosition = Math.max(0, page.scrollTop - pageSize);
    if(page.scrollTop != newPosition) {
      scrollHandlers[id] = setInterval("moveUpToPosition('" + id + "'," + newPosition + ")", slowFactor);
    }
  }
}
function showOlderThumbs(id) {
  if(typeof scrollHandlers[id] == 'undefined') {
    var page = document.getElementById(id);
    var newPosition = Math.min(page.scrollHeight - pageSize + thumbVerticalInterval, page.scrollTop + pageSize);
    if(page.scrollTop != newPosition) {
      scrollHandlers[id] = setInterval("moveDownToPosition('" + id + "'," + newPosition + ")", slowFactor);
    }
  }
}
// 2 different move up/down functions to avoid a lot of tests in a common function.
function moveUpToPosition(id, position) {
  var page = document.getElementById(id);
  var oldPosition = page.scrollTop;
  var smoothScrollStep = Math.max(1, (oldPosition - position) / 6);
  page.scrollTop = Math.max(oldPosition - smoothScrollStep, position);
  if(page.scrollTop == oldPosition) {
    clearInterval(scrollHandlers[id]);
    delete scrollHandlers[id];
  }
}
function moveDownToPosition(id, position) {
  var page = document.getElementById(id);
  var oldPosition = page.scrollTop;
  var smoothScrollStep = Math.max(1, (position - oldPosition) / 5);
  page.scrollTop = Math.min(oldPosition + smoothScrollStep, position);
  if(page.scrollTop == oldPosition) {
    clearInterval(scrollHandlers[id]);
    delete scrollHandlers[id];
  }
}

