{% comment %}
<!--
  Copyright (C) 2012 University of Dundee & Open Microscopy Environment.
  All rights reserved.

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU Affero General Public License as
  published by the Free Software Foundation, either version 3 of the
  License, or (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU Affero General Public License for more details.

  You should have received a copy of the GNU Affero General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
-->
{% endcomment %}

<script>

$(document).ready(function() {

    $("#right_panel").bind('resize', function(event) {

        // This behaviour is also in the metatdata_preview.html itself
        var vpWidth = $(this).width() - 50;
        // Use a 1:1 aspect ratio with the width.  Keep the viewport from
        // taking up more than 75% of the available height.
        var vpHeight = Math.max(
            300,
            Math.min(vpWidth, ($("#preview_tab").height() - 100) * 0.75)
        );

        if (OME.preview_viewport) {
            // Need to set a few sizes, then call viewport.refresh()
            $("#viewport").css({'width': vpWidth + 'px', 'height': vpHeight + 'px'});
            $("#viewport-vp").css({'width': vpWidth + 'px'});
            $("#viewport-top").css({'height': vpHeight + 'px'});
            OME.preview_viewport.refresh(true);
        }
    });

    $("#preview_tab").omeroweb_right_plugin({
        plugin_index:{{ forloop.counter }},
        load_plugin_content: function(selected, obj_dtype, obj_id) {    // Url based on selected object(s)
            var url;
            var well_index = selected[0]["index"] || 0;
            if (selected[0]["shareId"]) {
                url = '{% url 'webindex' %}metadata_preview/'+obj_dtype+'/'+obj_id+'/'+selected[0]["shareId"] + '/';
            } else {
                url = '{% url 'webindex' %}metadata_preview/'+obj_dtype+'/'+obj_id + '/';
            }
            if (well_index > 0) {
                url = url + '?index='+well_index;
            }
            //$this.html("<h1 style='padding: 20px 15px'>Loading...</h1>");
            $(this).load(url);
        },
        supported_obj_types: ['image', 'well']
    });

});

</script>