refresh_dmail();
var sf = 0;
var timeoutID2;
var dm = 0;

function title_dmail(stop_ref) {
		/*
		if(stop_ref != 'stop'){
			if(dm == 0) {
				document.title = 'Dmail - Dmail - Dmail - Dmail - Dmail '; dm = 1;
			}
			else if(dm == 1){
				document.title = '                                                             '; dm = 0;
			}
			else if(dm == 3){
				document.title = 'Активисты радио DFM-Ставрополь'; dm = 3;
			}	
			
			var tID2 = setTimeout(title_dmail, 500);					
		}
		else {
			var tID2 = setTimeout(title_dmail, 0);
			document.title = 'Активисты радио DFM-Ставрополь';
			dm = 3;
		}
		*/
	}


function refresh_dmail() {
	
	
	$.getJSON("http://26dfm.ru/jquery/dmail.php", function(json) {
	
		if(json.length) {		
				document.title = 'Dmail - Dmail - Dmail - Dmail - Dmail ';
				var answer_form = '<center><form action="" method="post" id="answer_form"><textarea class="txt" style="margin-left: 5px; width: 367px;" id="dm_msg" name="dm_msg_" value=""></textarea><input type="hidden" name="mes_id" value="'+json[0].mes_id+'"><input type="hidden" name="to" value="'+json[0].from_id+'"><br><input class="inputt" type="submit" name="dm_send" value="Ответить" onClick="return otvet();"><div id="bdin" style="display: none;"><img src="http://26dfm.ru/images/loading_.gif"> - Подождите, кажется отправляется...</div></form></center>';
			
				$('#dmail').html('<center><div id="indik" onClick="razvorot();" title="Нажмите сюда, что бы прочитать сообщение"><img src="http://26dfm.ru/images/mail.gif" alt="D-mail новое сообщение" /><br><br>Новое сообщение от<br>'+json[0].korona+' <b>'+json[0].from+'</b><br><a href="#" onClick="return razvorot();">читать</a></div></center><div id="dmail-content" style="padding: 10px; display: none;"><table border="0"><tr><td><img src="http://26dfm.ru/avatars/'+json[0].avatar+'"></td><td valign="top">'+json[0].korona+' <b>'+json[0].from+'</b><br>'+json[0].from_group+'<br>'+json[0].online+'<br>Дата:<br>'+json[0].date+'</td></tr></table><br><div style="overflow: auto; height: 170px; border: 1px solid #cccccc; padding: 5px; margin: 5px; background: #FFF; text-align:left;">'+json[0].msg+'</div><br>'+answer_form+'<div id="zak" style="position: absolute; right: 20px; top: 20px; text-align: right;"><a href="#" onClick="return close_mail_window('+json[0].mes_id+')" title="Закрыть это сообщение" class="button_close"></a><br><a href="index.php?page=user_dialog&amp;id='+json[0].from_id+'" title="Смотреть диалог с этим пользователем" class="button_beseda"></a><br><a href="index.php?page=user&amp;user_id='+json[0].from_id+'" title="Информация о пользователе" class="button_info"></a></div></div>');
				
				$('#indik').css('cursor','pointer');
				
				$('#dmail').css('display','none');
				$('#dmail').css('width','150px');
				$('#dmail').css('height','70px');				
				$("#dmail").slideDown(500);
				
		}
		else{
			
			$("#dmail").slideUp(500, function(){
			
				$("#dmail").html('');				
				
				/*window.location.reload();*/
				$('#dmail').fadeOut(300);				
				
				
			});
			
			timeoutID2 = setTimeout(refresh_dmail, 5000);
			
		}
	
	});
	
}

function razvorot(){	
	
	$("#dmail").animate({width: "400px",height: "483px"}, 100, function(){
		
		$('#indik').fadeOut(100,function(){								
		
		
		
			$('#dmail-content').css('border','1px solid #527285');
			
			$('#dmail-content').css('background','#ccd7e7');
			
			$('#dmail-content').css('height','460px');
			
			$('#dmail-content').fadeIn(300);
			
			$('#dm_msg').attr('value','').focus(); 
			
			document.title = 'Активисты радио DFM-Ставрополь';
		});
						
	});
						
}


function close_mail_window(mes_id) {
	$("#zak").html('<img src="http://26dfm.ru/images/loading_.gif"><br>Подождите пожалуйста.<br>Идет обработка.');
	$.getJSON("http://26dfm.ru/jquery/read_dmail.php?mes_id="+mes_id, function(json) {
		if(json.length) {
		
			$("#dmail").slideUp(500, function(){
			
				$("#dmail").html('');				
				
				/*window.location.reload();*/
				$('#dmail').fadeOut(300);
				refresh_dmail();
				document.title = 'Активисты радио DFM-Ставрополь';
				
			});
			
		
		}
	});
	return false;
}

function otvet() {
	
	if(document.getElementById("dm_msg").value.length < 1 || document.getElementById("dm_msg").value.length > 1000) {
		alert('Сообщение должно быть от 1 до 1000 символов!');
		$('#dm_msg').attr('value','').focus(); 
		return false;
	}
	else {
	
		$('#dmail-content').css('height','490px');
		
		$("#bdin").slideDown(300, function(){
		
		
				
				/*
				alert($("#answer_form").serialize());
				*/
				
				/*Пост отправка*/
				$.post("http://26dfm.ru/jquery/dmail_answer.php", $("#answer_form").serialize(), function(json) {
					
					
					$("#dmail").slideUp(500, function(){
					
					$("#dmail").html('');				
					$('#dmail').fadeOut(300);
					$("#bdin").slideDown(300);
					refresh_dmail();
					
					
					});	
					
				});
				/*Пост отправка конец*/
			
			
			});
		
		
		
		return false;
	}
	
	
}

