<%@ language = "JavaScript" %>
<% /*
     $Revision$
     $Id$

     File:    fac/fac_widgetspage.asp
              Container om widgets in een pagina te tonen
     Context:
*/ %>
<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/iface.inc"-->
<!-- #include file="../fac/fac_nieuws_forum.inc" -->
<!-- #include file="fac.inc" -->
<%
FCLTHeader.Requires({ plugins: ["jQuery"], js: ["jQuery-ui.js"]
                    });


var wdg_groep = getQParam("wgrp");
var old_mode = getQParamInt("old", 0) == 1;
// Indien niet expliciet oude modus, dan naar de moderne widget-pagina
if (!old_mode)
{
    var sql = "SELECT fac_widget_page_key"
            + "  FROM fac_widget_page"
            + " WHERE fac_widget_page_name = " + safe.quoted_sql(wdg_groep);
    var oRs = Oracle.Execute(sql);
    if (oRs.EoF) // Omdat je dit ook per ongeluk kan gebeuren, knallen we er hier niet met een AiAi uit maar met een aanwijzing
    {
      oRs.Close();
      abort_with_warning(L("fac_widget_page_notfound"));
    }
    var wgt_page_key = oRs("fac_widget_page_key").Value;
    oRs.Close();
    var url = "fac_widget_page.asp?wgt_page_key=" + wgt_page_key + String(Request.ServerVariables("QUERY_STRING"))
    Response.Redirect(url);
}

function createWidget(p_divid, p_title, p_height, p_width, p_url, p_refreshtime){
  // more=1 adds an extra container with handles to limit or expand the view
  //  var more = p_url.indexOf("more=1") > -1;
  var more = true;
  var safe_divid = p_divid.replace(/[^_a-zA-Z0-9\-]/g, ""); // Anders moeten we wel heel vaak safe.functies aanroepen
  if (more)
  {
%>
  <div class="widgetcontainer" style="width:<%=p_width%>%">
    <div class="widgetx" id="<%=safe_divid%>" style="width:100%;<%=(p_height ? 'height:' + p_height + 'px;' : '')%>">
<%
  }
  else
  {
%>
    <div class="widgetx" id="<%=safe_divid%>" style="width:<%=p_width%>%;height:<%=p_height%>px;float:left">
<%
  }
%>
       <div class="widgettitlex"><%=p_title%></div>
       <iframe src="<%=safe.htmlattr(p_url)%>&title_locked=1" id="<%=safe_divid%>frame" name="<%=safe_divid%>frame" refresher="<%=p_refreshtime%>"
           height="<%=(p_height - (p_title ? 20 : 0))%>px" width="100%" marginwidth="0" marginheight="0" frameborder="0" scrolling="no"
           onload="<%=(!p_height?'FcltMgr.iframeLoaded(this);':'') + 'iframeIsLoaded(\'iframe#' + safe_divid + 'frame\');' %>"
           onchange="<%=(more?safe_divid+'ChangeEvent();':'')%>"
           >
      </iframe>
   </div>
<%
  if (more)
  {
%>
  <div id="<%=safe_divid%>carddesc" class="fcltcard carddesc disable-sort-item" style="display:block;width:100%;">
        <table class="fcltcardtab" width="100%">
            <tbody>
                <tr>
                    <td width="100%">
                        <center><span aria-hidden="true"><%=I("fa-sort-down")%></span></center>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
    <div id="<%=safe_divid%>cardasc" class="fcltcard cardasc disable-sort-item" style="display:none;width:100%;">
        <table class="fcltcardtab" width="100%">
            <tbody>
                <tr>
                    <td width="100%">
                        <center><span aria-hidden="true"><%=I("fa-sort-up") %></span></center>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
    <script>
      function clickDesc(event) {
        // set container height to auto, hide carat down and show carat up
        $("#<%=safe_divid%>").css("height", "auto");
        $("#<%=safe_divid%>carddesc").css("display", "none");
        $("#<%=safe_divid%>cardasc").css("display", "block");
      }
      function clickAsc(event) {
        // set container height to initial height, hide carat down and show carat up
        $("#<%=safe_divid%>").css("height", "<%=p_height%>px");
        $("#<%=safe_divid%>cardasc").css("display", "none");
        $("#<%=safe_divid%>carddesc").css("display", "block");
      }
      function <%=safe_divid%>ChangeEvent(event) {
        // set div to initial height
        $("#<%=safe_divid%>").css("height", "<%=p_height%>px");
        // set explicit height of frame according to content
        $("#<%=safe_divid%>frame").css("height", $("body", $("#<%=safe_divid%>frame").contents()).height());
        // hide carats when the content fits within the div's initial height
        if ($("#<%=safe_divid%>").height() > $("#<%=safe_divid%>frame").height())
        {
          $("#<%=safe_divid%>carddesc, #<%=safe_divid%>cardasc").css("display", "none");
        }
        else
        {
          $("#<%=safe_divid%>carddesc").css("display", "block");
          $("#<%=safe_divid%>cardasc").css("display", "none");
        }
      }
      $("#<%=safe_divid%>carddesc td").on("click", clickDesc);
      $("#<%=safe_divid%>cardasc td").on("click", clickAsc);
      // carats default hidden
      $("#<%=safe_divid%>carddesc, #<%=safe_divid%>cardasc").css("display", "none");
    </script>
  </div>
<%
  }
%>
   <%if (p_refreshtime != -1 && p_refreshtime >= 60){
   %><script type='text/javascript'>
         setReloadTimer("<%=safe_divid%>frame", "<%=safe.jsstring(p_url)%>", <%=p_refreshtime%>);
     </script><%
   }
}
%>
   <html>
   <head>
