﻿//-----------------------------------------------------------------------------
// XTree
//
// Copyright 2005-2010 - Xcential Group LLC.
//
//-----------------------------------------------------------------------------

XTree.prototype = new XControl;
XTree.prototype.constructor = XTree;

//=============================================================================
// Constructor

function XTree(
   id,
   create
)
{
   id = (id == null) ? null : id;
   create = (create == null) ? true : false;

   if (create)
      return new XTree(id, false);

   //--------------------------------------------------------------------------
   // Private Interface

   //--------------------------------------------------------------------------
   // Privileged Interface

   this.valueOf = function()
   {

      return oSpec;
   }

   //--------------------------------------------------------------------------

   this.setObjectValue = function(
      id
   )
   {
      id = (id == null) ? null : id;

      oSpec = XTree.getSpecFrom(id);

      return oSpec;
   }

   //--------------------------------------------------------------------------
   // Initialization

   var oSpec = this.setObjectValue(id);

   XControl.controls[oSpec.id] = this;

}

XTree.prototype.objectClass = "XTree";

//=============================================================================
// Static Interface

XTree.getSpecFrom = function(
   id,
   uri
)
{
   id = (id == null) ? XUtils.generateId("urn:xcential-com:tree:", XUtils.SCHEME_RANDOM) : id;
   uri = (uri == null) ? null : uri;

   var spec = new Array();
   spec.id = id;
   spec.className = "XTree";
   spec.htmlNode = null;
   spec.visible = true;
   spec.left = 10;
   spec.top = 10;
   spec.right = null;
   spec.bottom = null;
   spec.width = null;
   spec.height = null;
   spec.shadowSize = 0;
   spec.src = null;
   spec.xDoc = null;
   spec.showIcons = true;

   return spec;
}

//=============================================================================
// Event Handlers

XTree.prototype.onTreeItemMouseOver = null;
XTree.prototype.onTreeItemMouseOut = null;
XTree.prototype.onTreeItemClick = null;

//-----------------------------------------------------------------------------

XTree.prototype.doHighlight = function(
   event,
   id
)
{
   event = (event == null) ? window.event : event;

   var spec = this.valueOf();

   XUI.doHighlight(event, spec.id + "." + id  + ".link");

}

//-----------------------------------------------------------------------------

XTree.prototype.doUnhighlight = function(
   event,
   id
)
{
   event = (event == null) ? window.event : event;

   var spec = this.valueOf();

   XUI.doUnhighlight(event, spec.id + "." + id  + ".link");

}

//-----------------------------------------------------------------------------

XTree.prototype.doTreeItemMouseOver = function(
   event,
   id
)
{
   event = (event == null) ? window.event : event;
   id = (id == null) ? -1 : id;

   var spec = this.valueOf();

   this.doHighlight(event, id)

   if (this.onTreeItemMouseOver)
      this.onTreeItemMouseOver(this, id);

}

//-----------------------------------------------------------------------------

XTree.prototype.doTreeItemMouseOut = function(
   event,
   id
)
{
   event = (event == null) ? window.event : event;
   id = (id == null) ? -1 : id;

   var spec = this.valueOf();

   this.doUnhighlight(event, id);

   if (this.onTreeItemMouseOut)
      this.onTreeItemMouseOut(this, id);

}

//-----------------------------------------------------------------------------

XTree.prototype.doTreeItemClick = function(
   event,
   id
)
{
   event = (event == null) ? window.event : event;
   id = (id == null) ? -1 : id;

   var spec = this.valueOf();

   if (this.onTreeItemClick)
      this.onTreeItemClick(this, id);
   else
   {
      var treeItemNode = $(spec.id + ".treeItem" + index);
      if (treeItemNode)
      {
         var links = treeItemNode.getElementsByTagName("a", treeItemNode);
         if (links.length > 0)
         {
            var link = links.item(0);
            link.click();
         }
      }
   }

}

//=============================================================================
// Public Interface

XTree.prototype.setClass = function(
   className
)
{
   className = (className == null) ? "XTree" : className;

   var spec = this.valueOf();

   spec.className = className;

   return className;
}

//-----------------------------------------------------------------------------

XTree.prototype.getSrc = function(
   castAs
)
{
   castAs = (castAs == null) ? null : castAs;

   var spec = this.valueOf();

   var src = spec.src;

   return (castAs == null) ? src : castAs(src);
}

//-----------------------------------------------------------------------------

XTree.prototype.setSrc = function(
   src
)
{
   src = (src == null) ? null : src.toString();

   var spec = this.valueOf();

   spec.src = src;
   if (src == null)
      spec.xDoc = null;
   else
      spec.xDoc = XDoc(src);

}

//-----------------------------------------------------------------------------

XTree.prototype.getXDoc = function(
   castAs
)
{
   castAs = (castAs == null) ? XDoc : castAs;

   var spec = this.valueOf();

   var xDoc = spec.xDoc;

   return castAs(xDoc);
}

