/*
* imgscale, Automatically scales images to fit or fill their parent container.
* Note: The defined parent container of the image must have a defined height and width in CSS.
* By: Kelly Meath
* Website : http://imgscale.kjmeath.com
* Version: 1.0.0
* Updated: March 22nd, 2011
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
(function($) {
    $.fn.imagescale = function(params) {

        params = $.extend( {parent: false, scale: 'fill', center: true, fade: 0}, params);
        
        var _parentHeight, _parentWidth, _imgHeight, _imgWidth, _imgNewWidth, _imgNewHeight, _marginLeft, _marginTop;
        
        this.each( function() {
            var $img = $(this);
            var $parent = ( !params.parent ? $img.parent() : $img.parents( params.parent ) );
            $parent.css({opacity: 0, overflow: 'hidden'});
            if( $parent.length > 0 ) {
                $img.removeAttr('height').removeAttr('width');
                if (this.complete) {
                    _scaleImage( $img, $parent, false );
                } else {
                    $img.load( function() {
                        _scaleImage( $img, $parent, true );
                    });
                }
            }
        });
        function _scaleImage( $img, $parent, _loadedImg ) {
        
            _parentHeight = $parent.height();
            _parentWidth = $parent.width();
            _parentRatio = _parentHeight/_parentWidth;
            _imgHeight = $img.height();
            _imgWidth = $img.width();
            
            _imgRatio = _imgHeight/_imgWidth;

        
            _compareShapes();


            function _compareShapes() {
                if( _parentRatio >= _imgRatio )
                    _resizeImage( 'p' );
                else
                    _resizeImage( 'l' );
                }

            function _resizeImage( _scale ) {
                
                switch( _scale ) {
                    case 'p':
                      if( params.scale == 'fit' )
                        $img.attr( 'width', _parentWidth );
                      else
                        $img.attr( 'height', _parentHeight );
                        break;
                    case 'l':
                        if( params.scale == 'fit' )
                          $img.attr( 'height', _parentHeight );
                        else
                          $img.attr( 'width', _parentWidth );
                        break;
                }
                if( params.center )
                  _repositionImage();
                else
                  _showImage();
            }

            function _repositionImage() {
                
                _imgNewWidth = $img.width();
                _imgNewHeight = $img.height();

                if( _imgNewHeight > _parentHeight ) {
                    _marginTop = '-' + ( Math.floor( ( _imgNewHeight - _parentHeight ) / 2 ) ) + 'px';
                    $img.css( 'margin-top', _marginTop );
                }

                if( _imgNewWidth > _parentWidth ) {
                    _marginLeft = '-' + ( Math.floor( ( _imgNewWidth - _parentWidth ) / 2 ) ) + 'px';
                    $img.css( 'margin-left', _marginLeft );
                }
                _showImage();
            }
            
            function _showImage(){
              if( params.fade > 0 && _loadedImg )
                $parent.animate({opacity : 1}, params.fade);
              else
                $parent.css({'opacity':1});
            }
        }
    };
})(jQuery);
