(function ($) {
    $.fn.simplecenter = function (simplecenteroptions) {
        $.globalSimplecenterOptions = $.extend($.fn.simplecenter.defaults, simplecenteroptions);

        $.imageWrapper = $(this);
        $.imageToCenter = $.imageWrapper.find(":first-child").hide();
        $.isIOS = testIOS();

        if (($.imageToCenter.css("position") != "relative") && ($.imageToCenter.css("position") != "absolute")) {
            $.imageToCenter.css("position", "relative");
        }

        $.imageToCenter.imagesLoaded(function () {

            if ($.isIOS) {
                $.imageRatio = $.imageToCenter.width() / $.imageToCenter.height();
            }

            $(this).fadeIn($.globalSimplecenterOptions.initial_transition_interval);
            $.imageToCenter.reposition();

        });

        $(window).bind("resize", function () {
            $.imageWrapper.find("img").reposition();
        });
    };

    $.fn.simplecenter.defaults = {
        initial_transition_interval: 1000
    };
})(jQuery);

$.fn.reposition = function () {

    if ($.isIOS) {
        $(this).css({ "height": $(window).height() });
    }

    $(this).css({ "margin-left": -($(this).width() / 2) });
};

function testIOS() {
    var deviceAgent = navigator.userAgent.toLowerCase();
    var agentID = deviceAgent.match(/(iphone|ipod|ipad)/);
    if (agentID) {
        return true;
    }
    else {
        return false;
    }
}
