﻿/*
** Javascript function object used for getting the server to check every now and again for
** Invitation or status updates independently of the chat application
*/ 
var SyfadisChatStatus = function() {

    var _interval = 3000;
    var _url;
    var _windowOptions = "";
    var _currentInvites = null;
    var _source = null;

    return {
        //
        // Starts the timer which polls the server for chat requests
        //
        StartTimer : function( interval, url, windowOptions, sourceButtonId ) {
            _interval = interval;
            _url = url;
            _windowOptions = windowOptions;
            _source = $get ( sourceButtonId );
            
            
            _currentInvites = [];
  
            // For the first call we don't use the normally longer interval : _interval.
            // Instead we use a short interval initially to get any current invitations.  If we
            // are using the main application page refreshes may reset the notification control
            // and therefore continually deffering the notification service
            setTimeout( GetGlobalChatStatus, 2000 );
            
        },
        
//        OpenChatWindow : function() {
//            _openedWindow = window.open( _url, '_blank', _windowOptions, false );
//            return false;
//        },
        
        OpenChatWindow : function(url, windowOptions ) {
            _openedWindow = window.open(url, '_blank', windowOptions, false);
            return false;
        }
    };
    
    /*
    ** Private functions
    */
    
//    function StartChatting() {
//        _openedWindow = window.open( _url, '_blank', _windowOptions, false );
//        return false;
//    }
    
//    function Cleanup () {
//        alert("unload");
//    }
    //
    // Call the WebService for status report
    // 
    function GetGlobalChatStatus() {
        try {
            ChatWebService.GetGlobalStatus( OnStatusUpdate, OnError );
        } catch (exception)
        {
        }
    }
    
    //
    // Handle data received from the server
    //
    function OnStatusUpdate( result ) {
        if ( result ) {
            var data = Sys.Serialization.JavaScriptSerializer.deserialize( result );

            var invite = null;
            var inviteId = null;
            for( var i in data ) {
                inviteId = data[i].InviteId;

                if ( ! _currentInvites[ inviteId ] ) {
                    _currentInvites[inviteId ] = inviteId;
                    openRadWindow(_url + "?ConfirmInvitation=true&idInvite=" + inviteId, '', '320', '150');
                    //window.open( _url + "?idInvite=" + inviteId, "_blank", _windowOptions );
                }
           }
        }

        setTimeout( GetGlobalChatStatus, _interval ); 
    }
    
    function OnError( error ) {
        // retry again in a couple of seconds
        setTimeout( GetGlobalChatStatus, 2000 );
    }

}();

if (typeof(Sys) !== "undefined") {
    Sys.Application.notifyScriptLoaded();
}
