// 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("Img/1.gif", "Img/Tree/I.gif", "Img/Tree/T.gif",
  "Img/Tree/L.gif", "Img/Tree/Plus.gif", "Img/Tree/LPlus.gif",
  "Img/Tree/Minus.gif", "Img/Tree/LMinus.gif",
  "Img/Icons/Book.gif", "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 selectedContentUrl = null;
function Tree_OnSelect(contentUrl, allowed, nodeId)
{
  if (contentUrl=="")
    return;
    
  selectedContentUrl = contentUrl;
  nodeIdHash[selectedContentUrl] = nodeId;
  SetContentUrl(selectedContentUrl);
}
function RefreshTree(nodeIdToActivate)
{
  var tocWindow = GetTocFrame().contentWindow;
  tocWindow.Refresh(nodeIdToActivate);
  var indexFrame = GetIndexFrame();
  if (indexFrame!=null) {
    var indexWindow = indexFrame.contentWindow;
    if (indexWindow!=null && indexWindow.Index_Refresh!=null)
      indexWindow.Index_Refresh();
  }
}
function OnSync(newVersionId)
{
  if (newVersionId!=null)
    SetVersionID(newVersionId);
  nodeIdHash = {};
  RefreshTree();
  var contentUrl = GetContentUrl();
  SendNodeIdRequest(contentUrl);
}

function OnPrint()
{
  GetContentFrame().contentWindow.print();
}

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 = "Content.aspx/";
var contentUrlSaved = null;
function GetContentUrl()
{
  var frame = GetContentFrame();
  var contentWindow = frame.contentWindow;
  var url;
  try {
    url = contentWindow.location.href; // this expression can throw an exception
  }
  catch(e) {
    return contentUrlSaved;
  }
  var index = url.indexOf(prefix);
  if (index==-1) {
    if (url=="about:blank")
      return null;
    return url;
  }
  url = url.substr(index+prefix.length);
  var questPos = url.indexOf("?");
  var versionPos = url.indexOf("__versionId", questPos);
  if(versionPos!=-1) {
    var firstPart = url.substring(0, versionPos);
    var secondPart = url.substr(versionPos+11);
    if(secondPart==null || secondPart=="" || secondPart.indexOf("&")==-1)
      url = firstPart.substring(0, firstPart.length-1);
    else {
      // not implemented
    }
  }  
  return url;
}

function SetContentUrl(url, quiet)
{ 
  var contentUrl = GetContentUrl();
  
  if (contentUrl==url || decodeURIComponent(contentUrl)==url)
    return;
  contentUrlSaved = url;
  if (!IsGlobalUrl(url)) {
    if(quiet==null || !quiet)
      url = prefix+url;
    else
      url = AppPath+prefix+url;
  }
  var versionID = GetVersionID();
  if (versionID!=null) {
    if (url.indexOf("?")==-1)
      url = url+"?";
    else
      url = url+"&";
    url = url+"__versionId="+versionID;
  }
  if (quiet==null || !quiet)
    GetContentFrame().src = url;
  else {
    GetContentFrame().contentWindow.location.replace(url);
  }
}
// ----------------------------------------------
function OnLoadContent()
{
  var searchFrame = GetSearchFrame();
  if (searchFrame!=null && searchFrame.contentWindow!=null && searchFrame.contentWindow.OnContentLoaded)
    searchFrame.contentWindow.OnContentLoaded();
}

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";
}

var timerId;
function Init()
{  
  var topHash = window.location.hash; 
  if(topHash.substr(0, 2)=="##") {
    var contentWindow = GetContentFrame().contentWindow;
    SetContentUrl(topHash.substr(2), true);
  }
  
  if (window.navigator.appName!='Netscape') {
    try {
      document.execCommand("BackgroundImageCache", false, true);
    } catch(err) {}
  }
  timerId = setInterval(CheckContentLoaded, 300);
}

var oldCookie;
var oldContentUrl = "";
var oldTopHash = "";
var nodeIdHash = {};
var manualGenNodeIdCount = -1;

function CheckContentLoaded()
{
  var contentWindow = GetContentFrame().contentWindow;
  if (contentWindow==null)
    return;
  var contentUrl = GetContentUrl();

  var nodeIdFromHash = nodeIdHash[contentUrl];
  if (window.navigator.cookieEnabled) {
    var cookie = document.cookie;
    if (cookie!=oldCookie) {
      oldCookie = cookie;      
      if (nodeIdFromHash==null) {
        var nodeId = GetCookie("nodeId");
        if (nodeId!=null) {
          manualGenNodeIdCount = -1;
          nodeIdHash[contentUrl] = nodeId;
          nodeIdFromHash = nodeId;
          __HVSyncToc(nodeId);
        }
      }
      var versionId = GetCookie("__versionId");
      if (versionId!=null && versionId!="" && versionId!=GetVersionID()) {
        //SetVersionID(versionId);
        OnSync(versionId);
      }
    }
  }
  
  nodeIdFromHash = nodeIdHash[contentUrl];
  if (manualGenNodeIdCount!=-1)
    manualGenNodeIdCount++;
  if (manualGenNodeIdCount==2) {
    if(selectedContentUrl!=contentUrl) {
      // send get nodeId request
      SendNodeIdRequest(contentUrl);
    }
    manualGenNodeIdCount = -1;
  }
  
  var contentLocation = contentWindow.location;
  var topHash = window.location.hash;
  if(contentUrl!=null && contentUrl!=oldContentUrl) {
    // fix IE sync on back button 
    if(nodeIdFromHash!=null) {
      manualGenNodeIdCount = -1;
      __HVSyncToc(nodeIdFromHash);
    }
    else {
      manualGenNodeIdCount = 0;
    }
    oldContentUrl = contentUrl;
    if(topHash!="##"+contentUrl && topHash!="##"+decodeURIComponent(contentUrl)) {
      topHash = "##"+contentUrl;
      window.location.replace(AppPath+"Default.aspx"+topHash);
    }
  }
  
  // fix Firefox back button
  if (topHash!=null && topHash!=oldTopHash) {
    oldTopHash = topHash;
    if(topHash!="##"+contentUrl && topHash!="##"+decodeURIComponent(contentUrl)) {
      SetContentUrl(topHash.substr(2), true);
    }
  }
}

function IsGlobalUrl(url)
{
  return url.indexOf("http://")>=0 ||
    url.indexOf("https://")>=0 ||
    url.indexOf("mailto://")>=0 ||
    url.indexOf("ftp://")>=0 ||
    url.indexOf("file://")>=0;
}

function SendNodeIdRequest(contentUrl)
{
  var httpRequest = util.CreateXmlHttpRequest();
  httpRequest.open("GET", "TreeActionServer.aspx?__getNodeId="+encodeURIComponent(contentUrl), false);
  httpRequest.onreadystatechange = function() {
      if (httpRequest.readyState==4 && httpRequest.responseText) {
        nodeIdHash[contentUrl] = httpRequest.responseText;
        __HVSyncToc(httpRequest.responseText);
      }
    }
  httpRequest.send(null);
}
