﻿//-----------------------------------------------------------------------------
// XButton
//
// Copyright 2005-2010 - Xcential Group LLC.
//
//-----------------------------------------------------------------------------

XButton.prototype = new XControl;
XButton.prototype.constructor = XButton;

//=============================================================================
// Constructor

function XButton(
   id,
   create
)
{
   id = (id == null) ? null : id;
   create = (create == null) ? true : false;

   if (create)
      return new XButton(id, false);

   //--------------------------------------------------------------------------
   // Private Interface

   //--------------------------------------------------------------------------
   // Privileged Interface

   this.valueOf = function()
   {

      return oSpec;
   }

   //--------------------------------------------------------------------------

   this.setObjectValue = function(
      id
   )
   {
      id = (id == null) ? null : id;

      oSpec = XButton.getSpecFrom(id);

      return oSpec;
   }

   //--------------------------------------------------------------------------
   // Initialization

   var oSpec = this.setObjectValue(id);

   XControl.controls[oSpec.id] = this;

}

XButton.prototype.objectClass = "XButton";

//=============================================================================
// Static Interface

XButton.getSpecFrom = function(
   id
)
{
   id = (id == null) ? XUtils.generateId("urn:xcential-com:button:", XUtils.SCHEME_RANDOM) : id;

   var spec = new Array();
   spec.id = id;
   spec.className = "XButton";
   spec.htmlNode = null;
   spec.mouseLeave = false;
   spec.visible = true;
   spec.left = 10;
   spec.top = 10;
   spec.height = 18;
   spec.shadowSize = 0;
   spec.label = "Button";
   spec.dropDown = false;
   spec.hasFocus = false;

   return spec;
}

//=============================================================================
// Event Handlers

XButton.prototype.doHighlight = function(
   event
)
{
   event = (event == null) ? window.event : event;

   var spec = this.valueOf();

   try
   {
      var buttonLeft = $(spec.id + ".left");
      if (buttonLeft)
      {
         var src = XUI(buttonLeft).getCurrentStyle("backgroundImage", "");
         XUI(buttonLeft).setRuntimeStyle("backgroundImage", src.replace(/(?:\.Highlight)?\.([^\.]*)$/, ".Highlight.$1"));
      }
      var buttonCenter = $(spec.id + ".center");
      if (buttonCenter)
      {
         var src = XUI(buttonCenter).getCurrentStyle("backgroundImage", "");
         XUI(buttonCenter).setRuntimeStyle("backgroundImage", src.replace(/(?:\.Highlight)?\.([^\.]*)$/, ".Highlight.$1"));
      }
      var buttonRight = $(spec.id + ".right");
      if (buttonRight)
      {
         var src = XUI(buttonRight).getCurrentStyle("backgroundImage", "");
         XUI(buttonRight).setRuntimeStyle("backgroundImage", src.replace(/(?:\.Highlight)?\.([^\.]*)$/, ".Highlight.$1"));
      }
      spec.hasFocus = true;
   }
   catch (error)
   {
      XApp.logEvent(XApp.EVENT_ERROR, error);
      XMsg.alert(error);
   }

}

//-----------------------------------------------------------------------------

XButton.prototype.doUnhighlight = function(
   event
)
{
   event = (event == null) ? window.event : event;

   var spec = this.valueOf();

   try
   {
      var buttonLeft = $(spec.id + ".left");
      if (buttonLeft)
      {
         var src = XUI(buttonLeft).getCurrentStyle("backgroundImage", "");
         XUI(buttonLeft).setRuntimeStyle("backgroundImage", src.replace(/(?:\.Highlight)?\.([^\.]*)$/, ".$1"));
      }
      var buttonCenter = $(spec.id + ".center");
      if (buttonCenter)
      {
         var src = XUI(buttonCenter).getCurrentStyle("backgroundImage", "");
         XUI(buttonCenter).setRuntimeStyle("backgroundImage", src.replace(/(?:\.Highlight)?\.([^\.]*)$/, ".$1"));
      }
      var buttonRight = $(spec.id + ".right");
      if (buttonRight)
      {
         var src = XUI(buttonRight).getCurrentStyle("backgroundImage", "");
         XUI(buttonRight).setRuntimeStyle("backgroundImage", src.replace(/(?:\.Highlight)?\.([^\.]*)$/, ".$1"));
      }
      spec.hasFocus = false;
   }
   catch (error)
   {
      XApp.logEvent(XApp.EVENT_ERROR, error);
      XMsg.alert(error);
   }

}

