/*


	jQuery - WaitForImgLoad
	
	---------------------------------------------------------------------------------------------------------------------------------------------------

	Changes on version 2.1:
	
	This version introduces the ability to resize images down specifying dimensions({width:'50%'}) as percentages.








*/

(function($){

	$.fn.WaitForImgLoad = function(options){
		
		var o = $.extend({
			onload: function(){},
			onerror: function(){},
			onImgError: function(){},
			width: null,
			height: null,
			removeAttributes: null	
		},options);
		
		
		
		this.count = 0;
		this.overallCount = 0;
		
		var obj = this;
		
		function getPercentage(nFull,nPartial)
		{
			return (nPartial / nFull);	
		}
		
		function setDimensions()
		{
			
			if(!o.width && !o.height)return;//If there is no width or height relative dimensions
						
			var iWidth = this.width; 
			var iHeight = this.height;
			
			
			var thisWidth = (!o.width) ? iWidth : ((typeof o.width == 'function') ? o.width() : o.width);
			var thisHeight = (!o.height) ? iHeight : ((typeof o.height == 'function') ? o.height() : o.height);
			
			var isWidthPercentage = (typeof thisWidth == 'string' && thisWidth.indexOf('%') != -1);
			var isHeightPercentage = (typeof thisHeight == 'string' && thisHeight.indexOf('%') != -1);
			
			//Check to see if width is a percentage string
			if(isWidthPercentage)
			{
				var pWidth = parseInt(thisWidth.replace('%',''));	
				thisWidth = Math.round((pWidth * iWidth) / 100);	
			}
			
			//Check to see if height is a percentage string
			if(!isWidthPercentage && isHeightPercentage)
			{
				var pHeight = parseInt(thisHeight.replace('%',''));	
				thisHeight = Math.round((pHeight * iHeight) / 100);	
			}
						
			var p = null;//Percentage
			
			while((iWidth > thisWidth) || (iHeight > thisHeight))
			{
				if(iWidth > thisWidth)
				{
					p = getPercentage(iWidth,thisWidth);
					iWidth = iWidth * p;
					iHeight = iHeight * p;
				}
				
				if(iHeight > thisHeight)
				{
					p = getPercentage(iHeight,thisHeight);
					iWidth = iWidth * p;
					iHeight = iHeight * p;
				}
			}
						
			if(p)//If percentage has been set
			{
				var finalWidth = Math.round(iWidth); 
				var finalHeight = Math.round(iHeight);
				setWidthHeight.call(this,finalWidth,finalHeight);
				/*
				$(this)
					.css({
						width: finalWidth + 'px',
						height: finalHeight + 'px'	
					})
					.attr({
						width: finalWidth,
						height: finalHeight	
					});	
				*/	
			}	
		}
		
		function setWidthHeight(nWidth,nHeight)
		{
			$(this)
				.css({
					width: nWidth + 'px',
					height: nHeight + 'px'	
				})
				.attr({
					width: nWidth,
					height: nHeight	
				});	
		}
		
		return this.each(function(){//Loop through each image
			if(o.removeAttributes)
			{
				$(this).removeAttr('width').removeAttr('height');	
			}
			if(this.complete) {
				var $o = obj;
				$o.overallCount += 1;
				$o.count += 1;
				if(o.width || o.height)
				{
					setDimensions.call(this);
				} else {
					setWidthHeight.call(this,this.width,this.height);		
				}
				if($o.count == $o.length)o.onload.call($o);	
				if($o.overallCount == $o.length && $o.count != $o.length)o.onerror.call($o);	
			} else {
				$(this)
				.bind('load',{obj:obj},function($e){
					var $o = $e.data.obj;
					$o.overallCount += 1;
					$o.count += 1;
					if(o.width || o.height)
					{
						setDimensions.call(this);
					} else {
						setWidthHeight.call(this,this.width,this.height);		
					}
					if($o.count == $o.length)o.onload.call($o);	
					if($o.overallCount == $o.length && $o.count != $o.length)o.onerror.call($o);					
				})
				.bind('error',{obj:obj},function($e){
					var $o = $e.data.obj;
					$o.overallCount += 1;	
					o.onImgError.call(this);
					if($o.overallCount == $o.length)o.onerror.call($o);	
				}).bind('abort',{obj:obj},function($e){
					var $o = $e.data.obj;
					$o.overallCount += 1;	
					if($o.overallCount == $o.length)o.onerror.call($o);	
				}).attr('src',this.src);
			}
		});
	};
	
})(jQuery);
