﻿(function($) {

    //object which contains all methods in relation to class
    var oldBg = null
    var animating = true
    var methods = {
        resizeImg: function(imgNames, height, width, animation,pause,timing,events) {
        img = imgNames;
            if (oldBg != null) {
                if (oldBg[0].src == img[0].src) {
                    animating = false
                } else {
                   if (events != 'resize') { animating = true }
                   else { animating = false }
                }
            }
            var windowDimension = []
            var imageDimension = []
            var finalRatio = []
            var finalDiff = []
            if (animation == null || oldBg == null) {
                img.addClass('hide');
                $('#pagebg').prepend(img)
                oldBg = $(img)
                $(img).delay(300).fadeIn(timing)
            }
            else if (animation == 'fade' && animating == true) {
                if (oldBg != null) {
                    $('#pagebg').prepend(img)
                    oldBg.fadeOut(timing)
                    $(img).fadeIn(timing, function() { oldBg = $(img) })
                }
            } else if (animation == 'slide' && animating == true) {
                if (oldBg != null) {
                    $('#pagebg').prepend(img)
                    var start = $(window).width()
                    start = '-' + start + 'px'
                    $(img).css({ 'left': start})
                    oldBg.delay(pause).animate({ 'left': $(window).width() }, timing, function() { $(this).remove() })
                    $(img).delay(pause).animate({ 'left': '0px' }, timing, function() { oldBg = $(img) })
                }
            }
            windowDimension.push($(window).width(), $(window).height())
            if (windowDimension[0] > width) {
                $(img).css({ 'width': windowDimension[0] })
            } else {
                $(img).css({ 'width': width })
            }
            if (windowDimension[1] > height) {
                $(img).css({ 'height': windowDimension[1] + 'px' })
            } else {
                $(img).css({ 'height': height })
            }
        }, //end resize
        bgOut: function() {
        oldBg.animate({ 'left': $(window).width() }, timing, function() { $(this).remove() })
        }
}//end methods

        $.fn.imgLoader = function(method) {
            //method calling logic
            if (methods[method]) {
                return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
            } else if (typeof method === 'object' || !method) {
                return methods.init.apply(this, arguments);
            } else {
                $.error('Method ' + method + ' does not exist');
            }
        }
    })(jQuery)
