/**
 * --------------------------------------------------------------------
 * jQuery-Plugin "pngFix"
 * Version: 1.1, 11.09.2007
 * by Andreas Eberhard, andreas.eberhard@gmail.com
 *                      http://jquery.andreaseberhard.de/
 *
 * Copyright (c) 2007 Andreas Eberhard
 * Licensed under GPL (http://www.opensource.org/licenses/gpl-license.php)
 *
 * Changelog:
 *    11.09.2007 Version 1.1
 *    - removed noConflict
 *    - added png-support for input type=image
 *    - 01.08.2007 CSS background-image support extension added by Scott Jehl, scott@filamentgroup.com, http://www.filamentgroup.com
 *    31.05.2007 initial Version 1.0
 * --------------------------------------------------------------------
 * @example $(function(){$(document).pngFix();});
 * @desc Fixes all PNG's in the document on document.ready
 *
 * jQuery(function(){jQuery(document).pngFix();});
 * @desc Fixes all PNG's in the document on document.ready when using noConflict
 *
 * @example $(function(){$('div.examples').pngFix();});
 * @desc Fixes all PNG's within div with class examples
 *
 * @example $(function(){$('div.examples').pngFix( { blankgif:'ext.gif' } );});
 * @desc Fixes all PNG's within div with class examples, provides blank gif for input with png
 * --------------------------------------------------------------------
 */

(function($) {

jQuery.fn.pngFix = function(settings) {

	// Settings
	settings = jQuery.extend({
		blankgif: 'blank.gif'
	}, settings);

	var ie55 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 5.5") != -1);
	var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);

	if (jQuery.browser.msie && (ie55 || ie6)) {

		//fix images with png-source
		jQuery(this).find("img[src$='.png']").each(function() {

			// Defining the image's width
			if(jQuery(this).attr('width') != 0)
				curImgWidth = jQuery(this).attr('width');
			else if(jQuery(this).width() != 0)
				curImgWidth = jQuery(this).width();
			else if (jQuery(this).css("width") != 0)
				curImgWidth = jQuery(this).css("width");
			else if (jQuery(this).get(0).runtimeStyle.width != 0) 
				curImgWidth = jQuery(this).get(0).runtimeStyle.width;

			// If the "px" is missing, we add it
			if(curImgWidth.toString().indexOf("px") == -1)
				curImgWidth = curImgWidth.toString()+"px";

			// Defining the image's height
			if(jQuery(this).attr('height') != 0)
				curImgHeight = jQuery(this).attr('height');
			else if(jQuery(this).height() != 0)
				curImgHeight = jQuery(this).height();
			else if (jQuery(this).css("height") != 0)
				curImgHeight = jQuery(this).css("height");
			else if (jQuery(this).get(0).runtimeStyle.height != 0) 
				curImgHeight = jQuery(this).get(0).runtimeStyle.height;

			// If the "px" is missing, we add it
			if(curImgHeight.toString().indexOf("px") == -1)
				curImgHeight = curImgHeight.toString()+"px";

			var prevStyle = '';
			var strNewHTML = '';
			var imgId = (jQuery(this).attr('id')) ? 'id="' + jQuery(this).attr('id') + '" ' : '';
			var imgClass = (jQuery(this).attr('class')) ? 'class="' + jQuery(this).attr('class') + ' replacedImgByPngFixJandK" ' : '';
			var imgTitle = (jQuery(this).attr('title')) ? 'title="' + jQuery(this).attr('title') + '" ' : '';
			var imgAlt = (jQuery(this).attr('alt')) ? 'alt="' + jQuery(this).attr('alt') + '" ' : '';
			var imgAlign = (jQuery(this).attr('align')) ? 'float:' + jQuery(this).attr('align') + ';' : '';
			if(jQuery(this).parent().attr('href') || jQuery(this).css('cursor') == "pointer" || jQuery(this).parent().css('cursor') == "pointer")
				imgHand = 'cursor:pointer;';
			else
				imgHand = '';

			
			if (this.style.border) {
				prevStyle += 'border:'+this.style.border+';';
				this.style.border = '';
			}
			if (this.style.padding) {
				prevStyle += 'padding:'+this.style.padding+';';
				this.style.padding = '';
			}
			if (this.style.margin) {
				prevStyle += 'margin:'+this.style.margin+';';
				this.style.margin = '';
			}
			var imgStyle = (this.style.cssText);

			strNewHTML += '<span '+imgId+imgClass+imgTitle+imgAlt+'style="';
			strNewHTML += 'position:relative;display:inline-block;background:transparent;'+imgAlign+imgHand;
			strNewHTML += 'width:' + curImgWidth + ';height:' + curImgHeight+';';
			strNewHTML += 'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src=\'' + jQuery(this).attr('src') + '\', sizingMethod=\'crop\');';
			strNewHTML += imgStyle+'"></span>';

			if (prevStyle != ''){
				strNewHTML = '<span style="position:relative;display:inline-block;'+prevStyle+imgHand+'width:' + curImgWidth + 'px;' + 'height:' + curImgHeight + 'px;'+'">' + strNewHTML + '</span>';
			}

// Bouton close
if(jQuery(this).parents("#boxRSS").length > 0 && jQuery(this).parent().hasClass("close")){
/*	alert(curImgWidth);
	alert(curImgHeight);
	alert(this.onclick);
	alert($(this).onclick);*/
//	alert(strNewHTML);
}

// Titre RSS
/*if(jQuery(this).parents("#boxRSS").length > 0 && jQuery(this).parent().hasClass("titre")){
	alert(curImgWidth);
	alert(curImgHeight);
	alert(strNewHTML);
}*/

// Bouton RSS
if(jQuery(this).parents("#boxRSS").length > 0 && jQuery(this).parent().get(0).tagName == "A"){
/*	alert(curImgWidth);
	alert(curImgHeight);
	alert(strNewHTML);*/
}

	
			jQuery(this).hide();
			jQuery(this).after(strNewHTML);
	
			// S'il y en avait un, on remet le onclick
//			var onClick = (jQuery(this).attr('onclick')) ? 'onclick="' + jQuery(this).attr('onclick') +'" ' : '';
			jQuery(this).siblings(".replacedImgByPngFixJandK").click(function(){
				jQuery(this).siblings("IMG").trigger("click");
			});
		});
		


		// fix css background pngs
		jQuery(this).find("*").each(function(){
			var bgIMG = jQuery(this).css('background-image');
			if(bgIMG.indexOf(".png")!=-1){
				// Si c'est le background des pieMenu, on le remplace par un jpg
				if(bgIMG.indexOf("bg_boxPieMenu") !=-1){
					newBgIMG = bgIMG.substring(0,bgIMG.indexOf("bg_boxPieMenu"))+'bg_boxPieMenu.jpg")';

					jQuery(this).css('background-image', newBgIMG);
				}
				else{
					var iebg = bgIMG.split('url("')[1].split('")')[0];
					jQuery(this).css('background-image', 'none');
					jQuery(this).get(0).runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + iebg + "',sizingMethod='scale')";					
				}				
			}
		});
		
		//fix input with png-source
		jQuery(this).find("input[src$='.png']").each(function() {
			var bgIMG = jQuery(this).attr('src');
			jQuery(this).get(0).runtimeStyle.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src=\'' + bgIMG + '\', sizingMethod=\'scale\');';
   		jQuery(this).attr('src', settings.blankgif)
		});
	
	}

	return jQuery;

};

})(jQuery);
