﻿//-----------------------------------------------------------------------------
// XTabs
//
// Copyright 2005-2010 - Xcential Group LLC.
//
//-----------------------------------------------------------------------------

XTabs.prototype = new XControl;
XTabs.prototype.constructor = XTabs;

//=============================================================================
// Constructor

function XTabs(
   id,
   create
)
{
   id = (id == null) ? null : id;
   create = (create == null) ? true : false;

   if (create)
      return new XTabs(id, false);

   //--------------------------------------------------------------------------
   // Private Interface

   this.getTabName = function(
      index
   )
   {

      var spec = this.valueOf();

      if (index >= spec.tabList.length)
         return null;

      if (!spec.tabList[index])
         return null;

      if (typeof(spec.tabList[index]) != "string")
         return spec.tabList[index][0];

      return spec.tabList[index];
   }

   //--------------------------------------------------------------------------

   this.getTabIconURL = function(
      index
   )
   {

      var spec = this.valueOf();

      if (index >= spec.tabList.length)
         return null;

      if (!spec.tabList[index])
         return null;

      if (typeof(spec.tabList[index]) != "string" && spec.tabList.length >= 2)
         return spec.tabList[index][1];

      return null;
   }

   //--------------------------------------------------------------------------

   this.getTabDataSet = function(
      index
   )
   {

      var spec = this.valueOf();

      if (index >= spec.tabList.length)
         return null;

      if (!spec.tabList[index])
         return null;

      if (typeof(spec.tabList[index]) != "string" && spec.tabList.length > 3)
         return spec.tabList[index][2];

      return null;
   }

   //--------------------------------------------------------------------------
   // Privileged Interface

   this.valueOf = function()
   {

      return oSpec;
   }

   //--------------------------------------------------------------------------

   this.setObjectValue = function(
      id
   )
   {
      id = (id == null) ? null : id;

      oSpec = XTabs.getSpecFrom(id);

      return oSpec;
   }

   //--------------------------------------------------------------------------
   // Initialization

   var oSpec = this.setObjectValue(id);

   XControl.controls[oSpec.id] = this;

}

XTabs.prototype.objectClass = "XTabs";

//=============================================================================
// Static Interface

XTabs.getSpecFrom = function(
   id
)
{
   id = (id == null) ? XUtils.generateId("urn:xcential-com:tabs:", XUtils.SCHEME_RANDOM) : id;

   var spec = new Array();
   spec.id = id;
   spec.className = "XTabs";
   spec.tabIndex = null;
   spec.htmlNode = null;
   spec.visible = true;
   spec.left = 10;
   spec.top = 10;
   spec.width = null;
   spec.height = null;
   spec.shadowSize = 0;
   spec.omitEndSpacers = false;
   spec.label = "";
   spec.tabList = [];
   spec.activeTabNum = null;

   return spec;
}

//=============================================================================
// Event Handlers

XTabs.prototype.onActivateTab = null;
XTabs.prototype.onDeactivateTab = null;
XTabs.prototype.onMaximize = null;
XTabs.prototype.onDemazimize = null;

XTabs.prototype.align = null;

//-----------------------------------------------------------------------------

XTabs.prototype.doKeyDown = function(
   event,
   tabNum
)
{
   event = (event == null) ? window.event : event;

   var keyCode = event.keyCode;

   try
   {

      if (keyCode == XUI.KEY_ENTER)
         this.setActiveTab(tabNum);

   }
   catch (error)
   {
      XApp.logEvent(XApp.EVENT_ERROR, error);
      XMsg.alert(error);
   }

}

//-----------------------------------------------------------------------------

XTabs.prototype.doMouseOver = function(
   event,
   tabNum
)
{
   event = (event == null) ? window.event : event;

   var spec = this.valueOf();

   try
   {
      var tabNode = $(spec.id + "." + this.getTabName(tabNum));
      if (tabNode)
      {
         if (tabNum != spec.activeTabNum)
            tabNode.className = tabNode.className.replace(/-Highlight/,"") + "-Highlight";
      }
   }
   catch (error)
   {
      XApp.logEvent(XApp.EVENT_ERROR, error);
      XMsg.alert(error);
   }

}

//-----------------------------------------------------------------------------

XTabs.prototype.doMouseOut = function(
   event,
   tabNum
)
{
   event = (event == null) ? window.event : event;

   var spec = this.valueOf();

   try
   {
      var tabNode = $(spec.id + "." + this.getTabName(tabNum));
      if (tabNode)
      {
         tabNode.className = tabNode.className.replace(/-Highlight/,"");
      }
   }
   catch (error)
   {
      XApp.logEvent(XApp.EVENT_ERROR, error);
      XMsg.alert(error);
   }

}

//-----------------------------------------------------------------------------

