var collapsedImg;
var expandedImg;
var endpointImg;


// object constructor for each outline entry
function dbRecord(mother,display,func,indent,statusMsg){
        
        this.mother = mother   // is this item a parent?
        this.display = display // text to display
        this.func = func       // link tied to text; if empty string, item appears as straight text
        this.indent = indent   // how many levels nested?
        this.statusMsg = statusMsg  // descriptive text for status bar 
        return this
}

// pre-load all images into cache


// **function that updates persistent storage of state**
// toggles an outline mother entry, storing new value in the cookie
function toggle(n) {
        if (n != 0) {
                var newString = ""
                var currState = getCurrState() // of whole outline
                var expanded = currState.charAt(n-1) // of clicked item
				
                newString += currState.substring(0,n-1)
                newString += expanded ^ 1 // Bitwise XOR clicked item
                newString += currState.substring(n,currState.length)
                setCurrState(newString) // write new state back to cookie
				updateItem(n, expanded ^ 1)
        }
}

function updateItem(n,expanded)
{
    if (document.all) 
	{
		if (document.all["div_"+n])
			document.all["div_"+n].style.display = parseInt(expanded)?"inline":"none";
		document.all["image_"+n].src = getGIF(n);
		OnContentStart()
	}

	
	else if (document.getElementById)
	{
		if (document.getElementById("div_"+n))
		{
			var l = document.getElementById("div_"+n);
			l.style.display = parseInt(expanded)?"inline":"none";
		//	l.style.visibility = parseInt(expanded)?"visible":"hidden";
			l.position="relative";
			l.display = "block";
			
		}
		document.getElementById("image_"+n).src = getGIF(n);
		OnContentStart()
	} 
	  			
}

function expandAll()
{
	var stat="";
	for (var i = 1;i< db.length;i++)
		stat+= "1";
	setCurrState(stat);
	
	if (document.all)  
	{
		//document.body.style.cursor = "wait";
        for (var j = 1;j< db.length;j++)
			updateItem(j,1)
		//document.body.style.cursor = "auto";
		return true;
	}
	return false;
}

function collapseAll()
{
	var stat="";
	for (var i = 1;i< db.length;i++)
		stat+= "0";
	setCurrState(stat);
	
	if (document.all )  
	{
		
		for (var j = 1;j< db.length;j++)
			updateItem(j,0)
		
		return true;
	}

	return false;
}

// **functions used in assembling updated outline**
// returns the proper GIF file name for each entry's control
function getGIF(n) 
{
        var mom = db[n].mother  // is entry a parent?
        var expanded = getCurrState().charAt(n-1) // of clicked item
        if (!mom) {
                return endpointWidget
        } else {
                if (expanded == 1) {
                        return expandedWidget
                }
        }
        return collapsedWidget
}

// returns the proper status line text based on the icon style
function getGIFStatus(n) 
{
        var mom = db[n].mother  // is entry a parent
        var expanded = getCurrState().charAt(n-1) // of rolled item
        if (!mom) return "No further items"
        else if (expanded == 1) return "Click to collapse nested items"
		return "Click to expand nested items"
}

function WriteTreeContent()
{
	var dynamic = ( document.all )//|| document.getElementById );
	//if (dynamic)  alert("dupa");
	var newOutline = ""
	if(db.length == 0)
	{
		if(WriteTreeContent.arguments[0] == "juris")
			newOutline = "<span class='"+ nodeStyle + "'>Pas de r&eacute;ponse pour cette lettre</span>";
		else
			newOutline = "<span class='"+ nodeStyle + "'>Pas de mot-cl&eacute; pour cette lettre</span>";
	}
	var prevIndentDisplayed = 0
	var showMyDaughter = 0
	// cycle through each entry in the outline array
	var i;
	max_indent=0;
	
    for (i = 1; i < db.length; i++) 
		max_indent = Math.max(max_indent,db[i].indent);
	
	var lastIndent = -1;
	lastEndpoint=1;

	for (i = 1; i < db.length; i++) 
	{
		var theGIF = getGIF(i) // get the image
		var theGIFStatus = getGIFStatus(i)  // get the status message
		var currIndent = db[i].indent           // get the indent level
		var expanded = parseInt(getCurrState().charAt(i-1)) // current state
		var endpoint=(theGIF == endpointWidget);
		// display entry only if it meets one of three criteria
		
		if (dynamic)
		{
			while (currIndent <= lastIndent)
			{
				if (!lastEndpoint || currIndent < lastIndent)
					newOutline += "</div>";
				lastIndent --; 
			}
			lastIndent = currIndent;
			newOutline += WriteTreeRow(i,1);
			if (!endpoint) 
				newOutline+="<div id=\"div_"+ i +"\" style=\"display: "+ ( expanded ? "inline" : "none")+ "\">";
			lastEndpoint = endpoint;	
		}
		else
		{
			if (currIndent == 0 || currIndent <= prevIndentDisplayed || (showMyDaughter == 1 && (currIndent - prevIndentDisplayed == 1)))
			{
				newOutline += WriteTreeRow(i,0);
				prevIndentDisplayed = currIndent ;
				showMyDaughter = expanded;				
			}
		}
		if (db.length > 25) 
		{
			document.write(newOutline);
			newOutline = "";
		}
	}
	
	if (dynamic)
	{
		while (currIndent <= lastIndent)
		{
			if (!lastEndpoint || currIndent < lastIndent)
				newOutline += "</div>";
			lastIndent --; 
		}
	}
	document.write(newOutline)

}


