<?xml version="1.0"?>
<?xml-stylesheet href="bookmarks.css" type="text/css"?>

<xul:window
  xmlns:html="http://www.w3.org/TR/REC-html40"
  xmlns:rdf="http://www.w3.org/TR/WD-rdf-syntax#"
  xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">

  <html:script>
  function BookmarksNewWindow()
  {
    var toolkitCore = XPAppCoresManager.Find("toolkitCore");
    if (!toolkitCore) {
      toolkitCore = new ToolkitCore();
      if (toolkitCore) {
        toolkitCore.Init("toolkitCore");
      }
    }
    if (toolkitCore) {
      toolkitCore.ShowWindow("resource://res/rdf/bookmarks.xul",window);
    }
  }
  function BookmarkProperties()
  {
    var tree = document.getElementById('bookmarksTree');
    var select_list = tree.getElementsByAttribute("selected", "true");

    if (select_list.length >= 1) {
      dump("Bookmark Properties: Have selection\n");
      var propsCore = XPAppCoresManager.Find(select_list[0].id);
      if (!propsCore) {
        dump("Bookmark Properties: no existing appcore\n");
        var propsCore = new DOMPropsCore();
        if (propsCore) {
          dump("Bookmark Properties: initing new appcore\n");
          propsCore.Init(select_list[0].id);
        } else {
          dump("Bookmark Properties: failed to create new appcore\n");
        }
      }
      if (propsCore) {
        dump("Bookmark Properties: opening new window\n");
        propsCore.showProperties("resource://res/rdf/bm-props.xul",
                                 window, select_list[0]);
        return true;
      }
    } else {
      dump("nothing selected!\n"); 
    }
    return false;
  }
  function OpenURL(event,node)
  {
    if (node.getAttribute('type') !=
        "http://home.netscape.com/NC-rdf#Bookmark") {
      return false;
    }
    url = node.getAttribute('id');

    // Ignore "NC:" urls.
    if (url.substring(0, 3) == "NC:") {
      return false;
    }

    /*window.open(url,'bookmarks');*/
    var toolkitCore = XPAppCoresManager.Find("ToolkitCore");
    if (!toolkitCore) {
      toolkitCore = new ToolkitCore();
      if (toolkitCore) {
        toolkitCore.Init("ToolkitCore");
      }
    }
    if (toolkitCore) {
      toolkitCore.ShowWindow(url,window);
    }

    dump("OpenURL(" + url + ")\n");

    return true;
  }

function doSort(sortColName)
{
        var node = document.getElementById(sortColName);
	// determine column resource to sort on
	var sortResource = node.getAttribute('resource');
	if (!node)	return(false);

        var sortDirection="ascending";
        var isSortActive = node.getAttribute('sortActive');
        if (isSortActive == "true")
        {
                var currentDirection = node.getAttribute('sortDirection');
                if (currentDirection == "ascending")
                        sortDirection = "descending";
                else if (currentDirection == "descending")
                        sortDirection = "natural";
                else    sortDirection = "ascending";
        }

	// get RDF Core service
	var rdfCore = XPAppCoresManager.Find("RDFCore");
	if (!rdfCore)
	{
		rdfCore = new RDFCore();
		if (!rdfCore)
		{
			return(false);
		}
		rdfCore.Init("RDFCore");
                XPAppCoresManager.Add(rdfCore);
	}
	// sort!!!
        rdfCore.doSort(node, sortResource, sortDirection);
        return(false);
}

  </html:script>

  <menubar>
    <menu name="File">
      <menuitem name="New Window" onclick="BookmarksNewWindow();"/>
      <menuitem name="New Bookmarks..."/>
      <menuitem name="New Folder..."/>
      <menuitem name="New Separator"/>
      <separator/>
      <menuitem name="Close"/>
    </menu>
    <menu name="Edit">
      <menuitem name="Undo"/>
      <menuitem name="Redo"/>
      <separator/>
      <menuitem name="Cut"/>
      <menuitem name="Copy"/>
      <menuitem name="Paste"/>
      <menuitem name="Delete"/>
      <menuitem name="Select All"/>
      <separator/>
      <menuitem name="Find in Bookmarks..."/>
      <menuitem name="Find Again"/>
      <separator/>
      <menuitem name="Bookmark Properties..." onclick="return BookmarkProperties();"/>
    </menu>
  </menubar>

<tree datasources="rdf:bookmarks" id="bookmarksTree" ondblclick="return OpenURL(event,event.target.parentNode);">
  <treecol id="NameColumn" rdf:resource="http://home.netscape.com/NC-rdf#Name"/>
  <treecol id="URLColumn" rdf:resource="http://home.netscape.com/NC-rdf#URL"/>
  <treecol id="ShortcutURLColumn" rdf:resource="http://home.netscape.com/NC-rdf#ShortcutURL"/>
  <treehead>
    <treeitem>
      <treecell onclick="return doSort('NameColumn');">
	<xul:observes element="NameColumn" attribute="sortActive"/>
	<xul:observes element="NameColumn" attribute="sortDirection"/>
      Name</treecell>
      <treecell onclick="return doSort('URLColumn');">
	<xul:observes element="URLColumn" attribute="sortActive"/>
	<xul:observes element="URLColumn" attribute="sortDirection"/>
      URL</treecell>
      <treecell onclick="return doSort('ShortcutURLColumn');">
	<xul:observes element="ShortcutURLColumn" attribute="sortActive"/>
	<xul:observes element="ShortcutURLColumn" attribute="sortDirection"/>
      Shortcut URL</treecell>
    </treeitem>
  </treehead>

  <treebody id="NC:BookmarksRoot">
  </treebody>
</tree>

</xul:window>

