var OPEN_ICON = "open.gif";
var CLOSE_ICON = "close.gif";
var FOLDER_ICON = "folder.gif";
var MENU_TITLE = "Example Menu";
var LINK_TARGET = "mainframe";

var toggleScript = "<script>function toggleMenu(id) { try {"+
"var e = document.getElementById(id);if (e == null) return;if (e.style.display != \"none\") {"+
"e.style.display = \"none\";var e = document.getElementById(\"sign\"+id).src = \""+OPEN_ICON+"\";"+		
"} else { e.style.display = \"block\";var e = document.getElementById(\"sign\"+id).src = \""+CLOSE_ICON+"\"; }"+
"} catch (exception) {} }</script>";

function objectTag() 
{
  	var sel = site.getSelection();

	if (sel.length == 0) 
	{
		//print("please select a root folder");
		return "error";
	}
	if (sel.length > 1)
	{
		//print("please select only one folder");
		return "error";
	}

	var folder = sel[0];
	if (DWfile.getAttributes(folder) != "D") // check if selection is a folder
	{
		//print("please select a folder");
		return "error";
	}
	
	var theFolder = new Folder(MENU_TITLE, folder);
	theFolder = list(theFolder);
	var active = dreamweaver.getDocumentPath("document");
	active = active.substring(0, active.lastIndexOf("/")+1);
  	var menu_tree = createTree(theFolder, active);
  	return toggleScript + menu_tree;
}

function list(folder) {	
	if (DWfile.getAttributes(folder.url) == "D") 
	{		
		var theFiles = DWfile.listFolder(folder.url);
		if (theFiles == null || theFiles.length < 1) return folder;	// don't operate on this folder if it contains no files
		//theFiles = theFiles.sort();	// uncomment to sort names lexically
		for (var i=0; i < theFiles.length; i++) 
		{
			var path = folder.url+"/"+theFiles[i];
			if (DWfile.getAttributes(path) == "D") // recurse if file is a folder
			{	
				if (theFiles[i] == "_notes") continue;	// leave Dreamweaver folders
				var f = new Folder(theFiles[i], path, theFiles[i]);
				folder.files[folder.files.length] = f;	// add new Folder
				list(f);
			} else {
				if (theFiles[i].indexOf("TMP") == 0) continue;	// leave temporary files
				var f = new File(theFiles[i], path, theFiles[i]);
				folder.files[folder.files.length] = f;	// add new File
			}
		}
	}
	return folder;
}

function createTree(folder, base) 
{

	theTree = "<div class='menu'>";
	traverse(folder, 0, base);
	
	function traverse(folder, id, base) 
	{
		if (arguments[1] == null) {id = "0";}
		theTree += folder.startTag
		theTree += "<a href='javascript:toggleMenu(\"menu"+id+"\")'>";
		if (folder.files.length >= 1) 
		{
			theTree += "<img src='"+CLOSE_ICON+"' border='0' id='signmenu"+id+"'>";
		}
		theTree += "<img src='"+FOLDER_ICON+"' border='0' id='foldermenu"+id+"'>";
		theTree += "<b>"+folder.name+"</b>";
		theTree += "</a>";
		
		theTree += "<div id='menu"+id+"' class='menuContainer'>";
		for (var i=0; i < folder.files.length; i++)
		{
			var f = folder.files[i];
			if (f.type == 2) 
			{
				traverse(f, id+"."+i, base);
			} else {
				var path = f.url.replace("|", ".");
				base = base.replace("|", "."); 
				path = f.url.replace( new RegExp(base), "");				
				theTree += "<a href='"+path+"' target='"+LINK_TARGET+"'>";
				theTree += f.startTag+f.name+f.endTag;
				theTree += "</a>";
			}
		}
		theTree += "</div>"; 	// enclonsing div menuContainer
		theTree += folder.endTag;
	}
	theTree += "</div>";
	return theTree;
}

/**
 * Two custom classes, File and Folder, are defined here. Using this two objects a complete directory
 * structure can be modeled. We're using custom objects in the code instead of direct translastion into 
 * HTML so that we can access the tree later. Or perhaps from a different application like Fireworks :)
 */
function File(name, url) 
{
	this.name = name;
	this.url = url;
}

File.prototype.type = 1;
File.prototype.startTag = "<div class='menufile'>";
File.prototype.endTag = "</div>";
File.prototype.toString = function () 
{
	return "File "+this.url;
};

function Folder(name, url, text) 
{
	this.name = name;
	this.url = url;
	this.files = new Array();
	this.text = text;
	this.extended = false;
}
Folder.prototype = File;
Folder.prototype.type = 2;
Folder.prototype.startTag = "<div class='menufolder'>";
Folder.prototype.endTag = "</div>";
Folder.prototype.toString = function () 
{
	return "#Folder "+this.url;
}

/** 
 * CSS used for the menu:
 * menuFile --> margin-left:16px	// style for files
 * menuFolder --> margin-left:15px	// style for folders
 * menuContainer --> 	// style for menu's folder 
 * menu --> // style for complete menu
 */