/*
 * jQuery ImageSize Plugin 1.0
 * www.heydays.no
 * Copyright 2011, HEYDAYS
*/

(function($) {
    $.fn.imageResize = function(options) {
 
        var settings = $.extend({
            width: 0,
			height: 0,
			crop: true,
			center: true
        }, options);
 
        return this.each(function() {
			
			if (this.tagName.toLowerCase() != 'img') return $(this);
			
			var width = this.naturalWidth;
			var height = this.naturalHeight;
			
			if (!width || !height) {
				var img = document.createElement('img');
				img.src = this.src;
				width = img.width;
				height = img.height;
			}
			
			var newWidth = width;
			var newHeight = height;
			var diff = 1;
			var posX = 0;
			var posY = 0;
			var eq = (settings.crop)?(width/height<=settings.width/settings.height):(width/height>=settings.width/settings.height);
			
			//if ($(this).attr('src').indexOf('.png')==-1) {
				if (eq) {
					diff = height/width;
					newWidth = settings.width;
					newHeight = newWidth*diff;
				}else{
					diff = width/height;
					newHeight = settings.height;
					newWidth = newHeight*diff;
				}
			//}
			
			newWidth = Math.ceil(newWidth);
			newHeight = Math.ceil(newHeight);
			
			if (settings.center) {
				posX = Math.round((settings.width/2)-(newWidth/2));
				posY = Math.round((settings.height/2)-(newHeight/2));
			}
				
			return $(this).attr('width', newWidth).attr('height', newHeight).css({top:posY,left:posX, width:newWidth, height:newHeight});
			
        });
    }
})(jQuery);

///////////////////
