// :folding=explicit:collapseFolds=1:

// {{{ constants
var MBOX_TAG                = 'mbox';
var MBOX_HEADER_TAG         = 'header';
var MBOX_BODY_TAG           = 'body';
var MBOX_FOOTER_TAG         = 'footer';
var MBOX_SHADE_BUTTON_TAG   = 'shade_button';
var MBOX_DETACH_BUTTON_TAG  = 'detach_button';
var DROPDOWN_TAG            = 'dropdown';
// }}} constants

// {{{ Constructor
function Phantom(node)
{
    var self = this;
    // {{{ old
    //this.Width = this.Node.offsetWidth;
    //this.Height = this.Node.offsetHeight;
    // }}} old

    // {{{ Members
    this.Focus = false;
    this.Drag = false;
    this.Node = node;
    this.Components = new Array();

    this.X = 0;
    this.Y = 0;

    this.mouseStartX = 0;
    this.mouseStartY = 0;
    
    // }}} Members

    // {{{ Methods
    this.getX = getX;
    this.getY = getY;
    this.getAbsX = getAbsX;
    this.getAbsY = getAbsY;

    this.setX = setX;
    this.setY = setY;

    this.getFocus = phantomGetFocus;

    this.setFocus = phantomSetFocus;
    this.setBlur  = phantomSetBlur;

    this.GoTo = GoTo;

    this.mboxShade = phantomMboxShade;
    this.mboxDetach = phantomMboxDetach;

    this.rGetNodesByWidgetNames = phantomRGetNodesByWidgetNames;
    // }}} Methods

    // {{{ Event handlers
    this.onClick = phantomOnClick;
    this.onMouseOver = phantomOnMouseOver;
    this.onMouseOut = phantomOnMouseOut;
    this.onMouseDown = phantomOnMouseDown;
    this.onMouseUp = phantomOnMouseUp;
    this.onMouseMove = phantomOnMouseMove;
    // }}} Event handlers

    // {{{ Init
    this.X = this.getX();
    this.Y = this.getY();

    this.initX = this.X;
    this.initY = this.X;

    PhListener.addPhantom(this);
    var widget_type = this.Node.getAttribute('PHANTOMWIDGET');

    if (widget_type == MBOX_TAG) // {{{
    {
        this.Components = this.rGetNodesByWidgetNames(this.Node, [MBOX_HEADER_TAG, MBOX_BODY_TAG, MBOX_FOOTER_TAG, MBOX_SHADE_BUTTON_TAG, MBOX_DETACH_BUTTON_TAG]);
        this.Components[MBOX_SHADE_BUTTON_TAG].onclick = function (evt) {
            if (!evt) { evt = window.event; };

            self.mboxShade(evt);

            return false;
        };
    } // }}} MBOX
    else if (widget_type == DROPDOWN_TAG) // {{{
    {
        
    } // }}}

    // }}} Init
}
// }}} Constructor

// {{{ Phantom methods

// {{{ getX()
function getX()
{
    return parseInt(this.Node.style.left);
}
// }}} getX()

// {{{ getY()
function getY()
{
    return parseInt(this.Node.style.top);
}
// }}} getY()

// {{{ getAbsX()
function getAbsX(node)
{
    return (node.offsetParent)? 
        node.offsetLeft + this.getAbsX(node.offsetParent):
        node.offsetLeft;
}
// }}} getAbsX()

// {{{ getAbsY()
function getAbsY(node)
{
    return (node.offsetParent)? 
        node.offsetTop + this.getAbsY(node.offsetParent):
        node.offsetTop;
}
// }}} getAbsY()

// {{{ setX()
function setX(x)
{
    if (this.LimitX != null)
    {
        if (x > 0) x = 0;
        if (x < -1*this.LimitX) x = -1*this.LimitX;
    }

    this.X = x;
    this.Node.style.left = this.X;
}
// }}} setX()

// {{{ setY()
function setY(y)
{
    if (this.LimitY != null)
    {
        if (y > 0) y = 0;
        if (y < -1*this.LimitY) y = -1*this.LimitY;
    }

    this.Y = y;
    this.Node.style.top = this.Y;
}
// }}} setY()