<%
   FCLTHeader.Generate();
%>
   <script type='text/javascript'>
        // FSN#22791 refresh als we in een inactief tab zitten is overbodig
        //           en (jqPlot) geeft errors
        var pendingReload = [];
        function fcltontabshow()
        {
            var t;
            for (t in pendingReload)
                pendingReload[t]();
            pendingReload = [];

            $("iframe").each(function ()
                {
                    if (this.getAttribute("refresher") > 0 && this.getAttribute("refresher") < 60)
                    {
                        this.src = this.src ;
                    }
                });
        }

        function doReload(pObject, pUrl, pTime) {
           if (FcltMgr.inInvisibleTab())
               pendingReload.push(function () { doReload(pObject, pUrl, pTime); });
           else
           {
               document.getElementById(pObject).src=pUrl;
               setReloadTimer(pObject, pUrl, pTime);
           }
        };
        function setReloadTimer(pObject, pUrl, pTime) {
          if ($('li[fcltclick^=doSubmit]', $("iframe:first").contents()).length > 0)
          {
              setTimeout('reloadFrameSelections("'+pObject+'", "'+pUrl+'", '+pTime+');', pTime*1000);
          }
          else
          {
              setTimeout('doReload("'+pObject+'", "'+pUrl+'", '+pTime+');', pTime*1000);
          }
        }

        function reloadFrameSelections(pObject, pUrl, pTime) {
          $('li[fcltclick^=doSubmit]', $("iframe:first").contents()).click();
          setReloadTimer(pObject, pUrl, pTime);
        }

         // start frame selections
         function startFrameSelections(event) {
           var $searchblock = $("table[id^=searchblock] input:visible, table[id^=searchblock] select:visible", $("iframe:first").contents());
           $("iframe:not(:first)").each(
              function(iframe_index, frame_elem)
              {
                var $frame_searchblock = $("table[id^=searchblock]", $(frame_elem).contents());
                $searchblock.each(
                  function(index, field_elem)
                  {
                    $("#" + $(field_elem).attr("id"), $frame_searchblock).remove();                          // remove old searchfield
                    $("table[id^=searchblock]", $(frame_elem).contents()).append($(field_elem).clone(true)); // put searchfield in frame
                    $("#" + $(field_elem).attr("id"), $frame_searchblock).val($(field_elem).val());          // transfer search values to frame
                  }
                );
                $('li[fcltclick^=doSubmit]', $(frame_elem).contents()).click();
              }
           );
         }

        function hideSearchDiv() {
            $("div.container-fluid, nav.search-page-header", $(this).contents()).hide();
        }
        function refreshChangeTrigger() {
          $("iframe:not(:first)").each(hideSearchDiv);
          $("li[fcltclick^=doSubmit]", $("iframe:first").contents()).off().on("click", startFrameSelections);
        }
        var framesLoaded = [];
        var framesPresent = [];
        function iframeIsLoaded(selector)
        {
          framesLoaded.push($(selector));
          // all frames are loaded
          if (framesLoaded.length == framesPresent.length)
          {
            refreshChangeTrigger();
          }
        }
   </script>
  </head>
  <body class="mainpage">
<% if (S("widgetportal") == 1 && getQParamInt("from_menu_key", -1) == -1)
       injectNews();

   if (user_key != -1) {

    var sql =   "SELECT COALESCE(fac_widget_id, 'widget_' || to_char(fac_widget_key)) fac_widget_id,"
              + lcl.xsqla('fac_widget_title', 'fac_widget_key') + ","
              + "       fac_widget_url,"
              + "       fac_widget_height fac_widget_height,"
              + "       fac_widget_width fac_widget_width,"
              + "       fac_widget_refreshtime"
              + "  FROM fac_widget"
              + " WHERE fac_widget_groep = " + safe.quoted_sql(wdg_groep)
              + "   AND (fac_functie_key IS NULL"
              + "     OR fac_functie_key IN"
              + "             (SELECT fac_functie_key FROM fac_v_webgebruiker"
              + "               WHERE prs_perslid_key = " + user_key + "))"
              + " ORDER BY fac_widget_volgnr";
    oRs = Oracle.Execute(sql);
    var first = true;
    var frames = [];
    while (!oRs.Eof)
    {
        l_divid = oRs("fac_widget_id").Value;
        l_url = oRs("fac_widget_url").Value;
        l_url += (l_url.indexOf("?") > 0? "&" : "?") + "keepfilters=1";
        l_title = fac.localstring(oRs("fac_widget_title").value||"");
        l_height = oRs("fac_widget_height").Value;
        l_width = oRs("fac_widget_width").Value||100;
        l_refreshtime = oRs("fac_widget_refreshtime").Value||-1;

        l_url += (l_url.indexOf("?")>0?"&":"?")+"widget_height=" + l_height;
        createWidget(l_divid, l_title, l_height, l_width, l_url, l_refreshtime);
        frames.push('"iframe#' + l_divid + 'frame"');
        oRs.MoveNext();
    }
    oRs.Close();
}
%>
</body>
<script>
  framesPresent = [ <%=frames.join(",")%> ];
</script>


<% ASPPAGE_END(); %>
