function stopMediaPlayer() { 
	try	{
		
	var embedTag = document.getElementById('jwMediaPlayer');
	if(embedTag)	{
			var parentNode = embedTag.parentNode;
			parentNode.removeChild(embedTag);
			parentNode.appendChild(embedTag); 
	}

	}catch(err)	{
	}
};

function stopMediaPlayerBackup() { 
	document.getElementById('jwMediaPlayer').sendEvent('stop');
};
addLoadEvent(function initArticlePage() {
		initPageLayout();
	    // this doesn't belong here but i couldn't get it to execute 
	    // from workspace.js
	   	initMenu("sortProjectsButton", "sortProjectsMenu", "up");
	   	initMenu("sortItemsButton", "sortItemsMenu", "up");
	   	initMenu("otherProjectsButton", "otherProjectsMenu", "down");
	   	if (typeof wsUncheckAll == 'function') { 
			wsUncheckAll(); 
		}
        initMenu("relatedContentButton", "relatedContentMenu", "down");
	    initMenu("notesButton", "notesMenu", "up");
	    
	    initContentPanes();
	    initTocContainer();
	    initNotesContainer();
	    checkForUpdate();
	    initNotes();
});


addResizeEvent(function resizeArticlePage() {
	    initPageLayout();
});

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function addResizeEvent(func){
  var oldonresize = window.onresize;
  if (typeof window.onresize != 'function') {
    window.onresize = func;
  } else {
    window.onresize = function() {
      oldonresize();
      func();
    }
  }
}

function initPageLayout() {    
    var headerHeight = 56;
    
    var pageHeading = document.getElementById("pageHeading");
    if (pageHeading) {
        headerHeight = pageHeading.offsetHeight;        
    }
    
    var geogDropDownForm = document.getElementById("geogDropDownForm");
	if (geogDropDownForm) {
		var sidebarControls = document.getElementById("sidebarControlsContainer");
		if (sidebarControls) {
			headerHeight = sidebarControls.offsetHeight;
		}	
	        headerHeight = headerHeight + geogDropDownForm.offsetHeight;        
    }
    
    var headerPane = document.getElementById("headerPane");
    if (headerPane) {
        headerPane.style.height = headerHeight + "px";
    }

	var closeSidebarButton = document.getElementById("closeSidebarButton");
    if (closeSidebarButton) {
        closeSidebarButton.style.top = (headerHeight + 6) + "px";
    }
        
    var articlePane = document.getElementById("articlePane");
    if (articlePane) {
        articlePane.style.top = headerHeight + "px";
    }
        
    var sidebarTitleBar = document.getElementById("sidebarTitleBar");
    if (sidebarTitleBar) {
        sidebarTitleBar.style.top = headerHeight + "px";
    }
        
    var sidebarContent = document.getElementById("sidebarContent");
    if (sidebarContent) {
        sidebarContent.style.top = (headerHeight + 23) + "px";
    }
}


function scrollToToc(tocId) {
    sidebarPane = document.getElementById("sidebarContent");
    tocDiv = document.getElementById("tocNode"+tocId);
    if (tocDiv && sidebarPane) {
        sidebarPane.scrollTop = tocDiv.offsetTop;
    }
}

function scrollToAnchor(anchorId) {
    articlePane = document.getElementById("articlePane");
    anchor = document.getElementById(anchorId);
    if (anchor && articlePane) {
        articlePane.scrollTop = anchor.offsetTop;
    }
}

function scrollToTop() {
      articlePane = document.getElementById("articlePane");
      if (articlePane) {
          articlePane.scrollTop = 0;
      }
      
        sidebarPane = document.getElementById("sidebarContent");
        if (sidebarPane) {
            sidebarPane.scrollTop = 0;
        }
        selectTocNode(-1);
  }

function activateContent() {
    this["isActive"] = true;
}

function deactivateContent() {
    this["isActive"] = false;
}

