
(function ($) {

    var defaults = {
        inset: [0, 0, 0, 0], // trbl
        crop: true
        /* 
        - true (scaled children will be smartly cropped so that no distortion occurs)   
        - false - child elements will be scaled both directions (distortion will occur)

        */

    };

    var pn = 'resizeto';

    $.fn[pn] = function () {

        var ret = this, all = this, cmd, options = {}, params = {}, a = arguments; if (a.length >= 1) { if (typeof (a[0]) == "string") { cmd = a[0]; } else { options = a[0]; } if (a.length >= 2) { params = a[1]; } }

        function dim(el, name, fallback) {

            var v = el.attr(name);

            if (!v) {

                // check for metadata

                if ($.fn.metadata) {
                    var cmd = el.metadata();
                    v = cmd[name];
                }

                // check for css

                if (!v) {
                    v = el.css(name);
                }

                if (!v || v == "") {
                    v = fallback;
                }
            }

            return v;

        };


        if (this.length) {
            this.each(function () {

                var self, trigger, d, p = params, o = {}, md = {}, el = $(this); if ($.fn.metadata) { md = el.metadata(); } if (!cmd) { d = el.data(pn); if (!d) { d = {}; el.data(pn, d); } $.extend(true, o, defaults, options, md[pn] || md || {}); d.options = o; } else { d = el.data(pn); if (d) { o = d.options; } else { return; } } self = function () { el[pn].apply(el, arguments); }; trigger = function (n, dt) { return el.trigger(jQuery.Event(pn + n), $.isArray(dt) ? dt : [dt]) !== false; };

                if (el.is("img")) {

                    // need to resize such the the image is kept vertically central, but the width is increased to the browser window width

                    if (o.width) {
                        var iw = o.width - o.inset[1] - o.inset[3];
                    }

                    if (o.height) {
                        var ih = o.height - o.inset[0] - o.inset[2];
                    }


                    // record proposed width and height
                    var pw = iw;
                    var ph = ih;

                    var x = false;
                    var y = false;

                    if (!d.factor) {
                        // find the proportion of initial height and width

                        var height = dim(el, "height", el.outerHeight());
                        var width = dim(el, "width", el.outerWidth());

                        if (width == 0) {
                            d.factor = 1;
                        } else {
                            d.factor = height / width;
                        }

                    }

                    if (o.width && o.height && o.crop) {

                        if (d.factor < 1) {

                            // width is larger (landscape), so crop the width to fit the window
                            ph = iw * d.factor;

                            // if the proposed height is less than the resize-height, we need to up the width, such that the height will fill the resize-height
                            if (ph < ih) {
                                ph = ih;
                                pw = ph / d.factor;
                            }

                        } else {

                            // height is larger (portrait), so crop the height to fit the window

                            pw = ih / d.factor;

                            // if the proposed width is less than the requested resize-width, we need to up the height, such that the width will fill the resize-width
                            if (pw < iw) {
                                pw = iw;
                                ph = pw * d.factor;
                            }

                        }

                        // calculate the x and y offsets
                        x = ((iw - pw) / 2) + o.inset[3];
                        y = ((ih - ph) / 2) + o.inset[0];


                    } else if (o.width && o.height) {
                        // no crop, simply distort
                        pw = iw;
                        ph = ih;
                    } else if (o.width) {
                        // width only, scale the height proportionally
                        pw = iw;
                        ph = d.factor * iw;
                    } else if (o.height) {
                        // height only, scale the width proportionally
                        ph = ih;

                        if (d.factor == 0) {
                            pw = 0;
                        } else {
                            pw = ih / d.factor;
                        }
                    }

                    if (x !== false) {
                        x = Math.ceil(x);
                        el.css({ left: x });
                    }

                    if (y !== false) {
                        y = Math.ceil(y);
                        el.css({ top: y });
                    }

                    pw = Math.ceil(pw);
                    ph = Math.ceil(ph);

                    el
            .css({ width: pw, height: ph });

                    d.left = x;
                    d.top = y;
                    d.height = ph;
                    d.width = pw;

                } // is("img")


            });
        }

        return ret;
    };

    $.fn[pn].defaults = defaults;

})(jQuery);