function WriteTreeRow(i,dynamic)
{
		var theGIF = getGIF(i) // get the image
		var theGIFStatus = getGIFStatus(i)  // get the status message
		var currIndent = db[i].indent           // get the indent level
		var expanded = parseInt(getCurrState().charAt(i-1)) // current state
		var endpoint=(theGIF == endpointWidget);
		
		var newOutline = "";

		newOutline+="<table border=\"0\" CELLPADDING=\"0\" CELLSPACING=\"0\" ><tr>";
		
		var pixIndent = (indentPixels * currIndent + 27);
		newOutline += "<td height=\""+lineHeight +"\" width=\""+pixIndent+"\"></td>";  
		newOutline += "<td valign=\"top\" WIDTH=" + widgetWidth + "><A HREF=\"#\" onMouseOver=\"window.status=\'" + theGIFStatus + "\';return true;\" onClick=\"toggle(" + i + "); " + ((theGIF != endpointWidget && !dynamic )?  " LoadContent('"+pageLink+"');" :" ") +" return false;\">"
		newOutline += "<IMG id=\"image_"+i+"\" SRC=\"" + theGIF + "\" HEIGHT=" + widgetHeight + " WIDTH=" + widgetWidth + " BORDER=0 vspace=\""+Math.floor((lineHeight-widgetHeight)/2) +"\"></A></td>"         
		newOutline += "<td valign=\"top\" width=\"9\"></td><td valign='middle' width=\""+(rightMargin - pixIndent - widgetWidth - 9) +"\"> ";
        if (db[i].func == "" || db[i].func == null) 
		{
			if (!endpoint && nodeStyle!="")
				newOutline += "<span class='"+ nodeStyle + "'>"+db[i].display+"</span>";   // no link, just a listed item  
			else if(endpoint && endpointStyle!="")
				newOutline += "<span class='"+ endpointStyle + "'>"+db[i].display+"</span>";   // no link, just a listed item  
			else	
				newOutline += db[i].display;   // no link, just a listed item  
		}
		else 
		{
			if (!endpoint && nodeStyle!="")
				newOutline += "<A HREF=\"#\" OnClick=\"" + db[i].func + ";return false;\" onMouseOver=\"window.status=\'" + db[i].statusMsg + "\';return true;\">" + "<span class='"+ nodeStyle + "'>"+db[i].display+"</span></a>";   // no link, just a listed item  
			else if(endpoint && endpointStyle!="")
				newOutline += "<A HREF=\"#\" OnClick=\"" + db[i].func + ";return false;\" onMouseOver=\"window.status=\'" + db[i].statusMsg + "\';return true;\">" + "<span class='"+ endpointStyle + "'>"+db[i].display+"</span></a>";   // no link, just a listed item  
			else
				newOutline += "<A HREF=\"#\" OnClick=\"" + db[i].func + ";return false;\" onMouseOver=\"window.status=\'" + db[i].statusMsg + "\';return true;\">" + db[i].display + "</A>"
		}
		newOutline+="</td></tr></table>";
		return newOutline;
}



// initialize 'current state' storage field
function InitTreeStatus(stat)
{
	stat=stat?"1":"0";
	if (getCurrState() == "" || getCurrState().length != (db.length-1)) 
	{
        initState = ""
        for (i = 1; i < db.length; i++) {
                initState += stat;
        }
        setCurrState(initState)
	}
	collapsedImg = new Image(widgetWidth,widgetHeight)
	collapsedImg.src = collapsedWidget
	expandedImg = new Image(widgetWidth,widgetHeight)
	expandedImg.src = expandedWidget
	endpointImg = new Image(widgetWidth,widgetHeight)
	endpointImg.src = endpointWidget
}


function OpenFaq(id,ord)
{  
   var wnd = window.top; 
   wnd.widTab = new Array();
   wnd.widCounter++;
   heig = screen.availHeight-30;
   wnd.widTab[0] = wnd.open("faq_tmpl.php?fid="+id+"&ord="+ord,"faq"+wnd.widCounter,"width=448,height="+heig+",top=0,left=0,resizable=1,scrollbars=1");
}

function OpenJuris(id)
{
	var wnd = window.top;
   wnd.widTab = new Array();
   wnd.widCounter++;
   heig = screen.availHeight-30;
	wnd.widTab[0] = wnd.open("juris_tmpl.php?aid="+id,"juris"+wnd.widCounter,"width=560,height="+heig+",top=0,left=0,resizable=1,scrollbars=1");
}
