// Définition d'une classe PNGHandler() pour la gestion de la transparence des PNG sous IE
function PNGHandler() {
  var self = this;

	//	Détection du système d'exploitation, du navigateur et de la version
  this.na = navigator.appName.toLowerCase();
  this.nv = navigator.appVersion.toLowerCase();
  this.isIE = this.na.indexOf('internet explorer')+1?1:0;
  this.isWin = this.nv.indexOf('windows')+1?1:0;
  this.ver = this.isIE?parseFloat(this.nv.split('msie ')[1]):parseFloat(this.nv);
  this.isMac = this.nv.indexOf('mac')+1?1:0;
  this.isOpera = (navigator.userAgent.toLowerCase().indexOf('opera ')+1 || navigator.userAgent.toLowerCase().indexOf('opera/')+1);
  if (this.isOpera) this.isIE = false;
	
	//	Initialisation de la variable contenant la méthode de transformation
  this.transform = null;

	//	--- Méthode de récupération des nodes par leur nom de classe CSS ---
  this.getElementsByClassName = function(className,oParent) {
    doc = (oParent||document);
    matches = [];
		
		// Récupération de tous les nodes du document et itération, rangement des "matches" dans un tableau
    nodes = doc.all||doc.getElementsByTagName('*');
    for (i=0; i<nodes.length; i++) {
      if (nodes[i].className == className || nodes[i].className.indexOf(className)+1 || nodes[i].className.indexOf(className+' ')+1 || nodes[i].className.indexOf(' '+className)+1) {
        matches[matches.length] = nodes[i];
      }
    }
    return matches;
  }

	//	--- Méthode de filtrage pour IE 5.5+ ---
	//	Crée un nouvel élément basé sur l'ancien pour éviter des problèmes de rendu.
	//	Utilise la méthode propriétaire "currentStyle" pour copier les styles.
  this.filterMethod = function(oOld) {
		var o = document.createElement('div');
		
		// http://msdn.microsoft.com/workshop/author/filter/reference/filters/alphaimageloader.asp
		var filterID = 'DXImageTransform.Microsoft.AlphaImageLoader'; 
    
		if (oOld.nodeName == 'DIV') {
			// Récupération du background
      var b = oOld.currentStyle.backgroundImage.toString();
      oOld.style.backgroundImage = 'none';
			
			// Parsing du background
      var i1 = b.indexOf('url("')+5;
      var newSrc = b.substr(i1,b.length-i1-2).replace('.gif','.png');
			
			// Remplacement du vieil élément par le nouveau
      o = oOld;
      o.style.writingMode = 'lr-tb';
      o.style.filter = "progid:"+filterID+"(src='"+newSrc+"',sizingMethod='crop')";
			
    } else if (oOld.nodeName == 'IMG') {
      // Suppression de l'ancienne image
			var newSrc = oOld.getAttribute('src').replace('.gif','.png');
      oOld.src = 'none.gif';
			
			// Application du filtre
      oOld.style.filter = "progid:"+filterID+"(src='"+newSrc+"',sizingMethod='crop')";
      oOld.style.writingMode = 'lr-tb';
    }
  }

	//	--- Méthode pour support natif du PNG ---
  this.pngMethod = function(o) {
		// Réupération du background
    bgImage = this.getBackgroundImage(o);
		
		// bgImage n'est pas vide, c'est un DIV
    if (bgImage) {
      o.style.backgroundImage = 'url('+bgImage.replace('.gif','.png')+')';
    } else if (o.nodeName == 'IMG') {
      o.src = o.src.replace('.gif','.png');
    } else if (!this.isMac) {
			// Ce n'est pas un DIV ou IMG
    }
  }
	
	//	--- Méthode de récupération du background ---
  this.getBackgroundImage = function(o) {
    var b, i1;
    var bgUrl = null;

		// Pas de support IE:MAC pour les DIV avec background PNG
    if (o.nodeName == 'DIV' && !(this.isIE && this.isMac)) {
      if (document.defaultView) {
        if (document.defaultView.getComputedStyle) {
					// Récupération de la propriété de style "background-image"
          b = document.defaultView.getComputedStyle(o,'').getPropertyValue('background-image');
          i1 = b.indexOf('url(')+4;
          bgUrl = b.substr(i1,b.length-i1-1);
        } else {
        }
      } else {
      }
    }
    return bgUrl;
  }

	//	--- Méthode de détermination de la méthode à utiliser pour la transformation ---
	// Si IE 5.5+/Win32: filter
	this.supportTest = function() {
    if (this.isIE && this.isWin && this.ver >= 5.5) {
			// IE 5.5+/Win32
      self.transform = self.filterMethod;
    } else if (!this.isIE && this.ver < 5) {
			// Navigateur non supporté
      self.transform = null;
    } else if (!this.isIE && this.ver >= 5 || (this.isIE && this.isMac && this.ver >= 5)) {
			// Navigateur supportant nativement la transparence PNG
      self.transform = self.pngMethod;
    } else {
      self.transform = null;
      return false;
    }
    return true;
  }

	/* 	Méthode d'initialisation de l'objet */
  this.init = function() {
		/* Test du support par le navigateur */
    if (this.supportTest()) {
			/* Récupération de tous les PNG et itération */
      this.elements = this.getElementsByClassName('png');
      for (var i=0; i<this.elements.length; i++) {
				/* Appel de la méthode de transformation pour chaque PNG */
        this.transform(this.elements[i]);
      }
    }
  }

}

/* Création d'une instance de l'objet */
var pngHandler = new PNGHandler();