//=============================================================================
// Public Interface

XButton.prototype.setClass = function(
   className
)
{
   className = (className == null) ? "XButton" : className;

   var spec = this.valueOf();
   spec.className = className;

   return className;
}

//-----------------------------------------------------------------------------

XButton.prototype.hasFocus = function()
{

   var spec = this.valueOf();

   return spec.hasFocus;
}

//-----------------------------------------------------------------------------

XButton.prototype.getLabel = function(
   castAs
)
{
   castAs = (castAs == null) ? null : castAs;

   var spec = this.valueOf();

   return (spec.label == null) ? null : (castAs == null) ? spec.label : castAs(spec.label);
}

//-----------------------------------------------------------------------------

XButton.prototype.setLabel = function(
   label
)
{
   label = (label == null) ? "" : label.toString();

   var spec = this.valueOf();

   spec.label = label;
   if (spec.htmlNode)
   {
      var labelNode = spec.htmlNode.ownerDocument.getElementById(spec.id + ".label");
      if (labelNode)
         labelNode.innerHtml = label;
   }
}

//-----------------------------------------------------------------------------

XButton.prototype.getDropDown = function(
   castAs
)
{
   castAs = (castAs == null) ? null : castAs;

   var spec = this.valueOf();

   return (spec.dropDown == null) ? null : (castAs == null) ? spec.dropDown : castAs(spec.dropDown);
}

//-----------------------------------------------------------------------------

XButton.prototype.setDropDown = function(
   dropDown
)
{
   dropDown = (dropDown == null) ? true : dropDown;

   var spec = this.valueOf();

   spec.dropDown = dropDown;
   if (spec.htmlNode)
   {
      var imageNode = spec.htmlNode.ownerDocument.getElementById(spec.id + ".right");
      if (imageNode)
         labelNode.src = "/app/pkgs/xfw/images/ButtonRight" + ((dropDown) ? "DropDown" : "") + ".gif";
   }
}

//-----------------------------------------------------------------------------

XButton.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;
}

//-----------------------------------------------------------------------------

