﻿(function($) {
    $.fullsize = { version: '0.2.0' };
    $.fullsize.objs = '';
    $.fullsize.target = null;

    $.fullsize.mecentered = function(sender, animated) {
        var objects;
        if (typeof (sender) == 'boolean') {
            animated = sender;
            objects = $($.fullsize.objs);
        } else {
            objects = $(sender);
        }
        objects.each(function() {
            var obj = $(this);
            var w = obj.width();
            var h = obj.height();
            var tw = $($.fullsize.target).width();
            var th = $($.fullsize.target).height();
            if (w > 0 && h > 0) {
                var imageRatio = w / h;
                var targetRatio = tw / th;
                var wratio = tw / w;
                var hratio = th / h;
                var wdiff = hratio * w;
                var hdiff = wratio * h;

                if (typeof (animate) == 'undefined')
                    var animate = false;

                var newh = Math.ceil(hdiff > th ? hdiff : th);
                var neww = Math.ceil(hdiff > th ? tw : wdiff);
                var mleft = neww > tw ? '-' + ((neww - tw) / 2) + 'px' : '0px';
                var mtop = newh > th ? '-' + ((newh - th) / 2) + 'px' : '0px';

                if (animate)
                    obj.animate({ 'width': neww + 'px', 'height': newh + 'px', 'margin-top': mtop, 'margin-left': mleft }, { duration: 'fast', queue: false });
                else
                    obj.css({ 'width': neww + 'px', 'height': newh + 'px', 'margin-top': mtop, 'margin-left': mleft });
            }
        });
    };

    $.fullsize.me = function(sender, animated) {
        var obj = $(sender);
        var w = obj.width();
        var h = obj.height();
        var tw = $($.fullsize.target).width();
        var th = $($.fullsize.target).height();
        if (w > 0 && h > 0) {
            var imageRatio = w / h;
            var targetRatio = tw / th;
            var wratio = tw / w;
            var hratio = th / h;
            var wdiff = hratio * w;
            var hdiff = wratio * h;

            if (typeof (animate) == 'undefined')
                var animate = false;

            if (animate)
                obj.animate({ 'width': Math.ceil(hdiff > th ? tw : wdiff) + 'px', 'height': Math.ceil(hdiff > th ? hdiff : th) + 'px' }, { duration: 'fast', queue: false });
            else
                obj.css({ 'width': Math.ceil(hdiff > th ? tw : wdiff) + 'px', 'height': Math.ceil(hdiff > th ? hdiff : th) + 'px' });
        }
    };

    $.fullsize.resize = function(animate) {
        var objects = $($.fullsize.objs);
        if (objects.length && !objects.get(0).complete) {
            objects.get(0).onload = function() { $.fullsize.resize(false); };
        } else if (objects.length) {
            objects.each(function() {
                var obj = $(this);
                if (this.src != '' && this.complete) {
                    var w = obj.width();
                    var h = obj.height();
                    var tw = $($.fullsize.target).width();
                    var th = $($.fullsize.target).height();

                    if (w > 0 && h > 0) {
                        var imageRatio = w / h;
                        var targetRatio = tw / th;
                        var wratio = tw / w;
                        var hratio = th / h;
                        var wdiff = hratio * w;
                        var hdiff = wratio * h;

                        if (typeof (animate) == 'undefined')
                            var animate = false;

                        if (animate)
                            obj.animate({ 'width': Math.ceil(hdiff > th ? tw : wdiff) + 'px', 'height': Math.ceil(hdiff > th ? hdiff : th) + 'px' }, { duration: 'fast', queue: false });
                        else
                            obj.css({ 'width': Math.ceil(hdiff > th ? tw : wdiff) + 'px', 'height': Math.ceil(hdiff > th ? hdiff : th) + 'px' });

                    }
                }
            });
        }
    };

    $.fn.fullsize = function(args) {
        var opts = {
            'target': window,
            'autoResize': true,
            'windowResize': $.fullsize.resize,
            'animated': true
        };

        $.extend(opts, args);

        $.fullsize.target = opts.target;

        $.fullsize.objs = this.selector;

        if (opts.autoResize) {
            $.fullsize.resize(false);

            $(window).resize(function() {
                opts.windowResize(opts.animated);
            });
        }
    };
})(jQuery)
