
/*
 * Class - TTreeMenu (Horizontal drop down)
 */

var TTreeMenu = Class.extend(TAbstractTreeMenu,
{
    /*
     * Method - initialize
     */
    initialize: function( name )
    {
        $C(TAbstractTreeMenu).initialize.call( this, name );

        this.divId = null;
        this.checkInProgress = false;
        this.visibleLevel = 1;
        this.className = "cssHddMenu";


        this.icon =
	      {
	          directory   : '', // to be set when class is initialized
	  	      arrowLeft		: 'arrow_left.gif',
	  	      arrowDown		: 'arrow_down.gif',
	  	      empty		    : 'empty.gif'
	      }
    },



    /*
     * Method - setDivId
     */
    setDivId: function( divId )
    {
        this.divId = divId;
    },



    /*
     * Method - draw
     */
    draw: function()
    {
        var html = "";
        html += this.node2html_h( this.tree );
        document.getElementById( this.divId ).innerHTML = html;
    },



    /*
     * Method - node2html_h
     *
     * Node to html
     */
    node2html_h: function( node )
    {
        var html = "";

        if ( node.subNodesCount == 0 ) return html;

        html += "<table id='" + this.name + "_wrapper' cellspacing=0 cellpadding=0 border=0 class=" + this.className + " style='width:100%;'><tr><td class=hwrap>";
        html += "<table cellspacing=0 cellpadding=0 border=0>";

        html += "<tr>";

        for ( i in node.subNodes )
        {
            html += "<td valign=middle id=" + node.subNodes[i].id + " class=hcell_out nowrap onmouseover='" + this.name + ".mi_onmouseover_h(this.id)' onmouseout='" + this.name + ".mi_onmouseout_h(this.id)'>";
            if ( node.subNodes[i].subNodesCount == 0 )
            {
                if ( this.callFunc )
                {   html += "<a href=# onclick='" + this.callFunc + "(\"" + node.subNodes[i].id + "\")'><div>" + node.subNodes[i].attributes["text"];
                }
                else
                {   html += "<a href='" + node.subNodes[i].attributes["link"] + "'" + (this.target?" target="+this.target:"") + "><div>" + node.subNodes[i].attributes["text"];
                }
            }
            else
            {   html += "<a href=# onclick='return false'><div>" + node.subNodes[i].attributes["text"];
            }

            if ( node.subNodes[i].subNodesCount == 0 )
            {   html += "&nbsp;<img id=" + node.subNodes[i].id + " src='" +  this.icon.directory + this.icon.empty + "' border=0>";
            }
            else
            {   html += "&nbsp;<img id=" + node.subNodes[i].id + " src='" +  this.icon.directory + this.icon.arrowDown + "' border=0>";
            }
            html += "</div></a>";
            html += "</td>";
        }

        html += "</tr>";
        html += "</table>";

        html += "</td></tr></table>";

        return html;
    },




    /*
     * Method - mi_onmouseover_h
     *
     * On mouse over - horizontal menu
     */
    mi_onmouseover_h: function( id )
    {
        var td = document.getElementById( id );
        td.className = "hcell_over";

        var node = this.tree.findNode( id );
        var html = this.node2html_v(node);

        this.visibleLevel = node.attributes["level"] + 1;
        this.checkVisibleNodes();

        var divId = "ddm" + this.visibleLevel;
        var d = document.getElementById( divId );
        if (!d)
        {
            d = document.createElement("DIV");
            d.id = divId;
        }

        d.innerHTML = html;

        document.body.appendChild( d );

        var p = td;
        var left = 0;
        while ( p )
        {
            left += p.offsetLeft;
            p = p.offsetParent;
        }

        var p = document.getElementById( this.name + "_wrapper" );
        var top = p.clientHeight; 
        while ( p )
        {
            top += p.offsetTop;
            p = p.offsetParent;
        }

        d.style.position = "absolute";
        d.style.top = top - 1;
        d.style.left = left;
    },




    /*
     * Method - mi_onmouseout_h
     *
     * On mouse out - horizontal menu
     */
    mi_onmouseout_h: function( id )
    {
        document.getElementById( id ).className = "hcell_out";

        this.visibleLevel = 1;

        if ( !this.checkInProgress )
        {
            this.checkInProgress = true;
            var funcName = this.name + ".checkVisibleNodes()";
            setTimeout( function() {eval( funcName )}, 1000 );
        }
    },    




    /*
     * Method - node2html_v
     *
     * Node to html
     */
    node2html_v: function( node )
    {
        var html = "";

        if ( node.subNodesCount == 0 ) return html;

        html += "<table cellspacing=0 cellpadding=0 border=0 class=" + this.className + "><tr><td class=vwrap>";
        html += "<table cellspacing=0 cellpadding=0 border=0>";

        for ( i in node.subNodes )
        {
            html += "<tr id=" + node.subNodes[i].id + " onmouseover='" + this.name + ".mi_onmouseover_v(this.id)' onmouseout='" + this.name + ".mi_onmouseout_v(this.id)'>";
            html += "<td valign=middle id=" + node.subNodes[i].id + "_td_left class=left_out nowrap>";
            if ( node.subNodes[i].subNodesCount == 0 )
            {
                if ( this.callFunc )
                {   html += "<a href=# onclick='" + this.callFunc + "(\"" + node.subNodes[i].id + "\")'><div>" + node.subNodes[i].attributes["text"] + "</div></a>";
                }
                else
                {   html += "<a href='" + node.subNodes[i].attributes["link"] + "'" + (this.target?" target="+this.target:"") + "><div>" + node.subNodes[i].attributes["text"] + "</div></a>";
                }
            }
            else
            {   html += "<a href=# onclick='return false'><div>" + node.subNodes[i].attributes["text"] + "</div></a>";
            }

            html += "</td><td align=left width=10px valign=middle id=" + node.subNodes[i].id + "_td_right class=right_out>";
            if ( node.subNodes[i].subNodesCount == 0 )
            {   html += "<a href=# onclick='return false'><div><img id=" + node.subNodes[i].id + " src='" +  this.icon.directory + this.icon.empty + "' border=0></div></a>";
            }
            else
            {   html += "<a href=# onclick='return false'><div><img id=" + node.subNodes[i].id + " src='" +  this.icon.directory + this.icon.arrowLeft + "' border=0></div></a>";
            }
            html += "</td></tr>";
        }

        html += "</table>";
        html += "</td></tr></table>";

        return html;
    },




    /*
     * Method - mi_onmouseover_v
     *
     * On mouse over - vertical menu
     */
    mi_onmouseover_v: function( id )
    {
        document.getElementById( id + "_td_left" ).className = "left_over";
        document.getElementById( id + "_td_right" ).className = "right_over";

        var node = this.tree.findNode( id );
        var html = this.node2html_v(node);

        this.visibleLevel = node.attributes["level"] + 1;
        this.checkVisibleNodes();

        var divId = "ddm" + this.visibleLevel;
        var d = document.getElementById( divId );
        if (!d)
        {
            d = document.createElement("DIV");
            d.id = divId;
        }

        var scrollWidth1 = document.body.scrollWidth;

        d.innerHTML = html;

        document.body.appendChild( d );

        var tr = document.getElementById( id );
        var p = tr;
        var top = 0; var left = 0;
        while ( p )
        {
            top += p.offsetTop;
            left += p.offsetLeft;
            p = p.offsetParent;
        }

        d.style.position = "absolute";
        d.style.top = top - 5;
        d.style.left = left + tr.offsetWidth - 3;

        var scrollWidth2 = document.body.scrollWidth;

        if ( scrollWidth2 > scrollWidth1 ) d.style.left = left - 3;
    },




    /*
     * Method - mi_onmouseout_v
     *
     * On mouse out - vertical menu
     */
    mi_onmouseout_v: function( id )
    {
        document.getElementById( id + "_td_left" ).className = "left_out";
        document.getElementById( id + "_td_right" ).className = "right_out";

        this.visibleLevel = 1;

        if ( !this.checkInProgress )
        {
            this.checkInProgress = true;
            var funcName = this.name + ".checkVisibleNodes()";
            setTimeout( function() {eval( funcName )}, 1000 );
        }
    },




    /*
     * Method - checkVisibleNodes
     *
     * Check if it is time to hide menus
     */
    checkVisibleNodes: function()
    {
        for (var i=(this.visibleLevel+1); i < 10; i++)
        {
            var e = document.getElementById( "ddm" + i );
            if (e) document.body.removeChild( e );
        }

        this.checkInProgress = false;
    }

});

