(function($){                              

    $.fn.extend({                           
    
        scale: function( arg1, arg2, arg3, arg4 )
        {
        
            return this.each( function()
            {

                var center = arg1;
                var stretch = arg2;
                var frame = arg4;
                var totalFrame = arg4 * 2;
                var padding = arg3 + totalFrame;
                var debug = false;

                var obj = $(this);
                
                obj.css('visibility','hidden');

                if( obj.attr('src') && !obj.attr('jquery_scale_orig-height') )
                {
                    
                    var date = new Date();
                    var cursrc = obj.attr("src");
                    var newsrc = cursrc;
                    if( cursrc.indexOf('?') != -1 )
                        newsrc = cursrc.substring( 0, cursrc.indexOf('?'));
                    newsrc = newsrc + "?" + date.getTime();
                    obj.attr( "src", newsrc );
                    this.onload = scale;
                    
                } else {
                    scale();
                }
                
                function scale()
                {

                    if( !obj.attr('jquery_scale_orig-height') ){
                        obj.attr('jquery_scale_orig-height', obj.height() );
                        obj.attr('jquery_scale_orig-width', obj.width() );
                        
                    } else {           
                        obj.height( parseInt( 
                            obj.attr('jquery_scale_orig-height') ) );
                        obj.width( parseInt( 
                            obj.attr('jquery_scale_orig-width') ) );
                        
                    }

                    if( obj.outerHeight(  ) > obj.parent().height() && 
                        obj.outerWidth(  ) <= obj.parent().width() ){

                        matchHeight();       
                    }
                    
                    else if( obj.outerWidth(  ) > obj.parent().width() && 
                             obj.outerHeight(  ) <= obj.parent().height() ){

                        matchWidth();    
                    }
                    
                    else if( obj.outerWidth(  ) < obj.parent().width() && 
                             obj.outerHeight(  ) < obj.parent().height() &&
                             stretch ){
                      
                        if( obj.parent().height()/obj.outerHeight(  ) <= 
                            obj.parent().width()/obj.outerWidth(  ) ){
                            
                            matchHeight();
                            
                        } else {
                            matchWidth();
                        }
                    
                    } else if( obj.outerWidth(  ) > obj.parent().width() && 
                               obj.outerHeight(  ) > obj.parent().height() ){
                               
                        if( obj.parent().height()/obj.outerHeight(  ) >
                            obj.parent().width()/obj.outerWidth(  ) ){
                            
                            matchWidth();
                            
                        } else {
                            matchHeight();
                        }                            

                    }

                    if( center )
                    {
                        obj.css( 'position', 'absolute' );
                        obj.css( 'margin-top', obj.parent().height()/2 - obj.outerHeight() / 2 );
                        obj.css( 'margin-left', obj.parent().width()/2 - obj.outerWidth() / 2 );
                    }

                    var image = obj.siblings('.image-frame');

                    if( image.length == 1 )
                    {
                        image.width( obj.width() + totalFrame );    
                        image.height( obj.height() + totalFrame );    
                        if( center )
                        {
                            image.css( 'position', 'absolute' );
                            image.css( 'margin-top', ( obj.parent().height()/2 - obj.outerHeight() / 2 ) - frame );
                            image.css( 'margin-left', ( obj.parent().width()/2 - obj.outerWidth() / 2 ) - frame );
                        }
                    }

                    this.onload = null;
                
                    obj.css('visibility','visible');

                }  
                
                function matchHeight()
                {
                    obj.width( obj.outerWidth(  ) * 
                        obj.parent().height()/ obj.outerHeight() - 
                        (obj.outerWidth() + padding - obj.width()));
                    obj.height( obj.parent().height() - 
                        (obj.outerHeight() + padding - obj.height()) );
                }
                
                function matchWidth()
                {
                    obj.height(  obj.outerHeight(  ) * 
                        obj.parent().width()/obj.outerWidth(  ) - 
                        (obj.outerHeight() + padding - obj.height())  );
                    obj.width( obj.parent().width() - 
                        (obj.outerWidth() + padding - obj.width()));
                }
                
            });     
        }           
    });             
})(jQuery);        

