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

     File:   Facilitor.asp

     =============== FACILITOR 5i MAIN ENTRY POINT =============
*/
%>
<!--#include file="../Shared/common.inc"-->
<!--#include file="../shared/iface.inc"-->
<!--#include file="fac_menu.inc" -->
<!--#include file="header.inc" -->
<!--#include file="../Shared/login.inc" -->
<!--#include file="../Shared/json2.js" -->

<%
  // Als we hier komen hebben we gegarandeerd een user_key > 0

  if (user.isGroupedUser() || Session("login_by_fallback"))
  {
    doLogoff();
    shared.simpel_page(L("lcl_no_auth"));
  }

  FCLTHeader.Requires({ plugins: ["jQuery"],
                        js: ["jquery-ui.js"]}); // css voor header.asp

  var noMenu = false;  // option: do not show a menu
  var noHeader = false;// option: do not show a header
  var noTabs = false;  // option: do not do tabs
  // Logging              option: for internal debugging purposes, bitcoded
  // IHateTabs            option: do not use our internal tabmanager
  // Jumpto               option: for deeplinking, uses an alternate body
  // Login                option: to impersonate a system user

  var dis = Request.QueryString("dis").Count > 0;  // is doorgeven voor het disablen van IkBenBekend in het inlogscherm

  // What browser/platform is used? Check moved to facilitor.inc
  if (Request.QueryString("noMenu").Count>0)    noMenu = true;
  if (Request.QueryString("noHeader").Count>0)  noHeader = true;
  if (Request.QueryString("IHateTabs").Count>0) noTabs = true;

  // URL param jumpto can be set like:
  // http://dit03/facilitorwork/default.asp?jumpto=visitors/register_input.asp
  if (Request.QueryString("Jumpto").Count>0) // Remember it
    Session("FirstPage")=''+Request.QueryString("Jumpto")

    if (   Session("FirstPage")==""
        || typeof(Session("FirstPage"))=="undefined" )
    {
        if (S("widgetportal") == 1) {
            firstPage = "appl/fac/fac_widgetspage.asp?wgrp=myfacilitor"
        } else {
            firstPage = "appl/fac/myfacilitor.asp";
        }
    }
    else // Deeplinking
    {
        firstPage = Session("FirstPage");
        Session.Contents.Remove("FirstPage");
    }

if (device.test(device.isTouch)) { %>
<html>
<% } else { %>
<html style='overflow:hidden'>
<% } %>
<head>
<%
    FCLTHeader.Generate()
    // If we think the user uses an unsupported browser, we warn the user.
    // Note: this check is informative only, the user could continue despite the issupported browser (like IE6)
    if (!device.test(device.isSupported) && S("checkBrowser")==1) {
%>
    <script type="text/javascript">
        alert('<%=safe.jsstring(L("lcl_invalid_browser") + "\n[" + Request.ServerVariables("HTTP_USER_AGENT") + "]")%>');
    </script>
<%
    }
    if (device.test(device.isTouch))
    {
        noTabs = true;
        noHeader = true;
        noMenu = true;
    }
