// This module requires Prototype 1.6.0.3+ and Scriptaculous 1.8.2

function Folder(resourceUID, folderName, description, accessLevel, folder_state)
{
	this.folderName = folderName;
	this.resourceName = folderName;
	this.resourceUID = resourceUID;
	this.description = description;
	this.description = this.description.replace(/<br><br>/g, "<br>");
	this.accessLevel = accessLevel;
	this.folder_state = folder_state;
	this.children_folders = new Array();
	this.children_resources = new Array();
	this.resourceType = "folder";
	this.objectType = "folder";
	this.parentResource = null;
}

Folder.prototype.addFolder = function(folderObj)
{
	folderObj.parentResource = this;
	this.children_folders[this.children_folders.length] = folderObj;
}

Folder.prototype.addResource = function(resourceObj)
{
	resourceObj.parentResource = this;
	this.children_resources[this.children_resources.length] = resourceObj;
}

Folder.prototype.findResource = function(resourceUID)
{
	var selectedResource = null;
	
	// loop through resources
	for(var i=0; i<this.children_resources.length; i++)
	{
		if(this.children_resources[i].resourceUID == resourceUID)
		{
			selectedResource = this.children_resources[i];
			break;
		}
	}
	// loop through folders
	if(!selectedResource)
	{
		for(var i=0; i<this.children_folders.length; i++)
		{
			if(this.children_folders[i].resourceUID == resourceUID)
			{
				selectedResource = this.children_folders[i];
				break;
			}
		}
	}
	// recurse through children
	if(!selectedResource)
	{
		for(var i=0; i<this.children_folders.length; i++)
		{
			selectedResource = this.children_folders[i].findResource(resourceUID);
			if(selectedResource)
				break;
		}
	}
	
	return selectedResource;
}

Folder.prototype.deleteResource = function(resourceUID)
{
	var selectedIndex = -1;
	var success = false;
	
	// loop through resources
	for(var i=0; i<this.children_resources.length; i++)
	{
		if(this.children_resources[i].resourceUID == resourceUID)
		{
			selectedIndex = i;
			break;
		}
	}
	if(selectedIndex >= 0)
	{
		this.children_resources.splice(selectedIndex, 1);
		success = true;
	}
	// loop through children
	if(!success)
	{
		for(var i=0; i<this.children_folders.length; i++)
		{
			if(this.children_folders[i].resourceUID == resourceUID)
			{
				selectedIndex = i;
				break;
			}
		}
		if(selectedIndex >= 0)
		{
			this.children_folders.splice(selectedIndex, 1);
			success = true;
		}
	}
	
	if(selectedIndex == -1)
	{
		for(var i=0; i<this.children_folders.length; i++)
		{
			success = this.children_folders[i].deleteResource(resourceUID);
			if(success)
			{
				break;
			}
		}
	}
	
	return success;
}
/*********** complete this **************/
Folder.prototype.swapResources = function(resourceUID_1, resourceUID_2)
{
	// find position of resource 1
	var res1_pos;
	for(var i=0; i<this.children_resources.length; i++)
	{
		if(this.children_resources[i].resourceUID == resourceUID_1)
		{
			res1_pos = i;
			break;
		}
	}
	
	// find position of resource 2
	var res1_pos2;
	for(var i=0; i<this.children_resources.length; i++)
	{
		if(this.children_resources[i].resourceUID == resourceUID_2)
		{
			res2_pos = i;
			break;
		}
	}
	
	if(typeof(res1_pos) == "number" && typeof(res2_pos) == "number")
	{
		var temp = this.children_resources[res1_pos];
		this.children_resources[res1_pos] = this.children_resources[res2_pos];
		this.children_resources[res2_pos] = temp;
	}
}










function Resource(resourceUID, resourceName, description, file_href, filesize, accessLevel, imgPreview)
{
	this.resourceName = resourceName;
	this.resourceUID = resourceUID;
	this.description = description;
	this.description = this.description.replace(/<br><br>/g, "<br>");
	//this.description = this.description.replace(/&nbsp;/g, " ");
	this.resourceSrc = file_href;
	this.filesize = filesize;
	this.accessLevel = accessLevel;
	this.imgPreview = imgPreview
	this.resourceType;
	this.objectType = "resource";
	this.parentResource = null;
	
	this.refreshResourceType();
}

Resource.prototype.updateResource = function(resourceName, description, file_href, filesize, accessLevel, imgPreview)
{
	this.resourceName = resourceName;
	this.description = description;
	this.description = this.description.replace(/<br><br>/g, "<br>");
	//this.description = this.description.replace("<br>", "\n");
	//this.description = this.description.replace("&nbsp;", " ");
	this.resourceSrc = file_href;
	this.filesize = filesize;
	this.accessLevel = accessLevel;
	this.imgPreview = imgPreview
	
	this.refreshResourceType();
}

Resource.prototype.refreshResourceType = function()
{
	resourceType = "";
	if(this.resourceSrc.indexOf("http://") == 0)
		this.resourceType = "href";
	else
	{
		var types = [
			["mp3", "audio"],
			["m3u", "audio"],
			["wma", "audio"],
			["xls", "excel"],
			["xlsx", "excel"],
			["csv", "excel"],
			["jpg", "image"],
			["jpeg", "image"],
			["tif", "image"],
			["tiff", "image"],
			["gif", "image"],
			["bmp", "image"],
			["pdf", "pdf"],
			["txt", "txt"],
			["doc", "word"],
			["docx", "word"],
			["ppt", "ppt"]
		];
		var extension = this.resourceSrc.substring(this.resourceSrc.lastIndexOf(".")+1, this.resourceSrc.length).toLowerCase();
		
		for(var i=0; i<types.length; i++)
		{
			if(types[i][0] == extension)
			{
				this.resourceType = types[i][1];
				break;
			}
		}
		if(this.resourceType == null)
			this.resourceType = "unknown";
	}
}





function Action(actionLabel, eventHandler)
{
	this.actionLabel = actionLabel;
	this.eventHandler = eventHandler;
}















function ResourceTree(root, containerId)
{
	var self = this;
	
	/******************************
	/ Custom User Settings
	/*****************************/
	// mode - determins how the tree behaves
	//   - download - the "public" mode where resources are displayed and intended to be downloaded
	//   - select - let the user select a particular resource and update a "hidden" input element (specified in the code)
	//   - manage - specify this mode to "manage" the tree (ie, select resources for action)   
	this.mode;
	this.mode_select_targetFieldId;
	this.enableSelect = false;
	
	// custom event actions
	this.onLoadResource;
	this.onManageResource;
	this.onDeleteResource;
	this.onSortResources;
	/*
	this.onNew_resource;
	this.onNew_folder;
	this.onModify_resource;
	this.onModify_folder;
	this.onDelete_resource;
	this.onDelete_folder;
	*/
	
	// custom actions via buttons
	this.actions = new Array();
	this.actionsContainer = document.createElement("div");
	this.actionsContainer.className = "actions";
	
	// accessLevel settings
	this.accessLevel_enabled = false;
	this.accessLevels;
	
	// styles
	this.treeWidth = "500px";
	this.treeHeight = "400px";
	
	// other settings
	this.topLevelLabel = "Resources";
	this.resourceUID_selected = null;
	
	
	
	/******************************
	/ Private settings
	/*****************************/
	
	// container is the overall container for our ResourceTree
	this.containerId = containerId;
	this.containerObj = document.getElementById(containerId);
	this.containerObj.className = "resourceTree_top";
	// treeObj is where the actual resource tree will be generated
	this.treeContainer = document.createElement("div");
	this.treeContainer.className = "resourceTree_tree";
	// root - very top of our logical resource tree
	this.root = root;
	// selectedResource - holds a reference to the currently selected resource node
	this.selectedResource = null;
	this.showResources = true;
	// splashObj - container for "splash" screen that overlays above the containerObj
	this.splashObj;
	
	// attach onload event to initiate tree
	this.addLoadEvent(function() { self.init(); });
};


