// размер квадратиков
var nImageSize = 10;
var strAll;
var isDrawn = 0;
// ключ активной записи
var strCurrent = "";

// массив с картинками
//элемент папки
var ImageList = new Array();
ImageList[0] = new Image();
ImageList[0].src = "images/menumarker1.gif";
//закрытая папка
ImageList[1] = new Image();
ImageList[1].src = "images/menumarker1.gif";
//открытая папка
ImageList[2] = new Image();
ImageList[2].src = "images/menumarker1act.gif";
//empty
ImageList[3] = new Image();
ImageList[3].src = "images/spacer.gif";

ImageList[4] = new Image();
ImageList[4].src = "images/menumarker1act.gif";

ImageList[5] = new Image();
ImageList[5].src = "images/menumarker1act.gif";

//-- модификация

ImageList[6] = new Image();
ImageList[6].src = "images/menumarker2.gif";

ImageList[7] = new Image();
ImageList[7].src = "images/menumarker2act.gif";

ImageList[8] = new Image();
ImageList[8].src = "images/menumarker3.gif";

ImageList[9] = new Image();
ImageList[9].src = "images/menumarker3act.gif";


// индексы картинок
var nItem = 0;
var nItem1 = 0;
var nItem2 = 6;
var nItem3 = 8;

var nPlus = 1;
var nPlus1 = 1;
var nPlus2 = 6;
var nPlus3 = -1;

var nMinus = 2;
var nMinus1 = 2;
var nMinus2 = 7;

var nEmpty = 3;

var actImage = 4;
var actImage1 = 4;
var actImage2 = 7;
var actImage3 = 9;

var actImageD = 5;
var actImageD1 = 5;
var actImageD2 = 7;


var activeItemKey = '';
var ck = document.cookie;
comma = ck.indexOf(';');
while( comma >= 0 || ck.length > 0 ) {
    if ( comma > 0 ) {
        ck_pair  =  ck.substr(0,comma);
        ck       = ck.substr(comma+2);
    }
    else {
    ck_pair  = ck;
    ck       = '';
    }
    equal    = ck_pair.indexOf('=');
    ck_name  = ck_pair.substr(0,equal);
    ck_value = ck_pair.substr(equal+1);
    if ( ck_name == 'ItemKey' ) {
    activeItemKey = ck_value;
    break;
    }
    comma    = ck.indexOf(';');
}


// добавление дочернего элемента к узлу
function node_addchild(new_node)
{
    if ( this.Item( new_node.Key ) != null )
    {
        //alert ( "Элемент с таким значением ключевого поля уже существует!!!" );
        return;
    }
    this.Items[this.ChildCount] = new_node;
    this.Items[this.ChildCount].Parent = this;
        
    this.ChildCount++;
    
    return true;
}

//и так понятно

function node_expand()
{
    if (this.Parent!=null)
     {
        if (!this.Parent.Expanded)
          {
            this.Parent.Expand();
          }
     }
    
    if (this.ChildCount != 0){
        this.Expanded = !this.Expanded;
        }
}

// возвращает объект по заданному ключу, если не найден то null
function node_item(strKey)
{
    var i, retValue;
    
    if (this.Key == strKey) {
        return this;
        }
    else
    {
        for ( i=0; i<this.ChildCount; i++ )
        {
            retValue = this.Items[i].Item( strKey );
            if ( retValue != null ){
                return retValue;
                }
        }
        
        if (i == this.ChildCount){
            return null;
            }
    }
}

//output head node
function node_paint(id,type)
{
    var i;
    strAll='';
    if (isDrawn==0)
    {
        document.write ('<span id="'+id+'">');
        document.write ('</span>');
//      isDrawn=1;
    }
    for ( i=0; i<this.ChildCount; i++ ){
        this.Items[i].PaintCh(0,type);
        }

    (document.getElementById(id)).innerHTML=strAll;
        
    if ( strCurrent != null && strCurrent != "" )   {
        ItemClick (strCurrent);
        }
    if ( activeItemKey != '' ) {
        for( var i = 6 ; i < activeItemKey.length ; i++ ) {
        workItemKey = activeItemKey.substr(0,i);
            Expand('',workItemKey);
            }
        }
}

