var OnAirMess = new Class({
	
	initialize: function(){
                this.domain="http://"+document.domain;
		        this.curent_pos=0;
		        this.message_title1='Ошибка';
		        this.message_title2='Сообщение';
		        this.message_error1='Сообщение не может быть пустым!';
		        this.message_error2='Вы ввели слишком длинное сообщение!';
		        this.message_error3='В данный момент вы не можете оставлять сообщения,\nтак как ваше сообщение уже присутствует в  последней десятке.';
		        this.message_content='Ваше сообщение успешно добавлено в горячую десятку! Теперь ваше сообщение видят все!';
		        this.roars;
	},
   
    SendMessage: function(){
  
   	       if(this.Validate($("onm_content").value)) return;
	    
    	       new Ajax(onair_mess.domain+"/ajax.php?do=onair_mess&op=send_mess", {
		    		method: 'post',
		    		data: {
		    		message:$("onm_content").value,
		    		conv_mess:global.encodeVAR($("onm_content").value),
		    		foto:global.member_id['foto']
		    		},
		    	     onComplete: function(){
                     onair_mess.Edit(false);
                     onair_mess.curent_pos=0;
                     onair_mess.GetData();
                     $("onm_content").value="";
                     //new Alert().alert(onair_mess.message_title2,onair_mess.message_content);
				}}).request();
    
    },
    
    Validate: function(message){
     	if(message=="") { new Alert().alert(this.message_title1,this.message_error1);
     	 return true;}
     	if(message.length>300) { new Alert().alert(this.message_title1,this.message_error2); return true;}
      /*
     	for(i=0;i<this.data.length;i++){
     		if(this.data[i].id==global.member_id['user_id']) {
     			this.Edit(false);
     			new Alert().alert(this.message_title1,this.message_error3);
     			return true;
     		}
     	}
     	 */ 	
     },
     	
     GetData: function(){
		    new Json.Remote(onair_mess.domain+"/uploads/common/onair_mess.dat?anticache="+$time(),{
		    onComplete: function(data){ 
		    	         onair_mess.data=data;
		    	         onair_mess.ShowMessage();
		                 },
		    method:'get'
		    }).send();
     },
      
      GoTo: function(location){
	      	if(location=='next') this.curent_pos=this.curent_pos+1;
	      	else this.curent_pos=this.curent_pos-1;
	       	this.ShowMessage();
      },
      
      ShowMessage: function(){
	         $("onm_name").innerHTML=this.data[this.curent_pos].name;
			 $("onm_message").innerHTML=this.data[this.curent_pos].message;
		     var user_path="/user/"+this.data[this.curent_pos].user_path;
	         $("onm_link").href=user_path+this.data[this.curent_pos].subdomain+".html";
	         $("onm_link2").href=user_path+this.data[this.curent_pos].subdomain+".html";
	         $("onm_avatar").src=user_path+"avatar."+this.data[this.curent_pos].foto;
	         $("onm_status").src=user_path+"status.gif?"+$time();
	
	        if(this.curent_pos>(this.data.length-2)) $("onm_next_button").style.display="none";
	        else $("onm_next_button").style.display="block";
	        if(this.curent_pos<1) $("onm_prev_button").style.display="none";
	        else $("onm_prev_button").style.display="block";
	   },

	   Edit: function(state){
         	if(!global.member_id['user_id']) { new Alert().alert(this.message_title1,'Только зарегестрированые пользователи могут оставлять сообщения!'); return;}
	   	    if(state){
   		 		$("onm_link2").href=global.member_id['path']+global.member_id['name'].toLowerCase()+".html";
                $("onm_avatar").src=global.member_id['foto'];
                $("onm_box").style.display="none";
	        	$("onm_editbox").style.display="block";
	        	$("onm_next_button").style.display="none";
	        	$("onm_prev_button").style.display="none";
	        	$("onm_content").focus();
	      	}else{
	      		this.ShowMessage();
				$("onm_box").style.display="block";
	        	$("onm_editbox").style.display="none";
	      	}
	     },
	     
	  
	     
	  GetBirthdayData: function(){
		    new Json.Remote(onair_mess.domain+"/uploads/common/birthday.dat?anticache="+$time(),{
		    onComplete: function(data){ 
		    	         onair_mess.birthday=data;
		    	         if(!onair_mess.birthday) return;
		    	         onair_mess.cur_max=(onair_mess.birthday.length)-1;
		    	         onair_mess.cur_birth=Math.floor(Math.random()*onair_mess.cur_max);
		    	         onair_mess.ShowBirthdayBox();
		    	         $('birthday_box').style.display='block';
		    	         onair_mess.slideshow=window.setInterval('onair_mess.GoToBirth("next");',4000);
		                 },
		    method:'get'
		    }).send();
     },
	
      StopSlide : function(){
	      	window.clearInterval(onair_mess.slideshow);
      },
     
      GoToBirth: function(location){
	      	if(location=='next') {
	      	 	if(this.cur_birth==this.cur_max) this.cur_birth=0;
	      	 	else this.cur_birth=this.cur_birth+1;
	      	}
	      	else {
	      		if(this.cur_birth==0) this.cur_birth=this.cur_max;
	      		else this.cur_birth=this.cur_birth-1;
	      	}
	       	this.ShowBirthdayBox();
      },
      
     ShowBirthdayBox: function(){
     	  $("bir_name").innerHTML=this.birthday[this.cur_birth].name;
    	  var user_path="/user/"+this.birthday[this.cur_birth].user_path;
	      $("bir_link").href=user_path+this.birthday[this.cur_birth].subdomain+".html";
	      $("bir_link2").href=user_path+this.birthday[this.cur_birth].subdomain+".html";
	      $("bir_avatar").src=user_path+"avatar."+this.birthday[this.cur_birth].foto;
	      $("bir_status").src=user_path+"status.gif?"+$time();
    }
    
});

var onair_mess = new OnAirMess();