ResourceTree.prototype.init = function()
{
	// build actions, if any
	if(this.actions.length > 0)
	{
		for(var i=0; i<this.actions.length; i++)
		{
			var button = document.createElement("input");
			button.type = "button";
			button.value = this.actions[i].actionLabel;
			button.rt_this = this;
			button.myAction = this.actions[i];
			button.onclick = function() {
				if(this.rt_this.selectedResource != null)
					this.myAction.eventHandler(this.rt_this.selectedResource.resourceUID, this.rt_this.selectedResource.resourceName); 
			};
			this.actionsContainer.appendChild(button);
		}
	}
	this.containerObj.appendChild(this.actionsContainer);
	if(this.mode == "download")
		this.actionsContainer.style.display = "none";
	// setup container styles
	this.containerObj.style.width = this.treeWidth;
	this.treeContainer.style.height = this.treeHeight;
	
	this.buildTree();
	
	// select initial resource if necessary
	if(typeof(this.resourceUID_selected) == "number")
	{
		var theResource = this.root.findResource(this.resourceUID_selected);
		if(theResource)
			this.updateSelectedResource(theResource);
	}
};


ResourceTree.prototype.findResource = function(resourceUID, currentNode)
{
	var theResource;
	if(!currentNode)
		currentNode = this.root;
	
	if(currentNode.resourceUID == resourceUID)
		return currentNode;
	else if(currentNode.resourceType == "folder")
	{
		// loop through resource children
		for(var i=0; i<currentNode.children_resources.length && theResource == null; i++)
			theResource = this.findResource(resourceUID, currentNode.children_resources[i]);
		if(theResource != null)
			return theResource;
		
		else
		{
			for(var i=0; i<currentNode.children_folders.length && theResource == null; i++)
				theResource = this.findResource(resourceUID, currentNode.children_folders[i]);
			if(theResource != null)
				return theResource;
			else
				return null;
		}
	}
	else
		return null;
	
	
}


ResourceTree.prototype.addAction = function(actionObj)
{
	this.actions[this.actions.length] = actionObj;
}


ResourceTree.prototype.enableAccessLevel = function(doEnable, accessLevels)
{
	if(doEnable)
	{
		this.accessLevel_enabled = true;
		this.accessLevels = accessLevels;
	}
	else
	{
		this.accessLevel_enabled = false;
		this.accessLevels = null;
	}
}


ResourceTree.prototype.buildTree = function()
{
	// clear out any old content
	this.treeContainer.innerHTML = "";
	this.selectedResource = null;
	
	// build tree
	var theTree = this.buildFolder(this.root);
	var ul = document.createElement("ul");
	ul.appendChild(theTree);
	this.treeContainer.appendChild(ul);
	this.containerObj.appendChild(this.treeContainer);
};



ResourceTree.prototype.buildFolder = function(folder)
{
	var li = document.createElement("li");
	li.id = this.containerId + "_resource_" + folder.resourceUID;
	if(folder.folder_state == "Closed" && this.resourceUID_selected != folder.resourceUID)
		li.className = "folder_closed resource_normal";
	else
		li.className = "folder_opened resource_normal";
		
	li.resourceObj = folder;
	li.title = folder.description;
		var fn = document.createElement("div");
		fn.id = "resource_" + folder.resourceUID + "_label";
		fn.className = "folderName";
		fn.appendChild(document.createTextNode(folder.folderName));
		fn.rt_this = this;
		fn.resourceUID = folder.resourceUID;
		fn.resourceObj = folder;
		fn.parentResource = folder.parentResource;
		//if(typeof(folder.resourceUID) == "number")
		//{
			fn.onclick = function() {
				if(this.rt_this.mode == "manage" || this.rt_this.enableSelect)
				{
					this.rt_this.updateSelectedResource(this.resourceObj);
					// don't ever close the children tree when in "manage" mode, only open
					if(document.getElementById(this.rt_this.containerId + "_resource_" + folder.resourceUID).className.indexOf("folder_closed") >= 0)
						this.rt_this.toggleFolder(this);
				}
				else
					this.rt_this.toggleFolder(this);
			};
		//}
		li.appendChild(fn);
		
		// children folders
		var ul = document.createElement("ul");
		ul.id = this.containerId + "_children_" + folder.resourceUID;
		// add any children folders
		if(folder.children_folders.length + folder.children_resources.length > 0)
		{
			if(folder.children_folders.length > 0)
			{
				for(var i=0; i<folder.children_folders.length; i++)
					ul.appendChild(this.buildFolder(folder.children_folders[i]));
				li.appendChild(ul);
			}
			if(!(this.enableSelect && !this.showResources))
			{
				if(folder.children_resources.length > 0)
					for(var i=0; i<folder.children_resources.length; i++)
						ul.appendChild(this.buildResource(folder.children_resources[i]));
			}
		}
		li.appendChild(ul);
		if(folder.folder_state == "Closed" && this.resourceUID_selected != folder.resourceUID)
			ul.style.display = "none";
		
	return li;
}



ResourceTree.prototype.buildResource = function(resource)
{
	var li = document.createElement("li");
	li.className = "resource " + resource.resourceType;
	li.id = this.containerId + "_resource_" + resource.resourceUID;
	li.rt_this = this;
	li.resourceObj = resource;
	li.title = resource.description;
	li.parentResource = resource.parentResource;
		var rn = document.createElement("div");
		rn.id = "resource_" + resource.resourceUID + "_label";
		rn.appendChild(document.createTextNode(resource.resourceName));
		li.appendChild(rn);
	li.resourceUID = resource.resourceUID;
	li.onclick = function() {
		if(this.rt_this.mode == "download")
			this.rt_this.getResourceDetails(this.resourceObj);
		else if(this.rt_this.mode == "manage" || this.rt_this.enableSelect)
			this.rt_this.updateSelectedResource(this.resourceObj);
	}
	return li;
}



ResourceTree.prototype.toggleMode = function()
{		
	this.mode = this.mode == "manage" ? "download" : "manage";
	
	// remove selectedResource if it's defined
	if(this.mode == "download")
	{
		this.actionsContainer.style.display = "none";
		if(this.selectedResource != null)
			this.updateSelectedResource(null);
	}
	else if(this.mode == "manage")
	{	
		this.actionsContainer.style.display = "";
		if(this.splashObj != null)
			this.toggleSplash();
	}
	else if(this.mode == "select")
	{
		this.actionsContainer.style.display = "";
		if(this.splashObj != null)
			this.toggleSplash();
	}
}



ResourceTree.prototype.toggleSplash = function()
{
	if(this.splashObj)
	{
		this.splashObj.parentNode.removeChild(this.splashObj);
		this.splashBkgObj.parentNode.removeChild(this.splashBkgObj);
		this.splashObj = null;
		this.splashBkgObj = null;
	}
	else
	{
		// splash bkg (must put on separate div due to ie translucent bug (ie, input not accepted for decendants of absolutely positioned elements with a progid:filter png applied to it)
		var splash_bkg = document.createElement("div");
		splash_bkg.id = this.containerId + "_theSplash_bkg";
		splash_bkg.className = "splash_bkg";
		splash_bkg.style.top = "0px";
		splash_bkg.style.left = "0px";
		splash_bkg.style.width = this.containerObj.clientWidth + "px";
		splash_bkg.style.height = this.containerObj.clientHeight + "px";
		this.splashBkgObj = splash_bkg;
		this.containerObj.appendChild(splash_bkg);
		
		var splash = document.createElement("div");
		splash.id = this.containerId + "_theSplash";
		splash.className = "splash";
		splash.style.top = "0px";
		splash.style.left = "0px";
		splash.style.width = this.containerObj.clientWidth + "px";
		splash.style.height = this.containerObj.clientHeight + "px";
		splash.style.overflow = "scroll";
		this.splashObj = splash;
		this.containerObj.appendChild(splash);
	}
};


