// Functions for preloading swapping images.
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

// Preload images used in tree
MM_preloadImages(AppPath+"/Img/1.gif", AppPath+"/Img/Tree/I.gif", AppPath+"/Img/Tree/T.gif",
  AppPath+"/Img/Tree/L.gif", AppPath+"/Img/Tree/Plus.gif", AppPath+"/Img/Tree/LPlus.gif",
  AppPath+"/Img/Tree/Minus.gif", AppPath+"/Img/Tree/LMinus.gif",
  AppPath+"/Img/Icons/Book.gif", AppPath+"/Img/Icons/Article.gif");
  
//-------------------------
function GetTocFrame()
{
  return top.document.getElementById("tocFrame");
}
function GetContentFrame()
{
  return top.document.getElementById("contentFrame");
}
function GetSearchFrame()
{
  return top.document.getElementById("searchFrame");
}
function GetIndexFrame()
{
  return top.document.getElementById("indexFrame");
}
//-------------------------
var selectedObjectId = null;
function Tree_OnSelect(objectId, allowed)
{
  nodeIdToActivate = null;
  if (objectId=="")
    return;
    
  selectedObjectId = objectId;
  SetContentUrl(selectedObjectId);
}
function RefreshTree(nodeIdToActivate)
{
  var tocWindow = GetTocFrame().contentWindow;
  if (nodeIdToActivate)
    tocWindow.Tree_Refresh("-1", nodeIdToActivate, true);
  else
    tocWindow.Tree_Refresh();
  var indexWindow = GetIndexFrame().contentWindow;
  debugger;
  indexWindow.Index_Refresh();
}
function OnSync()
{
  window.location.href = AppPath+"/Default.aspx/"+GetContentUrl();
}

function OnPrint(e)
{
  var a = document.getElementById("aPrint");
  var frame = GetContentFrame().contentWindow;
  try {
    var newLoc = frame.location.pathname;
    newLoc += (newLoc.indexOf('?')!=-1) ? '&' : '?';
    a.href = newLoc + 'print=true' + frame.location.hash;
  }
  catch(e) {}
}

function OnTabActivate(strTabStripName, strTabName)
{
  var divToc = document.getElementById(TocTabName+"_div");
  var divSearch = document.getElementById(SearchTabName+"_div");
  var divIndex = document.getElementById(IndexTabName+"_div");
  if (!divToc || !divSearch || !divIndex)
    return;
  if (strTabName==TocTabName) {
    divToc.style.display = "block";
    divSearch.style.display = "none";
    divIndex.style.display = "none";
  }
  else if (strTabName==SearchTabName) {
    divToc.style.display = "none";
    divSearch.style.display = "block";
    divIndex.style.display = "none";
  }
  else if (strTabName==IndexTabName) {
    divToc.style.display = "none";
    divSearch.style.display = "none";
    divIndex.style.display = "block";
  }
}

function GetVersionID()
{
  var input = document.forms[0]["__VERSIONID"];
  if (input!=null)
    return input.value;
}

function SetVersionID(value)
{
  var input = document.forms[0]["__VERSIONID"];
  if (input!=null)
    input.value = value;
}

var prefix = AppPath+"/Content.aspx/";
function GetContentUrl()
{
  var frame = GetContentFrame().contentWindow;
  var url;
  try {
    url = frame.location.href; // this expression can throw an exception
  }
  catch(e) {
    return null;
  }
  var index = url.indexOf(prefix);
  if (index==-1) {
    return null;
  }
  return url.substr(index+prefix.length);
}
function SetContentUrl(url)
{
  if (GetContentUrl()==url)
    return;
  GetContentFrame().contentWindow.location = AppPath+"/Content.aspx/"+url;
}
// ----------------------------------------------
var isHashSet = false;
function OnLoadContent()
{
  if (GetSearchFrame().contentWindow.OnContentLoaded)
    GetSearchFrame().contentWindow.OnContentLoaded();
    
  // add hash(#) from top frame to content frame
  if (isHashSet)
    return;
    
  isHashSet = true;   
  var contentLocation = GetContentFrame().contentWindow.location;
  var topHash = window.location.hash;
  if (contentLocation.hash=="" && topHash!="")
    contentLocation.hash = topHash;
}

var nodeIdToActivate = null;
function __HVSyncToc(nodeId)
{
  top.nodeIdToActivate = nodeId;
  var frame = GetTocFrame();
  if (!frame) return;
  var treeWindow = frame.contentWindow;
  if (treeWindow.ActivateNode)
    treeWindow.ActivateNode();
}

function ClosePopups()
{
  indexPopupDiv = parent.document.getElementById("divIndexPopup");
  indexPopupDiv.style.display = "none";
}