function initContentPanes() {
    if (document.body.addEventListener) {
                    var divs = document.getElementsByTagName('DIV');
    for (var d in divs) {
      if (divs[d].className && divs[d].className == 'content') {
        try {
          divs[d].addEventListener('DOMMouseScroll', scrollWheel, false);
          divs[d].onmouseover = activateContent;
          divs[d].onmouseout = deactivateContent;
        } catch (ex) {}
      }
    }
  }
  
  document.addEventListener('keypress', scrollKey, false);
  var activeContent = null;
}



    function minimizeToolbar() {
        try {
        var article = document.getElementById("articlePane");
		var sidebar = document.getElementById("sidebarContent");
		
		article.style.bottom = "10px";
		if (sidebar) { sidebar.style.bottom = "10px"; }
    
        var toolbarButtons = document.getElementById("toolbarButtonsPane");
        toolbarButtons.style.display="none";
        var minLink = document.getElementById("toolbarMinimizeButton");
        minLink.style.display="none";
        var maxLink = document.getElementById("toolbarMaximizeButton");
        maxLink.style.display="block";
        
		var toolbar = document.getElementById("toolbarPane");
		toolbar.className = "closed";
		toolbar.style.overflow = "hidden";
		} catch (ex) {
		}
	}
	
	function maximizeToolbar() {
	try {
	    var article = document.getElementById("articlePane");
		var sidebar = document.getElementById("sidebarContent");
		
		article.style.bottom = "41px";
		if (sidebar) { sidebar.style.bottom = "41px"; } 
		
       var toolbarButtons = document.getElementById("toolbarButtonsPane");
        toolbarButtons.style.display="block";
        var minLink = document.getElementById("toolbarMinimizeButton");
        minLink.style.display="block";
        var maxLink = document.getElementById("toolbarMaximizeButton");
        maxLink.style.display="none";
        
		var toolbar = document.getElementById("toolbarPane");
		toolbar.className = "";
		toolbar.style.overflow = "visible";  
		} catch (ex) {
		}    
	}
	
	function hideSideBar() {
		var article = document.getElementById("articlePane");
		var sidebar = document.getElementById("sidebarContent");
		var titlebar = document.getElementById("sidebarTitleBar");
		
		article.style.width = "100%";
		sidebar.style.display = "none";
		titlebar.style.display = "none";
	}
	
	function showSideBar() {
		var article = document.getElementById("articlePane");
		var sidebar = document.getElementById("sidebarContent");
		var titlebar = document.getElementById("sidebarTitleBar");
		article.style.width = "75%";
		sidebar.style.width = "25%";
		sidebar.style.display = "block";
		titlebar.style.display = "block";
	}
	
	function scrollWheel(event)
      {
	var sl = event.currentTarget.scrollLeft;
	var st = event.currentTarget.scrollTop;
  	if (event.type == 'DOMMouseScroll') {
    	st += (event.detail * 10);
    	event.currentTarget.scrollTop = st < 0 ? 0 : st;
    	event.preventDefault();
	}
      }
      
      function getActiveContent() {
          var activeContent = null;
          var divs = document.getElementsByTagName('DIV');
          for (var d in divs) {
              if (divs[d].className && divs[d].className == 'content' && divs[d]["isActive"] == true) {
                  activeContent = divs[d];
                  break;
              }
          }
          return activeContent;
      }
  
  function scrollKey(event) {
    var activeContent = getActiveContent();
	if (activeContent) {
		var sl = activeContent.scrollLeft;
		var st = activeContent.scrollTop;
		if (event.type == 'keypress') {
			if (event.keyCode == 39) { // right
				sl -= 10;
				activeContent.scrollLeft = st < 0 ? 0 : st;
				event.preventDefault();
			} else if (event.keyCode == 40) { //down
				st += 10;
				activeContent.scrollTop = st < 0 ? 0 : st;
				event.preventDefault();
			} else if (event.keyCode == 38) { //up
				st -= 10;
				activeContent.scrollTop = st < 0 ? 0 : st;
				event.preventDefault();
			} else if (event.keyCode == 37) { //left
				sl += 10;
				activeContent.scrollLeft = st < 0 ? 0 : st;
				event.preventDefault();
			}
		}
	}
  }
  
 
  
  
  function initTocContainer() {
      var toc = document.getElementById("articleToc");
      if (toc) {
          toc["isLoaded"] = false;
      }
  }
  
  function initNotesContainer() {
      var notes = document.getElementById("articleNotes");
      if (notes) {
          notes["isLoaded"] = false;
      }
  }
  
  var selectedToc = -1;
  

function loadToc(tocURL) {
  showToc();
  var toc = document.getElementById("articleToc");
  if (toc["isLoaded"] == false) {
    var tocTitle = document.getElementById("sideBarTitleTOC");
    tocTitle.style.display = "none";
    var notesTitle = document.getElementById("sideBarTitleNotes");
    notesTitle.style.display = "none";
    var loadingTitle = document.getElementById("sideBarTitleLoading");
    loadingTitle.style.display = "inline";
    netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
    var x = new net.ContentLoader(tocURL, setTocContent);
  }
}

/*
function loadSubToc(tocURL, tocId) {
  var tocButton = document.getElementById("articleTocButton");
  if (tocButton) { //if button exists we can load the toc
      showToc();
      var toc = document.getElementById("articleToc");
      selectedToc = tocId;
      if (toc["isLoaded"] == false) {
        var title = document.getElementById("sideBarTitle");
        title.innerHTML = "Loading...";
        netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
        var x = new net.ContentLoader(tocURL, setTocContent);
      } else if (tocId != -1) {
           selectTocNode(selectedToc);
           scrollToToc(selectedToc);
      }
  }
}*/

function loadSubToc(tocURL, tocId) {
  var tocButton = document.getElementById("articleTocButton");
  if (tocButton) { //if button exists we can load the toc
      var toc = document.getElementById("articleToc");
      if (toc.style.display == "block") {
          selectedToc = tocId;
          if (tocId != -1) {
           selectTocNode(selectedToc);
           scrollToToc(selectedToc);
          }
      }
  }
}


