﻿// KnowledgeGeneListControl JScript File

var globalKGListTimer;
var tempKGListSelectedItem;
var globalKGListSelectedItem;
var globalMyKnowledgeGridClientID;
var m_MyKnowledgeRemoveKGMessageBox = null;
var CurrentlyShowingMyKnowledgeMessageBox;

function KGMyKnowledgeControl_Grid_OnInit(_knowledgeGenesGridClientID)
{
 globalMyKnowledgeGridClientID = _knowledgeGenesGridClientID;
}

function KGMyKnowledgeControl_CallbackDone(_args, _context)
{
  // Check for a session time out before completing the callback
  if (CheckSessionTimeout())
    return;
  
  CloseMyKnowledgeMessageBox();
  
  // check for exceptions
  if(_args.length == 0 || _args.indexOf("System exception caught") == 0) // this might be too specific and miss some exceptions, but need to avoid showing message box if e.g. a KG happens to have 'exception' in its name or description
  {
    if (_args.indexOf('deadlocked on lock resources with another process') > -1)
      alert("Sorry, our servers are too busy at the moment\n\nPlease try again later");
    else
      alert("AJAX Failure:\n\n" + args);
    return;
  }
  
  // get the parameters
  var argsSplit = _args.split('[@|@]');
  var json = argsSplit[0];
  var htmlContent = argsSplit[1];
  
  var retMsg = eval( '(' + json + ')' );
  var holderId = retMsg.HolderId;
  
  var holder = document.getElementById(holderId);
  if (!holder)
  {
    KGMyKnowledgeControl_CallbackError('The holder doesn\'t exist ' + holderId);
    return;
  }
  
  // fill the control
  SetOuterHtml(holder, htmlContent);
  
  if (retMsg.Kgid == Active_KGID)
  {
    // update the button toolbar
    switchSubscribeButton(false);
  }
}

function KGMyKnowledgeControl_Grid_OnRemoveSelect(_kgid, _parentControlDivId, _callbackTriggerName, e)
{
  CancelBubble(e);
  KGMyKnowledgeControl_DislayRemoveKGMessage(_parentControlDivId, e);
  var args='Action=unsub|kgid='+ _kgid;
  eval(_callbackTriggerName + "('" + args + "')");
}

function KGMyKnowledgeControl_CallbackTrigger_Wrapped(_args, _getPageInfoName, _cbReference)
{
  // pass back the current page numbers
  // alternative could be to save it in the session, but could lead to a lot of bugs if it isn't cleared properly
  var pageInfo = eval(_getPageInfoName + "()");
  _args += '|curPage=' + pageInfo.CurPage;
  
  // this is only required when removing a kg
  //    if it's the only one on the last page, we'll need to decrease the current page (done at the server)
  if (_args.indexOf("Action=unsub") > -1 && pageInfo.CurPage > 1 && pageInfo.IsShowingSingleItem())
    _args += '|pageBack=true';
  
  CallbackTrigger_Wrapped(_args, _cbReference);
}

function KGMyKnowledgeControl_CallbackError(args, context)
{
  // Here we are dealing with authentication timeout before callback, 
  // the message from the server is 'elogin' signify an error with 
  // authentication. The response will be trimmed to form the args 
  // 'login' here..... 
   
  if(args == 'login')
  {
    window.location='home.aspx';
  }
  else
  {
    var alertMessage = "The KGMyKnowledgeControl encountered an error. \n\n" + args;
    alert(alertMessage);
  }
}