//-----------------------------------------------------------------------------

XTree.prototype.getShowIcons = function(
   castAs
)
{
   castAs = (castAs == null) ? null : castAs;

   var spec = this.valueOf();

   var showIcons = spec.showIcons;

   return (castAs == null) ? showIcons : castAs(showIcons);
}

//-----------------------------------------------------------------------------

XTree.prototype.showIcons = function(
   showIcons
)
{
   showIcons = (showIcons == null) ? true : showIcons;

   var spec = this.valueOf();

   spec.showIcons = showIcons;

   return showIcons;
}

//-----------------------------------------------------------------------------

XTree.prototype.isExpanded = function(
   id
)
{

   var spec = this.valueOf();

   if (XUI(spec.id + "." + id + ".expandIcon").getRuntimeStyle("display") == "none")
      return true;
   else
      return false;
}

//-----------------------------------------------------------------------------

XTree.prototype.expand = function(
   id,
   uncoverId
)
{
   uncoverId = (uncoverId == null) ? null : uncoverId;

   var spec = this.valueOf();

   XUI(spec.id + "." + id + ".expandIcon").setRuntimeStyle("display", "none");
   XUI(spec.id + "." + id + ".collapseIcon").setRuntimeStyle("display", XUI.INLINE_BLOCK_DISPLAY);

   if (spec.showIcons)
   {
      XUI(spec.id + "." + id + ".icon").setRuntimeStyle("display", "none");
      XUI(spec.id + "." + id + ".iconOpen").setRuntimeStyle("display", XUI.INLINE_BLOCK_DISPLAY);
   }


   var containerXUI = XUI(spec.id + "." + id + ".container");
   if (containerXUI.exists())
      containerXUI.setRuntimeStyle("display", "block");
   else
   {
      var container = $(spec.id + "." + id + ".placeHolder");
      if (container)
      {
         var docXRef = this.getSrc(XRef);
         var docURI = (docXRef != null) ? docXRef.getURI() : null;
         var docIdentifier = (docXRef != null) ? docXRef.getIdentifier() : null;
         var params = [
            ["domainURL", XApp.getDomainURL()],
            ["browserName", XApp.browserName],
            ["browserVersion", XApp.browserVersion],
            ["applicationNameHTML", XApp.APPLICATION_NAME_HTML],
            ["docURI", docURI],
            ["docIdentifier", docIdentifier],
            ["controlId", spec.id],
            ["className", spec.className],
            ["showIcons", XBoolean(spec.showIcons).toString()],
            ["id", id]
         ]
         XUI(container).setOuterHTML(spec.xDoc.transformTo("tree.html", params));
      }
   }

}

//-----------------------------------------------------------------------------

XTree.prototype.collapse = function(
   id
)
{

   var spec = this.valueOf();

   XUI(spec.id + "." + id + ".expandIcon").setRuntimeStyle("display", XUI.INLINE_BLOCK_DISPLAY);
   XUI(spec.id + "." + id + ".collapseIcon").setRuntimeStyle("display", "none");

   if (spec.showIcons)
   {
      XUI(spec.id + "." + id + ".icon").setRuntimeStyle("display", XUI.INLINE_BLOCK_DISPLAY);
      XUI(spec.id + "." + id + ".iconOpen").setRuntimeStyle("display", "none");
   }

   XUI(spec.id + "." + id + ".container").setRuntimeStyle("display", "none");

}

//-----------------------------------------------------------------------------

XTree.prototype.toHTML = function()
{

   var spec = this.valueOf();

   var controlHTML = "";

   controlHTML += "<div " +
      "id=\"" + spec.id + "\" " +
      "class=\"" + spec.className + "\" " +
      "style=\"" +
         "display: " + ((spec.visible) ? "block" : "none" ) + "; " +
         "left: " + XUtils.dimText(spec.left) + "; " +
         "top: " + XUtils.dimText(spec.top) + "; " +
      "\" " +
      "unselectable=\"on\" " +
   ">";

      if (spec.xDoc && spec.xDoc.isSomething())
      {
         var docXRef = this.getSrc(XRef);
         var docURI = (docXRef != null) ? docXRef.getURI() : null;
         var docIdentifier = (docXRef != null) ? docXRef.getIdentifier() : null;
         var params = [
            ["domainURL", XApp.getDomainURL()],
            ["browserName", XApp.browserName],
            ["browserVersion", XApp.browserVersion],
            ["applicationNameHTML", XApp.APPLICATION_NAME_HTML],
            ["docURI", docURI],
            ["docIdentifier", docIdentifier],
            ["controlId", spec.id],
            ["className", spec.className],
            ["showIcons", XBoolean(spec.showIcons).toString()]
         ]
         controlHTML += spec.xDoc.transformTo("tree.html", params);
      }

   controlHTML += "</div>";

   return controlHTML;
}

//=============================================================================
