/*
 * ICESOFT COMMERCIAL SOURCE CODE LICENSE V 1.1
 *
 * The contents of this file are subject to the ICEsoft Commercial Source
 * Code License Agreement V1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of the
 * License at
 * http://www.icesoft.com/license/commercial-source-v1.1.html
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations under
 * the License.
 *
 * Copyright 2009-2015 ICEsoft Technologies Canada, Corp. All Rights Reserved.
 */

if (window.ice && window.ice.icepush) {
    (function(namespace) {
        //include functional.js
        //include oo.js
        //include collection.js
        //include string.js
        //include window.js

        var heartbeatUpdateListeners = [];
        namespace.onHeartbeatUpdate = function(callback) {
            append(heartbeatUpdateListeners, callback);
        };

        onLoad(window, function() {
            var connection = namespace.push.connection;

            //change heartbeat at server's request
            connection.onReceive(function(header, text, xml) {
                var interval = header('ice.push.heartbeat');
                if (interval) {
                    namespace.push.connection.changeHeartbeatInterval(Number(interval));
                    broadcast(heartbeatUpdateListeners, [Number(interval)]);
                }
            });

            //publish function used to pause the blocking connection
            namespace.push.pauseBlockingConnection = function(url) {
                namespace.push.connection.pauseConnection();
                namespace.push.connection.controlRequest(noop, function(header) {
                    //mark request as a pushID parking request
                    header('ice.parkids', 'true');
                    header('ice.notifyBack', url)
                }, noop);
                namespace.push.connection.pauseConnection();
            };

            //publish function used to resume running the blocking connection
            namespace.push.resumeBlockingConnection = function() {
                //the last response before pausing the connection was aborted thus failing to increment the sequenceNo
                connection.resumeConnection();
            };
        });
    })(window.ice);
}