%>
<script type="text/javascript">
    function RefreshFacilitor()
    {
      alert("<%=L("lcl_session_expired")%>");
      window.location.href="<%=rooturl%>/default.asp?fac_id=<%=customerId%>";
    }

    function myResize()
    {
        if ($("#menutabs").length)
        {
            // Bepaal de breedte van de individuele tabjes
            var ww = 0;
            $("div#menutabs ul.ui-tabs-nav li a").each(function () {
                    ww += $(this).outerWidth()
                });
            ww += 22;
            if (ww > $("#menutabs").width()) // Nooit versmallen
                $("#menutabs").width(ww);

            var divLeft = $("#menutabs").is(":visible")?$("#menutabs").outerWidth():0;
            $("#menudivider").css("left", divLeft + "px");
            $("#maintabs").css("left", divLeft + $("#menudivider").outerWidth() + "px");

            var newWidth = $(window).width() - divLeft - $("#menudivider").outerWidth() - 12;
            var marge = $("#maintabs").innerWidth() - $("#maintabs").width();
            $("#maintabs").width(newWidth + marge);
        }
        else
        {
            $("#maintabs").css("left", "0px");
            $("#maintabs").width("100%");
        }

        var divTop = $("#frheader").is(":visible")?$("#frheader").height():0;
        $("#topdivider").css("top", divTop + "px");
         $("#headtoggler").css("left", $(window).width() - $("#headtoggler").outerWidth() - 3 + "px");
        //$("#headtoggler").css("left", 5 + "px");

        var newTop = divTop + $("#topdivider").outerHeight();
        $("#maintabs,#menutabs,#menudivider").css("top", newTop);
        var newHeight = $(window).height() - divTop - $("#topdivider").outerHeight();
        $("#menudivider").height(newHeight);
        $("#menutoggler").css("top", newHeight - 5 - $("#menutoggler").outerHeight() + "px");
        $("#menutabs").height(newHeight - $("#menutabs").outerHeight() + $("#menutabs").height() - 30);
        $("#maintabs").height(newHeight - $("#menutabs").outerHeight() + $("#menutabs").height());

        var tabsHeaderHeight = ($("#menutabs>.ui-tabs-nav").outerHeight());
        var contentHeight = $("#menutabs").height()- tabsHeaderHeight;
        $("#menutabs .ui-layout-content").height(contentHeight).css("overflow","auto");

        FcltMgr._pageManager._timedResizeActiveTab();
    }

    function toggleMenu(elm)
    {
        $('#menutabs').toggle();
        $(elm).toggleClass('dividerclosed');
        window.myResize();
    }

   jQuery(document).ready(function() {
        FcltMgr.setData("expired", RefreshFacilitor);

        $('#menutabs').click(function () {
            $("#menufilter").focus();
        } );
        $("#menufilter").keyup(function(e) {
            if (e.which == 27) {
                $("#menufilter").val("");
            }
        });

<% if (!noTabs) { %>
        FcltMgr.HostTabs("maintabs");
        window.onbeforeunload = /*FcltMgr.*/confirmExit;
        window.onunload = /*FcltMgr.*/forcedExit;
        function confirmExit()
        {
            var anyHot = false;
            var i;
            for (i = FcltMgr._pageManager._$tabshost.find( ".ui-tabs-nav li" ).length-1; i > 0 && !anyHot; i--)
            {
                if (FcltMgr._pageManager._panelHot(i))
                    anyHot = true;
            }
            if (anyHot)
                return L("lcl_shared_closeandignore");
            else
                return;
        }

        function forcedExit()
        {
            var i;
            for (i = FcltMgr._pageManager._$tabshost.find( ".ui-tabs-nav li" ).length-1; i > 0; i--)
            {
                var hotwnd = FcltMgr._pageManager._panelHot(i);
                if (hotwnd && hotwnd.onCloseHot)
                    hotwnd.onCloseHot();
            }
        }
        $(window).resize(myResize);
        window.setTimeout("myResize()", 10);

<% }
   extra="";
   if (Session("logging")>0 && this.Oracle)
   {
       var extra = " {"+custpath.substr(custpath.length-4)+':'+Oracle.RealConnection.Properties("User Name")+'@'+Oracle.RealConnection.Properties("Data source") + "}"
       extra = extra.toUpperCase();
   }
%>
    window.parent.document.title = "<%=L("lcl_facilitor_ie_title")%><%=extra%>";

<% if (!noTabs) {%>
    FcltMgr.openDetail("<%=firstPage%>", L("lcl_body_portal"), { noclose: true });
<%
    if (user.options("autoopen"))
    {
        for (menu in user.options("autoopen"))
        {
            var pg = user.options("autoopen")[menu]; // TODO: minder lelijk de ../../ er af.
            if (pg.u.substr(0,6) == "../../")
                pg.u = pg.u.substr(6);
            if (pg.f)
                pg.u += (pg.u.indexOf("?")>0?"&":"?") + "fcltfilters=" + safe.url(pg.f);
            %>FcltMgr.openDetail("<%=safe.jsstring(pg.u)%>", "<%=safe.jsstring(pg.t)%>", { noactivate: (<%=pg.activate?0:1%>), showpin: true });<%
        }
   }
 } %>

    $("#menutabs").tabs();
    $("#menutabs").tabs('option', 'active', ($("#PersFAC").length && $("#ProfFAC").length)?1:0);
    <% if (!noTabs && S("menu_tab_toggles_portalmenu")==1) {
        var menuitems  = fillMenuArray(true); // portalOnly
        %>
     $("#menutabs").tabs({
                beforeActivate:
                    function ( event, ui )
                    {
                        if ($("#menufilter").val()) // reset filter bij tabwissel
                            $("#menufilter").val("").trigger("keyup");
                    },
                activate:
                    function(event, ui)
                    {
                        var filled = [ <%=menuitems[0]?1:0%>,<%=menuitems[1]?1:0%>,<%=menuitems[2]?1:0%> ];
                        var groep = { "PersFAC": 0,
                                      "ProfFAC": 1,
                                      "MgtFAC": 2
                                    }[ui.newPanel[0].id];
                        if (filled[groep] == 1)
                        {
                            FcltMgr.openDetail("appl/fac/fac_portal_menu.asp?groep=" + groep, { titel: ui.newTab.text(), reuse: true });
                        }
                    }
                });
    <% } %>

    $("li.mk").click(function()
        {
            $($(this).children()[0]).nextAll("ul").toggle();
            // Probeer het net geopende blok in beeld te brengen
            var top = $(this).position().top - $(this).closest("div").position().top;
            var bottom = top + $(this).height();

            var div = $(this).closest("div.ui-layout-content");
            if (bottom > div.height() + div.scrollTop())
                div.scrollTop(Math.min(top, bottom - div.height() + 5));
        });

     $("#menutabs").filterMenuByText("#menufilter");

   }); // jQuery.ready

