/* *********************************
ProjectName:	CP Menu
Name:	CP Menu Client script
Version:		0.4
Last Modified:	19/09/2004
By:				Benjamin
********************************* */

function CPMenu(Prefix,thePath)
{
    // bevat het laatste folderID dat geopend is
	this.LastFolder=0;
	// bevat het laatste folderID of LinkID dat aangeklikt is
    this.LastLink=0;
	// de 'naam' van dit menu
    this.Prefix=Prefix;
	// aantal links of folders gebruikt
    this.Counter=1;
	// geeft elk verzoek aan de server een volgnummer en negeer daarmee refreshing
	this.TransferID=0;
	// platte dataArray met alle gebruikte links
    this.Data=Array();
	// gebruikte link voor smartupdate
    this.ServerApp="listmenu.php";
	// geeft aan of er dynamicUpdate gebruikt wordt
	this.DynamicUpdate=true;
	// default target
	this.Target="content";
	// moeten er a tags gebruikt worden
	this.UseAnchors=false;
	// wat is de inspringbreedte
	this.FolderIndentWidth=14;
	this.LinkIndentWidth=14;

	// functiemappings
	this.loadlevel = loadlevel;
    this.add = add;
    this.closeFromTo = closeFromTo;
    this.openTo = openTo;
    this.openToUrl = openToUrl;
    this.drawlevel = drawlevel;
	this.oneFullNode = oneFullNode;
    this.oneElement = oneElement;
    this.showorhide = showorhide;
    this.setSelectedLink = setSelectedLink;
    this.NavigateTo = NavigateTo;
    this.getMainDiv = getMainDiv;
    this.getIDNumber = getIDNumber;
    this.getID = getID;
    
}

// vraagt 1 level op bij de server
function loadlevel(theId,thePath)
{
    // Als het niet de root betreft kan het pijltje een zandlopertje worden 
    if (theId>0)
    {
        myTD=document.getElementById("S" + this.Prefix + theId );
        myTD.className="waiting";
    }
    // Window een url geven, resultaat is scriptuitvoer,
    // de functie drawlevel word aangeroepen.
    myFrame=document.getElementById("menurefresh");
	this.TransferID++;
    myFrame.src=this.ServerApp+"?level="+theId+"&prefix="+this.Prefix+"&path="+thePath+"&transferid="+this.TransferID;

}

// menuentry toevoegen
		// ### ### change
function add(theNewId,theParent,theTitle,theUrl,thePath,theTarget)
{
	myNewId=theNewId;
	if (myNewId==0 && this.DynamicUpdate==true)
	{
		//alert(this.Counter);
		myNewId=this.Counter;
		this.Counter++;
	}
	myTarget=theTarget;
	if (theTarget=="")
	{
		myTarget=this.Target
	}

	this.Data[myNewId]=new Array(myNewId,theParent,theTitle,theUrl,thePath,myTarget);

}

// sluit alle menutjes tot aan de ouder van het nieuw gekozen menu of tot aan de root 
function closeFromTo(ToID)
{
    //alert("closeFromTo("+ToID+")");
    var myParent;
    var myCurrentLast;
    
    if (this.LastFolder>0)
    {
        FromID=this.LastFolder;
    }
    else
    {
        FromID="";
    }
    if ( ToID != "" && FromID != "" && ToID != FromID && document.getElementById( "C"+this.Prefix+FromID).innerHTML!="")
    {
        myParent=ToID;
        myCurrentLast=FromID;
        do
        {
            if (myCurrentLast) 
            {
                this.showorhide(myCurrentLast, "",0);
                myCurrentLast=this.getID(document.getElementById( "C"+this.Prefix+myCurrentLast ).parentNode.parentNode.id);
            }

        }
        while ( myParent!=myCurrentLast && myCurrentLast != "0" && myCurrentLast);
    }
}

// opent alle relevante menutjes totdat het path of url bereikt is 
function openToUrl(ToUrl)
{
	var vindId;
	vindId=0;
	for (ikk=1;ikk<this.Data.length ;ikk++ )
	{
		//alert(this.Data[ikk][3]);
		if (String(this.Data[ikk][3]).toLowerCase() == String(ToUrl).toLowerCase() || String(this.Data[ikk][4]).toLowerCase() == String(ToUrl).toLowerCase())
		{
			vindId=ikk;
		}
	}
	if (vindId>0)
	{
		this.openTo(vindId);
	}
}