XButton.prototype.toHTML = function(
   state
)
{
   state = (state == null) ? [] : state;

   var spec = this.valueOf();

   var controlHTML = "";

   // Note: Due to the general bugginess of Mozilla when it comes to inline
   //       blocks, this double box strategy is used. It works, for reasons
   //       unknown.

   if (XApp.isBrowser("Firefox"))
   {
      controlHTML += "<span class=\"InlineBlock\">";
   }

   controlHTML += "<span " +
      "id=\"" + spec.id + "\" " +
      "class=\"" + spec.className + " Button HotSpot\" " +
      ((spec.tabIndex != null && !XApp.isBrowser("Firefox",3.0)) ? "tabindex=\"" + spec.tabIndex + "\" " : "") +
      "style=\"" +
         ((spec.width) ? "left: 0px; " : "") +
         ((spec.width) ? "width: " + XUtils.dimText(spec.width) + "; " : "") +
         ((spec.height) ? "height: " + XUtils.dimText(spec.height) + "; " : "") +
      "\" " +
      ((XApp.isBrowser("MSIE")) ? "onmouseenter=\"XControl.controls['" + spec.id + "'].doMouseEnter(event)\" " : "") +
      ((!XApp.isBrowser("MSIE")) ? "onmouseover=\"XControl.controls['" + spec.id + "'].doMouseOver(event);XControl.controls['" + spec.id + "'].doHighlight(event)\" " : "") +
      ((!XApp.isBrowser("MSIE")) ? "onmouseout=\"XControl.controls['" + spec.id + "'].doMouseOut(event);XControl.controls['" + spec.id + "'].doUnhighlight(event)\" " : "") +
      ((XApp.isBrowser("MSIE")) ? "onmouseleave=\"XControl.controls['" + spec.id + "'].doMouseLeave(event)\" " : "") +
      "onfocus=\"XControl.controls['" + spec.id + "'].doFocus(event)\" " +
      "onblur=\"XControl.controls['" + spec.id + "'].doBlur(event)\" " +
      "onclick=\"XControl.controls['" + spec.id + "'].doClick(event)\" " +
      "onkeydown=\"XControl.controls['" + spec.id + "'].doKeyDown(event)\" " +
   ">";

   controlHTML += "<span " +
      "id=\"" + spec.id + ".left\" " +
      "class=\"" + spec.className + "Left ButtonLeft\" " +
      "style=\"" +
      ((spec.height) ? "height: " + XUtils.dimText(spec.height) + "; " : "") +
      "\" " +
      "onmouseover=\"XControl.controls['" + spec.id + "'].doMouseOver(event);XControl.controls['" + spec.id + "'].doHighlight(event)\" " +
      "onmouseout=\"XControl.controls['" + spec.id + "'].doMouseOut(event);XControl.controls['" + spec.id + "'].doUnhighlight(event)\" " +
   ">&nbsp;</span>";
      controlHTML += "<span " +
         "id=\"" + spec.id + ".center\" " +
         "class=\"" + spec.className + "Center ButtonCenter\" " +
         "style=\"" +
         ((spec.width) ? "width: " + XUtils.dimText(spec.width-5-((spec.dropDown) ? 23 : 5)) + "; " : "") +
         ((spec.height) ? "height: " + XUtils.dimText(spec.height) + "; " : "") +
         "\" " +
         "onmouseover=\"XControl.controls['" + spec.id + "'].doMouseOver(event);XControl.controls['" + spec.id + "'].doHighlight(event)\" " +
         "onmouseout=\"XControl.controls['" + spec.id + "'].doMouseOut(event);XControl.controls['" + spec.id + "'].doUnhighlight(event)\" " +
      ">";
         controlHTML += spec.label;
      controlHTML += "</span>";
   if (spec.dropDown)
   {
      controlHTML += "<span " +
         "id=\"" + spec.id + ".right\" " +
         "class=\"" + spec.className + "RightDropDown ButtonRightDropDown\" " +
         "style=\"" +
         ((spec.height) ? "height: " + XUtils.dimText(spec.height) + "; " : "") +
         "\" " +
         "onmouseover=\"XControl.controls['" + spec.id + "'].doMouseOver(event);XControl.controls['" + spec.id + "'].doHighlight(event)\" " +
         "onmouseout=\"XControl.controls['" + spec.id + "'].doMouseOut(event);XControl.controls['" + spec.id + "'].doUnhighlight(event)\" " +
      ">&nbsp;</span>";
   }
   else
   {
      controlHTML += "<span " +
      "id=\"" + spec.id + ".right\" " +
      "class=\"" + spec.className + "Right ButtonRight\" " +
      "style=\"" +
      ((spec.height) ? "height: " + XUtils.dimText(spec.height) + "; " : "") +
      "\" " +
      "onmouseover=\"XControl.controls['" + spec.id + "'].doMouseOver(event);XControl.controls['" + spec.id + "'].doHighlight(event)\" " +
      "onmouseout=\"XControl.controls['" + spec.id + "'].doMouseOut(event);XControl.controls['" + spec.id + "'].doUnhighlight(event)\" " +
      ">&nbsp;</span>";
   }

   controlHTML += "</span>";

   if (XApp.isBrowser("Firefox"))
   {
      controlHTML += "</span>";
   }

   return controlHTML;
}

//=============================================================================