function KGMyKnowledgeControl_DislayRemoveKGMessage(_parentControlDivId, e)
{
  m_MyKnowledgeRemoveKGMessageBox = document.getElementById("MyKnowledgeRemoveKGMessageBox");
  
  if(m_MyKnowledgeRemoveKGMessageBox == null)
  {
    m_MyKnowledgeRemoveKGMessageBox = document.createElement("div");
    m_MyKnowledgeRemoveKGMessageBox.setAttribute("id", "MyKnowledgeRemoveKGMessageBox");
    m_MyKnowledgeRemoveKGMessageBox.style.display = "none";
    m_MyKnowledgeRemoveKGMessageBox.style.width = "420px"
    m_MyKnowledgeRemoveKGMessageBox.style.height = "100px";
    m_MyKnowledgeRemoveKGMessageBox.style.position = "absolute";
    m_MyKnowledgeRemoveKGMessageBox.style.zIndex = TopZIndex();
    
    m_MyKnowledgeRemoveKGMessageBox.innerHTML= "<div style=\"position:absolute;width:420px;height:100px;border:gray solid 1pt;background:white\"><table width=\"100%\"><tr><td width = \"15%\" align=\"right\" style=\"font-size: xx-small\"><br/><br/><img src=\"Images/loadingbar.gif\" /></td><td width = \"75%\" align=\"left\" style=\"font-size: xx-small\"><br/><br/>Removing Knowledge Gene<sup>&#174;</sup> from My Knowledge Genes....<br/>Note, this Knowledge Gene<sup>&#174;</sup> map will still be available in the original author's My Profile page.</td><tr><td width = \"10%\" align=\"right\" style=\"font-size: xx-small\"><br/><br/></td></tr></table></div>";

    //m_MyKnowledgeRemoveKGMessageBox.innerHTML = "<div style='position:absolute;width:420px;height:120px;border:gray solid 1pt;background:white'><table border='0' cellpadding='5' cellspacing='0'><tr><td colspan='2' bgcolor='#BFD4F3' style='color:black;font-size:12pt'>Removing Knowledge Gene from My Knowledge</td></tr><tr><td rowspan='2' bgcolor='#ffffff' valign='top'><img src='images/recycle.png' height='50' width='50'/></td><td bgcolor='#ffffff'>Please wait...<br />Note, this Knowledge Gene will still be available in the original author's My Profile page.</td></tr></table></div>";
    
    var parentControlDiv = document.getElementById(_parentControlDivId);
    InsertAdjacentElement(parentControlDiv,"afterBegin", m_MyKnowledgeRemoveKGMessageBox);
  }
  else
  {
    m_MyKnowledgeRemoveKGMessageBox.style.zIndex = TopZIndex(); //Show on top of other items
  }
  
  PositionMyKnowledgeMessageBox(_parentControlDivId, e);
}

function TopZIndex()
//Find highest z index in current document and returns this value + 1 (so that
//the control using this value will appear on top of everything currently in  
//the page, but will appear behind everything called subsequently).  JPC.
//(inspired by http://domscripting.com/presentations/atmedia2006/slides/, slide 12)
{
  var maxZindex = 0;
  var elems = document.getElementsByTagName("div");
  
  for (var ti=0; ti<elems.length; ti++)
  {
    if (elems[ti].style.zIndex > maxZindex)
    {
       maxZindex = parseInt(elems[ti].style.zIndex);
    }
  }

  return maxZindex + 1;
}


/////////////////////
// Positions the suppled message box in the centre of the map view
function PositionMyKnowledgeMessageBox(_parentControlDivId, e)
{
  var parentControl = document.getElementById(_parentControlDivId); 
  
  //Displays messagebox in middle of visible screen.  JPC.
  e = e || window.event;
  var x = e.x || e.pageX;
  var y = e.y || e.pageY;
  m_MyKnowledgeRemoveKGMessageBox.style.top = y + 20;     
  m_MyKnowledgeRemoveKGMessageBox.style.left = x - 15; // - (m_MyKnowledgeRemoveKGMessageBox.style.width / 2);
  m_MyKnowledgeRemoveKGMessageBox.style.display = "block";
  m_MyKnowledgeRemoveKGMessageBox.style.zIndex = TopZIndex();
  m_MyKnowledgeRemoveKGMessageBox.style.visibility = "visible";
  CurrentlyShowingMyKnowledgeMessageBox = true;
}

//////////////////
// Closes the message box
function CloseMyKnowledgeMessageBox()
{
  if(m_MyKnowledgeRemoveKGMessageBox != null)
  {
    m_MyKnowledgeRemoveKGMessageBox.style.visibility = "hidden";
    m_MyKnowledgeRemoveKGMessageBox.style.display = "none";
    CurrentlyShowingMyKnowledgeMessageBox = false;
  }
}