// opent alle relevante menutjes totdat het ID bereikt is 
function openTo(ToID)
{
	//alert("closeFromTo("+ToID+")");
    todoArr=new Array();
    var myCurrentLast;
    myCurrentLast=ToID;
    if (this.Data[myCurrentLast])
    {
        this.closeFromTo(0);
        do
        {
            if (myCurrentLast) 
            {
                //todoArr.push(myCurrentLast);
	            mycount=todoArr.length;
	            todoArr[mycount]=myCurrentLast;
				
                myCurrentLast=this.Data[myCurrentLast][1];
            }

        }
        while ( myCurrentLast != "0" && myCurrentLast);
		for (ill=(todoArr.length-1); ill>=0 ; ill--)
		{
			this.drawlevel(todoArr[ill],this.Data[todoArr[ill]][4],0);
		}
	}
}

// tekent 1 nieuw level
function drawlevel(theId,thePath,theTransferID)
{
	if (theTransferID==this.TransferID || this.DynamicUpdate==false)
	{
		this.oneFullNode(theId,thePath);
	}
	
	
}

function oneFullNode(theId,thePath)
{
    //alert("oneFullNode("+theId+","+thePath+")");
    var myDiv;
    var currentLink;
    var tmpCell;
    var myCell;
    var myText;
    var myParentId;
    var myCurrentLast;
    var myCancel;
	//var myMenu;
	var myTmpMenu;
    
	//myMenu=this.Data[theId];
    theTempID=theId;
    // 0 toevoegen bij root
    if (theTempID == 0) theTempID="";
    // hierin wordt het nieuwe item geladen
    myDiv=document.getElementById(this.Prefix+theTempID);
    // hiermee weet het script de volgende keer dat hij deze moet opruimen
    // eerst eventuele menus dichtklappen
    
    myParentId = this.getID(myDiv.parentNode.id);
    if (document.getElementById( "C"+this.Prefix+myParentId ))
    {
        myCurrentLast=this.getID(document.getElementById( "C"+this.Prefix+myParentId ).id);
        myCancel=false;
        do
        {
            if (myCurrentLast==this.LastFolder) 
            {
                
                myCancel=true;
            }
            myCurrentLast=this.getID(document.getElementById( "C"+this.Prefix+myCurrentLast ).parentNode.parentNode.id);

        }
        while ( myCurrentLast != "0" && myCurrentLast);
    }
    if (myParentId && !myCancel) this.closeFromTo(myParentId)
    // nieuwe menuitem
    var myNewDiv="";
    if(theId > 0)
        {
        // ## geen rootnode
        //textindent van ouder
        myCell=document.getElementById("S"+this.Prefix+theId);
        myText=document.getElementById("T"+this.Prefix+theId).innerHTML;
        myIndent=new String(myCell.style.width);
        myIndent = parseInt(myIndent.substring(0,(myIndent.length-2))) ;
        // menuitem opnieuw tekenen
        if (myDiv.parentNode.id!="" )
            {
			myTmpMenu=this.Data[theId];
            myNewDiv = this.oneElement(theId, myIndent, myText, thePath, myTmpMenu[3], myTmpMenu[5], 1);
            }
        }
    else
        {
        // ## wel rootnode
        //textindent van ouder
        myIndent= 3
        // 0 toevoegen bij root
        theId=0;
        // menuitem opnieuw tekenen
        myNewDiv = "<div name='" + this.Prefix + theId + "' id='" + this.Prefix + theId + "' ><div>";
        }
    // verzamelDIV voor sub-menuitems
    if (myDiv.parentNode.id!="" || theId=="0") myNewDiv += "<div class='opensub' name='C" + this.Prefix + theId + "' id='C" + this.Prefix + theId + "'>";

	myTmpDiv="";
	for (i=1; i<this.Data.length; i++)
    {
        if (this.Data[i][4]=="" && this.Data[i][1]==theId)
        {
			// sub-menuitem tekenen (links)
			myNewDiv += this.oneElement(this.Data[i][0], ( myIndent + this.LinkIndentWidth ), this.Data[i][2], "", this.Data[i][3], this.Data[i][5], 0 );
        }
        if (this.Data[i][4]!="" && this.Data[i][1]==theId)
        {
			// sub-menuitem tekenen (folders)
			myTmpDiv += this.oneElement(this.Data[i][0], ( myIndent + 1 + this.FolderIndentWidth), this.Data[i][2], this.Data[i][4], this.Data[i][3], this.Data[i][5],  0 );
        }
    }
	myNewDiv += myTmpDiv;

	if (myDiv.parentNode.id!="" || theId=="0") myNewDiv += "</div>";

    
    if (myDiv.parentNode.id=="" && theId!="0")
    {
        document.getElementById("C"+myDiv.id).innerHTML=myNewDiv;
    }
    else
    {
        newdiv = document.createElement("div");
        newdiv.innerHTML=myNewDiv;
        myDiv.parentNode.replaceChild(newdiv, myDiv);
    }

    currentLink = document.getElementById("T"+this.Prefix+theId);
    if (currentLink)
    {
        currentLink.className="textselect";
    }


    // text deselecteren
    tmpCell=document.getElementById("T"+this.Prefix+this.LastLink);
    if (tmpCell && theId!=this.LastLink) tmpCell.className="menutext";
    // onthouden dat dit item geselecteerde text heeft
    this.LastLink=theId;
    // onthouden dat dit het laatstgetekende menu is
    if (document.getElementById( "C"+this.Prefix+theId ).innerHTML!="") 
    {
        this.LastFolder=theId;
    }

}

