


var Messanger = new Class({
	initialize: function(){
        this.domain="http://"+document.domain+"/";
        this.call_to_agent =0.3;
        this.curent_mess=0;
        this.user_path=false;
        this.counter=0;
        this.blink=false;
        this.blink_flag=false;
        this.standart_title=false;
        this.firs_mess=false;
        this.sound_path='http://dev.tusa.co.il//uploads/common/1.wav';
        this.from=false;
          
	},      
	

	 CancelSubscr: function(mess_id){
    	if (!confirm("Больше не сообщать вам о новых комментариях в этой записи ?"))  return;
        	    	         
		new Ajax(this.domain+"ajax.php?do=subscribe&op=cancel", {
			method: 'get', 
			data: { 'mess_id':mess_id}, 
			update: 'message_box'
		 }).request();
	 },
	
	approveFriend: function(uid, act){
     new Ajax('/ajax.php?do=friends&op=approve', {
      data: {user_id: uid, action: act},
      method:'post'
     }).request();
    },

	
	SoundSwitch: function(stat){
	    	$("mess_main").style.display="none";
	    	$("mess_loading").style.display="block"; 
		
		new Ajax(this.domain+"ajax.php?do=inmail&op=sound",{
				method: 'get',  
				update:'message_box',
				data: { 'stat':stat},
				onComplete: function(){ 
				     if(stat=='on') new Alert().alert("Сообщение","Звук включён! Теперь вы будите слышать как приходят сообщения!");
				     else new Alert().alert("Сообщение","Звук отключён!");
				     
				}
			 }).request();
	     
	 },
		
	Sound_play: function(src){
		return;
		this.Stop_play();
			var elm;
			if (typeof document.all != "undefined") {
			elm = document.createElement("bgsound");
			this.elm.src = src;
			document.body.appendChild(elm);
			return true;
			}
	},
	
	 Stop_play: function(){
	     if (this.elm) { this.elm.parentNode.removeChild(this.elm);	 this.elm = null; }
	 },
	
	
    Call2Agent: function(){
    	   if(!global.member_id) return;	
    	   if(mess.counter<5){
        	   if(!this.user_path) this.user_path=global.member_id['path'];
    	        new Json.Remote(this.user_path+"agent.dat?cache="+$time(),{
			    onComplete: function(data){ 
			    	if(!data) return;
				    if(mess.curent_mess!=data.mess_id){
				      if(mess.firs_mess){
					      mess.blink_flag=true;
					      mess.BlinkTitle(1);
					      mess.sound=true;	
					      mess.ShowBox();
					  }
					 mess.firs_mess=true;   
				     mess.curent_mess=data.mess_id;
				    }
                },
	    	    method:'get'
		       }).send();
    	   }
        window.setTimeout('mess.Call2Agent();',this.call_to_agent*60000);
		   
    },
    
  
    BlinkTitle: function(param){
	      if(param==1 && this.standart_title) return;
	        if(this.blink_flag){
		      	if(!this.standart_title) this.standart_title=document.title;
	
		      	if(!this.blink) {  	document.title='Вам сообщение!'; this.blink=true;}
		    	else{ document.title='----------------------------';   this.blink=false;}
	   	
		    	setTimeout("mess.BlinkTitle(0);",800);
		    }else{
		      	document.title=this.standart_title;
		      	this.standart_title=false;
		    }
	},
    
	ShowLoading: function(){
		$("mess_main").style.display="none";
	    $("mess_loading").style.display="block";
	},
    
    
    ShowBox: function(mess_id){
    	
    	if(global.member_id=='null') global.member_id = global.getUserData();
    	if(!global.member_id) return;
        if(mess_id) this.ShowLoading();
	    	         
		new Ajax(this.domain+"ajax.php?do=inmail&op=messanger",{
			method: 'get',  
			data: { 'mess_id':mess_id,'from':mess.from}, 
			update: 'message_box'
		 }).request();
	 },
    
	 
	 
	  BlockSpamer: function(spamer_id,mess_id){
    	if (!confirm("Вы действительно хотите сообщить о спаме?"))  return;
        	    	         
		new Ajax(this.domain+"ajax.php?do=inmail&op=blockspamer", {
			method: 'get', 
			data: { 'spamer_id':spamer_id,'mess_id':mess_id}, 
			update: 'message_box',
			onComplete: function(){
			        new Alert().alert("Сообщение","Ваше сообщение отправленно!");
				}
		 }).request();
	 },
	 
    SubLook: function(mess_id,url){
		this.blink_flag=false;
		  
		if(mess_id) this.ShowLoading();
		
		new Ajax(this.domain+"ajax.php?do=inmail&op=remove_row",{
				method: 'get',  
				data: { 'mess_id':mess_id},
				onComplete: function(){
			        document.location=url;
				}
			 }).request();
	},
	 
	Close: function(mess_id){
		this.blink_flag=false;
		  
		if(mess_id) this.ShowLoading();
		
		new Ajax(this.domain+"ajax.php?do=inmail&op=remove_row",{
				method: 'get',  
				data: { 'mess_id':mess_id},
				update:'message_box'
			 }).request();
	},
	
	
	CloseAll: function(){
		if (!confirm("Вы уверены что хотите закрыть все сообщения?"))  return;
		this.blink_flag=false;
		  
		 this.ShowLoading();
		
		new Ajax(this.domain+"ajax.php?do=inmail&op=close_all",{
				method: 'get',  
				update:'message_box',
				onComplete: function(){
					 new Alert().alert("Сообщение","Все сообщения закрыты!");
				}
			 }).request();
	},

	SendWindow: function(user_id,mess_id){
		if(!global.member_id) {
				new Alert().alert("Сообщение","Писать сообщения могут только зарегестрированые пользователи!");
			return;
		}
		var pop_width=600; var pop_height=370;
		if(mess_id) var param="?mess_id="+mess_id;
		else param="";
	    var s=this.domain+"inmail/sendwin/"+user_id+".html"+param;
	
	    newWin = window.open(s,user_id,'width='+pop_width+',height='+pop_height+',resizable=no,scrollbars=no'); 
	    newWin.moveTo((screen.width-pop_width)/2,(screen.height-pop_height)/2);
	    newWin.focus();
	},
	
	    
    RunAgent: function(){
    	window.setTimeout('mess.Call2Agent();',mess.call_to_agent*60000);
    },
    
    GoToPage: function (url){
      window.open(url,'newwin','width='+window.screen.availWidth+',height='+window.screen.availHeight+',top=0,left=0,menubar=yes,location=yes,resizable=yes,scrollbars=yes,status=yes'); 
    }
 
});

var mess = new Messanger();

 