<%  if (Session("logging")&2) { %>
    window.open("../../<%=Application(customerId + "_logfilename")%>.html", "logger<%=customerId%>");
    self.focus();
<% }

if (S("NotifyMessageInterval")>0)
{ %>
    function process_message(data)
    {
        if (data.msg)
            alert(data.msg);
        if (data.nextCheck > 0)
            window.setTimeout("showNotification()", data.nextCheck);
    }
    function showNotification()
    {
        $.getJSON("NotifyMessage.asp", process_message);
    }
    // Uitstellen opdat andere onderdelen van de voorpagina
    // een fractie sneller in beeld komen
    window.setTimeout("showNotification()", 1000);
<% } %>

function menu(evt, item, regel, menu_key)  // Eén menuitem
{
    FcltMgr.stopPropagation(evt);
    if (menu_key > 0)
    {
        item += item.indexOf("?") > -1?"&":"?";
        item += "from_menu_key=" + menu_key;
    }
    FcltMgr.openDetail(item, regel.textContent||regel.innerText);
}

</script>

</head>
<%
 if (!device.test(device.isTouch)) { %>
<body>
  <form name="u2" action="../shared/LogOff.asp" method="post" onSubmit="logOff();">
<% IFACE.FORM_END(); %>
  </form>
<% if (!noHeader) {  %>
  <div id="frheader">
    <% generateHeader(); %>
  </div>
  <div id="topdivider" class='ui-layout-resizer' onclick="$('#frheader').toggle();$(this).toggleClass('dividerclosed');window.myResize()">
    <div id="headtoggler" class='toggler' title='<%=L("lcl_toggler")%>'></div>
  </div>

<script>
    jQuery.fn.filterMenuByText = function(textbox, selectSingleMatch) {
      return this.each(function() {

        var menu = this;
        var lastActive = null;
        $(textbox).bind('change keyup', function() {
          if (!lastActive)
            lastActive = $("#menutabs").find("div.ui-tabs-panel:visible").attr("id");
          $(menu).find("li.mk,li.mklogo").show(); // Alle koppen zichtbaar
          $(menu).find("li.mk ul").show(); // Alle koppen open
          $(menu).find("div.filtermenukop").show(); // En vervangen door eigen filterkoppen
          $(menu).find("div.ui-tabs-panel").show(); // Alle panels tonen

          var search = $.trim($(this).val()).toLowerCase();
          if (!search)
          {
            $(menu).find("li.mk ul").hide(); // Alle koppen dicht
            $(menu).find("div.ui-tabs-panel").each(function () {
                if ($("li.mk ul", this).length <= <%=S("menu_collapse_threshold")%>)
                    $("li.mk ul", this).show() // Maar tabjes met weinig koppen zijn doorgaans default open
               });
            $(menu).find("li.mi").show();
            $(menu).find("div.filtermenukop").hide();
            $(menu).find("div.ui-tabs-panel").hide(); // Alle panels verbergen
            $("#" + lastActive).show() // maar de actieve weer terug
            lastActive = null;

            return;
          }

          // Nu het echte zoeken
          $(menu).find("li.mi").each(function(){
            var text = $(this).text().toLowerCase();
            var moretext = ($(this).prop('title')||"").toLowerCase();
            $(this).toggle(text.indexOf(search) > -1 || moretext.indexOf(search) > -1);
          });

          // Koppen hidden waar niets over is
          $(menu).find("li.mk,li.mklogo").each(function(){
            // mist inactieve tabs var cnt=$(this).find("li.mi:visible").length;
            var cnt = $(this).find("li.mi").filter(function() {
                  return $(this).css('display') !== 'none';
                }).length;
            $(this).toggle(cnt > 0);
          });
          return;
        });
      });
    };

</script>
<% }

if (S("PerformInterval")>0 && user.checkAutorisation("WEB_PERMON", true)) { %>
 <iframe id="bgPerformance"  src="../Fac/PerformanceTester.asp" style="display:none"><!--het frame voor performancemetingen--></iframe>
<% }
    if (!noMenu) {
    %><div id="menutabs" <%= S("menu_start_open")?"":" style='display:none'" %>>
        <%  generateMenu(1); %>
    </div>
      <div id="menudivider" class='ui-layout-resizer <%= S("menu_start_open")?"":"dividerclosed" %>' onclick="toggleMenu(this);">
        <div id="menutoggler" class='toggler' title='<%=L("lcl_toggler")%>'></div>
      </div><%
    }
%>    <div id="maintabs">

<% if (noTabs) { /* de onload is voor FireFox */ %>
    <iframe src="../../<%=firstPage%>" name="firstpage"
            style='min-height: 100%;'
            width="100%" marginwidth="0" marginheight="0" frameborder="0" scrolling="auto"
            onload="$(this).height(window.innerHeight-2)"></iframe>
<% } else { %>
        <span class="tabcloseall"
              title="<%=L("lcl_close_all_tabs")%>"
              onClick="FcltMgr.closeAll()">x</span>
        <span class="tabcloseall"
              title="<%=L("lcl_current_tab")%>"
              onClick="FcltMgr.saveTab()"><img alt='' src="../Pictures/pin.png"></span>
        <ul><!--hier komen de tabs--></ul>
<% } %>
    </div>

</body>
<% } // if (!isTouch)
else { //if (isTouch)

%>
<body class="touch">
  <div id="touchmenu">
  <% generateMenu(2); %>
  </div>
  <% generateHeaderFunctions({touch: true}); %>
  <script>
    $("#touchmenu").tabs();
    var n = $("#touchmenu ul.ui-tabs-nav li").length;
    var w = (96/n);
    $("#touchmenu ul.ui-tabs-nav li").width(w+'%');
   </script>
</body>
<% } // if (isTouch)
%>
</html>