ResourceTree.prototype.updateSelectedResource = function(resourceObj)
{
	// remove selection from previous resource
	if(this.selectedResource != null)
	{
		// remove management toolbox
		if(this.mode == "manage")
		{
			document.getElementById("resource_" + this.selectedResource.resourceUID + "_tools").parentNode.removeChild(document.getElementById("resource_" + this.selectedResource.resourceUID + "_tools"));
		}
		this.jscss("remove", document.getElementById("resource_" + this.selectedResource.resourceUID + "_label"), "selected");
		if(this.selectedResource.objectType == "folder")
		{
			var theResource = document.getElementById(this.containerId + "_resource_" + this.selectedResource.resourceUID);
			if(theResource)
				this.jscss("swap", theResource, "resource_selected", "resource_normal");
		}
	}
	
	// assign new selected resource
	this.selectedResource = resourceObj;
	if(this.enableSelect)
	{
		if(this.mode_select_targetFieldId.length > 0)
			document.getElementById(this.mode_select_targetFieldId).value = resourceObj ? resourceObj.resourceUID : "";
		/*if(this.selectedResource != null)
		{
			this.jscss("add", document.getElementById("resource_" + resourceObj.resourceUID + "_label"), "selected");
			if(resourceObj.objectType == "folder")
			{
				var theResource = document.getElementById(this.containerId + "_resource_" + resourceObj.resourceUID);
				if(theResource)
					this.jscss("swap", theResource, "resource_normal", "resource_selected");
			}
		}*/
	}
	
	if(this.selectedResource != null)
	{
		this.jscss("add", document.getElementById("resource_" + resourceObj.resourceUID + "_label"), "selected");
		if(resourceObj.objectType == "folder")
		{
			var theResource = document.getElementById(this.containerId + "_resource_" + resourceObj.resourceUID);
			if(theResource)
				this.jscss("swap", theResource, "resource_normal", "resource_selected");
		}
		
		// build toolbox
		if(this.mode == "manage")
		{
			var tools = document.createElement("div");
			tools.id = "resource_" + this.selectedResource.resourceUID + "_tools";
			tools.className = "toolbox";
			
			if(this.accessLevel_enabled && typeof(resourceObj.resourceUID) == "number")
			{
				var accessLevel_div = document.createElement("div");
				accessLevel_div.innerHTML = "(access = " + resourceObj.accessLevel + ")";
				accessLevel_div.style.cssFloat = "right";
				accessLevel_div.style.styleFloat = "right";
				accessLevel_div.style.color = "#666666";
				tools.appendChild(accessLevel_div);
			}
			
			if(this.selectedResource.objectType == "folder")
			{
				var createFolder = document.createElement("span");
				createFolder.rt_this = this;
				createFolder.className = "tool";
				createFolder.style.padding = "0px 5px"
				createFolder.innerHTML = "[Create Sub-Folder]";
				createFolder.onclick = function() {
					this.rt_this.action_manageResource(null, "folder", resourceObj);
				};
				tools.appendChild(createFolder);
				
				if(typeof(this.selectedResource.resourceUID) == "number")
				{
					var modify = document.createElement("span");
					modify.rt_this = this;
					modify.resourceObj = resourceObj;
					modify.className = "tool";
					modify.style.padding = "0px 5px"
					modify.innerHTML = "[Modify]";
					modify.onclick = function() {
						this.rt_this.action_manageResource(this.resourceObj);
					};
					tools.appendChild(modify);
					/*
					var deleteItem = document.createElement("span");
					deleteItem.rt_this = this;
					deleteItem.resourceObj = resourceObj;
					deleteItem.className = "tool";
					deleteItem.innerHTML = "&nbsp;[Delete Folder]&nbsp;";
					deleteItem.onclick = function() {
						this.rt_this.action_deleteResource(this.resourceObj);
					};
					tools.appendChild(deleteItem);
					*/
				}
				
				var createResource = document.createElement("span");
				createResource.rt_this = this;
				createResource.className = "tool";
				createResource.style.padding = "0px 5px"
				createResource.style.whiteSpace = "nowrap";
				createResource.innerHTML = "[Add Resource]";
				createResource.onclick = function() {
					this.rt_this.action_manageResource(null, "resource", resourceObj);
				};
				tools.appendChild(createResource);
				
				if(typeof(resourceObj.resourceUID) == "number")
				{
					var deleteItem = document.createElement("span");
					deleteItem.rt_this = this;
					deleteItem.resourceObj = resourceObj;
					deleteItem.className = "tool";
					deleteItem.style.padding = "0px 5px"
					deleteItem.style.whiteSpace = "nowrap";
					deleteItem.innerHTML = "[Delete Folder]";
					deleteItem.onclick = function() {
						this.rt_this.action_deleteResource(this.resourceObj);
					};
					tools.appendChild(deleteItem);
				}
				
				var sortContents = document.createElement("span");
				sortContents.rt_this = this;
				sortContents.resourceObj = resourceObj;
				sortContents.className = "tool";
				sortContents.style.padding = "0px 5px"
				sortContents.style.whiteSpace = "nowrap";
				sortContents.innerHTML = "[Sort Resources]";
				sortContents.onclick = function() {
					this.rt_this.action_sortResources("resource", this.resourceObj);
				};
				tools.appendChild(sortContents);
				
				var sortContents_folder = document.createElement("span");
				sortContents_folder.rt_this = this;
				sortContents_folder.resourceObj = resourceObj;
				sortContents_folder.className = "tool";
				sortContents_folder.style.padding = "0px 5px"
				sortContents_folder.style.whiteSpace = "nowrap";
				sortContents_folder.innerHTML = "[Sort Folders]";
				sortContents_folder.onclick = function() {
					this.rt_this.action_sortResources("folder", this.resourceObj);
				};
				tools.appendChild(sortContents_folder);
			}
			else if(this.selectedResource.objectType == "resource")
			{
				var view = document.createElement("span");
				view.rt_this = this;
				view.resourceObj = resourceObj;
				view.className = "tool";
				view.style.padding = "0px 5px"
				view.innerHTML = "[View]";
				view.title = resourceObj.resourceSrc;
				view.onclick = function() {
					window.open(this.resourceObj.resourceSrc);
				};
				tools.appendChild(view);
				
				var modify = document.createElement("span");
				modify.rt_this = this;
				modify.resourceObj = resourceObj;
				modify.className = "tool";
				modify.style.padding = "0px 5px"
				modify.innerHTML = "[Modify]";
				modify.onclick = function() {
					this.rt_this.action_manageResource(this.resourceObj);
				};
				tools.appendChild(modify);
				
				var deleteItem = document.createElement("span");
				deleteItem.rt_this = this;
				deleteItem.resourceObj = resourceObj;
				deleteItem.className = "tool";
				deleteItem.style.padding = "0px 5px"
				deleteItem.innerHTML = "[Delete]";
				deleteItem.onclick = function() {
					this.rt_this.action_deleteResource(this.resourceObj);
				};
				tools.appendChild(deleteItem);
			}
			this.insertAfter(tools, document.getElementById("resource_" + this.selectedResource.resourceUID + "_label"));
			//document.getElementById("resource_" + this.selectedResource.resourceUID).appendChild(tools);
		}
	}
}


