﻿// returns the total amount of horizonal padding on the item
function getHPadding(item) {
    return parseInt($(item).css("paddingLeft").replace('px','')) + parseInt($(item).css("paddingRight").replace('px',''))
}

// returns the total amount of vertical padding on the item
function getVPadding(item) {
    return parseInt($(item).css("paddingTop").replace('px','')) + parseInt($(item).css("paddingBottom").replace('px',''))
}

function swapImage(gallery, path)
{

    var _gallery = $(gallery);
    // exit if we are processing
    if (!_gallery.data("processing")) {
        
        _gallery.data("processing", true);
        
        var loadingTimer = 1;
        
        // position the loading icon
        //_gallery.find(".loading").css({left: (_gallery.find(".large_image_outer").width() - _gallery.find(".loading").width())/2, top: (_gallery.find(".large_image_outer").height() - _gallery.find(".loading").height())/2});
        _gallery.find(".loading").css({left: 0, top: 0});
        
        // fade and resize to very small
        _gallery.find(".large_image").animate({opacity: 0}, 300, '', null).animate({width: '10px', height: '10px'}, 1, '', function() {
	    
            // show the loading icon - move it and fade it in
            loadingTimer = setTimeout(function(){clearTimeout(loadingTimer); _gallery.find(".loading").fadeIn(300, null);}, 1000);
            var imgPreloader = new Image();

            // once image is preloaded, resize image container
            imgPreloader.onload=function(){
                var newWidth = imgPreloader.width;
                var newHeight = imgPreloader.height;
                
                // clear the timer for the loader
                clearTimeout(loadingTimer);
                
                // fade out the loader anyway, even if not shown
                _gallery.find(".loading").stop().fadeOut(100, 
                    function() {
                    
                        // define the function for resizing the image
                        function showImageAndFinish () {
                        
                            // check if we need to resize
                            _gallery.find(".large_image").animate({width: newWidth, height: newHeight}, 1, '', null).animate({opacity: 1}, 300, '', function() {_gallery.data("processing", false);});;
                        }
                        
                        _gallery.find(".large_image").bind('load', function(){
                            
                            _gallery.find(".large_image").unbind('load');
                            if ((_gallery.find(".large_image_outer").width() != newWidth + getHPadding(_gallery.find(".large_image"))) || (_gallery.find(".large_image_outer").height() != newHeight + getVPadding(_gallery.find(".large_image")))) {
                                _gallery.find(".large_image_outer").animate({width: newWidth + getHPadding(_gallery.find(".large_image"))}, 300, '', null).animate({height: newHeight + getVPadding(_gallery.find(".large_image"))}, 300, '', showImageAndFinish);
                            } else {
                               showImageAndFinish();
                            }
                        });                            
                        
                        // swap image and start reshape if necessary
                        _gallery.find(".large_image").attr('src', imgPreloader.src);
                    });
	        }
        
            imgPreloader.src = path;
        });
    }
}