function oneElement(theId, theIndent, theText, thePath, theUrl, theTarget,  doesExist)
{
    tmpString =  "";
    tmpString += "<div ";
    tmpString += "name='" + this.Prefix + theId + "' id='" + this.Prefix + theId + "' ";
    // als het path niet leeg is hij er uit zien als een folder
    if (thePath != "")
    {
        tmpString += "class='menufolder' ";
    }
    else
    {
        doesExist = 0;
        tmpString += "class='menulink' ";
    }
    tmpString += ">";
    tmpString += "<table id='O" + this.Prefix + theId + "' border='0' width='100%'  cellpadding='0' cellspacing='0'";
    // als het path niet leeg is moet je hem kunnen openklikken
    if (thePath != "")
    {
        tmpString += "onClick='";
        // Bestaat het item al?
        if (doesExist == 0 )
		{
			if (this.DynamicUpdate==true)
			{
				// er word een subfolder geschreven
				tmpString += this.Prefix + ".loadlevel(" + theId + ",\"" + thePath + "\"); ";
			} 
			else
			{
				// er word een subfolder geschreven
				tmpString += this.Prefix + ".drawlevel(" + theId + ",\"" + thePath + "\",0); ";
			}
		}
        else
        {
            // de folder wordt opnieuw getekend
            tmpString += this.Prefix +".showorhide(" + theId + ",\"" + thePath + "\",1); ";
        }
        if (theUrl != null && theUrl != "" )
        {
            if (this.UseAnchors==false) tmpString += this.Prefix +".NavigateTo(" + theId + ",\""+theUrl+"\",\"" + theTarget + "\"); ";
        }
        tmpString += "' ";
    }
    else
    {
        // er word een link geklikt
        tmpString += "onClick='";
        tmpString += this.Prefix + ".setSelectedLink(" + theId + "); ";
        if (theUrl != "")
        {
            if (this.UseAnchors==false) tmpString += this.Prefix +".NavigateTo(0,\""+theUrl+"\",\"" + theTarget + "\"); ";
        }
        tmpString += "' ";
    }
    tmpString += "cellpadding='0' cellspacing='0'><tr>";
    // cell met pijltje maken
    tmpString += "<td ";

    tmpString += "style='vertical-align:top; background-position:90% 0%; background-position:" + (theIndent-13) + " 2;";
    tmpString += "width:" + theIndent + "px; ";
    tmpString += "' ";
    if (doesExist == 0 )
    {
        // er word een subfolder geschreven (dus dicht)
        tmpString += "class='closed' ";
    }
    else
    {
        // de folder wordt opnieuw getekend  (dus open)
        tmpString += "class='opened' ";
    }
    tmpString += "name='S" + this.Prefix + theId + "' id='S" + this.Prefix + theId + "' >";
    tmpString += "&nbsp;</td>";
    // cell met text maken
    tmpString += "<td class='menutext' name='T" + this.Prefix + theId + "' id='T" + this.Prefix + theId + "' ";
    tmpString += ">";
    if (this.UseAnchors==true) tmpString += "<a href='"+theUrl+"' target='" + theTarget + "' >";
    tmpString += "" + theText + "";
    if (this.UseAnchors==true) tmpString += "</a>";
    tmpString += "</td></tr></table>";
    tmpString += "</div>";
    return tmpString;
}