ResourceTree.prototype.toggleFolder = function(folderObj)
{
	var resource = document.getElementById(this.containerId + "_resource_" + folderObj.resourceUID);
	var children = document.getElementById(this.containerId + "_children_" + folderObj.resourceUID);
	
	var oldStatus = resource.className.indexOf("folder_opened") >= 0 ? "opened" : "closed";
	
	var newStatus;
	if(oldStatus == "opened")
		newStatus = "closed";
	else if(oldStatus == "closed")
		newStatus = "opened";
	
	this.jscss("swap", resource, "folder_" + oldStatus, "folder_" + newStatus);
	children.style.display = newStatus == "opened" ? "" : "none";
};


ResourceTree.prototype.getResourceDetails = function(resourceObj)
{
	this.toggleSplash();
	this.splashObj.appendChild(document.createTextNode("Loading resource details..."));
	
	this.getResourceDetails_onDone(resourceObj);
	/*
	// dynamic load
	var iframe_nav = this.iframe_request();
	var href = this.onLoadResource;
	
	href = this.href_appendQueryString(href, "id", 3);
	href = this.href_appendQueryString(href, "dId", this.rand());
	this.getFrame(iframe_nav).location.href = href;
	*/
}

ResourceTree.prototype.getResourceDetails_onDone = function(resourceObj)
{
	this.splashObj.innerHTML = "";
	var splash_content = document.createElement("div");
	splash_content.className = "splashContent";
		// title
		var splash_title = document.createElement("div");
		splash_title.className = "title";
		splash_title.style.paddingBottom = "10px";
		splash_title.appendChild(document.createTextNode(resourceObj.resourceName + " - "));
			if(resourceObj.resourceType != "href")
			{
				var splash_size = document.createElement("span");
				splash_size.style.fontSize = "14px";
				var filename = resourceObj.resourceSrc.split("/")[resourceObj.resourceSrc.split("/").length-1];
				splash_size.appendChild(document.createTextNode(filename + " (" + resourceObj.filesize + ")"));
				splash_title.appendChild(splash_size);
			}
		splash_content.appendChild(splash_title);
		// image preview
		if(resourceObj.imgPreview.length > 0)
		{
			var splash_imgPreview = document.createElement("img");
			splash_imgPreview.src = resourceObj.imgPreview;
			splash_imgPreview.setAttribute("align", "left");
			splash_imgPreview.style.margin = "2px 8px";
			splash_imgPreview.style.border = "2px solid black";
			splash_content.appendChild(splash_imgPreview);
		}
		// description
		var splash_desc = document.createElement("div");
		splash_desc.className = "description";
		splash_desc.innerHTML = resourceObj.description;
		splash_content.appendChild(splash_desc);
		// buttons
		var splash_buttonContainer = document.createElement("div");
		splash_buttonContainer.style.cssText = "text-align:center; padding-top:20px;";
			var splash_cancelButton = document.createElement("a");
			splash_cancelButton.appendChild(document.createTextNode("Cancel"));
			splash_cancelButton.href = "javascript:void(0);";
			splash_cancelButton.className = "splashButton";
			splash_cancelButton.rt_this = this;
			splash_cancelButton.onclick = function() {
				this.rt_this.toggleSplash();
			};
			splash_buttonContainer.appendChild(splash_cancelButton);
			var splash_downloadButton = document.createElement("a");
			splash_downloadButton.className = "splashButton";
			splash_downloadButton.innerHTML = "Download";
			splash_downloadButton.href = resourceObj.resourceSrc;
			splash_downloadButton.target = "_blank";
			splash_downloadButton.rt_this = this;
			splash_downloadButton.onclick = function() {
				this.rt_this.toggleSplash();
			};
			splash_buttonContainer.appendChild(splash_downloadButton);
		splash_content.appendChild(splash_buttonContainer);
	this.splashObj.appendChild(splash_content);
}










