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

    /**
     * Loading element Types
     */
    PHP2Ajax.LOADER_TYPE_INLINE    = 'inline';
    PHP2Ajax.LOADER_TYPE_OVERPAGE  = 'overpage';

    /**
     * PHP2Ajax is the namespace for AJAX JavaScript functions and Classes.
     *
     * @author   NULL <null.is.not.0@gmail.com>
     * @version  $Id: ajaxloader.js, v 2.3.0 2006/10/24 $
     * @access   public
     * @package  php2
     */
    PHP2Ajax.LoadManager = function(id, flatElementId, loaderType, sRootUrl)
    {
        this.id = ((id) ? id : 'ajaxLoadManager');

        // --- Set ajaxPage Object --- //
        if (flatElementId) this.flatElement = document.getElementById(flatElementId);

        // --- Set Loader Type --- //
        this.setLoaderType(loaderType);

        this.sRootUrl = ((sRootUrl) ? sRootUrl : '/');

        // --- Setting Loading Data Element --- //
        if (!document.getElementById(this.id))
        {
            preloaderHTML  = '<table id="' + this.id + '" class="ajaxLoader" style="padding-left: 10px; position: absolute; display: none;" border="0" cellpadding="0" cellspacing="0">';
            preloaderHTML += '<tr><td id="' + this.id + '_innerCell" valign="middle" align="center">';
            preloaderHTML += '<img src="' + this.sRootUrl + 'images/ajax/loading.circle.gif" height="28" width="28" /> <b><span id="' + this.id + 'Caption">Loading ...</span></b>';
            preloaderHTML += '</td></tr>';
            preloaderHTML += '</table>';

            document.body.insertAdjacentHTML("afterbegin", preloaderHTML);
        }

        // --- Assigning PreLoader HTML Controls --- //
        this.loader = document.getElementById(this.id);
        this.captionElement = document.getElementById(this.id + 'Caption');

        // --- Set Inline Preloader Height and Width --- //
        this.inlineWidth   = 100;
        this.inlineHeight  = 25;
        this.opacity       = 0.30;
    }

    /**
     * Show Loading Div element
     *
     * @param  string loaderType
     */
    PHP2Ajax.LoadManager.prototype.setLoaderType = function(loaderType)
    {
        if (loaderType && (loaderType == PHP2Ajax.LOADER_TYPE_OVERPAGE))
        {
            this.loaderType = PHP2Ajax.LOADER_TYPE_OVERPAGE;
        }
        else
        {
            this.loaderType = PHP2Ajax.LOADER_TYPE_INLINE;
        }
    }

    /**
     * Assigns Loader text Caption
     *
     * @param  string caption
     */
    PHP2Ajax.LoadManager.prototype.setCaption = function(caption)
    {
        this.caption = caption;

        this.captionElement.innerHTML = this.caption;
    }

    /**
     * Assign Loader Class Name
     *
     * @param  string className
     */
    PHP2Ajax.LoadManager.prototype.setClassName = function(className)
    {
        this.loader.className = className;
    }

    /**
     * Show Loading Div element
     *
     * @param  HTMLElement htmlObject
     */
    PHP2Ajax.LoadManager.prototype.show = function()
    {
        if (this.loaderType == PHP2Ajax.LOADER_TYPE_INLINE)
        {
            var posX  = HTMLElement.findPosX(this.flatElement) + this.flatElement.offsetWidth + 10;
            var posY  = HTMLElement.findPosY(this.flatElement);
            if (posX + this.inlineWidth >= HTMLElement.getBrowserWidth())
            {
                posX = posX - this.flatElement.offsetWidth - 20 - this.inlineWidth;
            }

            // alert(posX + " " + HTMLElement.findPosX(this.flatElement) + " " + this.flatElement.offsetWidth);
            this.setLoaderSize(posX, posY, this.inlineWidth, this.inlineHeight);

            // --- Setting Preloader Opacity --- //
            this.loader.style.opacity     = null;
            this.loader.style.MozOpacity  = null;
            if (this.loader.style.filter) this.loader.style.filter = null;
        }
        else
        {
            var posX = HTMLElement.findPosX(this.flatElement);
            var posY = HTMLElement.findPosY(this.flatElement);
            var width  = this.flatElement.offsetWidth;
            var height = this.flatElement.offsetHeight;

            this.setLoaderSize(posX, posY, width, height);

            // --- Setting Preloader Opacity --- //
            this.loader.style.opacity     = this.opacity;
            this.loader.style.MozOpacity  = this.opacity;
            if (this.loader.style.filter != null) this.loader.style.filter = "alpha(opacity="+ (this.opacity * 100) +")";
        }

        this.loader.style.display  = 'inline';
        // alert('Loading ' + this.loader.id);
    }

    /**
     * Show Loading Div element
     *
     * @param  integer posX
     * @param  integer posY
     * @param  integer width
     * @param  integer height
     */
    PHP2Ajax.LoadManager.prototype.setLoaderSize = function(posX, posY, width, height)
    {
        // --- Find and Set current Preloader Position --- //
        this.loader.style.left    = posX + "px";
        this.loader.style.top     = posY + "px";
        this.loader.style.padding = 0;
        this.loader.style.width   = width + "px";
        this.loader.style.height  = height + "px";

        // --- Inner Cell --- //
        innerCell = document.getElementById(this.id + "_innerCell");
        if (innerCell != null)
        {
            innerCell.style.left    = posX + "px";
            innerCell.style.top     = posY + "px";
            innerCell.style.width   = width + "px";
            innerCell.style.height  = height + "px";
        }
    }

    /**
     * Hide Loading Div element
     *
     */
    PHP2Ajax.LoadManager.prototype.hide = function()
    {
        // --- Setting Loading Data Command --- //
        if (typeof(this.loader) != "undefined") this.loader.style.display  = 'none';
        // alert('Unloading ' + this.loader.id + " " + this.loader.style.display);
    }
