﻿var g_exMapCtl; // the control for show/hide, also stores some properties

// called when the user first clicks on a KG
// should only be called if the KG has at least one app that the user can execute
// but there are checks later on to make sure
function KGExecuteMapControl_ShowMenu(e, _mode, _strApps, _kgid, _authorID, _conceptName, _knowdeId, _url, _groupID)
{
  //debugger;
  if (!g_exMapCtl)
  {
    var controlId = KGExecuteMapControl_GetControlDivId();
    g_exMapCtl = document.getElementById(controlId);
  }
  
  if (g_exMapCtl)
  {
    // set some properties on the control so we can access them in the onclick methods
    g_exMapCtl.mode = _mode;
    g_exMapCtl.strApps = _strApps;
    g_exMapCtl.kgid = _kgid;
    g_exMapCtl.authorID = _authorID;
    g_exMapCtl.knowdeId = _knowdeId; // may be null, if so still set it here and we can get null back later
    g_exMapCtl.conceptName = _conceptName; // may be null, if so still set it here and we can get null back later
    g_exMapCtl.url = _url;
    g_exMapCtl.groupID = _groupID;
    
    // position and show the control
    //KGExecuteMapControl_PositionMenu(e);  //NB - there's a bug here at the moment when you try to go to a knowde via a url and it doesn't pass in the event.  But we're no longer showing this control so I've just commented out the call for the time being.
    //g_exMapCtl.style.display = 'block';
    // don't show the menu, directly execute it
    window.setTimeout('KGExecuteMapControl_Execute()', 5);
  }
  
  if(e != null)
    CancelBubble(e);
}

// position with cursor from event object passed in
function KGExecuteMapControl_PositionMenu(e)
{
  if (g_exMapCtl)
  {
    var x = e.clientX - 10;
    var y = e.clientY - 10;
      
    //make sure the control isn't off the page 
    var maxX = document.body.clientWidth - g_exMapCtl.clientWidth - 2;  
    maxX = Math.max(maxX, 0); // maxX shouldn't be less than zero
    x = Math.min(x, maxX); // x shouldn't be greater than maxX
    
    g_exMapCtl.style.left = x + 'px';
    g_exMapCtl.style.top = y + 'px';
  }
}

// hide the menu and clear its properties
// shouldn't need to clear it's properties because they're reset in show, but it can't hurt
function KGExecuteMapControl_HideMenu()
{
  if (g_exMapCtl)
  {
    g_exMapCtl.style.display = 'none';
    
    g_exMapCtl.mode = '';
    g_exMapCtl.strApps = '';
    g_exMapCtl.kgid = '';
    g_exMapCtl.authorID = '';
    g_exMapCtl.knowdeId = ''; 
    g_exMapCtl.conceptName = '';
    g_exMapCtl.groupID = '';
  }
}

var KGExecuteMapControl_HideMenuTimer;
function KGExecuteMapControl_MouseOut()
{
  KGExecuteMapControl_HideMenuTimer = setTimeout("KGExecuteMapControl_HideMenu()", 500);
}

function KGExecuteMapControl_MouseOver() // cancel the hide so the menu doesn't disappear
{
  clearTimeout(KGExecuteMapControl_HideMenuTimer);
}

// user chose to learn the KG, so load the map
function KGExecuteMapControl_Learn()
{
  //if (confirm('Do you want to view the hyperknowledge map?'))
  {
    if (g_exMapCtl)
      KGExecuteMapControl_LoadKG(true, g_exMapCtl.mode, g_exMapCtl.authorID, g_exMapCtl.kgid, g_exMapCtl.knowdeId, g_exMapCtl.conceptName, g_exMapCtl.groupID);
  }
  KGExecuteMapControl_HideMenu();
}

// user chose to execute the KG, set the ApplicationID session property (or choose one from multiple?) then load the KG
function KGExecuteMapControl_Execute()
{
  if (g_exMapCtl)
  {
    var strApps = g_exMapCtl.strApps;
    if (!strApps)
      KGExecuteMapControl_HandleNoApp();
    else 
    {
      if (strApps.indexOf('@@') == -1) // only one app
      {
        var app = KGExecuteMapControl_GetApp(strApps, g_exMapCtl.mode);
        if (app)
          KGExecuteMapControl_StartExecuteApp(app);
        else
          KGExecuteMapControl_HandleNoApp();
      }
      else // seems there are multiple apps
      {
        var arrStrApps = strApps.split('@@');
        var arrApps = new Array();
        
        var count = 0;
        for (var j in arrStrApps)
        {
          var app = KGExecuteMapControl_GetApp(arrStrApps[j], g_exMapCtl.mode);
          if (app)
          {
            arrApps[count] = app;
            count ++;
          }
        }
        
        if (count == 0) // double check count incase string was in the wrong format
          KGExecuteMapControl_HandleNoApp();
        else if (count == 1)
          KGExecuteMapControl_StartExecuteApp(arrApps[0]);
        else
          KGExecuteMapControl_HandleMultipleApps(arrApps);
      }
    }
  }
  KGExecuteMapControl_HideMenu();
}

// returns an object to represent the application from a string of "id|name"
// app.Id and app.Name
// returns null if there's a problem e.g. string in wrong format
function KGExecuteMapControl_GetApp(_strApp, _mode)
{
  var app = new Object();
  if (_strApp)
  {
    var posPipe = _strApp.indexOf('|');
    if (posPipe > -1)
    {
       app.Id = _strApp.substring(0,posPipe);
       app.Name = _strApp.substring(posPipe + 1);
    }else if(_mode != null && _mode == 'MyAlert')
    {
      app.Id = _strApp;
    }
  }
  
  if (app && app.Id)
    return app;
}

