
jQuery.fn.scaler = function(el,settings) {
    settings = jQuery.extend({marginWidth: 0}, settings);

   return this.each(function(){

	//initializing variables
	var $self = jQuery(this);

	var nibble = settings.nibble != null ? settings.nibble : 0;

	//get the dimensions using dimensions plugin
	var width = $self.innerWidth() - nibble;
	var height = $self.innerHeight() - nibble;
	var multiplier, new_width, new_height;

	$self.find(el).each(function () {
			var img_width = jQuery(this).width();
			var img_height = jQuery(this).height();
//jQuery(this).log("container_w = " + width + ", container_h = " + height + ", img_w = " + img_width + ", img_h = " + img_height);
			if (img_width > width || img_height > height)
			{
				if (img_width >= img_height) {
					if (width > 0) {
						jQuery(this).css("width", width);
						multiplier = width / img_width;
						new_height = Math.ceil(img_height * multiplier);

						if (new_height > height)
						{
							jQuery(this).css("width", "auto");
							jQuery(this).css("height", height);
						}
					}
				}
				else
				{
					if (height > 0) {
						jQuery(this).css("height", height);
						multiplier = height / img_height;
						new_width = Math.ceil(img_width * multiplier);
					}

					if (new_width > width)
					{
						jQuery(this).css("height", "auto");
						jQuery(this).css("width", width);
					}
				}
			}

			if (settings.makevisible) {
				jQuery(this).css("visibility", "visible");
			}

			if (settings.center) {
				jQuery(this).center();
			}
		});
   });
};
