$j.fn.monitor_SSE_interface = function(options)
{ var settings = $j.extend(
           {  'debug' : 0,
              'group' : 0,
              'from_id' : 0,
              'int_update_terminal_status' : 60
           }, options);

    if (settings.debug)
    { $j('div#block_main').before('<div id="chat_debug_info"><div class="info"></div><br><strong>Отладка SSE:</strong><br><textarea></textarea></div>') ;
      show_debug_info('init') ;
    }

    window.onload = function setDataSource()
    {
		if (!!window.EventSource)
        {
			var source = new EventSource("/sse.php?from_id="+settings.from_id+'&group='+settings.group);

			source.addEventListener("message", function(e) {notifyUser(e) ;}, false);
			source.addEventListener("open", function(e) {if (settings.debug)show_debug_info('SSE OPENED') ;}, false);
			source.addEventListener("error", function(e) {if (e.readyState == EventSource.CLOSED) {if (settings.debug)show_debug_info('SSE CLOSED') ;}}, false);
		}
        else
        {   alert('SSE notSupported') ;
			//document.getElementById("notSupported").style.display = "block";
		}
    }

    /*$j('body').everyTime(settings.int_update_terminal_status*1000,'check_vt',function(){send_ajax_request({cmd:'aspmo/get_panel_terminal_status',group_id:settings.group})}) ;*/

    function show_debug_info(title,data)
    { var debug='' ;
      debug+='group='+settings.group+'<br>' ;
      debug+='from_id='+settings.from_id+'<br>' ;
      $j('div#chat_debug_info div.info').html(debug) ;
      var d = new Date();

      var data_str=htmlspecialchars(JSON.stringify(data, null, 4)) ;
      debug=d.toUTCString()+" - "+title+"\n\n"+data_str+"\n------------------------------------------------------------\n" ;
      var textarea=$j('div#chat_debug_info textarea') ;
      $j(textarea).append(debug) ;
      $j(textarea).animate({scrollTop:textarea[0].scrollHeight-textarea[0].clientHeight});
    }


    function notifyUser(mess)
    {  // var obj = $j.unserialize(mess.data);
       var obj = eval('('+mess.data+')') ;
       if (obj.notife_text)
       { var volume = (obj.volume) ? obj.volume : 0 ;
         var audio = (obj.audio) ? obj.audio : 0 ;
         if (obj.notife=='error')      new jBox('Notice', {content: obj.notife_text,color:'red',audio:audio,volume:volume});
         else if (obj.notife=='alert') new jBox('Notice', {content: obj.notife_text,color:'yellow',audio:audio,volume:volume});
         else if (obj.notife=='info')  new jBox('Notice', {content: obj.notife_text,color:'green',audio:audio,volume:volume});
         else                          new jBox('Notice', {content: obj.notife_text,audio:audio,volume:volume});
       }

       if (obj.update_page)  location.reload() ;

       //if (obj.ajax_cmd) send_ajax_request({cmd:obj.ajax_cmd,member_id:obj.member_id,terminal_id:obj.terminal_id})  ;
       if (obj.tr_id!=undefined && obj.tr_id>0 )
       {   if ($j('div#panel_last_mo tr#mo_tr_'+obj.mo_tr_id).length) $j('div#panel_last_mo tr#mo_tr_'+obj.mo_tr_id).replaceWith(obj.mo_tr) ;
           else                                                       $j('div#panel_last_mo tr.head').after(obj.mo_tr) ;
           //$j('div#debug_mo').html('last mo id: '+obj.mo_tr_id) ;
           $j('div.list_mo_sessions td.photo div.img img').hover(panel_image_preview_on,panel_image_preview_off) ;
           $j('td.preview_graf').hover(preview_graf_on,preview_graf_off) ;
       }


       // обновляем статусы у иконок терминалов
       if($j.isArray(obj.terminal_status) || typeof(obj.terminal_status) == 'object') for(var i in obj.terminal_status)
       {
          $j('#panel_terminal_status div#t_'+i).attr('status',obj.terminal_status[i]) ;
       }

       if (settings.debug)
       {   obj.mo_tr='...' ;
           show_debug_info('notifyUser',obj) ;
       }

       return true;
    }



} ;

function htmlspecialchars(string){ return $j('<span>').text(string).html() ;}


