/** resizer plugin
 * resize image to max available window height ( unless orginal height is smaller )
 */
(function($) {
    $.fn.resizer = function (options, callback) {
        config = $.extend({}, options);
        resizeImage(this);
        if ( typeof callback  == 'function'){
            callback.call(this);
        }
    }

// aspect ratio calculation from: http://andrew.hedges.name/experiments/aspect_ratio/
    function resizeImage ( $obj ) {
        // get screen width ( we need at list 30% for the right column ) so if the resized width exceeds this at another margin
        var org_height      = $($obj).attr('org_height');
        var org_width       = $($obj).attr('org_width');
        var resize_to       = $(window).height();
        var screen_width    = $(window).width ();
        var max_width       = ( ( screen_width / 3 ) * 2 ) - 150; // add 50 extra margin for 4:3 screens ( right column is wide enough for buttons )
        // extra margin set ?
        if ( config.margin && config.margin > 0 ) {
            resize_to -= config.margin;
        }

        if ( !config.force_window_height || config.force_window_height != 1 ) {
            // first calculate if we can resize by height.
            var calced_width    = org_width / org_height * resize_to;
            if ( calced_width > max_width ) {
                myconsole.log ( 'use max width' );
                // not possible so calculate by width
                var resize_to = org_height / org_width * max_width;
                var resize_to_width = max_width;
            } else {
                var resize_to_width = calced_width;
            }
        } else { // height was forced.. calculate width always on height.
            var resize_to_width = org_width / org_height * resize_to;
        }
        $($obj).attr('height', parseInt ( resize_to) );
        $($obj).attr('width', parseInt ( resize_to_width ) );
        // always set container if needed.
        if ( config.placeholderDiv ) {
            // commented by mediablox tegen de hapering op 7-3-2011
            $(config.placeholderDiv).width($($obj).width());
        }
    }
})(jQuery);

