/**
 * Javascript class HttpPush
 *
 * @category Public
 * @package BM
 * @author Rico Sonntag <rico.sonntag@netresearch.de>
 * @version $Id: http.push.js 1318 2010-06-21 12:23:31Z rico.sonntag $
 */
function HttpPush()
{
    var _pushUrl = null;
    var _cometd = null;
    var _connected = false;
    var _retries = 1;
    var _maxRetries = 3;

    /**
     * Get push url
     *
     * @access public
     * @return pushUrl
     */
    this.getUrl = function() {
        return _pushUrl;
    };

    /**
     * Set push url
     *
     * @access public
     * @param url
     */
    this.setUrl = function(url) {
        _pushUrl = url;
    };

    /**
     * Listener for unsuccessful events
     *
     * @param event
     */
    this.unsuccessfulEvent = function(event) {
        if (event.channel === '/meta/handshake') {
            // handshake failed
            if (!event.successful) {

                ++_retries;

                // Abort further connection attempts
                if (_retries > _maxRetries) {
                    event.advice.reconnect = 'none';

                    if (_cometd) {
                        _cometd.disconnect();
                    }

                    return;
                }

                // Perform disconnection if an error occurs
                if (event.error) {
                    event.advice.reconnect = 'none';

                    if (_cometd) {
                        _cometd.disconnect();
                    }
                }
            }
        }
    };

    /**
     * Listener for connect events
     *
     * @param event
     */
    this.connectEvent = function(event) {
        var wasConnected = _connected;
        _connected = event.successful === true;

        // connection established
        if (!wasConnected && _connected) {
            this.connected();
        } else if (wasConnected && !_connected) {
            // disconnected
            this.disconnected();
        }
    };

    /**
     *
     */
    this.connected = function() {
        alert('Needs to be overwritten by child class!');
    };

    /**
     *
     */
    this.disconnected = function() {
        alert('Needs to be overwritten by child class!');
    };
}

/**
 * Initialize the push and bind some event listeners to the meta channels
 *
 * @param params
 */
HttpPush.prototype.init = function(params) {
    try {
        _cometd = jQuery.cometd;

        if (_cometd) {
            // Configure cometd
            _cometd.configure( {
                // parmater : value // default
                'url' : this.getUrl(),
                'logLevel' : 'warn', // info
                'maxConnection' : 2, // 2
                'maxNetworkDelay' : 10000 // 10000
            });

            // Set up some event listeners
            _cometd.addListener('/meta/unsuccessful', this, this.unsuccessfulEvent);
            _cometd.addListener('/meta/connect', this, this.connectEvent);

            // Perform handshake
            _cometd.handshake(params);
        }
    } catch (ex) {
    }
};