function showorhide(theId, thePath,byUser)
{
    //alert("function showorhide("+theId+","+thePath+","+byUser+")");
    var myDiv;
    var mySpan;
    var currentLink;
    var isAlt;
    var myParent;
    var myCurrentLast;
    var myCancel;
    currentLink = document.getElementById("T"+this.Prefix+theId);
    // wil de gebruiker een refresh?
    myDiv=document.getElementById("C" + this.Prefix + theId );
    if(byUser && window.event && this.DynamicUpdate==true /*&& myDiv.style.display!="none" */) isAlt=window.event.altKey; 
    else isAlt=0;
    if (isAlt) // && false
    {
            if (currentLink)
            {
                currentLink.className="textselect";
            }
        this.closeFromTo(this.getID(myDiv.parentNode.parentNode.id));
        myDiv.innerHTML="";
        this.LastFolder=theId;
        this.loadlevel(theId,thePath);
            myDiv.style.display="";
        mySpan=document.getElementById("S" + this.Prefix + theId );
            mySpan.className="opened";

    }
    else
    {
        // cell waar pijltje in staat
                    

        mySpan=document.getElementById("S" + this.Prefix + theId );
        if ( myDiv.style.display=="none" && byUser==1 )
        {
            // openen
            document.getElementById("T"+this.Prefix+this.LastLink).className="menutext";
            this.LastLink=theId;

            if (byUser==1 )
            {
                // eerst andere menus sluiten
                myCurrentLast=this.getID(document.getElementById( "C"+this.Prefix+theId ).parentNode.parentNode.id);
                myCancel=false;
                do
                {
                    if (myCurrentLast==this.LastFolder) 
                    {
                        
                        myCancel=true;
                    }
                    myCurrentLast=this.getID(document.getElementById( "C"+this.Prefix+myCurrentLast ).parentNode.parentNode.id);

                }
                while ( myCurrentLast != "0" && myCurrentLast);
                if (!myCancel) this.closeFromTo(this.getID(myDiv.parentNode.parentNode.id));
            }
            myDiv.style.display="";
            mySpan.className="opened";
            if (currentLink)
            {
                currentLink.className="textselect";
            }
        }
        else
        {
            // sluiten
            myDiv.style.display="none";
            mySpan.className="closed";
        }
        // onthouden dat dit het laatstgetekende menu is
        // hiermee weet het script de volgende keer dat hij deze moet opruimen
        if (document.getElementById( "C"+this.Prefix+theId ).innerHTML!="") 
        {
            this.LastFolder=theId;
        }
        
    }
}

function setSelectedLink(theId)
{
//  alert("setSelectedLink("+theId+")");
    var currentLink;
    var tmpCell;
    currentLink = document.getElementById("T"+this.Prefix+theId);
    tmpCell=document.getElementById("T"+this.Prefix+this.LastLink);
    if (tmpCell) tmpCell.className="menutext";
    if (currentLink)
    {
        currentLink.className="textselect";
    }
    this.LastLink=theId;

}

// browsen naar een link
function NavigateTo(theId,theUrl, theTarget)
{
    var myCell;
    var myCancel=false
    if (theId>0)
    {
        myCell=document.getElementById("S"+this.Prefix+theId);
        if (myCell.className=="closed") myCancel=true;
    }

    //alert(myCancel);
    if (!myCancel)
    {
        if (theTarget!="" && window.top.frames[theTarget] )
        {
            window.top.frames[theTarget].location.href=theUrl;
        }
        else
        {
            window.open(theUrl,theTarget)
        }
    }

}

// eerste karakter weghalen van sub-ID
function getMainDiv(theId)
{
    tmpStr=new String(theId);
    return tmpStr.substr(1);
}

// nummer verkrijgen van ID
function getIDNumber(theId)
{
    tmpStr=new String(theId);
    return tmpStr.substr(this.Prefix.length);
}

// nummer verkrijgen van sub-ID
function getID(theId)
{
    return this.getIDNumber(this.getMainDiv(theId));
}