ResourceTree.prototype.action_manageResource = function(resourceObj, resourceType, parentResource)
{
	this.toggleSplash();
	
	var targetIframeId = this.containerId + "_manageResource_iframe";
	var actionLabel, titleLabel, accessLevel_max;
	var onAction = this.onManageResource;
	onAction = this.href_appendQueryString(onAction, "parentResource", resourceObj ? (resourceObj.parentResource ? resourceObj.parentResource.resourceUID : "") : parentResource.resourceUID);
	
	// action = create
	if(resourceObj == null)
	{
		onAction = this.href_appendQueryString(onAction, "manageType", "create");
		actionLabel = "Create New ";
		if(resourceType == "folder")
		{
			actionLabel += "Folder";
			titleLabel = "Folder Name";
		}
		else if(resourceType == "resource")
		{
			actionLabel += "Resource";
			titleLabel = "Resource Name";
		}
	}
	// action = modify
	else
	{
		resourceType = resourceObj.objectType;
		onAction = this.href_appendQueryString(onAction, "manageType", "modify");
		onAction = this.href_appendQueryString(onAction, "resourceUID", resourceObj.resourceUID);
		actionLabel = "Modify ";
		if(resourceObj.objectType == "folder")
		{
			actionLabel += "Folder";
			titleLabel = "Folder Name";
		}
		else if(resourceObj.objectType == "resource")
		{
			actionLabel += "Resource";
			titleLabel = "Resource Name";
		}
	}
		
	var container_top = document.createElement("div");
	container_top.style.padding = "10px";
		var theTitle = document.createElement("div");
		theTitle.style.cssText = "font-size:18px; font-weight:bold;";
		theTitle.innerHTML = actionLabel;
		container_top.appendChild(theTitle);
		var theForm = document.createElement("form");
		theForm.id = this.containerId + "_manageResource_form";
		theForm.name = theForm.id;
		if(resourceType == "resource") {
			theForm.enctype = "multipart/form-data";
			theForm.encoding = "multipart/form-data";
		}
		theForm.containerId = this.containerId;
		theForm.action = onAction;
		theForm.target = targetIframeId;
		theForm.method = "post";
		theForm.style.overflow = "auto";
		theForm.displayResourceContainer = function(resourceType) {
			if(resourceType == "href")
			{
				document.getElementById(this.containerId + "_manageResource_row_href").style.display = "";
				document.getElementById(this.containerId + "_manageResource_row_file").style.display = "none";
			}
			else if(resourceType == "file")
			{
				document.getElementById(this.containerId + "_manageResource_row_href").style.display = "none";
				document.getElementById(this.containerId + "_manageResource_row_file").style.display = "";
			}
		};
			var theTable = document.createElement("table");
			theTable.style.width = "95%";
			theTable.border = 0;
				var row_resTitle = theTable.insertRow(-1);
					var c1 = row_resTitle.insertCell(-1);
					c1.width = "125px";
						var c1_label = document.createElement("label");
						c1_label.setAttribute("for", this.containerId + "_manageResource_title");
						c1_label.innerHTML = titleLabel + ":";
						c1.appendChild(c1_label);
					var c2 = row_resTitle.insertCell(-1);
						var input_title = document.createElement("input");
						input_title.type = "text";
						input_title.id = this.containerId + "_manageResource_title";
						input_title.name = "resourceName";
						input_title.style.width = "95%";
						//input_title.style.overflow = "auto";
						c2.appendChild(input_title);
				var row_resDesc = theTable.insertRow(-1);
				row_resDesc.style.verticalAlign = "top";
					var c1 = row_resDesc.insertCell(-1);
						var c1_label = document.createElement("label");
						c1_label.setAttribute("for", this.containerId + "_manageResource_description");
						c1_label.innerHTML = "Description:";
						c1.appendChild(c1_label);
					var c2 = row_resDesc.insertCell(-1);
						var input_description = document.createElement("textarea");
						input_description.rows = 6;
						input_description.style.width = "95%";
						input_description.id = this.containerId + "_manageResource_description";
						input_description.name = "resourceDesc";
						c2.appendChild(input_description);
				if(resourceType == "folder")
				{
					var row_folderState = theTable.insertRow(-1);
					row_folderState.style.verticalAlign = "top";
						var c1 = row_folderState.insertCell(-1);
							var c1_label = document.createElement("label");
							c1_label.setAttribute("for", this.containerId + "_manageResource_folderState");
							c1_label.innerHTML = "Initial Folder State:";
							c1.appendChild(c1_label);
						var c2 = row_folderState.insertCell(-1);
							var select_folderState = document.createElement("select");
							select_folderState.id = this.containerId + "_manageResource_folderState";
							select_folderState.name = "folder_state";
							select_folderState.size = 1;
							select_folderState.options[select_folderState.options.length] = new Option("Opened", "Opened");
							select_folderState.options[select_folderState.options.length] = new Option("Closed", "Closed");
							c2.appendChild(select_folderState);
				}
				if(this.accessLevel_enabled)
				{
					var row_access = theTable.insertRow(-1);
					row_access.style.verticalAlign = "top";
						var c1 = row_access.insertCell(-1);
							var c1_label = document.createElement("label");
							c1_label.setAttribute("for", this.containerId + "_manageResource_accessLevel");
							c1_label.innerHTML = "Access Level:";
							c1.appendChild(c1_label);
						var c2 = row_access.insertCell(-1);
							var select_access = document.createElement("select");
							select_access.id = this.containerId + "_manageResource_accessLevel";
							select_access.name = "accessLevel";
							select_access.size = 1;
							// populate access level select box
							for(var i=0; i<this.accessLevels.length; i++)
							{
								// new resource
								if(resourceObj == null)
								{
									if(typeof(parentResource.resourceUID) != "number" || this.accessLevels[i][0] >= parentResource.accessLevel)
										select_access.options[select_access.options.length] = new Option(this.accessLevels[i][0] + " - " + this.accessLevels[i][1], this.accessLevels[i][0]);
								}
								// existing resource
								else
								{
									if(typeof(resourceObj.parentResource.resourceUID) != "number" || this.accessLevels[i][0] >= resourceObj.parentResource.accessLevel)
									{
										select_access.options[select_access.options.length] = new Option(this.accessLevels[i][0] + " - " + this.accessLevels[i][1], this.accessLevels[i][0]);
									}
								}
							}
							c2.appendChild(select_access);
							if(resourceType == "folder")
							{
								var accessNote = document.createElement("div");
								accessNote.style.cssText = "font-style:italic; color:#444444; font-size:.9em;";
								accessNote.innerHTML = "Note: reducing the Access Level of this folder will also reduce the Access Level of any of its sub-resources with a level higher than the one you specify here.";
								c2.appendChild(accessNote);
							}
				}		
				if(resourceType == "resource")
				{
					var row_resType = theTable.insertRow(-1);
						var c1 = row_resType.insertCell(-1);
							var c1_label = document.createElement("label");
							c1_label.setAttribute("for", this.containerId + "_manageResource_resType");
							c1_label.innerHTML = "Resource Type:";
							c1.appendChild(c1_label);
						var c2 = row_resType.insertCell(-1);
							var select_srcType = document.createElement("select");
							select_srcType.id = this.containerId + "_manageResource_resType";
							select_srcType.name = "srcType";
							select_srcType.size = 1;
							select_srcType.idPrefix = this.containerId + "_manageResource_row_";
							select_srcType.theForm = theForm;
							select_srcType.onchange = function() {
								this.theForm.displayResourceContainer(this.value);
							};
							select_srcType.options[select_srcType.options.length] = new Option("Hyperlink", "href");
							select_srcType.options[select_srcType.options.length] = new Option("File", "file");
							c2.appendChild(select_srcType);
							
					var row_resFile = theTable.insertRow(-1);
					row_resFile.id = this.containerId + "_manageResource_row_file";
						var c1 = row_resFile.insertCell(-1);
							var c1_label = document.createElement("label");
							c1_label.setAttribute("for", this.containerId + "_manageResource_file");
							c1_label.innerHTML = "File:";
							c1.appendChild(c1_label);
						var c2 = row_resFile.insertCell(-1);
							// file options container
							var fileOptions_container = document.createElement("div");
							fileOptions_container.id = this.containerId + "_fileOptions";
							fileOptions_container.style.display = "none";
								if(resourceObj != null && resourceObj.resourceType != "href")
								{
									var fileOptions_view = document.createElement("a");
									fileOptions_view.innerHTML = "View File";
									fileOptions_view.target = "_blank";
									fileOptions_view.href = resourceObj.resourceSrc;
									fileOptions_container.appendChild(document.createTextNode("["));
									fileOptions_container.appendChild(fileOptions_view);
									fileOptions_container.appendChild(document.createTextNode("] "));
								
									var fileOptions_upload = document.createElement("a");
									fileOptions_upload.innerHTML = "Replace Existing File";
									fileOptions_upload.href = "javascript:void(0);";
									fileOptions_upload.theForm = theForm;
									fileOptions_upload.containerId = this.containerId;
									fileOptions_upload.onclick = function() {
										document.getElementById(this.containerId + "_fileOptions").style.display = "none";
										document.getElementById(this.containerId + "_fileUpload").style.display = "";
									};
									fileOptions_container.appendChild(document.createTextNode("["));
									fileOptions_container.appendChild(fileOptions_upload);
									fileOptions_container.appendChild(document.createTextNode("]"));
								c2.appendChild(fileOptions_container);
							}
							// upload container
							var fileUpload_container = document.createElement("div");
							fileUpload_container.id = this.containerId + "_fileUpload";
							fileUpload_container.style.display = "none";
								var input_resourceSrc_file = document.createElement("input");
								input_resourceSrc_file.type = "file";
								input_resourceSrc_file.id = this.containerId + "_manageResource_file";
								input_resourceSrc_file.name = "resourceSrc";
								input_resourceSrc_file.setAttribute("name", "resourceSrc");
								input_resourceSrc_file.style.width = "95%";
								fileUpload_container.appendChild(input_resourceSrc_file);
							c2.appendChild(fileUpload_container);
					
					// hyperlink
					var row_resHref = theTable.insertRow(-1);
					row_resHref.id = this.containerId + "_manageResource_row_href";
						var c1 = row_resHref.insertCell(-1);
							var c1_label = document.createElement("label");
							c1_label.setAttribute("for", this.containerId + "_manageResource_href");
							c1_label.innerHTML = "Hyperlink:";
							c1.appendChild(c1_label);
						var c2 = row_resHref.insertCell(-1);
							var input_resourceSrc_href = document.createElement("input");
							input_resourceSrc_href.type = "text";
							input_resourceSrc_href.style.width = "95%";
							input_resourceSrc_href.id = this.containerId + "_manageResource_href";
							input_resourceSrc_href.name = "resourceSrc";
							c2.appendChild(input_resourceSrc_href);
					
					// image preview
					var row_imgPreview = theTable.insertRow(-1);
					row_imgPreview.id = this.containerId + "_manageResource_row_imgPreview";
						var c1 = row_imgPreview.insertCell(-1);
							var c1_label = document.createElement("label");
							c1_label.setAttribute("for", this.containerId + "_manageResource_imgPreview");
							c1_label.innerHTML = "Preview Image:";
							c1.appendChild(c1_label);
						var c2 = row_imgPreview.insertCell(-1);
							// image preview options container
							var imgPreviewOptions_container = document.createElement("div");
							imgPreviewOptions_container.id = this.containerId + "_imgPreviewOptions";
							if(resourceObj != null && typeof(resourceObj.imgPreview) == "string" && resourceObj.imgPreview.length > 0)
							{
								var imgPreviewOptions_view = document.createElement("a");
								imgPreviewOptions_view.innerHTML = "View Image";
								imgPreviewOptions_view.target = "_blank";
								imgPreviewOptions_view.href = resourceObj.imgPreview;
								imgPreviewOptions_container.appendChild(document.createTextNode("["));
								imgPreviewOptions_container.appendChild(imgPreviewOptions_view);
								imgPreviewOptions_container.appendChild(document.createTextNode("] "));
							
								var imgPreviewOptions_upload = document.createElement("a");
								imgPreviewOptions_upload.innerHTML = "Replace Existing File";
								imgPreviewOptions_upload.href = "javascript:void(0);";
								imgPreviewOptions_upload.theForm = theForm;
								imgPreviewOptions_upload.containerId = this.containerId;
								imgPreviewOptions_upload.onclick = function() {
									document.getElementById(this.containerId + "_imgPreviewOptions").style.display = "none";
									document.getElementById(this.containerId + "_imgPreviewUpload").style.display = "";
								};
								imgPreviewOptions_container.appendChild(document.createTextNode("["));
								imgPreviewOptions_container.appendChild(imgPreviewOptions_upload);
								imgPreviewOptions_container.appendChild(document.createTextNode("]"));
							}
							c2.appendChild(imgPreviewOptions_container);
							
							// image preview upload container
							var imgPreviewUpload_container = document.createElement("div");
							imgPreviewUpload_container.id = this.containerId + "_imgPreviewUpload";
							//imgPreviewUpload_container.style.display = "none";
								var input_resourceSrc_imgPreview = document.createElement("input");
								input_resourceSrc_imgPreview.type = "file";
								input_resourceSrc_imgPreview.style.width = "95%";
								input_resourceSrc_imgPreview.id = this.containerId + "_manageResource_imgPreview";
								input_resourceSrc_imgPreview.name = "imgPreview";
								imgPreviewUpload_container.appendChild(input_resourceSrc_imgPreview);
								var imgComment = document.createElement("span");
								imgComment.style.cssText = "font-style:italic; color:#666666;";
								imgComment.innerHTML = "&nbsp;&nbsp;(Note: image file type must be JPEG)";
								imgPreviewUpload_container.appendChild(imgComment);
							c2.appendChild(imgPreviewUpload_container);
				}
			theForm.appendChild(theTable);
			var div_buttons = document.createElement("div");
			div_buttons.style.textAlign = "center";
			div_buttons.style.paddingTop = "10px";
				var c1_resourceType = document.createElement("input");
				c1_resourceType.type = "hidden";
				c1_resourceType.name = "resourceType";
				c1_resourceType.value = resourceType;
				div_buttons.appendChild(c1_resourceType);
				var c1_cancel = document.createElement("input");
				c1_cancel.type = "button";
				c1_cancel.value = "Cancel";
				c1_cancel.rt_this = this;
				c1_cancel.onclick = function() {
					this.rt_this.toggleSplash();
				};
				div_buttons.appendChild(c1_cancel);
				var c1_submit = document.createElement("input");
				c1_submit.type = "submit";
				c1_submit.value = "Submit";
				c1_submit.targetIframeId = targetIframeId;
				c1_submit.containerId = this.containerId;
				c1_submit.onclick = function() {
					var errors = [];
					
					if(document.getElementById(this.containerId + "_manageResource_title").value.length == 0)
						errors[errors.length] = "You must specify a Resource Name";
					
					var imgPreview = document.getElementById(this.containerId + "_manageResource_imgPreview").value;
					if(imgPreview.length > 0)
					{
						var ext = imgPreview.substr(imgPreview.lastIndexOf(".")+1).toLowerCase();
						if(!(ext == "jpg" || ext == "jpeg"))
							errors[errors.length] = "If you specify an image preview, it must be of file type jpg";
					}
					
					if(errors.length > 0)
					{
						var errorMessage = "The following errors were encountered:";
						
						for(var i=0; i<errors.length; i++)
							errorMessage += "\n  - " + errors[i];
						alert(errorMessage);
						return false;
					}
					else
						return true;
				}
				div_buttons.appendChild(c1_submit);
			theForm.appendChild(div_buttons);
		container_top.appendChild(theForm);
	
	// populate form
	if(resourceObj != null)		// modify resource
	{
		input_title.value = resourceObj.resourceName;
		var newDesc = resourceObj.description.replace(/<br>/g, "\n");
		newDesc = newDesc.replace(/&nbsp;/, " ");
		input_description.value = newDesc;
		if(this.accessLevel_enabled)
		{
			select_access.value = resourceObj.accessLevel;
		}
		if(resourceType == "resource")
		{
			if(resourceObj.resourceType == "href")
			{
				select_srcType.value = "href";
				input_resourceSrc_href.value = resourceObj.resourceSrc;
				fileUpload_container.style.display = "";
			}
			else
			{
				select_srcType.value = "file";	
				input_resourceSrc_href.value = "http://";		// default to empty href, in case they switch to web resource
				fileOptions_container.style.display = "";		// display file upload input
			}
			
			// image preview
			if(resourceObj.imgPreview.length > 0)
			{
				imgPreviewOptions_container.style.display = "";
				imgPreviewUpload_container.style.display = "none";
			}
			else
			{
				imgPreviewOptions_container.style.display = "none";
				imgPreviewUpload_container.style.display = "";
			}
		}
		else if(resourceType == "folder")
		{
			select_folderState.value = resourceObj.folder_state;
		}
	}
	else		// new resource
	{
		if(resourceType == "resource")
		{
			input_resourceSrc_href.value = "http://";		// default to empty href
			fileUpload_container.style.display = "";		// show the file upload input
			imgPreviewOptions_container.style.display = "none";
			imgPreviewUpload_container.style.display = "";
		}
	}
	
	// setup target iframe
	if(!document.getElementById(targetIframeId))
	{
		var iframe_loader = document.createElement("iframe");
		if(!debugger_showIframe)
			iframe_loader.style.display = "none";
		iframe_loader.name = targetIframeId;
		iframe_loader.id = targetIframeId;
		document.body.appendChild(iframe_loader);
		this.getFrame(targetIframeId).name = targetIframeId;
	}
	
	this.splashObj.appendChild(container_top);
	
	if(resourceType == "resource")
		select_srcType.onchange();
	
	input_title.focus();
}




