<?xml version="1.0" encoding="UTF-8"?>


<Module>
  <ModulePrefs title="Directory List Gadget" author_email="JBell@ngenera.com">
  	<require feature="rpc"/>
  	<require feature="pubsub"/>
  </ModulePrefs>
  <UserPref name="userguid" display_name="User GUID" required="true" />
  <Content><![CDATA[
<script src="http://storagetest.ngenplatform.com:8080/javascripts/gadgets/base64.js"></script>
<script src="http://storagetest.ngenplatform.com:8080/javascripts/gadgets/xml_util.js"></script>
<script src="http://storagetest.ngenplatform.com:8080/javascripts/gadgets/platform.js"></script>
<script src="http://storagetest.ngenplatform.com:8080/javascripts/prototype.js"></script>
<script>
	var divIds = 1000;
	var queue = [];
	var pref = new gadgets.Prefs();
	
	function isImage(item){
		return item.mimeType && item.mimeType.indexOf("image/") >= 0;
	}
	
	function getLatestVersion(item){
		return item.history[item.history.length - 1];
	}
	
	function startup() {
		nGen.init("http://provisiontest.ngenplatform.com/provisions", "6b0c73c0-bsga-serv-stor-001b7744stor", buildGui);
	}
	
	function gotoItem(item) {
		var url = nGen.storage.getResourceUrl(item);
		nGen.storage.getTicketUrl(url, function(ticketUrl) {
			var w = window;
			if (w.parent) {
				w = w.parent;
			}
			w.location = ticketUrl;
		});
	}
	
	function displayDocument(item, row) {
		var a = document.createElement("a");
		a.href = "javascript:void(0)";
		Event.observe(a, "click", function() {
			gotoItem(item);
		});
		a.appendChild(document.createTextNode(item.name));
		row.insertCell(0).appendChild(a);
				
		var latestVersion = getLatestVersion(item);
		row.insertCell(1).appendChild(document.createTextNode(latestVersion.name));
		row.insertCell(2).appendChild(document.createTextNode(latestVersion.date));
	}
	
	function displayImage(item, imageTable, row, index){
		if (index % 2 == 0) {
			row = imageTable.insertRow(1 + index / 2);
		}
		var cell = row.insertCell(2 * (index % 2));
		cell.width = "1%";
		cell.vAlign = "top";
		var button = document.createElement("input");
		button.type = "radio";
		button.value = item.id;
		button.name = "existing_repository_id";
		cell.appendChild(button);
		cell = row.insertCell(2 * (index % 2) + 1);
		cell.vAlign = "top";
		var sep = document.createElement("br");
		cell.appendChild(sep);
		var url = nGen.storage.getResourceUrl(item);
		queue.push(function() {
			nGen.storage.getTicketUrl(url, function(ticketUrl) {
				var a = document.createElement("a");
				a.href = "javascript:void(0)";
				var img = document.createElement("img");
				img.width = 50;
				img.title = item.name;
				img.alt = item.name;
				img.src = ticketUrl;
				Event.observe(a, "click", function() {
					gotoItem(item);
				});
				a.appendChild(img);
				cell.insertBefore(a, sep);
			});
		});	
		var latestVersion = getLatestVersion(item);
		cell.appendChild(document.createTextNode(latestVersion.name));
		return row;
	}
	
    function buildGui() {
    	var userGuid = pref.getString("userguid");
		nGen.storage.directoryList("users/" + userGuid + "/repository", true, function(items) {
			var header = document.createElement("h3");
			header.appendChild(document.createTextNode("Contents of User's Repository"));
			
			docItems = [];
			images = [];
			items.each(function(item) {
				if (isImage(item)) {
					images.push(item);
				}
				else {
					docItems.push(item);
				}
			});
			
			var docTable = document.createElement("table");
			var row = docTable.insertRow(0);
			var cell = row.insertCell(0);
			cell.cols = 3;
			var b = document.createElement("b");
			b.appendChild(document.createTextNode("Files:"));
			cell.appendChild(b);
			
			row = docTable.insertRow(1);
			row.insertCell(0).appendChild(document.createTextNode("Name"));
			row.insertCell(1).appendChild(document.createTextNode("Version"));
			row.insertCell(2).appendChild(document.createTextNode("Uploaded"));
			
			for (var i = 0; i < docItems.length; i++) {
				row = docTable.insertRow(parseInt(i) + 2);
				displayDocument(docItems[i], row);
			}
			
			var imageTable = document.createElement("table");
			imageTable.width = 130;
			row = imageTable.insertRow(0);
			cell = row.insertCell(0);
			cell.cols = 4;
			var b = document.createElement("b");
			b.appendChild(document.createTextNode("Images:"));
			cell.appendChild(b);
			
			for (var i = 0; i < images.length; i++) {
				row = displayImage(images[i], imageTable, row, parseInt(i));
			}
			var div = $('gadget_div');
			div.appendChild(header);
			div.appendChild(docTable);
			div.appendChild(imageTable);
			queue.each(function(c) {
				c.call(this);
			});
		});
	}
	
    gadgets.util.registerOnLoadHandler(startup);
</script>
<div id="gadget_div"></div>
  ]]></Content>
</Module>