// output node with children
function node_paint_child(nHistory,type)
{
    var strOutput = "", i;
        
    if ( this.Key != "" && this.Key != null )
    {
        strOutput += "<TABLE cellSpacing=\"2\" cellPadding=\"0\" border=\"0\" width=\"210\" class=\"submenubg\"><TBODY><TR>";
        
        //space
        for ( i=0; i<nHistory; i++ )
        {
            strOutput += "<td align=\"center\" valign=\"top\" style=\"padding-top: 10px;\" height=\"" + nImageSize + "\" width=\"" + nImageSize + "\"><img src=\"" + ImageList[nEmpty].src + "\" width=" + (nImageSize*2) + " height=" + nImageSize + "></td>";
        }

        if ( this.Active ) this.Expand();

        // plus minus
        if ( this.ChildCount != 0 ) 
            strOutput += "<td align=\"center\" valign=\"top\" style=\"padding-top: 10px; padding-left: 10px; padding-right: 3px;\" height=\"" + nImageSize + "\" width=\"" + nImageSize + "\" onClick=\"Expand(event,'" + this.Key + "');\"><img onClick=\"Expand(event,'" + this.Key + "');\" id=\"sgn" + this.Key + "\" src=\"" + ImageList[ this.Expanded ? eval("nMinus"+this.level):eval("nPlus"+this.level)  ].src + "\"></td>";
        else 
            strOutput += "<td align=\"center\" valign=\"top\" style=\"padding-top: 10px; padding-left: 10px; padding-right: 3px;\" height=\"" + nImageSize + "\" width=\"" + nImageSize + "\"><img id=\"sgn" + this.Key + "\" src=\"" + ImageList[this.Active ? eval("actImage"+this.level):eval("nItem"+this.level)].src + "\"></td>";
        
        strOutput += "</td>";
        // text
        strOutput += "<td style=\"align=\"left\" valign=\"middle\" height=\"" + nImageSize + "\">";
        if ( this.Document == null || this.Document == "" )
            strOutput += "<span onclick=\"Expand(event,'" + this.Key + "');\" id=\"txt" + this.Key + "\" style=\"cursor:hand;\" unselectable='on'>" + this.Text + "</span>";
        else
            //strOutput += ( (this.Active || type == 0) ? "<span class=\"menuunlink\" style=\"color: #597383;\">" + this.Active + this.Text + "</span>":"<a onClick=\"Expand(event,'" + this.Key + "'); return false;\" id=\"txt" + this.Key + "\" class=\"menulink\" target=\"" + this.Target + "\" href=\"" + this.Document + "\">" + this.Active + this.Text + "</a>");
              //strOutput += ( (type == 0) ? "<span class=\"menuunlink\" style=\"color: #597383;\">" + this.Text + "</span>":"<a onClick=\"Expand(event,'" + this.Key + "'); ItemClick('" + this.Key + "'); return false;\" id=\"txt" + this.Key + "\" class=\"menulink\" target=\"" + this.Target + "\" href=\"" + this.Document + "\">" + this.Text + "</a>");
             strOutput += ( (type == 0) ? "<span class=\"menuunlink\" style=\"color: #597383;\">" + this.Text + "</span>":"<a onClick=\"return Expand(event,'" + this.Key + "');\" id=\"txt" + this.Key + "\" class=\"menulink\" target=\"" + this.Target + "\" href=\"" + this.Document + "\">" + this.Text + "</a>");
        strOutput += "</td>";
        
        strOutput += "</tr></tbody></table>";
        
//*     strOutput += "</td></tr>";
    }
    strAll+=strOutput;
    
    // children (if need)
    if ( this.ChildCount > 0 )
    {
        strAll+=  "<div id=\"div" + this.Key + "\" style=\"DISPLAY: " + ( this.Expanded ? "block":"none" ) + "; VISIBILITY: " + ( this.Expanded ? "visible":"hidden" ) + "\">" ;
        
        for ( i=0; i<this.ChildCount; i++ )
            this.Items[i].PaintCh (nHistory+1);
        
        strAll+=  "</div>" ;
    }
}

// define new class node
function Node(strKey, strText, strTarget, strDocument, nImage, lvl, isAct)
{
    this.Active = isAct;
    this.level = lvl;
    //this.Image = nImage;      // prev image
    this.Key = strKey;          // key
    this.Text = strText;        // text which is displayed as link
    this.Target = strTarget;    // name of frame in which document must be loaded
    this.Document = strDocument;// name of document
    this.ChildCount = 0;        // count of children
    this.Parent = null;         // count of children
    //this.Expanded = true; // state of node
    this.Items = new Array();   // array of children
    
    // method
    this.Addchild = node_addchild;
    this.Expand = node_expand;
    this.Item = node_item;
    this.Paint = node_paint;
    this.PaintCh = node_paint_child;
}

//////////////////////
// handling methods //
//////////////////////
function Expand (event,strKey )
{
    if ( event ) event.cancelBubble = true;

    if ( strKey == '' ) return false;
        
    var nNode = Head.Item(strKey);
    
    nNode.Expand();

    if ( nNode.Expanded )
    {
        //alert(strKey);
        eval ( "if (document.getElementById(\"div" + strKey + "\")) document.getElementById(\"div" + strKey + "\").style.visibility = \"visible\"" );
        eval ( "if (document.getElementById(\"div" + strKey + "\")) document.getElementById(\"div" + strKey + "\").style.display = \"block\"" );
        eval ( "if (document.getElementById(\"sgn" + strKey + "\")) document.getElementById(\"sgn" + strKey + "\").src = \"" + ImageList[ eval('actImage'+nNode.level) ].src + "\"" );
        return true;
    }
    else
    {
        eval ( "if (document.getElementById(\"div" + strKey + "\")) document.getElementById(\"div" + strKey + "\").style.visibility = \"hidden\"" );
        eval ( "if (document.getElementById(\"div" + strKey + "\")) document.getElementById(\"div" + strKey + "\").style.display = \"none\"" );
        
        if (eval('nPlus'+nNode.level)!=-1) {
          eval ( "if (document.getElementById(\"sgn" + strKey + "\")) document.getElementById(\"sgn" + strKey + "\").src = \"" + ImageList[ eval('nPlus'+nNode.level) ].src + "\"" );
        } else {
          return true;
        }
        if (!eval("document.getElementById(\"div" + strKey + "\")")) return true;
        //if ( isNaN( parseInt(nNode.Image) ) )
        //  eval ( "document.getElementById(\"img" + strKey + "\").src = \"" + nNode.Image + "\"" );
        //else
        //  eval ( "document.getElementById(\"img" + strKey + "\").src = \"" + ImageList[nNode.Image].src + "\"" );
        
        //проверка а не надо ли переместится вверх по дереву
//      if ( strCurrent != null && strCurrent != "" && nNode.Expanded == false && nNode.Item (strCurrent) != null && nNode.Document != null && nNode.Document != "" )
//          ItemClick (strKey); 
    }
    
    return false;
    
}