ResourceTree.prototype.action_manageResource_onDone = function(manageType, newResourceObj, parentResource)
{
	this.buildTree();
	this.toggleSplash();
	
	this.updateSelectedResource();
}




ResourceTree.prototype.action_deleteResource = function(resourceObj)
{
	var infoMessage;
	if(resourceObj.objectType == "resource")
		infoMessage = "Are you sure you want to delete this resource?";
	else if(resourceObj.objectType == "folder")
		infoMessage = "Deleting this folder will remove all resources and sub-folders within it.  Are you sure you want to delete this resource?";
	
	if(window.confirm(infoMessage))
	{
		var iframe = this.iframe_request();
		var href = this.onDeleteResource;
		href = this.href_appendQueryString(href, "resourceUID", resourceObj.resourceUID);
		href = this.href_appendQueryString(href, "dId", this.rand());
		this.getFrame(iframe).location.href = href;
	}
}



ResourceTree.prototype.action_deleteResource_onDone = function(resourceUID)
{
	var resourceObj = this.findResource(resourceUID);
	var success = this.root.deleteResource(resourceUID);
	
	if(success)
	{
		var victim = document.getElementById(this.containerId + "_resource_" + resourceUID);
		victim.parentNode.removeChild(victim);
		this.selectedResource = null;
	}
}


