/*
********************************************************************************************************************************************************************
* Name              :
*
* Description       :
*
* Author(S)         : JSHUMAKER
*
* Creation Date     : 1/11/2007
*
* Version           : 1.0
*
*
* Modifications:
*    MOD ID          Date              Developer Name                   Description
*    ------------    --------------    -----------------------------    ----------------------------------------------------
*
* VSS Info:
*      $Archive:
*      $Author:
*              $Date:
*              $Revision:
*
*  COPYRIGHT 2005 TDCI Inc.
********************************************************************************************************************************************************************
*/

GraphicButton = Class.create();
GraphicButton.prototype = 
{
    initialize: function(OuterDivID)
    {
	    // state variables
        this.IsToggle = false;
        this.IsMouseOver = false;
        this.ContinuousInterval = -1;
        this.ClientID = OuterDivID;
        this.ChangeFailed = false;
        this.ContinuousStarted = false;

        // object variables
        this.OuterDiv = null;
        this.Image = null;
        this.TextSpan = null;
        this.DataField = null;

        //Assign references to the object variables
        this.OuterDiv = $(OuterDivID);
        this.ReferenceStructure();

        //Change VB true/false values to javascript true/false values
        this.ParseInitialValues();

        //Make sure the client on click is cleared
        //  This functionality will be executed during the mouse down event.
        //  This simplifies the event handling and toggling.
        this.OuterDiv.onclick = null;

        //Build up pointers to all event handlers so that they can be removed with
        //  stopObserving.
        this.EventHandlerDown = this.MouseDown.bindAsEventListener(this);
        this.EventHandlerUp = this.MouseUp.bindAsEventListener(this);
        this.EventHandlerOver = this.MouseOver.bindAsEventListener(this);
        this.EventHandlerOut = this.MouseOut.bindAsEventListener(this);
        this.EventHandlerDoubleClick = this.DoubleClick.bindAsEventListener(this);
        this.EventHandlerMouseClick = this.MouseClick.bindAsEventListener(this);


        //Attach Events
        //    Prototype automatically does cleanup on these events so 
        //    a disposal method is not necessary
        
        //Use the SetEnabled & SetHoverEnabled methods to correctly hook the right event handlers.
        if (this.OuterDiv.ControlEnabled)
        {
            this.OuterDiv.ControlEnabled = false;
            this.SetEnabled(true);

            if (this.OuterDiv.HoverEnabled)
            {
                this.OuterDiv.HoverEnabled = false;
                this.SetHoverEnabled(true);
            }
        }

        //Determine State of Control
        if (1 == this.OuterDiv.ButtonType)
        {
            this.IsToggle = true;
        }


        if (this.IsToggle && this.OuterDiv.ControlEnabled && this.OuterDiv.ControlSelected)
        {
            this.OuterDiv.ControlSelected = true;
        }
        else
        {
            this.OuterDiv.ControlSelected = false;
        }

        
        this.UpdateAttributes();

        return true;
    },
    
    
//Clientside Properties*****************************

    //Layout Properties
    SetButtonType: function(ButtonType)
    {
        if (this.OuterDiv.ButtonType != ButtonType)
        {
            switch (ButtonType)
            {
                case 0:
                    this.IsToggle = false;
                    
                    this.OuterDiv.ControlSelected = false;
                    break;
                default:
                    this.IsToggle = true;
            }
            
            this.OuterDiv.ButtonType = ButtonType;
            this.UpdateAttributes();
            
//            this.SaveState();
        }
    },
    
    GetButtonType: function()
    {
        return this.OuterDiv.ButtonType;
    },
    
    
    
    //Behavior Properties
    SetClientClickInContext: function(Object, Command)
    {
        this.OuterDiv.OnEnabledClick = Command;
        this.OuterDiv.OnEnabledClickInContext = Object;
        this.OuterDiv.OnEnabledClickInContextFunction = function(HandledEvent)
        {
            return eval(Command);
        };
        this.OuterDiv.OnEnabledClickInContextPointer = this.OuterDiv.OnEnabledClickInContextFunction.bind(Object);
        
//        this.SaveState();
    },

    SetClientClick: function(Command)
    {
        this.OuterDiv.OnEnabledClick = Command;
        this.OuterDiv.OnEnabledClickInContext = null;
        
//        this.SaveState();
    },

    GetClientClick: function()
    {
        return this.OuterDiv.OnEnabledClick;
    },
    
    SetClientContinuousClick: function(Command)
    {
        this.OuterDiv.OnContinousClick = Command;
    },
            
    GetClientContinuousClick: function()
    {
        return this.OuterDiv.OnContinousClick;
    },
    
    SetClientContinuousClickAddonInContext: function(Object, Command)
    {
        this.OuterDiv.OnContinuousClickAddon = Command;
        this.OuterDiv.OnContinuousClickAddonInContext = Object;
        this.OuterDiv.OnContinuousClickAddonInContextFunction = function()
        {
            return eval(Command);
        };
        this.OuterDiv.OnContinuousClickAddonInContextPointer = this.OuterDiv.OnContinuousClickAddonInContextFunction.bind(Object);
    },

    SetClientContinuousClickAddon: function(Command)
    {
        this.OuterDiv.OnContinuousClickAddon = Command;
        this.OuterDiv.OnContinuousClickAddonInContext = null;
    },
    
    GetClientContinuousClickAddon: function()
    {
        return this.OuterDiv.OnContinuousClickAddon;
    },
    
    UsingContinuousClick: function()
    {
        if (this.OuterDiv.OnContinuousClickAddon.length + this.OuterDiv.OnContinousClick.length > 0)
        {
            return true;
        }
        
        return false;
    },
    
    SetContinuousClickInterval: function(Interval)
    {
        Interval = parseInt(Interval);
        
        if (Interval >= 0)
        {
            this.OuterDiv.ContinousClickInterval = Interval;
        }
    },
            
    GetContinuousClickInterval: function()
    {
        return this.OuterDiv.ContinousClickInterval;
    },
            
    SetClientChangeInContext: function(Object, Command)
    {
        this.OuterDiv.OnClientChange = Command;
        this.OuterDiv.OnClientChangeInContext = Object;
        this.OuterDiv.OnClientChangeInContextFunction = function()
        {
            return eval(Command);
        };
        this.OuterDiv.OnClientChangeInContextPointer = this.OuterDiv.OnClientChangeInContextFunction.bind(Object);
    },
    
    SetClientChange: function(Command)
    {
        this.OuterDiv.OnClientChange = Command;
        this.OuterDiv.OnClientChangeInContext = null;
        
//        this.SaveState();
    },
    
    GetClientChange: function()
    {
        return this.OuterDiv.OnClientChange;
    },
    
    
    
    //Status Properties
    SetEnabled: function(ControlEnabled)
    {
        if (this.OuterDiv.ControlEnabled != ControlEnabled && !this.OuterDiv.ToggleDisabled)
        {
            if (ControlEnabled)
            {
                Event.observe(this.OuterDiv, 'mousedown', this.EventHandlerDown);
                Event.observe(this.OuterDiv, 'mouseup', this.EventHandlerUp);
                Event.observe(this.OuterDiv, 'dblclick', this.EventHandlerDoubleClick);
                Event.observe(this.OuterDiv, 'click', this.EventHandlerMouseClick);
            }
            else
            {
                Event.stopObserving(this.OuterDiv, 'mousedown', this.EventHandlerDown);
                Event.stopObserving(this.OuterDiv, 'mouseup', this.EventHandlerUp);
                Event.stopObserving(this.OuterDiv, 'dblclick', this.EventHandlerDoubleClick);
                Event.stopObserving(this.OuterDiv, 'click', this.EventHandlerMouseClick);

                this.StopContinuousClick();
            }

            this.OuterDiv.ControlEnabled = ControlEnabled;
            this.UpdateAttributes();

            //this.SaveState();
        }
        else if (this.OuterDiv.ToggleControlEnabled != ControlEnabled && this.OuterDiv.ToggleDisabled)
        {
            this.OuterDiv.ToggleControlEnabled = ControlEnabled;
        }
    },
    
    GetEnabled: function()
    {
        return this.OuterDiv.ControlEnabled;
    },
    
    SetToggleDisabled: function(Disabled)
    {
        if (Disabled != this.OuterDiv.ToggleDisabled)
        {
            this.OuterDiv.ToggleDisabled = Disabled;
            
            if (Disabled)
            {
                this.OuterDiv.ToggleControlEnabled = this.OuterDiv.ControlEnabled;
                this.OuterDiv.ControlEnabled = false;
            }
            else
            {
                this.OuterDiv.ControlEnabled = this.OuterDiv.ToggleControlEnabled;
                this.OuterDiv.ToggleControlEnabled = null;
            }
        }
    },
    
    SetHoverEnabled: function(ControlHoverEnabled)
    {
        if (this.OuterDiv.HoverEnabled != ControlHoverEnabled && !this.OuterDiv.ToggleHoverDisabled)
        {
            if (ControlHoverEnabled)
            {
                Event.observe(this.OuterDiv, 'mouseover', this.EventHandlerOver);
                Event.observe(this.OuterDiv, 'mouseout', this.EventHandlerOut);
            }
            else
            {
                Event.stopObserving(this.OuterDiv, 'mouseover', this.EventHandlerOver);
                Event.stopObserving(this.OuterDiv, 'mouseout', this.EventHandlerOut);
            }

            this.OuterDiv.HoverEnabled = ControlHoverEnabled;
            this.UpdateAttributes();

//            this.SaveState();
        }
        else if (this.OuterDiv.HoverEnabled != ControlHoverEnabled && this.OuterDiv.ToggleHoverDisabled)
        {
            this.OuterDiv.ToggleHoverEnabled = this.OuterDiv.HoverEnabled;
        }
    },
    
    GetHoverEnabled: function()
    {
        return this.OuterDiv.HoverEnabled;
    },

    SetToggleHoverDisabled: function(Disabled)
    {
        if (Disabled != this.OuterDiv.ToggleHoverDisabled)
        {
            this.OuterDiv.ToggleHoverDisabled = Disabled;
            
            if (Disabled)
            {
                this.OuterDiv.ToggleHoverEnabled = this.OuterDiv.HoverEnabled;
                this.OuterDiv.HoverEnabled = false;
            }
            else
            {
                this.OuterDiv.HoverEnabled = this.OuterDiv.ToggleHoverEnabled;
                this.OuterDiv.ToggleHoverEnabled = null;
            }
        }
    },

    SetSelected: function(ControlSelected)
    {
        if (this.IsToggle && ControlSelected == !this.OuterDiv.ControlSelected && this.OuterDiv.ControlEnabled)
        {
            if (this.ChangeSelected(ControlSelected) && ControlSelected)
            {
                this.ClientClick();
            }
            
            this.UpdateAttributes();
        }
    },
    
    GetSelected: function()
    {
        return this.OuterDiv.ControlSelected;
    },
    
    
    //State Properties
    SetImage: function(Image)
    {
        if (null != this.Image)
        {
            this.Image.src = Image;
        }
    },
    
    GetImage: function()
    {
        if (null == this.Image)
        {
            return null;
        }
        
        return this.Image.src;
    },
    
    SetText: function(Text)
    {
        if (null != this.TextSpan)
        {
            this.CompatSetText(this.TextSpan, Text);
            
//            this.SaveState();
        }
    },
    
    GetText: function()
    {
        if (null == this.TextSpan)
        {
            return null;
        }
        
        return this.CompatGetText(this.TextSpan);
    },
    
    SetCommand: function(Command)
    {
        this.OuterDiv.Command = Command;
    },
    
    GetCommand: function()
    {
        return this.OuterDiv.Command;
    },
    
    SetCollapse: function(Collapse)
    {
        if (Collapse == true)
        {
            this.OuterDiv.cssText = "visibility: hidden;";
        }
        else
        {
            this.OuterDiv.cssText = "visibility: visible;";
        }
    },
    
//Event Handlers *************************
    MouseOver: function()
    {
        if (!this.IsToggle || !this.OuterDiv.ControlSelected)
        {
            this.IsMouseOver = true;
            this.UpdateAttributes();
        }

        eval(this.OuterDiv.OnClientMouseOver);

        return true;
    },
    
    MouseDown: function(HandledEvent)
    {
        if (Event.isLeftClick(HandledEvent) && this.OuterDiv.ControlEnabled)
        {
            if (!this.IsToggle)
            {
                this.OuterDiv.ControlSelected = true;

                if (this.UsingContinuousClick())
                {
                    this.StartContinuousClick();
                }
            }
            
            this.UpdateAttributes();
        }
        
        return true;
    },

    MouseUp: function(HandledEvent)
    {        
        this.OuterDiv.LeftButton = Event.isLeftClick(HandledEvent);
    
        if (this.OuterDiv.LeftButton && this.OuterDiv.ControlEnabled)
        {
            if (!this.IsToggle)
            {
                this.OuterDiv.ControlSelected = false;
            
                if (this.UsingContinuousClick())
                {
                    this.StopContinuousClick();
                }
            }
            
            this.UpdateAttributes();
        }
        
        return true;
    },

    MouseClick: function(HandledEvent)
    {
        if (this.OuterDiv.LeftButton && this.OuterDiv.ControlEnabled)
        {
            if (!this.IsToggle)
            {
                if (this.UsingContinuousClick())
                {
                    this.StopContinuousClick();
                }
                else
                {
                    this.ClientClick(HandledEvent);
                }
            }
            else if(this.ChangeSelected(!this.OuterDiv.ControlSelected))
            {
                this.ClientClick(HandledEvent);
            }
            
            this.UpdateAttributes();
        }        
    },

    DoubleClick: function(HandledEvent)
    {
        if (this.OuterDiv.LeftButton && this.OuterDiv.ControlEnabled)
        {
            if (!this.IsToggle && this.UsingContinuousClick())
            {
                this.StartContinuousClick();
            }

            this.MouseClick(HandledEvent);
        }
    },

    MouseOut: function()
    {
        if (!this.IsToggle)
        {
            this.OuterDiv.ControlSelected = false;
        
            if (this.UsingContinuousClick())
            {
                this.StopContinuousClick();
            }
        }

        this.IsMouseOver = false;
        this.UpdateAttributes();
        eval(this.OuterDiv.OnClientMouseOut);
        
        return true;
    },
    

//Helper Functions **********************
    ChangeSelected: function(SelectedValue)
    {
        var ChangeEvent = this.OuterDiv.OnClientChange;
        
        if (this.IsToggle && this.OuterDiv.ControlSelected != SelectedValue)
        {
            this.ChangeFailed = false;
            if (this.OuterDiv.OnClientChangeInContext != null)
            {
                if (this.OuterDiv.OnClientChangeInContextPointer() == false)
                {
                    this.ChangeFailed = true;
                }
            }
            else
            {
                if (eval(ChangeEvent) == false)
                {
                    this.ChangeFailed = true;
                }
            }
            
            if (!this.ChangeFailed)
            {
                this.OuterDiv.ControlSelected = SelectedValue;
            }
        }
        
        return !this.ChangeFailed;
    },

    ClientClick: function(HandledEvent)
    {
        if (!this.UsingContinuousClick())
        {
            if (this.OuterDiv.OnEnabledClickInContext != null)
            {
                this.OuterDiv.OnEnabledClickInContextPointer(HandledEvent)
            }
            else if (this.OuterDiv.OnEnabledClick.length > 0)
            {
                eval(this.OuterDiv.OnEnabledClick)
            }
        }
    },
    
    StartContinuousClick: function()
    {
        this.ContinuousStarted = true;
        var ClientStatement = "";
        if (!this.IsToggle && this.UsingContinuousClick())
        {
            if (this.OuterDiv.OnContinuousClickAddonInContext != null)
            {
                this.OuterDiv.OnContinuousClickAddonInContextPointer();
            }
            else
            {
                ClientStatement += this.OuterDiv.OnContinuousClickAddon;
            }
            ClientStatement += this.OuterDiv.OnContinousClick;
            eval(ClientStatement);

            if (this.OuterDiv.ContinousClickInterval > 0 && this.ContinuousInterval == -1)
            {
                this.ContinuousInterval = setInterval(this.StartContinuousClick.bind(this), this.OuterDiv.ContinousClickInterval);
            }
        }

        this.ContinuousStarted = false;
    },
    
    StopContinuousClick: function()
    {
        if (!this.IsToggle && this.ContinuousInterval != -1)
        {
            if (this.ContinuousStarted == false)
            {
                clearInterval(this.ContinuousInterval);
                this.ContinuousInterval = -1;
                
                this.OuterDiv.ControlSelected = false;
            }
            else
            {
                setTimeout(this.StopContinuousClick.bind(this), this.OuterDiv.ContinuousClickInterval);
            }
        }
    },
    
    
//Display Helper Functions**********************
    UpdateAttributes: function()
    {
        //Clear previous styles/classNames.
        this.OuterDiv.className = "";
        
        //Determine if the button is disabled
        if (!this.OuterDiv.ControlEnabled)
        {
            this.OuterDiv.className = this.OuterDiv.CssClassDefault + " " + this.OuterDiv.CssClassDefault + "_Disabled";
            
            return true;
        }
        
        //Apply hover style if mouse is over
        if (this.OuterDiv.HoverEnabled && this.IsMouseOver && !this.OuterDiv.ControlSelected)
        {
            this.OuterDiv.className = this.OuterDiv.CssClassDefault + " " + this.OuterDiv.CssClassDefault + "_HoverOver";
            
            return true;
        }
        
        //Determine pressed or unpressed styling.
        if (this.OuterDiv.ControlSelected)
        {
            this.OuterDiv.className = this.OuterDiv.CssClassDefault + " " + this.OuterDiv.CssClassDefault + "_Down";
        }
        else
        {
            this.OuterDiv.className = this.OuterDiv.CssClassDefault + " " + this.OuterDiv.CssClassDefault + "_Up";
        }
        
        return true;
    },



//Setup Helper Functions**************
    ParseInitialValues: function()
    {
        //Fix for FireFox
        //  Firefox does not permit custom attributes to be accessed as follows:
        //      element.CustomAttribute
        //  Instead you have to do element.getAttribute("CustomAttribute")
        //  Instead of performing this and changing all of the javascript code
        //  we will parse all custom attributes initially and reassign them to 
        //  there element.CustomAttribute counterparts.
        this.OuterDiv.OnEnabledClick = this.OuterDiv.getAttribute("OnEnabledClick");
        this.OuterDiv.OnClientMouseOver = this.OuterDiv.getAttribute("OnClientMouseOver");
        this.OuterDiv.OnClientMouseOut = this.OuterDiv.getAttribute("OnClientMouseOut");
        this.OuterDiv.OnContinousClick = this.OuterDiv.getAttribute("ContinuousClick");
        this.OuterDiv.ContinousClickInterval = this.OuterDiv.getAttribute("ContinuousInterval");
        this.OuterDiv.OnClientChange = this.OuterDiv.getAttribute("OnClientChange");
        this.OuterDiv.TypeOfDisplay = this.OuterDiv.getAttribute("TypeOfDisplay");
        this.OuterDiv.TypeOfOrientation = this.OuterDiv.getAttribute("TypeOfOrientation");
        this.OuterDiv.ButtonType = this.OuterDiv.getAttribute("ButtonType");
        this.OuterDiv.ControlSelected = this.OuterDiv.getAttribute("ControlSelected");
        this.OuterDiv.ControlEnabled = this.OuterDiv.getAttribute("ControlEnabled");
        this.OuterDiv.HoverEnabled = this.OuterDiv.getAttribute("HoverEnabled");
        this.OuterDiv.UseStartupScript = this.ConvertToBoolean(this.OuterDiv.getAttribute("UseStartupScript"));

        this.OuterDiv.CssClassDefault = this.OuterDiv.getAttribute("CssClassDefault");
        
        this.OuterDiv.OnContinuousClickAddon = "";
        
        
        //Create the command object
        this.OuterDiv.Command = new ControlCommand(this.OuterDiv.getAttribute("Command"));
        
        
        //Convert VB Boolean Values
        this.OuterDiv.ControlEnabled = this.ConvertToBoolean(this.OuterDiv.ControlEnabled);
        this.OuterDiv.ControlSelected = this.ConvertToBoolean(this.OuterDiv.ControlSelected);
        this.OuterDiv.HoverEnabled = this.ConvertToBoolean(this.OuterDiv.HoverEnabled);
        
        //Convert VB Integer Values
        this.OuterDiv.ButtonType = parseInt(this.OuterDiv.ButtonType);
        this.OuterDiv.TypeOfDisplay = parseInt(this.OuterDiv.TypeOfDisplay);
        this.OuterDiv.TypeOfOrientation = parseInt(this.OuterDiv.TypeOfOrientation);
        this.OuterDiv.ContinousClickInterval = parseInt(this.OuterDiv.ContinousClickInterval);
        
        this.OuterDiv.ToggleDisabled = false;
        this.OuterDiv.ToggleHoverDisabled = false;
        
        return true;
    },
    
    ReferenceStructure: function()
    {
        //Local Variables
        var ButtonChildren;
        var Pos = 0;
        var Child;
        var NodeName = "";

        //Search through the outer div's children to find inner components.
        ButtonChildren = this.OuterDiv.childNodes;
        
        for (Pos = 0; Pos < ButtonChildren.length; Pos++)
        {
            //Find the node name of the child.
            Child = ButtonChildren.item(Pos);
            NodeName = Child.nodeName;
            
            if (NodeName == "IMG")
            {
                //This child is the image area.
                this.Image = Child;
            }
            else if (NodeName == "SPAN")
            {
                //This child is the text area.
                this.TextSpan = Child;
            }
            else if (NodeName == "INPUT")
            {
                //This child is the data field area.
                this.DataField = Child;
            }
        }
    },

//Browser Compatability Helpers**********
    CompatGetText: function(Target)
    {
        if (Target.innerText == null)
        {
            return Target.textContent;
        }
        
        return Target.innerText;
    },
    
    CompatSetText: function(Target, Text)
    {
        if (Target.innerText == null && Target.textContent != null)
        {
            Target.textContent = Text;
        }
        else if (Target.innerText != null && Target.textContent == null)
        {
            Target.innerText = Text;
        }
        else
        {
            Target.innerText = Text;
            Target.textContent = Text;
        }
    },

    ConvertToBoolean: function(Value)
    {
        if (Value == "True")
        {
            return true;
        }
        
        return false;
    }
};