// Cross-browser event-handling for IE5+, NS6+, and Mozilla/Gecko by Scott Andrew
function addEvent(elm, evType, fn, useCapture) {
  if (elm.addEventListener) {
    elm.addEventListener(evType, fn, useCapture);
    return true;
  } else if (elm.attachEvent) {
    var r = elm.attachEvent('on' + evType, fn);
    EventCache.add(elm, evType, fn);
    return r;
  } else {
    elm['on' + evType] = fn;
  }
}

// Finds the element who initiated the event
function findTarget(e) {
  var target;
  if (window.event && window.event.srcElement) {
    target = window.event.srcElement;
  } else if (e && e.target) {
    target = e.target;
  }
  if (!target) {
    return null;
  } else {
    return target;
  }
}

function fadeOpacity() {
  if (spihOnglets.currentOpacity >= 100) {
    clearInterval(spihOnglets.currentOnglet.fadeInterval);
    return;
  } 
  spihOnglets.currentOpacity += 20;
  if (spihOnglets.currentSection != null) {  
    spihOnglets._setOpacity(spihOnglets.currentSection, spihOnglets.currentAvantage, spihOnglets.currentOpacity);
  }
}

function Onglets(sectionList, activeId) {
  var self = this;
  this.currentOnglet = document.getElementById(activeId + '-link');
  this.currentSection = document.getElementById(activeId + '-section');
  this.currentAvantage = document.getElementById(activeId + '-avantage');
  this.currentOpacity = 0;
  
  for (var j = 0; j < sectionList.length; j++) {
    var id = sectionList[j];
    var onglet = document.getElementById(id + '-link');
    addEvent(onglet, 'click', function(e) { self._onMouseClick(e) }, false);
  }
  
}

Onglets.prototype.openDoc = function(e) {
  var el = document.getElementById('documentation-link');
  this._fadeTab(el);
}

Onglets.prototype._onMouseClick = function(e) {
  var el = findTarget(e);
  this._fadeTab(el);
}

Onglets.prototype._fadeTab = function(el) {
  
  if (el != null) {
  
    while (el != null && el.tagName != 'LI') {
      el = el.parentNode;
    }
  
    var subId = el.id.substring(0, el.id.length - 5);
    var section = document.getElementById(subId + '-section');
    var avantage = document.getElementById(subId + '-avantage');
    
    this.currentOpacity = 10;
    this.currentOnglet.className = 'inactive-link';
    this.currentOnglet = el;
    this.currentOnglet.className = 'active-link';
  
    this.currentSection.style.display = 'none';
    if (this.currentAvantage != null) {
      this.currentAvantage.style.display = 'none';
    }
    this.currentAvantage = avantage;
    this.currentSection = section;
    
    if (this.currentSection != null) {
      this.currentSection.style.display = 'block';
    }
    if (this.currentAvantage != null) {
      this.currentAvantage.style.display = 'block';
    }
    this._setOpacity(this.currentSection, this.currentAvantage, this.currentOpacity);
    
    if (this.currentOnglet.fadeInterval) {
      clearInterval(this.currentOnglet.fadeInterval);
    }
    this.currentOnglet.fadeInterval = setInterval('fadeOpacity()', 5);
  }  
}

Onglets.prototype._setOpacity = function(object1, object2, value) {
  var op = value / 100;
  if (typeof object1.style.opacity != 'undefined') {
    object1.style.opacity = op;
    if (object2 != null) object2.style.opacity = op;
  } else if (typeof object1.style.MozOpacity != 'undefined') {
    object1.style.MozOpacity = op;
    if (object2 != null) object2.style.MozOpacity = op;
  } else if (typeof object1.style.KhtmlOpacity != 'undefined') {
    object1.style.KhtmlOpacity = op;
    if (object2 != null) object2.style.KhtmlOpacity = op;
  }
}

function openScreenshot(e) {
  var target = findTarget(e);
  var screenshot = document.getElementById('screenshot');
  var clicClose = document.getElementById('clic-close');
  var src = 'img/gal/' + target.id + '.png';
  var img = screenshot.firstChild;
  img.src = src;
  screenshot.style.display = 'block';
  clicClose.style.display = 'block';
}

function closeScreenshot(e) {
  var screenshot = document.getElementById('screenshot');
  var clicClose = document.getElementById('clic-close');
  var img = screenshot.firstChild;
  screenshot.style.display = 'none';
  clicClose.style.display = 'none';
  img.src = '';
}
