/*
 * Gestion de la transparence sous IE6.
 * 
 * Classe JS inspiree de SuperSleight (http://24ways.org/2007/supersleight-transparent-png-in-ie6).
 *
 * La transparence PNG est geree sous IE6 pour :
 * - les tags <img src="blah.png"/> qui ont au moins une hauteur ou une largeur renseignee.
 * - les arrieres plans png sur n'importe quel element qui a au moins une hauteur ou une largeur renseignee.
 *
 * Problemes connus :
 * - on perd le positionnement des backgrounds png transparents
 * - les background-repeat ne sont pas geres (on les laisse tels quels sans transparence)
 */

function pngfix(){}

pngfix.loadPngs = function(root){
	if (navigator.userAgent.indexOf("MSIE 6") != -1) {
		
		if (!root) {
			root = document;
		}
		
		//éviter de freezer le navigateur lorsqu'on charge des pages contenant trop d'images à traiter
		var listImages = $(root).find("img");
		if (listImages.length > 200) {
			//alert("skip too many img "+root.nodeName+" "+root.id+" : "+listImages.length);
			return;
		}
		
		//traitement des IMG ayant un png en src
		listImages.each( 
			function() {
				if (this.src.match(/\.png$/i) !== null) {
					pngfix.el_fnFixPng(this);
				}
			}
		);
		
		//traitement des éléments ayant un png en background-image
		$(root).find("p").each( 
			function() {
				if (this.currentStyle.backgroundImage.match(/\.png/i) !== null) {
					pngfix.bg_fnFixPng(this);
				}
			}
		);
	}
}

/**
 * Traitement des elements ayant une image PNG en arriere plan.
 */
pngfix.bg_fnFixPng = function(obj) {
	var bg	= obj.currentStyle.backgroundImage;
	var src = bg.substring(5,bg.length-2);
	
	//on ne gere pas les background-repeat
	//une hauteur ou une largeur semble suffire pour le mode 'crop'
	if (obj.currentStyle.backgroundRepeat == 'no-repeat' && (obj.currentStyle.height != "auto" || obj.currentStyle.width != "auto")){
	
		//alert("apply "+obj.nodeName+" / "+obj.currentStyle.height+" - "+obj.currentStyle.width+ "-" + src);
		
		//modif Cardiweb pour eviter les soucis avec les images d'arriere plan - on garde la taille originale de l'image en arriere plan
		var mode = 'crop';
		//var mode = 'scale';
		//if (obj.currentStyle.backgroundRepeat == 'no-repeat') {
		//	mode = 'crop';
		//}
		obj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='" + mode + "')";
		obj.style.backgroundImage = 'url('+constants.contextPath+'/cb-statics/bo/img/blank.gif'+')';
		
		return true;
	}
	else{
		//on ne peut rien faire, il faut au moins avoir un width ou height pour faire marcher la transparence en mode 'crop'...
		//resultat: on laisse le PNG tel quel sans la transparence
		//alert("ignore "+obj.nodeName+" / "+obj.currentStyle.height+" - "+obj.currentStyle.width+ "-" + src);
		return false;
	}
};

/**
 * Traitement des tags IMG ayant une image PNG en src.
 */
pngfix.el_fnFixPng = function(img) {
	var src = img.src;
	
	//une hauteur ou une largeur semble suffire pour le mode 'crop'
	if (img.width != "" || img.height != ""){
		img.style.width = img.width + "px";
		img.style.height = img.height + "px";
		
		img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='scale')";
		img.src = constants.contextPath+'/cb-statics/bo/img/blank.gif';
		
		return true;
	}
	else{
		//on ne peut rien faire, il faut au moins avoir un width ou height pour faire marcher la transparence en mode 'crop'...
		//resultat: on laisse le PNG tel quel sans la transparence
		
		return false;
	}
};

