/**
 * @author Peter Meyer
 * @desc Fits an element to its parent with jQuery
 * @version 0.1
 * @example
 * $("element").fitToParent({
 *
 * 		expandToFit: false,
 *      maintainAspectRatio: true
 *
 * });
 * @obs With no arguments, the default is above
 * @license free
 * @param bool expandToFit, bool maintainAspectRatio
 *
 */
jQuery.fn.fitToParent = function(params) {
    ///	<summary>
    ///		Fits an element to its parent with jQuery.
    ///	</summary>
    ///	<param name="settings" type="Options">A set of key/value pairs that configure the fitToParent operation.</param>
    ///	<returns type="jQuery" />

    var options = { expandToFit: false, maintainAspectRatio: true };

    op = jQuery.extend(options, params);

    return this.each(function() {
        var $item = jQuery(this);

        var $parent = $item.parent();
        var width = $parent.width();
        var height = $parent.height();
        var ar = width / height;

        var _w = $item.width();
        var _h = $item.height();
        width -= ($item.outerWidth() - _w);
        height -= ($item.outerHeight() - _h);

        if (_w > width || _h > height || op.expandToFit) {
            if (op.maintainAspectRatio) {
                var _ar = _w / _h;
                var z = (ar > _ar ? height / _h : width / _w);
                $item.css({ height: _h * z, width: _w * z });
            } else {
                $item.css({ height: height, width: width });
            }
        }
    });

};

