<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements.  See the NOTICE file
  distributed with this work for additional information
  regarding copyright ownership.  The ASF licenses this file
  to you 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.
-->
<Module>
    <ModulePrefs title="Photo List" description="View Photos From An Album" height="400" width="650">
        <Require feature="embedded-experiences"></Require>
        <Require feature="open-views"></Require>
    </ModulePrefs>
    <Content type="html" view="embedded, default">
   <![CDATA[
        <style type="text/css">
        .photo {
            float: left;
            margin-left: 20px;
            margin-right: 20px;
            margin-bottom: 70px;
            maring-top: 20px;
            padding: 2px 2px 2px 2px;
            border: 1px solid black;
        }

        #wrapper{
            border: 1px solid black;
        }

        #header{
            font-size: 120%;
            padding: 10px 10px 10px 10px;
            color: #0F67A1;
        }

        .clear{
            clear: both;
        }
     </style>


     <script type="text/javascript">

         var currentSite;

         function initAlbum(){
             opensocial.data.getDataContext().registerListener('org.opensocial.ee.context', function(key){
                 createAlbumHTML(opensocial.data.getDataContext().getDataSet(key));
             });
         };
         var context;
         function createAlbumHTML(context){
             this.context=context;
             var photos = context.photoUrls;
             var result = "";
             for(var i = 0; i < photos.length; i++){
                 count = i+1;
                 result = result + '<li style="text-decoration: underline; color: blue; cursor: pointer" onclick="showPreviewPhoto(\''+i+'\',\''+context.eeGadget+'\')">Photo '+count+'</li></br>';
             }
             document.getElementById('header').innerHTML = context.albumName;
             document.getElementById("album").innerHTML = result;
         };

         gadgets.util.registerOnLoadHandler(initAlbum);

         function showPreviewPhoto(index, eeGadget) {

            var eeDataModel = {
                'gadget' : eeGadget,
                'context' : {
                  "albumName": context.albumName,
                  "photoUrls": [context.photoUrls[index]]
              }
            };

            // Test the use case of Url data model
            if (index == context.photoUrls.length-1) {
              eeDataModel = {
                'url' : context.photoUrls[index],
                'context' : {
                  "albumName": context.albumName,
                  "photoUrls": [context.photoUrls[index]]
                }
              };
            }
            var navigateCallback = function(site, metadata){currentSite = site; console.log("Nagivate callback");};
            var returnCallback = function(returnValue){console.log("Return Value: " + returnValue);};
            gadgets.views.openEmbeddedExperience(returnCallback, navigateCallback, eeDataModel, {'viewTarget' : 'preview'});
         };

         function closePreview(){
            if(currentSite != null){
                gadgets.views.close(currentSite);
            }
            return false;
         };
        </script>

     <div id="wrapper">
         <div id="header"></div>
         <div id="album"></div>
         <div class="clear"/>
         <a href="#" onclick="closePreview();">Close Preview</a>
     </div>
  ]]>
    </Content>
</Module>