function setTocContent() {
    var toc = document.getElementById("articleToc");
    //Doing this thru protocol listener instead
    //toc.innerHTML = this.req.responseText;
    toc["isLoaded"] = true;
    var tocTitle = document.getElementById("sideBarTitleTOC");
    tocTitle.style.display = "inline";
    var notesTitle = document.getElementById("sideBarTitleNotes");
    notesTitle.style.display = "none";
    var loadingTitle = document.getElementById("sideBarTitleLoading");
    loadingTitle.style.display = "none";
    
    var anchor = getURLAnchor();
    var anchorTocIdx = anchor.indexOf(".toc");
    if (anchorTocIdx != -1) {
       selectedToc = anchor.substr(1, anchorTocIdx-1);
    }    
    
    if (selectedToc != -1) {
        selectTocNode(selectedToc);
        scrollToToc(selectedToc);
    }
}

function getURLAnchor() {
  var strAnchor = "";
  var strHref = window.location.href;  
  var anchorIdx = strHref.indexOf("#");
  if ( anchorIdx != -1 ) {
    strAnchor = strHref.substr(anchorIdx);
  }
  return strAnchor;
}

function showToc() {
    var toc = document.getElementById("articleToc");
    var notes = document.getElementById("articleNotes");
    notes.style.display = "none";
    toc.style.display = "block";
    var tocTitle = document.getElementById("sideBarTitleTOC");
    tocTitle.style.display = "inline";
    var notesTitle = document.getElementById("sideBarTitleNotes");
    notesTitle.style.display = "none";
    var loadingTitle = document.getElementById("sideBarTitleLoading");
    loadingTitle.style.display = "none";

    showSideBar();
}

function showNotes() {
    var tocTitle = document.getElementById("sideBarTitleTOC");
    tocTitle.style.display = "none";
    var notesTitle = document.getElementById("sideBarTitleNotes");
    notesTitle.style.display = "inline";
    var loadingTitle = document.getElementById("sideBarTitleLoading");
    loadingTitle.style.display = "none";
    var toc = document.getElementById("articleToc");
    var notes = document.getElementById("articleNotes");
    notes.style.display = "block";
    toc.style.display = "none";
    showSideBar();
    return true;
}

function showMenu(containerId, menuId)
{
  var container = document.getElementById(containerId);
  var menu = document.getElementById(menuId);
  menu.style.display = "block"; 
}

function mouseOverMenu()
{
  menu = document.getElementById(this["menuId" ]);
  showMenu(this["containerId"], this["menuId"]);
  clearTimeout(menu["timeout"]);
}

function hideMenuDelayed()
{
  menu = document.getElementById(this["menuId"]);
  menu["timeout"] = setTimeout("hideMenu('"+menu.id+"')", 333);
}

function hideMenu(menuId) {
    menu = document.getElementById(menuId);
    if (menu) {
        menu.style.display = "none";
    }
}

function clickMenuButton()
{
  container = document.getElementById(this["containerId"]);
  menu = document.getElementById(this["menuId"]);

  if (menu.style.display != "block") showMenu(this["containerId"], this["menuId"]);
  else menu.style.display = "none";
  
  if ((this["direction"] == "up")) {
      var top = container.offsetTop - (menu.offsetHeight) + 17;
  } else {
      var top = container.offsetTop + container.offsetHeight;
  }
  var left = container.offsetLeft;
  menu.style.top = top + "px";
  menu.style.left = left + "px";
  return false;
}

function initMenu(containerId, menuId, direction)
{
    container = document.getElementById(containerId);
    menu = document.getElementById(menuId);
    
    if (container) {
        if (menu) {
          container["containerId"]     = container.id;
          menu["containerId"]     = container.id;
          container["menuId"]      = menu.id;
          menu["menuId"]      = menu.id;
          container["direction"] = direction;
          menu["direction"] = direction;

           container.onclick     = clickMenuButton;
           container.onmouseout  = hideMenu;
           menu.onmouseover =     mouseOverMenu;
           menu.onmouseout  =     hideMenuDelayed;
           
        }
   }
}

function swapMenuItem(elIdToHide, elIdToShow) {
    itemToHide = document.getElementById(elIdToHide);
    if (itemToHide) itemToHide.className = "disabledMenuItem";  
    
    itemToShow = document.getElementById(elIdToShow);
    if (itemToShow) itemToShow.className = "enabledMenuItem";
    return true;
}

function articleUpdateCheck(articleId, updateUrl, pid) {
   if(articleId && updateUrl) {
   		try {
			netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
            var url = updateUrl + '?articleId=' + articleId + '&pid=' + pid;

	    	var x = new net.ContentLoader(url, articleUpdateAvailable);

    	} catch(err) {
    		alert(err.message);
    	}
   }
}


function articleUpdateAvailable() {
	// xml parsing broke in Webrenderer, using responseText
	var res = this.req.responseText;
	if(res) {
		var isUpdated = res.indexOf('update=\"true');
		if(isUpdated > 0) {
			updatedEl = document.getElementById('contentUpdate');
			updatedEl.style.display = "table-cell";	
		}
	}

}
	
	
  
	