XTabs.prototype.doMaximize = function(
   event
)
{
   event = (event == null) ? window.event : event;

   var spec = this.valueOf();

   try
   {

      this.maximize(event);

   }
   catch (error)
   {
      XApp.logEvent(XApp.EVENT_ERROR, error);
      XMsg.alert(error);
   }

}

//-----------------------------------------------------------------------------

XTabs.prototype.doDemaximize = function(
   event
)
{
   event = (event == null) ? window.event : event;

   var spec = this.valueOf();

   try
   {

      this.demaximize(event);

   }
   catch (error)
   {
      XApp.logEvent(XApp.EVENT_ERROR, error);
      XMsg.alert(error);
   }

}

//=============================================================================
// Public Interface

XTabs.prototype.setClass = function(
   className
)
{
   className = (className == null) ? "XTabs" : className;

   var spec = this.valueOf();
   spec.className = className;

   return className;
}

//-----------------------------------------------------------------------------

XTabs.prototype.setPosition = function(
   left,
   top
)
{
   left = (left == null) ? null : left;
   top = (top == null) ? null : top;

   var spec = this.valueOf();

   spec.left = (left) ? left : spec.left;
   spec.top = (top) ? top : spec.top;

   if (spec.htmlNode)
   {
      if (top || left)
         this.applyPositioning();
      else
         XUI(spec.htmlNode).setRuntimeStyle("position", "static");
   }

   return spec.htmlNode;
}

//-----------------------------------------------------------------------------

XTabs.prototype.omitEndSpacers = function(
   omitEndSpacers
)
{
   omitEndSpacers = (omitEndSpacers == null) ? true : omitEndSpacers;

   var spec = this.valueOf();

   spec.omitEndSpacers = omitEndSpacers;

   return omitEndSpacers;
}

//-----------------------------------------------------------------------------

XTabs.prototype.setTabList = function(
   tabList
)
{
   tabList = (tabList == null) ? [] : tabList;

   var spec = this.valueOf();

   if (spec.htmlNode)
   {
      var tabsNode = $(spec.id);
      if (tabsNode)
      {
         XUI(tabsNode).setOuterHTML(this.toHTML());
         tabsNode = $(spec.id);
         spec.htmlNode = tabsNode;
      }
   }

   spec.tabList = tabList;
   if (tabList.length > 0)
      this.setActiveTab(0);

}

//-----------------------------------------------------------------------------

XTabs.prototype.getTabList = function()
{

   var spec = this.valueOf();

   return spec.tabList;
}

//-----------------------------------------------------------------------------

XTabs.prototype.setActiveTab = function(
   tabNameOrNum
)
{

   var spec = this.valueOf();

   var tabNum = null;
   var tabNode = null;
   switch (typeof(tabNameOrNum))
   {
      case "number":
         tabNum = (tabNum >= 0 && tabNum < spec.tabList.length) ? tabNameOrNum : null;
         break;
      case "string":
         for (var i=0; i<spec.tabList.length; i++)
         {
            if (tabNameOrNum.toLowerCase() == this.getTabName(i).toLowerCase())
            {
               tabNum = i;
               break;
            }
         }
         break;
   }

   if (spec.htmlNode)
   {
      if (spec.activeTabNum != null && tabNum != spec.activeTabNum)
      {
         var activeTabName = this.getActiveTabName();
         var activeTabNode = $(spec.id + "." + activeTabName);
         activeTabNode.className = spec.className + "Tab Tab HotSpotG";
         if (this.onDeactivateTab)
            this.onDeactivateTab(this, activeTabName);
         spec.activeTabNum = null;
      }

      if (tabNum != null)
      {
         spec.activeTabNum = tabNum;
         var activeTabName = this.getActiveTabName();
         var activeTabNode = $(spec.id + "." + activeTabName);
         activeTabNode.className = spec.className + "Tab-Active Tab-Active HotSpotG-Active";
         if (this.onActivateTab)
            this.onActivateTab(this, activeTabName);
      }
   }
   else
      spec.activeTabNum = tabNum;

}

//-----------------------------------------------------------------------------

XTabs.prototype.getActiveTabName = function()
{

   var spec = this.valueOf();

   return (spec.activeTabNum != null) ? this.getTabName(spec.activeTabNum) : null;
}

//-----------------------------------------------------------------------------

XTabs.prototype.getActiveTabNum = function()
{

   var spec = this.valueOf();

   return spec.activeTabNum;
}

//-----------------------------------------------------------------------------

XTabs.prototype.getActiveTabDataSet = function()
{

   var spec = this.valueOf();

   return this.getTabDataSet(spec.activeTabNum);
}

//-----------------------------------------------------------------------------

XTabs.prototype.maximize = function(
   event
)
{
   event = (event == null) ? null : event;

   var spec = this.valueOf();

   if (this.onMaximize)
      this.onMaximize(event);

   xMaximizeButton = XUI(spec.id + ".maximizeButton");
   if (xMaximizeButton)
      xMaximizeButton.hide();

   xDemaximizeButton = XUI(spec.id + ".demaximizeButton");
   if (xDemaximizeButton)
      xDemaximizeButton.show(XUI.INLINE_BLOCK_DISPLAY);

}

