
/**
 * File contains JS Library for Helper Control
 *
 * JavaScript  version 1
 * @category   JavaScript Libraries
 * @author     NULL <null.is.not.0@gmail.com>
 * @copyright  (c) 2007-2008 by NULL
 * @version    SVN: $Id: 206$
 * @link       NULL <null.is.not.0@gmail.com>
 * @since      File available since Release 2.3.0
 */

if (typeof(PHP2Controls) == 'undefined') PHP2Controls = new Object();

    /**
     * Last Showed Helper Object
     */
    PHP2Controls.lastShowedHelper = null;

    /**
     * PHP2Controls.Helper is the namespace and JS Class for Helper control.
     *
     * @author   NULL <null.is.not.0@gmail.com>
     * @version  $Id: helper.common.js, v 2.3.0 2006/09/19 $
     * @access   public
     * @package  php2
     */
    PHP2Controls.Helper = function(objectName, flatObjectId, hlpImageObjectId, hlpObjectOffset)
    {
        // --- Creating Protect Frame for IE --- //
        /**
         * Unique Control ID
         *
         * @var  string
         */
        this.id             = objectName;

        /**
         * Control HTML Element (Helper DIV)
         *
         * @var  DomElement
         */
        this.htmlObject = document.getElementById(this.id);

        /**
         * Helper Flat HTML Control
         *
         * @var  DomElement
         */
        this.htmlFlatObject = document.getElementById(flatObjectId);

        /**
         * Control HTML Element (Helper DIV)
         *
         * @var  DomElement
         */
        this.htmlImageObject = (hlpImageObjectId) ? document.getElementById(hlpImageObjectId) : document.getElementById(this.id + '_image');

        /**
         * Unique Protect Frame ID
         *
         * @var  string
         */
        this.protectFrameId = null;

        /**
         * Current Helper object Offset
         *
         * @var  string
         */
        this.hlpObjectOffset = (hlpObjectOffset) ? hlpObjectOffset : 10;

        /**
         * Reference to the Current Helper Object
         *
         * @var  string
         */
        this.currentObject = this;

        /**
         * Visibility flag
         *
         * @var  string
         */
        this.visible = false;

        /**
         * Offset Width of the control
         *
         * @var  string
         */
        this.offsetWidth = 0;

        /**
         * Auto Hide TimeOut for helper Object
         *
         * @var  integer
         */
        this.autoHideTimeOut = 10000;

        /**
         * Hide TimeOut for helper Object
         *
         * @var  integer
         */
        this.hideTimeOut = 500;

        /**
         * Hide Helper Signal
         *
         * @var  boolean
         */
        this._hideSignal = false;

    }

    /**
     * Shows/Hides Helper Object
     *
     * @param   string winObjectId MessageBox object ID
     * @param   string protectFrameId MessageBox Protectd IE Frame ID
     * @return  void
     */
    PHP2Controls.Helper.prototype.show = function()
    {
        // --- Hiding Last Helper if it is Not Current Helper --- //
        if (PHP2Controls.lastShowedHelper && (PHP2Controls.lastShowedHelper.id != this.id))
        {
            PHP2Controls.lastShowedHelper.hide();
        }
        else if (PHP2Controls.lastShowedHelper && (PHP2Controls.lastShowedHelper.id == this.id))
        {
            if (this.visible)
            {
                return true;
            }
        }

        // --- Setting Last Showed Helper As Current Helper
        PHP2Controls.lastShowedHelper = this;
        this._hideSignal              = false;

        // --- If Current object is not Visible - Setting it As visible --- //
        if (!this.visible)
        {
            // --- Settinng Auto hide TimeOut for The current Helper Object --- //
            var currentHelperObject = this;
            currentHelperObject.autoHide = function()
            {
                currentHelperObject.hide();
            }
            if (typeof(currentHelperObject.autoHide) == 'function') timerID = setTimeout(currentHelperObject.autoHide, this.autoHideTimeOut);

            // --- Displaing Helper and Setting its offset Width --- //
            this.htmlObject.style.display  = 'inline';
            if (this.htmlObject.offsetWidth && !this.offsetWidth) this.setOffsetWidth(this.htmlObject.offsetWidth);

            // --- Finding Flat Object and its position --- //
            hlpFlatObject  = (this.htmlImageObject) ? this.htmlImageObject : this.htmlFlatObject;
            offsetLeft     = HTMLElement.findPosX(hlpFlatObject);
            offsetTop      = HTMLElement.findPosY(hlpFlatObject);

            currPosX       = HTMLElement.findPosX(this.htmlObject);
            this.htmlObject.style.top      = offsetTop + "px";

            // alert(offsetLeft + " " + hlpFlatObject.id + " " + hlpFlatObject.offsetWidth + " " + this.offsetWidth);
            // --- Checking browser width and Setting Helper Object Left position --- //
            if((offsetLeft + hlpFlatObject.offsetWidth + this.offsetWidth) >=  HTMLElement.getBrowserWidth())
            {
                var htmlObjectPosLeft   = (offsetLeft - this.offsetWidth - this.hlpObjectOffset);
                var htmlObjectPosRight  = (offsetLeft - this.hlpObjectOffset);
                if (this.htmlFlatObject)
                {
                    // --- Helper Object Cannot Override Control Object --- //
                    var htmlFlatObjectOffsetLeft   = HTMLElement.findPosX(this.htmlFlatObject);
                    var htmlFlatObjectOffsetWidth  = this.htmlFlatObject.offsetWidth;
                    if (
                         ((htmlObjectPosLeft >= htmlFlatObjectOffsetLeft) && (htmlObjectPosLeft < htmlFlatObjectOffsetLeft + htmlFlatObjectOffsetWidth))
                         ||
                         ((htmlObjectPosRight >= htmlFlatObjectOffsetLeft) && (htmlObjectPosRight < htmlFlatObjectOffsetLeft + htmlFlatObjectOffsetWidth))
                       )
                    {
                        // --- Moving Helper Control to The Left --- //
                        htmlObjectPosLeft   = (htmlFlatObjectOffsetLeft - this.offsetWidth - this.hlpObjectOffset);
                        htmlObjectPosRight  = (htmlFlatObjectOffsetLeft - this.hlpObjectOffset);
                    }
                }

                this.htmlObject.style.left     = htmlObjectPosLeft + "px";
                this.htmlObject.style.right    = htmlObjectPosRight + "px";
            }
            else
            {
                this.htmlObject.style.left     = (offsetLeft + hlpFlatObject.offsetWidth + this.hlpObjectOffset) + "px";
            }

            // --- Protecting Helper Div in IE --- //
            this.protectFrameId = HTMLElement.protectIEDiv(this.id);
            this.visible = true;
        }
        else
        {
            // this.hide();
        }
    }

    /**
     * Auto Hiding Helper Object
     *
     * @return  void
     */
    /*PHP2Controls.Helper.prototype.autoHide = function()
    {
        this.hide();
    }*/

    /**
     * Hiding Helper Object
     *
     * @return  void
     */
    PHP2Controls.Helper.prototype.hide = function()
    {
        if (this.visible)
        {
            if (this._hideSignal)
            {
                this.htmlObject.style.display = 'none';
                this.visible                  = false;
                this._hideSignal              = false;

                // --- Hiding Protect frame --- //
                if (this.protectFrameId && (document.all.item(this.protectFrameId) != null)) document.all.item(this.protectFrameId).style.display  = 'none';
            }
            else
            {
                // --- Settinng hide TimeOut for The current Helper Object --- //
                var currentHelperObject          = this;
                currentHelperObject._hideSignal  = true;
                currentHelperObject.hideDelayed  = function()
                {
                    currentHelperObject.hide();
                }
                timerID = setTimeout(currentHelperObject.hideDelayed, this.hideTimeOut);

            }
        }
    }

    /**
     * Setting Helper Object offset Width
     *
     * @return  void
     */
    PHP2Controls.Helper.prototype.setOffsetWidth = function(offsetWidth)
    {
        this.offsetWidth = offsetWidth;
    }
