﻿/*
* TreeMenu - javascript class for the TreeMenu user control
* Written by Lior Frenkel 22.7.2007 , bvtech-ltd, www.bvtech-ltd.com
*
*
*
*/

function TreeMenu()
{
    var self = this;
    var m_sClientId = "";
    var m_sClosedRowImage = "";
    var m_sOpenedRowImage = "";
    var m_sHighlightColor = "";
    var m_sHiddenCurrentItemIdFieldId = null;
    var m_sSeperator = "";
    var m_sSeperatorCss = "";
    var m_sNavigationItemCss = "";
    var m_sNavigationPathDivId = "";
    var m_bNavigationItemPressed = false;
    
    // properties ------------------------------------------------------------------------------------------
    this.ClientId = function(sId)
    {
        m_sClientId = sId;
    };
    this.ClosedRowImage = function(sImagePath)
    {
        m_sClosedRowImage = sImagePath;
    };
    this.OpenedRowImage = function(sImagePath)
    {
        m_sOpenedRowImage = sImagePath;
    };
    this.HighlightColor = function(sColor)
    {
        m_sHighlightColor = sColor;
    };
    this.HiddenCurrentItemIdFieldId = function(sId)
    {
        m_sHiddenCurrentItemIdFieldId = sId;
    };
    this.Seperator = function(sSeperator)
    {
        m_sSeperator = sSeperator;
    };
    this.SeperatorCss = function(sSeperatorCss)
    {
        m_sSeperatorCss = sSeperatorCss;
    };
    this.NavigationItemCss = function(sNavigationItemCss)
    {
        m_sNavigationItemCss = sNavigationItemCss;
    };
    this.NavigationPathDivId = function(sNavigationPathDivId)
    {
        m_sNavigationPathDivId = sNavigationPathDivId;
    };
    this.NavigationItemPressed = function(bNavigationItemPressed)
    {
        m_bNavigationItemPressed = bNavigationItemPressed;
    };
    // -- end properties ------------------------------------------------------------------------------------
    
    
    // -- public functions ----------------------------------------------------------------------------------
    this.Load = function()
    {
        var sCurrentItemId = document.getElementById(m_sHiddenCurrentItemIdFieldId).value;
        if(sCurrentItemId != "")
        {
            showCurrentRow(m_sClientId + "_row" + sCurrentItemId);
            BuildNavigationPath(m_sClientId + "_row" + sCurrentItemId);
        }
    };
    
    this.OnItemClick = function(rowId)
    {
        ShowHideChildren(rowId);
        BuildNavigationPath(rowId);
        m_bNavigationItemPressed = false;
    }
    
    // -- private functions ----------------------------------------------------------------------------------
    function ShowHideChildren(rowId, bSetHiddenField) 
    {
        if(bSetHiddenField == null)
        {
            bSetHiddenField = true;
        }
        try
        {
            var oRow = document.getElementById(rowId);
            var oRowChildrenDiv = document.getElementById(rowId + "_children"); 
            var oRowImage = document.getElementById(rowId + "_image");   
            if(oRowChildrenDiv)    
            {        
                var isHidden = (oRowChildrenDiv.style.display == "none");
                var bHighlight = (oRow.attributes.getNamedItem("HighLight").value == "true");     
                if(isHidden)        
                {
                    if(!m_bNavigationItemPressed)
                    {   
                        oRowChildrenDiv.style.display = "";
                        if(m_sOpenedRowImage != "")
                        {
                            oRowImage.src = m_sOpenedRowImage;
                        } 
                    }
                    if(bHighlight)
                    {
                        oRow.style.color = m_sHighlightColor;
                    } 
                    if(bSetHiddenField)
                    {
                        document.getElementById(m_sHiddenCurrentItemIdFieldId).value = rowId.substring(m_sClientId.length+4);
                    }
                }       
                else        
                {     
                    oRowChildrenDiv.style.display = "none";    
                    if(m_sClosedRowImage != "")
                    {
                        oRowImage.src = m_sClosedRowImage;
                    }  
                    if(!m_bNavigationItemPressed)
                    {     
                        oRow.style.color = "";
                    }
                    // remove HighLight from other leaves
                    RemoveRemoveHighLightFromAllLeaves(); 
                    if(bSetHiddenField)
                    {
                        var oParent = oRow.parentNode;
                        document.getElementById(m_sHiddenCurrentItemIdFieldId).value = oParent.id.substring(m_sClientId.length+4,oParent.id.lastIndexOf("_children"));
                    }
                }
            }
            else
            {
                var bHighlight = (document.getElementById(rowId).attributes.getNamedItem("HighLight").value == "true");
                // remove HighLight from other leaves
                RemoveRemoveHighLightFromAllLeaves();
                if(bHighlight)
                {
                    // HighLight
                    oRow.style.color = m_sHighlightColor;
                } 
            }
        }
        catch(exc)
        {}
    };
    
    function showCurrentRow(id)
    {
        if(id != "")
        {
            var oDiv = document.getElementById(id);
            if(oDiv != null){
                ShowHideChildren(id,false);
                var oParent = oDiv.parentNode;
                while(oParent.id != (m_sClientId + "_baseRow"))
                {
                    var sParentId = oParent.id.substring(0,oParent.id.lastIndexOf("_children"));
                    ShowHideChildren(sParentId,false);
                    oParent = oParent.parentNode;
                }
            }
        }
    };
    
    
    
    function RemoveRemoveHighLightFromAllLeaves()
    {
        var oBaseRow = document.getElementById(m_sClientId+"_baseRow");
        for(var i=0; i<oBaseRow.children.length;i++)
        {
            if(oBaseRow.children[i].id.lastIndexOf("_children") == -1)
            {
                RemoveHighLightRecursive(oBaseRow.children[i]);
            }
        }
    }
    
    function RemoveHighLightRecursive(oRow)
    {
        // get children
        var oRowChildrenDiv = document.getElementById(oRow.id + "_children");
        
        if(oRowChildrenDiv == null) // this is a leaf
        {
            oRow.style.color = "";
            return;
        }
        else
        {
            // remove from all children
            for(var i=0; i<oRowChildrenDiv.children.length;i++)
            {
                if(oRowChildrenDiv.children[i].id.lastIndexOf("_children") == -1)
                {
                    RemoveHighLightRecursive(oRowChildrenDiv.children[i]);
                }
            }
        }
    }
    
    function BuildNavigationPath(id)
    {
        try
        {
        
            if(id != "")
            {
                var sResultPath = "";
                var oDiv = document.getElementById(id);
                if(oDiv != null){
                    var sItemID = oDiv.id.replace(m_sClientId+"_row","");
                    //sResultPath = "<a class=\""+m_sNavigationItemCss+"\" href=\"javascript:;\" onclick=\"oTreeMenu_"+m_sClientId+".NavigationItemPressed(true);"+oDiv.attributes.getNamedItem("onclick").value+"\">" + GetInnerText(oDiv)  + "</a>";
                    sResultPath = "<a class=\""+m_sNavigationItemCss+"\" href='ANTSearchItems.aspx?Subject="+sItemID+"'>" + GetInnerText(oDiv)  + "</a>";
                    var oParent = oDiv.parentNode;
                    while(oParent.id != (m_sClientId + "_baseRow"))
                    {
                        var sParentId = oParent.id.substring(0,oParent.id.lastIndexOf("_children"));
                        oParent = document.getElementById(sParentId);
                        sItemID = oParent.id.replace(m_sClientId+"_row","");
                        //sResultPath = "<a class=\""+m_sNavigationItemCss+"\" href=\"javascript:;\" onclick=\"oTreeMenu_"+m_sClientId+".NavigationItemPressed(true);"+oParent.attributes.getNamedItem("onclick").value+"\">" + GetInnerText(oParent) + "</a>" + "<span class=\""+m_sSeperatorCss+"\">" + m_sSeperator + "</span>" + sResultPath;
                        //sResultPath = "<a class=\""+m_sNavigationItemCss+"\" href='ANTSearchItems.aspx?Subject="+sItemID+"'>" + GetInnerText(oParent) + "</a>" + "<span class=\""+m_sSeperatorCss+"\">" + m_sSeperator + "</span>" + sResultPath;
                        sResultPath = "<a class=\""+m_sNavigationItemCss+"\" href='ANTSearchItems.aspx?Subject="+sItemID+"'>" + GetInnerText(oParent) + "</a>" + "<span class=\""+m_sSeperatorCss+"\">"  +  m_sSeperator + sResultPath  + "</span>";
                        oParent = oParent.parentNode;
                    }
                    var PathDiv = document.getElementById(m_sNavigationPathDivId);
                    
                    if(PathDiv != null){
                        PathDiv.innerHTML = "";
                        PathDiv.innerHTML = sResultPath;
                    }
                }
            }
        }
        catch(ex)
        {
        }
    }
}

function GetInnerText(oElement)
{
    if(document.all)
    {
        return oElement.innerText;
    } 
    else
    {
        return oElement.textContent;
    }

}