	if(typeof IS_LOGIN_USER == 'undefined'){
		var IS_LOGIN_USER = false;
	}
	if(typeof APPLICATION == 'undefined'){
		var APPLICATION = '';
	}

	var InstantManager = new Object();

	InstantManager.registerGetter = [];
	InstantManager.maxReguest = 120;

	InstantManager.startTimer = function(){
		InstantManager.requestInterval = setInterval(function(){InstantManager.send('log=1');},30000);
	}
	InstantManager.stopTimer = function (){
		clearInterval(this.requestInterval);
	}
	InstantManager.resetMax = function(){

		var tmp = InstantManager.maxReguest;

		InstantManager.maxReguest = 120;

		if(tmp == 0){
			InstantManager.login();
		}
	}
	InstantManager.Loader = function (request,resultate){
		//trace(InstantManager.maxReguest+ ' :: '+request);
		var d = new Date();
		request += d.getTime();
		Svincs.Ajax.Request(APPLICATION + 'SERVISES.php',request,resultate,true);
	}
	InstantManager.send = function(request){
		if(this.maxReguest > 0){
			if(!request) request = '';

			var uservars = '';
			for(var i =0;i < InstantManager.registerGetter.length;i++){
				uservars += '&users_id[]='+InstantManager.registerGetter[i][0];
			}
			request += uservars;
			request += '&isnew=1';
			//debugger;
			InstantManager.Loader(request,InstantManager.getResultate);
			InstantManager.maxReguest--;
		}else{
			InstantManager.logout();
		}
	}
	InstantManager.logout = function (){
		var img = new Image();
			var d = new Date();
			img.src = APPLICATION + 'SERVISES.php?unlog=1&'+d.getTime();
		InstantManager.stopTimer();
	}
	InstantManager.login = function(){
		InstantManager.send('log=1');
		InstantManager.startTimer();
	}
	InstantManager.listenerUserOnLine = function (id,obj,exec){
		var check = false;
		for(var i=0;i<InstantManager.registerGetter.length;i++){
			if(InstantManager.registerGetter[i][0] == id){
				check = true; break;
			}
		}
		if(!check) InstantManager.registerGetter[InstantManager.registerGetter.length] = [id,obj,exec];
	}
	InstantManager.openReader = function(id){
		InstantManager.openMessanger(APPLICATION + 'instant_messenger.php?id='+id,'Read_'+id);
	}
	InstantManager.openSender = function(id){
		InstantManager.openMessanger(APPLICATION + 'instant_messenger_send.php?id='+id,'Send_'+id);
	}
	InstantManager.openHistory = function(id){
		var obj = window;
		if(opener){
			obj = opener;
		}
		obj.InstantManager.openMessanger(APPLICATION + 'instant_messenger_history.php?id='+id,'History_'+id);
	}
	InstantManager.openMessanger = function (url,title){
		Svincs.Window.openDialog(url,{
			dialog : true,
			resizable : 1,
			title : title
		});
	}
	InstantManager.getResultate = function (xml){
		if(xml){
			var _error = xml.getElementsByTagName('error');
			if(_error.length){
				var error_code = _error.item(0).firstChild.nodeValue;
				if(error_code == 1){
					alert('You not registred.Pleace login if wont use this interface!',function(){
						window.location.href = 'regist.php';
					});
				}
			}
//debugger
			var _new = xml.getElementsByTagName('new');
			for(var i=0;i<_new.length;i++){
				InstantManager.openReader(_new.item(i).firstChild.nodeValue);
			}

			var _targets = xml.getElementsByTagName('user');
			for(var i =0;i < InstantManager.registerGetter.length;i++){
				InstantManager.updateTarget(i,_targets);
			}
		}
	}
	InstantManager.updateTarget = function (id,_targets){
		for(var i=0;i<_targets.length;i++){
			if(_targets.item(i).firstChild.nodeValue == InstantManager.registerGetter[id][0]){
				InstantManager.registerGetter[id][2](1,InstantManager.registerGetter[id][1]);
				return ;
			}
		}
		InstantManager.registerGetter[id][2](0,InstantManager.registerGetter[id][1]);
	}
	InstantManager.checkUserOnLine = function (id,exec){
		Svincs.Ajax.Request(APPLICATION + 'SERVISES.php','isonline='+id,exec);
	}
	InstantManager.checkUserBlocken = function(id,exec){
		Svincs.Ajax.Request(APPLICATION + 'SERVISES.php','isblock='+id,exec);
	}

	if(!window.location.href.match(/instant_messenger/)){
		Svincs.addEvent(window,'unload',function(){
			if(IS_LOGIN_USER) InstantManager.logout();
		});
		Svincs.addEvent(window,'load',function(){
			if(IS_LOGIN_USER) InstantManager.login();
		});
		Svincs.addEvent(document,'mousemove',function(){
			if(IS_LOGIN_USER) InstantManager.resetMax();
		});
		Svincs.addEvent(document,'click',function(){
			if(IS_LOGIN_USER) InstantManager.resetMax();
		});
	}

	/// Popups

	function openChat(link) {
		Svincs.Window.openDialog(link + 'chat/flashchat.php',{
			dialog : false,
			resizable : 0,
			status : 0,
			title : 'chat',
			width : 725,
			height : 700
		});
	}
	function openTop10() {
		Svincs.Window.openDialog(APPLICATION + 'top10.php',{
			dialog : false,
			resizable : 0,
			status : 0,
			title : 'top10',
			width : 410,
			height : 435
		});
	}
	function openModPanel(url) {
		Svincs.Window.openDialog(url,{
			dialog : false,
			resizable : 1,
			scrollbars : 1,
			status : 0,
			title : 'MP',
			width : 940,
			height : 500
		});
		return false;
	}