/*----------------------------------------------------------------------------
 *nw.js: nexwave javascript for NSI documention
 *----------------------------------------------------------------------------
  This file is part of the DITA Open Toolkit project hosted on
  Sourceforge.net. See the accompanying license.txt file for
  applicable licenses.
   Copyright (c) 2007-2008 NexWave Solutions All Rights Reserved. 
   www.nexwave-solutions.com Nadege Quaine
  */
 
var oldNode = null;
var oldColor = null;
var activeNodeColor = "#abcbff";

     var showedNode = null;
	 var oldNodeElement = null;

     /* This function synchronises the navigation frame containing the toc 
		with the page currently displayed in the contentwin frame*/
	 var frameSync = function(e) {   

	 /*Check that the frames and the toc tree exist.*/
		if (top.tocwin == null) {
			return;
		}
		if (top.contentwin == null) {
			return;
		}
		if (top.tocwin.tree == null) {
			return;
		}
		
     /*First, find out which document is displayed in the contentwin frame:*/
		
        /* retrieve the id (DC.Identifier) of the document*/
        metatags = top.contentwin.document.getElementsByTagName("meta");
        for (cnt = 0; cnt < metatags.length; cnt++)
        {
            var name = metatags[cnt].getAttribute("name");
            var idContent=null; // 
        
            if (metatags[cnt].getAttribute("name") == "DC.Identifier") {
                idContent = metatags[cnt].getAttribute("content");
				break;
            }
        }
        /* idContent contains the document identifier.
		Now, look for the same document in the tree (identified by its id as well) */

        showedNode = null;
		
		// Get the node which has an id property equal to the id of the currently displayed document
        if (idContent != null) {
            showedNode = top.tocwin.tree.getNodeByProperty("idObj", idContent);
        }

		/* If the node  was not found in the tree, or if the id was not retrieved from the current document,
			showedNode is null.
			We assume that the document is not part of this manual.
			TODO: T B I
		*/
        if (showedNode == null) {
            /*This topic is not part of this manual, therefore, the toc will not be updated.
			We tell the reader by displaying a div of tooltip style*/
			var idnode="para_not_manual";
			var setting = "color:#D0D0E0\;" +"float:right\;" + "font-style:italic\;" 
				+"background-color:#FFFFDE\;"+"border-style:solid\;"+"border-width:1px\;"
				+"border-color:#D0D0E0\;";
			nodetemp = top.contentwin.document.getElementById(idnode);

			if (nodetemp)
			{
				  if (nodetemp.parentNode)
					nodetemp.parentNode.removeChild(nodetemp);

			}
			var newInfo = top.contentwin.document.createElement("div");
			var texte_newInfo = top.contentwin.document.createTextNode("This topic is not part of this manual.");
			newInfo.appendChild(texte_newInfo);
			newInfo.setAttribute('style',setting);
			newInfo.setAttribute('id',idnode);
			// if IE only:
			if (document.all) {
				newInfo.style.setAttribute("cssText", setting);
			}
			

			top.contentwin.document.getElementsByTagName("h1")[0].parentNode.insertBefore(newInfo,top.contentwin.document.getElementsByTagName("h1")[0].nextSibling);
			return;
        } 
		else {
            
			// Oldnode contains the currently active node of the toc tree.
			// We retrieve the properties of the label of this node
	        if (top.tocwin.oldNode != null) {
				if (top.tocwin.oldNode.getElId() == showedNode.getElId()) {
					// Scroll to the node position
					scrollToElem(showedNode);
					return;
				}
			}
			
			// top.navigation.tree.expandAll();
	        if (top.tocwin.oldNode != null) {
				oldNodeElement = top.tocwin.oldNode.getLabelEl();
	            oldNodeElement.style.color = top.tocwin.oldColor;
	        } 

			// Update oldnode for next step
			top.tocwin.oldNode = showedNode;
				
			expandBranch(showedNode);

			// Update current node properties
	        var labelElem =showedNode.getLabelEl();
		top.tocwin.oldColor = labelElem.style.color;
	        labelElem.style.color = activeNodeColor;

            // Scroll to the node position
            scrollToElem(showedNode);

        }
		return;
    } 

   /* This function finds the position of a html element in the page:
    because the offsetTop and offsetLeft properties are relative to the parent element,
    we have to sum up the location properties of the parents to get the position relative to the window.
    */
    function findHTMLPos(obj) {
		var curleft = curtop = 0;
		if (obj.offsetParent) {
			curleft = obj.offsetLeft
			curtop = obj.offsetTop
			while (obj = obj.offsetParent) {
				curleft += obj.offsetLeft
				curtop += obj.offsetTop
			}
		}
		return [curleft,curtop];
	}
	
	/*This function expands the node given as parameter only.*/
	function expandBranch(node) {
		var arrayNode = new Array();
		tempNode=node;
		prevNode=null;
		var i = 0;
		arrayNode[i] = tempNode;
		while (tempNode.parent != null )
		{		
			i++;
			arrayNode[i] = 	tempNode.parent;
			tempNode = tempNode.parent;
		}
		cpt = arrayNode.length;
        for (i = cpt-1;i>=0;i--) {
			arrayNode[i].expand();
		}
	}

	function scrollToElem(node) {
            // get node position in the window of the browser
            elem = node.getEl();
            positions = findHTMLPos(elem);
            
            // window scroll to this position
            top.tocwin.scrollTo(0, positions[1] - 30);	
	}
	
