$(document).ready(function(){
	
	var $is = false;

    //-------------------------------------------------------------------------+
    // Обработчик "Скрыть" он же "Просто удалить" для автора сообщения
    //-------------------------------------------------------------------------+
    $(".social_vote_simple_hide").click(function()
    {
        var topic_id    = $(this).parent().parent().find("input[name='topic_id']").val();
        var msg_id      = $(this).parent().parent().find("input[name='msg_id']").val();
        var warning     = $(this).parent().parent().find("input[name='warning']").val();

        $.getJSON("/forum/index.php?action=all_ajax_social_vote_simple", {msg_id: msg_id, topic_id: topic_id}, function(data){
            if (data.result == 1) {
                $("#social_vote_message_"+msg_id).hide("slow");
                $("#message_table_"+msg_id).attr("className", "message_hidden");
                //$("#message_body_"+msg_id).html("<font color='Red'>"+warning+"</font>"+$("#message_body_"+msg_id).html());
            } else {
                alert("Сбой при голосовании: "+data.resultStatus);
            }
        });

        return false;
    });

    //-------------------------------------------------------------------------+
    // Обработчик "Просто удалить"
    //-------------------------------------------------------------------------+
    $(".social_vote_simple").click(function()
    {
        var msg_id   = $(this).parent().attr('msg_id');
        var topic_id = $(this).parent().attr('topic_id');

		$("#social_vote_menu").css("display", "none");

        $.getJSON("/forum/index.php?action=all_ajax_social_vote_simple", {msg_id: msg_id, topic_id: topic_id}, function(data){
            if (data.result == 1) {
                $("#social_vote_message_"+msg_id).hide("slow");
            } else {
                alert("Сбой при голосовании: "+data.resultStatus);
            }
        });

        return false;
    });
	
    //-------------------------------------------------------------------------+
    // Обработчик "Предупреждение"
    //-------------------------------------------------------------------------+
    $(".social_vote_warning").click(function()
    {
        var msg_id   = $(this).parent().attr('msg_id');
        var topic_id = $(this).parent().attr('topic_id');

		$("#social_vote_menu").css("display", "none");

        $.getJSON("/forum/index.php?action=all_ajax_social_vote_warning", {msg_id: msg_id, topic_id: topic_id}, function(data){
            if (data.result == 1) {
                $("#social_vote_message_"+msg_id).hide("slow", function(){
                    $("#social_vote_message_"+msg_id).html("Сообщено модератору");
                    $("#social_vote_message_"+msg_id).show("slow");
                });
            } else {
                alert("Сбой при голосовании: "+data.resultStatus);
            }
        });

        return false;
    });

    //-------------------------------------------------------------------------+
    // Обработчик "Флуд/Милиция"
    //-------------------------------------------------------------------------+
    $(".social_vote_strict").click(function()
    {
        var msg_id   = $(this).parent().attr('msg_id');
        var topic_id = $(this).parent().attr('topic_id');
        
		$("#social_vote_menu").css("display", "none");

        $.getJSON("/forum/index.php?action=all_ajax_social_vote_strict", {msg_id: msg_id, topic_id: topic_id}, function(data){
            if (data.result == 1) {
                $("#social_vote_message_"+msg_id).hide("slow", function(){
                    $("#social_vote_message_"+msg_id).html("Сообщено модератору");
                    $("#social_vote_message_"+msg_id).show("slow");
                });
            } else {
                alert("Сбой при голосовании: "+data.resultStatus);
            }
        });

        return false;
    });

    //-------------------------------------------------------------------------+
    // Обработчик экспертного рейтинга
    //-------------------------------------------------------------------------+
    $(".expert_vote").click(function()
    {
        var expert_rate = $(this).parent().parent().find("input:radio:checked").val();
        var topic_id    = $(this).parent().parent().find("input[name='topic_id']").val();
        var msg_id      = $(this).parent().parent().find("input[name='msg_id']").val();
        
        // проверяем входные параметры
        if (expert_rate == 'undefined' || msg_id == 'undefined') {
            return;
        }

        // отправляем запрос
        $.getJSON("/forum/index.php?action=all_ajax_expert_vote", {topic_id: topic_id, msg_id: msg_id, expert_rate: expert_rate}, function(data){
            if (data.result == 1) {
                $('#expert_vote_'+msg_id).hide("slow");
                $('#message_rate_'+msg_id).html(data.rate);
                $('#message_count_'+msg_id).html(data.count);
            } else {
                alert("Сбой при голосовании: "+data.resultStatus);
            }
        });
        
        return false;
    });

    //-------------------------------------------------------------------------+
    // Обработчик сохранения материала в архив
    //-------------------------------------------------------------------------+
    $(".all_save_to_archive").click(function()
    {
        var topic_id        = $(this).parent().parent().find("input[name='topic_id']").val();
        var message_id      = $(this).parent().parent().find("input[name='message_id']").val();

        // проверяем входные параметры
        if (topic_id == 'undefined' || message_id == 'undefined') {
            alert("Ошибка сохранения материала в ваш личный архив!");
            return;
        }
        
        // отправляем запрос
        $.getJSON("/forum/index.php?action=all_ajax_save_to_archive", {topic_id: topic_id, message_id: message_id}, function(data) {
            if (data.result == 1) {
            	$('#save_to_archive_'+message_id).hide();
            	
		        // экспертное голосование по максимальной отметке
		        if ($('#expert_vote_'+message_id).length != 0 && $('#expert_vote_'+message_id).css('display') != 'none') {
	            	var expert_rate = encodeURI("+3");
			        $.getJSON("/forum/index.php?action=all_ajax_expert_vote", {topic_id: topic_id, msg_id: message_id, expert_rate: expert_rate}, function(data){
			            if (data.result == 1) {
			                $('#expert_vote_'+message_id).hide("slow");
			                $('#message_rate_'+message_id).html(data.rate);
			                $('#message_count_'+message_id).html(data.count);
			            } else {
			                //alert("Сбой при голосовании: "+data.resultStatus);
			            }
			        });
		        }
            } else {
                alert(data.result_status);
            }
        });
        
        return false;
    });

    //-------------------------------------------------------------------------+
    // Обработчик сохранения материала в журнал
    //-------------------------------------------------------------------------+
    $(".all_save_to_journal").click(function()
    {
        var topic_id        = $(this).parent().parent().find("input[name='topic_id']").val();
        var message_id      = $(this).parent().parent().find("input[name='message_id']").val();

        // проверяем входные параметры
        if (topic_id == 'undefined' || message_id == 'undefined') {
            alert("Ошибка сохранения материала в ваш личный журнал!");
            return;
        }
        
        // отправляем запрос
        $.getJSON("/forum/index.php?action=all_ajax_save_to_journal", {topic_id: topic_id, message_id: message_id}, function(data) {
            if (data.result == 1) {
                $('#save_to_journal_'+message_id).hide();
            } else {
                alert(data.result_status);
            }
        });
        
        return false;
    });
    
    //-------------------------------------------------------------------------+
    // Обработчик голосования Этикет/Удалить
    //-------------------------------------------------------------------------+
    $(".social_vote").click(function()
    {
    	var left = $(this).offset().left + $(this).width();
    	var top  = $(this).offset().top;

        var msg_id   = $(this).find("#msg_id").val();
        var topic_id = $(this).find("#topic_id").val();
        
    	var menu = $("#social_vote_menu");
    	menu.css('top', top);
    	menu.css('left', left);
    	menu.attr('msg_id', msg_id);
    	menu.attr('topic_id', topic_id);
    	
    	if (menu.css('display') == 'none') {
    		menu.css('display', 'block');
    		setTimeout(function() { $("#social_vote_menu").css("display", "none"); }, 3000);
    	} else {
    		menu.css('display', 'none');
    	}
    	
    	return false;
    });

    //-------------------------------------------------------------------------+
    // Скрывает выпадающее меню Этикет/Удалить
    //-------------------------------------------------------------------------+
	$("#social_vote_menu").hover(
		function () {
		}, 
		function () {
			$(this).css('display', 'none');
		}
    );

    //-------------------------------------------------------------------------+
    // Обработчик сохранения в закладки
    //-------------------------------------------------------------------------+
    $(".message_anchor").click(function()
    {
        var msg_id   = $(this).find("#msg_id").val();
        var topic_id = $(this).find("#topic_id").val();
    	
        $.getJSON("/forum/index.php?action=all_ajax_message_anchor", {msg_id: msg_id, topic_id: topic_id}, function(data){
            if (data.result == 1) {
                alert("Сообщение сохранено в закладках");
            } else {
                alert("Сбой при голосовании: "+data.resultStatus);
            }
        });

        return false;
    });

    //-------------------------------------------------------------------------+
    // Обработчик дебага социального голосования
    //-------------------------------------------------------------------------+
    $(".social_vote_debug").click(function()
    {
        var msg_id   = $(this).find("#msg_id").val();
        var topic_id = $(this).find("#topic_id").val();

        if ($("#show_svd_"+msg_id).css('display') == 'none') {
            $("#show_svd_"+msg_id).show();
            $.get("/forum/index.php?action=all_ajax_social_vote_debug", {topic_id: topic_id, msg_id: msg_id}, function(data){
                $("#show_svd_"+msg_id).html(data);
            });
        } else {
            $("#show_svd_"+msg_id).hide();
        }
        
        return false;
    });

    //-------------------------------------------------------------------------+
    // Обработчик дебага экспертного голосования
    //-------------------------------------------------------------------------+
    $(".expert_vote_debug").click(function()
    {
        var msg_id   = $(this).find("#msg_id").val();
        var topic_id = $(this).find("#topic_id").val();

        if ($("#show_evd_"+msg_id).css('display') == 'none') {
            $("#show_evd_"+msg_id).show();
            $.get("/forum/index.php?action=all_ajax_expert_vote_debug", {topic_id: topic_id, msg_id: msg_id}, function(data){
                $("#show_evd_"+msg_id).html(data);
            });
        } else {
            $("#show_evd_"+msg_id).hide();
        }
        
        return false;
    });
    
    $(".member_expert_rate_debug").click(function () {
    	
        var member_id = $(this).find("#member_id").val();
        
    	var popupWin = window.open("/forum/index.php?action=all_ajax_member_expert_vote_debug&member_id="+member_id, 'Expert Debugger', 'location=no,menubar=no,scrollbars=yes,titlebar=yes,toolbar=no,resizable=yes,width=1024,height=768,screenX=20,screenY=20');
    	popupWin.focus();
    	return false;
    });
});