// somehow the ExecuteMapControl has shown without an appplication, so there's probably a bug
function KGExecuteMapControl_HandleNoApp()
{
  //if (confirm('No application available. Do you want to view the Knowledge Gene?'))
  //    just load the KG - the message doesn't make sense if you don't see the control
  {
    if (g_exMapCtl)
      KGExecuteMapControl_LoadKG(true, g_exMapCtl.mode, g_exMapCtl.authorID, g_exMapCtl.kgid, g_exMapCtl.knowdeId, g_exMapCtl.conceptName, g_exMapCtl.groupID);
  }
}
    
function KGExecuteMapControl_StartExecuteApp(_app)
{
  if (_app)
  {
    // set the AppID
    if (window.m_KGApplicationControl_appID != null)
      m_KGApplicationControl_appID = _app.Id;
    //if (window.m_KGApplicationControl_appID != null) globalAppId = m_KGApplicationControl_appID;
    SetSessionProperty('ApplicationID', _app.Id);
    //alert('Execute Application \r\nappId = ' + _app.Id + '\r\nappName = ' + _app.Name);
    //if (confirm('Do you want to view the hyperknowledge map?'))
    {
      if (g_exMapCtl)
      {
        if(g_exMapCtl.mode == 'MyAlert')
        {
          LoadApplicationControlAlert();
        }
        else
        {
          m_KGApplicationControl_appKGID = g_exMapCtl.kgid;
          SetSessionProperty('ApplicationKGID', g_exMapCtl.kgid);
          // Production site: replace -99999 with the actual DashboardAppID to boost performance by not loading the map
          if((m_KGApplicationControl_appID == -99999 && g_exMapCtl.mode == 'Category' && window.location.href.toLowerCase().indexOf('home.aspx?category=') != -1) || (window.location.href.toLowerCase().indexOf('apphome.aspx') != -1))
            LoadApplicationControl('true');
          else
            KGExecuteMapControl_LoadKG(false, g_exMapCtl.mode, g_exMapCtl.authorID, g_exMapCtl.kgid, g_exMapCtl.knowdeId, g_exMapCtl.conceptName, g_exMapCtl.groupID);
        }
      }
    }
  }
  else
    KGExecuteMapControl_HandleNoApp();
}

// this needs coding
function KGExecuteMapControl_HandleMultipleApps(_arrApps)
{
  var message = "There are multiple apps: ";
  
  var app;   
  for (var j in _arrApps)
  {
    app = _arrApps[j];
    if (app)
      message += '\r\nappId = ' + app.Id + ';  appName = ' + app.Name;
  }
  //alert(message);
  
  //if (confirm('Do you want to view the hyperknowledge map?'))
  {
    if (g_exMapCtl)
      KGExecuteMapControl_LoadKG(false, g_exMapCtl.mode, g_exMapCtl.authorID, g_exMapCtl.kgid, g_exMapCtl.knowdeId, g_exMapCtl.conceptName, g_exMapCtl.groupID);
  }
}
                
function KGExecuteMapControl_Highlight(_cell)
{
  if (_cell)
    _cell.style.background = '#DDDDDD';
}

function KGExecuteMapControl_UnHighlight(_cell)
{
  if (_cell)
    _cell.style.background = '';
}

// continue with the KG load code that used to be called directly from the KGClick
// allowText boolean = whether to allow call to ShowTextView - not for execute
// knowdeId and conceptName might be null
// authorId could be personId or userKey depending on mode
function KGExecuteMapControl_LoadKG(_allowText, _mode, _authorID, _kgid, _knowdeId, _conceptName, _groupID)
{
  switch (_mode)
  {
    case "MyHK":
    case "Category":
      KGTabbedWindowsControl_MyHK_KGSelected(null, _authorID, _kgid, false, _groupID);
      break;
    case "IndexedDocs":
      RelatedConcept = _conceptName; 
      KGTabbedWindowsControl_KGSelected(_knowdeId, _authorID, _kgid, false, false, _groupID);
      break;
    case "UOS": // User Object Summary Control = authored maps
      KGTabbedWindowsControl_MyHK_KGSelected(null, _authorID, _kgid, false, _groupID);
      break;
    case "MyKnowl": // MyKnowledgeControl = My Hyperknowledge on profile page
      KGTabbedWindowsControl_MyHK_KGSelected(null, _authorID, _kgid, false, _groupID);
      break;
    /*
    case "MyHK":
    case "Category":
    case "IndexedDocs":
      var link = document.getElementById(_kgid);
      if (link)
        location.href = link.href;
      break;
    // how to stop the text view from showing for execute?
    */
  }
}

function KGExecuteMapControl_LoadCurrentKG(_allowText, _authorID, _kgid)
{
  if(globalSplitterSetting == "split") // SplitView == true
  {
    // if the map isn't showing, show it
    var mapCollapsed = !globalMapPaneOpen; //if false or null then map is visible
    if (mapCollapsed) 
      DoExpandMapViewPane("split");
    if (_allowText)
      ShowTextView(null, _authorID, _kgid, false, true, true);
  }
  else
    SwitchPaneView('map');
}