ResourceTree.prototype.action_sortResources = function(resourceType, parentResourceObj)
{
	var resourceArray;
	if(resourceType == "resource")
		resourceArray = parentResourceObj.children_resources;
	else if(resourceType == "folder")
		resourceArray = parentResourceObj.children_folders;
		
	if(resourceArray.length == 0)
	{
		alert("There must be at least two items in a folder to sort them.  Please choose a folder containing more items.");
		return;
	}
	
	this.toggleSplash();
	var container_top = document.createElement("div");
	container_top.style.width = "90%";
	container_top.style.margin = "20px auto 0px auto";
	container_top.style.border = "1px dotted #999999";
	container_top.style.backgroundColor = "#eeeeee";
	container_top.style.padding = "10px";
		// title
		var title = document.createElement("div");
		title.style.cssText = "font-size:larger; font-weight:bold;";
		title.innerHTML = "Sort Resources";
		container_top.appendChild(title);
		// instructions
		var instructions = document.createElement("div");
		instructions.style.padding = "10px 5px";
		instructions.innerHTML = "Drag the items below in the order you want them displayed.  Click on the [Save Changes] button once you are finished.";
		container_top.appendChild(instructions);
		// resource tree
		var resourceTree = document.createElement("div");
		resourceTree.className = "resourceTree_tree";
		resourceTree.style.paddingLeft = "30px"
		resourceTree.style.border = "1px dotted #aaaaaa";
		resourceTree.style.backgroundColor = "#f9f9f9";
			var ul = document.createElement("ul");
			ul.id = this.containerId + "_sorter";
			for(var i=0; i<resourceArray.length; i++)
			{
				var res = resourceArray[i];
				li = document.createElement("li");
				li.id = "sort_li_" + res.resourceUID;
				li.resourceUID = res.resourceUID;
				li.className = "resource " + (resourceType == "folder" ? "folder_closed" : res.resourceType);
				li.style.cursor = "move";
				li.innerHTML = res.resourceName;
				ul.appendChild(li);
			}
			resourceTree.appendChild(ul);
		container_top.appendChild(resourceTree);
		// buttons
		var div_buttons = document.createElement("div");
		div_buttons.style.textAlign = "center";
		div_buttons.style.paddingTop = "10px";
			// parent resourceUID
			var input_parentResource = document.createElement("input");
			input_parentResource.type = "hidden";
			input_parentResource.name = "parentResource";
			input_parentResource.value = parentResourceObj.resourceUID;
			div_buttons.appendChild(input_parentResource);
			// cancel button
			var input_cancel = document.createElement("input");
			input_cancel.type = "button";
			input_cancel.value = "Cancel";
			input_cancel.rt_this = this;
			input_cancel.onclick = function() {
				this.rt_this.toggleSplash();
			};
			div_buttons.appendChild(input_cancel);
			// submit button
			var input_submit = document.createElement("input");
			input_submit.resourceList = ul;
			input_submit.rt_this = this;
			input_submit.parentResourceObj = parentResourceObj;
			input_submit.type = "submit";
			input_submit.value = "Save Changes";
			input_submit.onclick = function() {
				this.rt_this.action_sortResources_do(this.resourceList, this.parentResourceObj.resourceUID);
			};
			div_buttons.appendChild(input_submit);
		container_top.appendChild(div_buttons);
		
	this.splashObj.appendChild(container_top);
	
	Sortable.create(this.containerId + "_sorter");
}




ResourceTree.prototype.action_sortResources_do = function(resourceList, parentResourceUID)
{
	var resourcesOrder = "";
	var resources = resourceList.getElementsByTagName("li");
	for(var i=0; i<resources.length; i++)
	{
		resourcesOrder = this.list_append(resourcesOrder, resources[i].id.replace("sort_li_", ""));
	}
	
	var iframeId = this.iframe_request();
	var href = this.onSortResources;
	href = this.href_appendQueryString(href, "resourcesOrder", resourcesOrder);
	href = this.href_appendQueryString(href, "parentResourceUID", parentResourceUID);
	href = this.href_appendQueryString(href, "dId", rand());
	this.getFrame(iframeId).location.href = href;
}


if(typeof(HTMLElement) != "undefined")
{
	if(typeof(Node) != "undefined")
	{
		Node.prototype.swapNode = function (node) {
		  var nextSibling = this.nextSibling;
		  var parentNode = this.parentNode;
		  node.parentNode.replaceChild(this, node);
		  parentNode.insertBefore(node, nextSibling);  
		}
	}
}