// {{{ GoTo()
function GoTo(x, y)
{
    this.setX(x);
    this.setY(y);
}
// }}} GoTo()

// {{{ phantomGetFocus()
function phantomGetFocus()
{
    return this.Focus;
}
// }}} phantomGetFocus()

// {{{ phantomSetFocus()
function phantomSetFocus()
{
    this.Focus = true;

    var phantomClassFocus = this.Node.getAttribute('phantomclassfocus');
    if (phantomClassFocus)
    {
        this.Node.setAttribute('CLASS', phantomClassFocus);
    }
}
// }}} phantomSetFocus()

// {{{ phantomSetBlur()
function phantomSetBlur()
{
    this.Focus = false;

    var phantomClassBlur = this.Node.getAttribute('phantomclassblur');
    if (phantomClassBlur)
    {
        this.Node.setAttribute('CLASS', phantomClassBlur);
    }
}
// }}} phantomSetBlur()

// {{{ phantomMboxShade()
function  phantomMboxShade(evt)
{
    var d = 'none';
    var sbstr = '[+]';
    if (this.Components[MBOX_BODY_TAG].style.display == d)
    {
        d = 'block';
        sbstr = '[-]';
    }
    this.Components[MBOX_BODY_TAG].style.display = d;

    textNode = document.createTextNode(sbstr);
    this.Components[MBOX_SHADE_BUTTON_TAG].replaceChild(textNode, this.Components[MBOX_SHADE_BUTTON_TAG].firstChild);
}
// }}} phantomMboxShade()

// {{{ phantomRGetNodesByWidgetNames()
function phantomRGetNodesByWidgetNames(node, wnames)
{
    var nodes = new Array();

    var node_children = node.childNodes;
    for (var i in node_children)
    {
        if (node_children[i].getAttribute)
        {
            for (var k in wnames)
            {
                if (node_children[i].getAttribute('PHANTOMWIDGET') == wnames[k])
                {
                    nodes[wnames[k]] = node_children[i];
                }
            }

            if (node_children[i].childNodes)
            {
                var r = this.rGetNodesByWidgetNames(node_children[i], wnames);
                for (var n in r) nodes[n] = r[n];
            }
        }
    }

    return nodes;
}
// }}} phantomRGetNodesByWidgetNames()

// {{{ phantomMboxDetach()
function phantomMboxDetach(evt)
{
    this.setX(this.getAbsX(this.Node));
    this.setY(this.getAbsY(this.Node));
    this.Node.style.position = 'absolute';
}
// }}} phantomMboxDetach()

// }}} Phantom methods

// {{{ Event handlers

    // {{{ phantomOnClick()
    function phantomOnClick()
    {
        
    }
    // }}} phantomOnClick()

    // {{{ phantomOnMouseOver()
    function phantomOnMouseOver()
    {
    
    }
    // }}} phantomOnMouseOver()

    // {{{ phantomOnMouseOut()
    function phantomOnMouseOut()
    {
    
    }
    // }}} phantomOnMouseOut()

    // {{{ phantomOnMouseDown()
    function phantomOnMouseDown(evt)
    {
        if (!this.Drag)
        {
            this.Drag = true;
            this.mouseStartX = evt.clientX;
            this.mouseStartY = evt.clientY;
            if (evt.preventDefault)
            {
                evt.preventDefault();
            }
            else
            {
                evt.returnValue = true;
            }
        }
    }
    // }}} phantomOnMouseDown()

    // {{{ phantomOnMouseUp()
    function phantomOnMouseUp(evt)
    {
        this.Drag = false;
    }
    // }}} phantomOnMouseUp()

    // {{{ phantomOnMouseMove()
    function phantomOnMouseMove(evt)
    {
        if (this.Drag)
        {
            var mouse_currX = evt.clientX;
            var mouse_currY = evt.clientY;
            var difX = mouse_currX - this.mouseStartX;
            var difY = mouse_currY - this.mouseStartY;
            this.mouseStartX = mouse_currX;
            this.mouseStartY = mouse_currY;

            this.GoTo(this.getX() + difX, this.getY() + difY);
        }
    }
    // }}} phantomOnMouseMove()

// }}} Event handlers

