// 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("StaticFile.axd/Img/1.gif", "StaticFile.axd/Img/Tree/I.gif", "StaticFile.axd/Img/Tree/T.gif",
  "StaticFile.axd/Img/Tree/L.gif", "StaticFile.axd/Img/Tree/Plus.gif", "StaticFile.axd/Img/Tree/LPlus.gif",
  "StaticFile.axd/Img/Tree/Minus.gif", "StaticFile.axd/Img/Tree/LMinus.gif",
  "StaticFile.axd/Img/Icons/Book.gif", "StaticFile.axd/Img/Icons/Article.gif");

//-------------------------
function GetTocFrame()
{
  return this.document.getElementById("tocFrame");
}
function GetContentFrame()
{
  return this.document.getElementById("contentFrame");
}
function GetSearchFrame()
{
  return this.document.getElementById("searchFrame");
}
function GetIndexFrame()
{
  return this.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()
{
  var contentWindow = GetContentFrame().contentWindow;
  contentWindow.focus();
  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";
    var frame = GetSearchFrame();
    var window = frame.contentWindow;
    if (window.ActivateSearchStringInput!=null)
      window.ActivateSearchStringInput();
  }
  else if (strTabName==IndexTabName) {
    divToc.style.display = "none";
    divSearch.style.display = "none";
    divIndex.style.display = "block";
    var frame = GetIndexFrame();
    var window = frame.contentWindow;
    if (window.ActivateIndexInput!=null)
      window.ActivateIndexInput();
  }
}

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);
  }
}

var searchValueSaved = null;
var searchPrefix = "Search.aspx/";
function GetSearchValue()
{
  var frame = GetSearchFrame();
  var contentWindow = frame.contentWindow;
  var url;
  try {
    url = contentWindow.location.href; // this expression can throw an exception
  }
  catch (e) {
    return searchValueSaved;
  }
  var questPos = url.indexOf("?");
  var valuePos = url.indexOf("value=", questPos);
  if (valuePos!=-1) {
    var secondPart = url.substr(valuePos+6);
    if (secondPart==null || secondPart=="")
      return null;
    else {
      var index = secondPart.indexOf("&");
      if (index==-1)
        return secondPart;
      else
        return secondPart.substring(0, index);
    }
  }
  return null;
}

function SetSearchUrl(value)
{
  var contentWindow = GetSearchFrame().contentWindow;
  if (TabStrip_ToggleTab==null || contentWindow==null)
    return false;
  var searchValue = GetSearchValue();

  if (value==searchValue)
    return false;

  TabStrip_ToggleTab("tabStrip", SearchTabName);

  searchUrlSaved = value;
  url = AppPath+"Search.aspx?value="+value;
  contentWindow.location.replace(url);
  return true;
}

function SetIndexValue(value)
{
  var conentWindow = GetIndexFrame().contentWindow;
  if (conentWindow!=null && conentWindow.SetIndexValue!=null && TabStrip_ToggleTab!=null) {
    TabStrip_ToggleTab("tabStrip", IndexTabName);
    conentWindow.SetIndexValue(value);
    return true;
  }
  else
    return false;
}

// ----------------------------------------------
function OnLoadContent()
{
  var searchFrame = GetSearchFrame();
  if (searchFrame!=null && searchFrame.contentWindow!=null && searchFrame.contentWindow.OnContentLoaded)
    searchFrame.contentWindow.OnContentLoaded();
}

var nodeIdToActivate = null;
function __HVSyncToc(nodeId)
{
  this.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.length>=3 && topHash.substr(0, 3)=="##?" || topHash=="##!") && 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 topHash = window.location.hash;
  
  //fix Safari URI bug
  topHash = decodeURIComponent(topHash);
  
  // redirect to default content
  if (topHash==null || topHash.length==0)
    if (DefaultContentUrl != null && DefaultContentUrl.length > 0) {
      topHash = "##"+DefaultContentUrl;
      window.location.replace(window.location.href+topHash);
      return;
    }
  if (topHash.length>=4 && topHash.substr(0, 4)=="##?=") {
    var indexWindow = GetIndexFrame().contentWindow;
    if (indexWindow!=null) {
      if (!SetIndexValue(decodeURIComponent(topHash.substr(4))))
        return;
      else
        window.location.replace(window.location.pathname+window.location.search+"##");
    }
    else
      return;
  }
  else if (topHash.length>=3 && topHash.substr(0, 3)=="##?") {
    var searchWindow = GetSearchFrame().contentWindow;
    if (searchWindow!=null) {
      if (!SetSearchUrl(topHash.substr(3)))
        return;
      else
        window.location.replace(window.location.pathname+window.location.search+"##");
    }
    else
      return;
  }

  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;
  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(window.location.pathname+window.location.search+topHash);
    }
  }

  // fix Firefox back button
  if (!(topHash.length>=3 && topHash.substr(0, 3)=="##?") && 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);
}