ResourceTree.prototype.action_sortResources_onDone = function(resources, parentResourceUID)
{
	var ul = document.getElementById(this.containerId + "_children_" + parentResourceUID);
	// loop through each new resource
	
	for(var i=0; i<resources.length; i++)
	{
		var newPos = i;
		var resourceUID_current = resources[i];
		var resources_old = ul.getElementsByTagName("li");
		// loop through old resources to re-arrange
		for(var j=0; j<resources_old.length; j++)
		{
			if(resources_old[j].resourceUID == resourceUID_current)
			{
				oldPos = j;
				resources_old[oldPos].swapNode(resources_old[newPos]);
				// swap internal data structure
				var parentObj = this.findResource(parentResourceUID);
				parentObj.swapResources(resourceUID_current, resources_old[j].resourceUID);
			}
		}
	}
	this.toggleSplash();
	alert("Resources successfully sorted.");
	
}
/*
ResourceTree.prototype.action_sortResources = function(parentResourceObj)
{
	if(parentResourceObj.children_resources.length == 0)
	{
		alert("There are no resources to sort in this folder!  Please choose a folder containing resources.");
		return;
	}
	
	this.toggleSplash();
	var container_top = document.createElement("div");
	container_top.style.width = "90%";
	container_top.style.margin = "20px auto 0px auto";
	container_top.style.border = "1px dotted #999999";
	container_top.style.backgroundColor = "#eeeeee";
	container_top.style.padding = "10px";
		// title
		var title = document.createElement("div");
		title.style.cssText = "font-size:larger; font-weight:bold;";
		title.innerHTML = "Sort Resources";
		container_top.appendChild(title);
		// instructions
		var instructions = document.createElement("div");
		instructions.style.padding = "10px 5px";
		instructions.innerHTML = "Drag the items below in the order you want them displayed.  Click on the [Save Changes] button once you are finished.";
		container_top.appendChild(instructions);
		// resource tree
		var resourceTree = document.createElement("div");
		resourceTree.className = "resourceTree_tree";
		resourceTree.style.paddingLeft = "30px"
		resourceTree.style.border = "1px dotted #aaaaaa";
		resourceTree.style.backgroundColor = "#f9f9f9";
			var ul = document.createElement("ul");
			ul.id = this.containerId + "_sorter";
			for(var i=0; i<parentResourceObj.children_resources.length; i++)
			{
				var res = parentResourceObj.children_resources[i];
				li = document.createElement("li");
				li.id = "sort_li_" + res.resourceUID;
				li.resourceUID = res.resourceUID;
				li.className = "resource " + res.resourceType;
				li.style.cursor = "move";
				li.innerHTML = res.resourceName;
				ul.appendChild(li);
			}
			resourceTree.appendChild(ul);
		container_top.appendChild(resourceTree);
		// buttons
		var div_buttons = document.createElement("div");
		div_buttons.style.textAlign = "center";
		div_buttons.style.paddingTop = "10px";
			// parent resourceUID
			var input_parentResource = document.createElement("input");
			input_parentResource.type = "hidden";
			input_parentResource.name = "parentResource";
			input_parentResource.value = parentResourceObj.resourceUID;
			div_buttons.appendChild(input_parentResource);
			// cancel button
			var input_cancel = document.createElement("input");
			input_cancel.type = "button";
			input_cancel.value = "Cancel";
			input_cancel.rt_this = this;
			input_cancel.onclick = function() {
				this.rt_this.toggleSplash();
			};
			div_buttons.appendChild(input_cancel);
			// submit button
			var input_submit = document.createElement("input");
			input_submit.resourceList = ul;
			input_submit.rt_this = this;
			input_submit.parentResourceObj = parentResourceObj;
			input_submit.type = "submit";
			input_submit.value = "Save Changes";
			input_submit.onclick = function() {
				this.rt_this.action_sortResources_do(this.resourceList, this.parentResourceObj.resourceUID);
			};
			div_buttons.appendChild(input_submit);
		container_top.appendChild(div_buttons);
		
	this.splashObj.appendChild(container_top);
	
	dragsort.makeListSortable(document.getElementById(this.containerId + "_sorter"));
}




ResourceTree.prototype.action_sortResources_do = function(resourceList, parentResourceUID)
{
	var resourcesOrder = "";
	var resources = resourceList.getElementsByTagName("li");
	for(var i=0; i<resources.length; i++)
	{
		resourcesOrder = this.list_append(resourcesOrder, resources[i].id.replace("sort_li_", ""));
	}
	
	var iframeId = this.iframe_request();
	var href = this.onSortResources;
	href = this.href_appendQueryString(href, "resourcesOrder", resourcesOrder);
	href = this.href_appendQueryString(href, "parentResourceUID", parentResourceUID);
	href = this.href_appendQueryString(href, "dId", rand());
	this.getFrame(iframeId).location.href = href;
}

if(typeof(Node) != "undefined")
{
	Node.prototype.swapNode = function (node) {
	  var nextSibling = this.nextSibling;
	  var parentNode = this.parentNode;
	  node.parentNode.replaceChild(this, node);
	  parentNode.insertBefore(node, nextSibling);  
	}
}


ResourceTree.prototype.action_sortResources_onDone = function(resources, parentResourceUID)
{
	var ul = document.getElementById(this.containerId + "_children_" + parentResourceUID);
	// loop through each new resource
	
	for(var i=0; i<resources.length; i++)
	{
		var newPos = i;
		var resourceUID_current = resources[i];
		var resources_old = ul.getElementsByTagName("li");
		// loop through old resources to re-arrange
		for(var j=0; j<resources_old.length; j++)
		{
			if(resources_old[j].resourceUID == resourceUID_current)
			{
				oldPos = j;
				resources_old[oldPos].swapNode(resources_old[newPos]);
				// swap internal data structure
				var parentObj = this.findResource(parentResourceUID);
				parentObj.swapResources(resourceUID_current, resources_old[j].resourceUID);
			}
		}
	}
	this.toggleSplash();
	alert("Resources successfully sorted.");
	
}
*/
























ResourceTree.prototype.findChildNode = function(parentNode, via_tagName, via_className)
{
	var use_tagName = typeof(via_tagName) != "undefined";
	var use_className = typeof(via_className) != "undefined";
	var theNode;
	
	for(var i=0; i<parentNode.childNodes.length; i++)
	{
		// not a text node
		if(parentNode.childNodes[i].nodeType != 3)
		{
			if((use_tagName && parentNode.childNodes[i].tagName == via_tagName.toUpperCase()  || !use_tagName) && 
				 (use_className && (new RegExp("\b?" + via_className + "\b?")).test(parentNode.childNodes[i].className) || !use_className))
			{
				theNode = parentNode.childNodes[i];
				break;
			}
		}
	}
	return theNode;
};




ResourceTree.prototype.findChildNodes = function(parentNode, via_tagName, via_className)
{
	var use_tagName = typeof(via_tagName) != "undefined";
	var use_className = typeof(via_className) != "undefined";
	var theNodes = new Array();
	
	for(var i=0; i<parentNode.childNodes.length; i++)
	{
		// not a text node
		if(parentNode.childNodes[i].nodeType != 3)
		{
			if((use_tagName && parentNode.childNodes[i].tagName == via_tagName.toUpperCase()  || !use_tagName) && 
				 (use_className && (new RegExp("\b?" + via_className + "\b?")).test(parentNode.childNodes[i].className) || !use_className))
			{
				theNodes[theNodes.length] = parentNode.childNodes[i];
			}
		}
	}
	return theNodes;
};




ResourceTree.prototype.getElementsByClass = function(searchClass, node, tag) 
{
	// Taken from http://www.dustindiaz.com/top-ten-javascript/
	var classElements = new Array();
	if(node == null)
		node = document;
	if(tag == null)
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("\b?" + searchClass + "\b?");
	for(i=0, j=0; i<elsLen; i++) 
	{
		if(pattern.test(els[i].className)) 
		{
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
};




ResourceTree.prototype.jscss = function(a,o,c1,c2)
{
  switch (a){
    case 'swap':
      o.className=!this.jscss('check',o,c1)?o.className.replace(c2,c1): o.className.replace(c1,c2);
    break;
    case 'add':
      if(!this.jscss('check',o,c1)){o.className+=o.className?' '+c1:c1;}
    break;
    case 'remove':
      var rep=o.className.match(' '+c1)?' '+c1:c1;
      o.className=o.className.replace(rep,'');
    break;
    case 'check':
      return new RegExp('\\b'+c1+'\\b').test(o.className)
    break;
  }
};




ResourceTree.prototype.iframe_counter = 1;
ResourceTree.prototype.iframe_request = function()
{
	var iframe_loader = document.createElement("iframe");
	var iframe_id = "iframe_loader_" + this.iframe_counter++;
	iframe_loader.id = iframe_id;
	iframe_loader.name = iframe_id;
	iframe_loader.style.left = 0;
	iframe_loader.style.top = 0;
	iframe_loader.style.border = "0px solid black";
	iframe_loader.style.width = "500px";
	iframe_loader.style.height = "300px";
	if(!debugger_showIframe)
		iframe_loader.style.display = "none";
	
	document.body.appendChild(iframe_loader);
	return iframe_id;
};




ResourceTree.prototype.href_appendQueryString = function(href, varName, varValue)
{
	if(href.indexOf('?') >= 0)
		href += "&" + varName + "=" + varValue;
	else
		href += "?" + varName + "=" + varValue;
	return href;
}





ResourceTree.prototype.list_append = function(list, item, delimiter)
{
	if(delimiter == null)
		delimiter = ",";
		
	if(list.length > 0)
		list += delimiter + item;
	else
		list += item;
	return list;
}




ResourceTree.prototype.rand = function()
{
	return (new Date()).valueOf();
}




ResourceTree.prototype.getFrame = function(iframeId)
{
	return document.getElementById(iframeId).contentWindow;
};




ResourceTree.prototype.addLoadEvent = function(func) 
{
  if(window.addEventListener)
		window.addEventListener("load", func, false);
	else
		window.attachEvent("onload", func);
};



ResourceTree.prototype.insertAfter = function(newElement,targetElement) 
{	
	var parent = targetElement.parentNode; 	
	if(parent.lastchild == targetElement) 
		parent.appendChild(newElement);		
	else 
		parent.insertBefore(newElement, targetElement.nextSibling);		
}
