/* 
 * jQuery plugin - Preload And Resizing Image
 * Version 2.0
 * Developped by Guillaume DIAS COELHO
*/

(function($) {
	$.fn.resizing = function(options){
		var params = $.extend({}, $.fn.resizing.defaults, options);
        
		if (params.debugMode){$("body").append("<div id='debug'></div>").css({"width":"500px","color":"#000"});}			
		
		return this.each(function() {
			var conteneur = $(this).parent();
			ImgUrl = $(this).attr('src');
			var obj = $(this);
			
			obj.addClass("hide"); // On cache l'image pour le resize

			var maxHeight = params.maxHeight; // On recupere la heuteur dans le param
			var maxWidth = params.maxWidth; // On recupere la largeur dans le param
			
			
			obj.parent().append("<div class='replace'><img src='"+params.replaceBy+"' /></div>"); // on ajoute le noImg
			
			var img = new Image();
			
				img.onload = function(){
					if (img.complete){// Image chargée
						var h = nH = img.height; // on prends la hauteur de l'image chargée
						var w = nW = img.width; // on prends la largeur de l'image chargée
						
						if ((h >= maxHeight) || (w >= maxWidth)) {
							if ((h >= maxHeight) && (w >= maxWidth)) {
								if (h > w) {
									nH = maxHeight;
									nW = parseInt((w * nH) / h, 10);
								} else {
									nW = maxWidth;
									nH = parseInt((h * nW) / w, 10);
								}
							} else if ((h > maxHeight) && (w < maxWidth)) {
								nH = maxHeight;
								nW = parseInt((w * nH) / h, 10);
							} else if ((h < maxHeight) && (w > maxWidth)) {
								nW = maxWidth;
								nH = parseInt((h * nW) / w, 10);
							}
						}
						//console.log("img WxH : "+img.width+"x"+img.height+" | "+nW+"x"+nH);
						
						obj.css({"height":nH+"px", "width":nW+"px"}) // On modifie la largeur et la hauteur de l'image après chargement
						obj.removeClass('hide'); // On affiche l'image après le resize
						obj.parent().find('div.replace').addClass("hide"); // On cache l'image de chargement
					} else {
						// Just for fucking IE !!
						var h = nH = img.height; // on prends la hauteur de l'image chargée
						var w = nW = img.width; // on prends la largeur de l'image chargée
						
						if ((h >= maxHeight) || (w >= maxWidth)) {
							if ((h >= maxHeight) && (w >= maxWidth)) {
								if (h > w) {
									nH = maxHeight;
									nW = parseInt((w * nH) / h, 10);
								} else {
									nW = maxWidth;
									nH = parseInt((h * nW) / w, 10);
								}
							} else if ((h > maxHeight) && (w < maxWidth)) {
								nH = maxHeight;
								nW = parseInt((w * nH) / h, 10);
							} else if ((h < maxHeight) && (w > maxWidth)) {
								nW = maxWidth;
								nH = parseInt((h * nW) / w, 10);
							}
						}
						obj.css({"height":nH+"px", "width":nW+"px"}) // On modifie la largeur et la hauteur de l'image après chargement
						obj.removeClass('hide'); // On affiche l'image après le resize
						obj.parent().find('div.replace').addClass("hide"); // On cache l'image de chargement
					}
				}
				img.src = ImgUrl; // On lance le pré-chargement de l'image
		});
	};
	$.fn.resizing.defaults = {
	    maxHeight: 52,
	    maxWidth: 52,
	    replaceBy: "http://www.creetik.com/img/ajax-loader.gif",
	    debugMode:0
	}
}) (jQuery)