//-----------------------------------------------------------------------------

XTabs.prototype.demaximize = function(
   event
)
{
   event = (event == null) ? null : event;

   var spec = this.valueOf();

   if (this.onDemaximize)
      this.onDemaximize(event);

   xDemaximizeButton = XUI(spec.id + ".demaximizeButton");
   if (xDemaximizeButton)
      xDemaximizeButton.hide();

   xMaximizeButton = XUI(spec.id + ".maximizeButton");
   if (xMaximizeButton)
      xMaximizeButton.show(XUI.INLINE_BLOCK_DISPLAY);

}

//-----------------------------------------------------------------------------

XTabs.prototype.toHTML = function(
   state
)
{
   state = (state == null) ? [] : state;

   var spec = this.valueOf();

   var controlHTML = "";

   controlHTML += "<table " +
      "id=\"" + spec.id + "\" " +
      "class=\"" + spec.className + " Control\" " +
      "style=\"" +
         ((this.align == "left") ? "margin-left: 0px; " : "") +
         ((this.align == "center") ? "margin-left: auto; " : "") +
         ((this.align == "right") ? "margin-left: auto; " : "") +
         ((this.align == "left") ? "margin-right: auto; " : "") +
         ((this.align == "center") ? "margin-right: auto; " : "") +
         ((this.align == "right") ? "margin-right: 0px; " : "") +
         ((spec.width != null) ? "width: " + XUtils.dimText(spec.width) + ";" : "") +
         ((spec.height != null) ? "height: " + XUtils.dimText(spec.height) + ";" : "") +
      "\" " +
      ">";
      controlHTML += "<tr>";
         if (!spec.omitEndSpacers)
         {
            controlHTML += "<td " +
               "class=\"" + spec.className + "OpenTabLeft\"" +
               "style=\"" +
               "\"" +
            "></td>";
         }
         for (var i=0; i<spec.tabList.length; i++)
         {
            var tabName = this.getTabName(i);
            var tabIconURL = this.getTabIconURL(i);
            var tabActive = (spec.activeTabNum == i) ? true : false;
            controlHTML += "<td " +
               "id=\"" + spec.id + "." + this.getTabName(i) + "\" " +
               "class=\"" + spec.className + ((tabActive) ? "Tab-Active Tab-Active HotSpotG-Active" : "Tab Tab HotSpotG") + "\" " +
               ((spec.tabIndex != null) ? "tabindex=\"" + (spec.tabIndex+i) + "\" " : "") +
               "style=\"" +
               "\"" +
               "onkeydown=\"XControl.controls['" + spec.id + "'].doKeyDown(event," + i + ")\" " +
               "onclick=\"XControl.controls['" + spec.id + "'].setActiveTab(" + i + ")\" " +
               "onmouseover=\"XControl.controls['" + spec.id + "'].doMouseOver(event," + i + ")\" " +
               "onmouseout=\"XControl.controls['" + spec.id + "'].doMouseOut(event," + i + ")\" " +
            ">" +
               ((tabIconURL != null) ? "<img class=\"XTabsTabIcon TabIcon\" src=\"" + tabIconURL + "\"/>&#160;" : "") +
               tabName +
            "</td>";
         }
         if (!spec.omitEndSpacers)
         {
            controlHTML += "<td " +
               "class=\"" + spec.className + "OpenTabRight\"" +
               "style=\"" +
               "\"" +
            "></td>";
         }
         if (this.onMaximize || this.onDemaximize)
         {
            controlHTML += "<td " +
            ">" +
               "<img " +
                  "id=\"" + spec.id + ".maximizeButton\" " +
                  "class=\"" + spec.className + "HeaderButton LinkImage\" " +
                  "src=\"" + "/app/pkgs/xfw/images/MaximizeButton.gif" + "\" " +
                  "style=\"" +
                     "display: " + XUI.INLINE_BLOCK_DISPLAY + "; " +
                  "\" " +
                  "onclick=\"XControl.controls['" + spec.id + "'].doMaximize()\" " +
               "/>" +
               "<img " +
                  "id=\"" + spec.id + ".demaximizeButton\" " +
                  "class=\"" + spec.className + "HeaderButton LinkImage\" " +
                  "src=\"" + "/app/pkgs/xfw/images/DemaximizeButton.gif" + "\" " +
                  "style=\"" +
                     "display: " + XUI.HIDDEN_DISPLAY + "; " +
                  "\" " +
                  "onclick=\"XControl.controls['" + spec.id + "'].doDemaximize()\" " +
               "/>" +
            "</td>";
         }
      controlHTML += "</tr>";
   controlHTML += "</table>";

   return controlHTML;
}

//=============================================================================
