/**
 * Express message library.
 *
 * @package      TenisPortal
 * @subpackage   JavaScript
 * @author       Svec Jiri <jiri.svec@livesport.cz>
 * @copyright    (c) 2008 Livesport, s.r.o.; {@link http://www.livesport.cz/}
 * @since        2006
 */

/**
 * Constructor.
 */
function ExpressMessage()
{
    this._init();
};

/**
 * Initialization of the object.
 * 
 * @return {ExpressMessage}   Returns a fluent interface.
 */
ExpressMessage.prototype._init = function()
{
    this.last      = 0;
    this.handler   = null;
    this.activeGet = false;
    
    // default timeout (in miliseconds)
    this.timeOut   = 5000;
    
    return this;
};

/**
 * Show express message content.
 * 
 * @param  {Integer} id   ID of express message. 
 * @return {ExpressMessage}   Returns a fluent interface.
 */
ExpressMessage.prototype.show = function(id)
{
    if (this.last != 0) {
        cJsLib.rmCls(cJsLib.$('p-emsg-' + this.last),'active');
        cJsLib.setCls(cJsLib.$('emsg-' + this.last), 'hidden');
    }
    
    if (id != this.last && id != 0) {
        this.activeGet = true;
        var ai         = ajaxObject.length;
        ajaxObject[ai] = new XHR();
        var ajax       = ajaxObject[ai];
        
        ajax.onCompletion = function(responseXML, response)
        {
            if (typeof this.last != "undefined" || this.last != null) {
                contElem = cJsLib.$('emsg-' + this.last);
                cJsLib.setCls(contElem, 'hidden');
                cJsLib.setCls(cJsLib.$('p-emsg-' + this.last), 'express');
            }
            
            mContainer           = cJsLib.$('emsg-' + id);
            mContainer.innerHTML = response;
            this.last            = id;

            cJsLib.addCls(cJsLib.$('p-emsg-' + id), 'active');
            cJsLib.rmCls(cJsLib.$('emsg-' + id));
            
            this.activeGet = false;
        };

        ajax.setMethod('GET').setVar('id', id).setRequestFile('/res/ajax/get-express-message.php');
        ajax.runAjax();

        this.last = id;
    } else {
        this.last = 0;
    }
    
    return this;
};

/**
 * Clear timeout handler.
 * 
 * @return {ExpressMessage}   Returns a fluent interface.
 */
ExpressMessage.prototype.focus = function()
{
    clearTimeout(this.handler);
    
    return this;
};

/**
 * Set timeout handler.
 *
 * @return {ExpressMessage}   Returns a fluent interface.
 */
ExpressMessage.prototype.blur = function()
{
    this.handler = setTimeout('eMsg.show(0)', this.timeOut);
    
    return this;
};