/*
 * Различные функции и управление по всему сайту.
 */

function makeEditor(){
    $(document).ready(function()	{
        // Add markItUp! to your textarea in one line
        // $('textarea').markItUp( { Settings }, { OptionalExtraSettings } );
        $('#markItUp').markItUp(mySettings);
        /*
        // You can add content from anywhere in your page
        // $.markItUp( { Settings } );	
        $('.add').click(function() {
            $.markItUp( { 	openWith:'<opening tag>',
                            closeWith:'<\/closing tag>',
                            placeHolder:"New content"
                        }
                    );
            return false;
        });
        
        // And you can add/remove markItUp! whenever you want
        // $(textarea).markItUpRemove();
        $('.toggle').click(function() {
            if ($("#markItUp.markItUpEditor").length === 1) {
                $("#markItUp").markItUpRemove();
                $("span", this).text("get markItUp! back");
            } else {
                $('#markItUp').markItUp(mySettings);
                $("span", this).text("remove markItUp!");
            }
            return false;
        });
        */
    });
}

function makeUserMenusToggle(){
    $(".COMMENTS_Block_NickName").click(function(){
        $(".COMMENTS_HIDDEN_MENU", this).slideToggle('fast');
        // return false;
    });
    
    $(".COMMENTS_Block_NickName>a.author").removeAttr('href');
}

function Add_Comments(module, id, page){
    var comments_cont = $(".COMMENTS_Main_field");
    var url;
    
    if (page){
        url = "/aside/sonet/comments/get_list/" + module + "/"+id+"/"+page;
    } else {
        url = "/aside/sonet/comments/get_list/" + module + "/"+id;
    }
    
    
    comments_cont.load(url,'', function(){
        makeUserMenusToggle();
        ShowCommentsForm()
        makeEditor();
        makeSubmitComment();
        ajaxAlerts();
        commentsManagement();
    });
    
    return false;
}

function refreshComments(){
    Add_Comments($("#category_id").val(), $("#target_id").val());
}

function ShowCommentsForm(){
    $("#COMM_SHOW_BUTTON").click(function(){
        $(this).slideUp("slow");
        $("#COMM_SHOW_FIELD").slideDown("slow");
    });
}

function makeSubmitComment(){
    $("#COMM_SHOW_FIELD form").ajaxForm(function(response){
        
        switch(response){
            case 'success':
                refreshComments();
                break;
            case 'empty':
                jAlert("Введите текст и тему сообщения", 'Предупреждение');
                break;
            case 'time':
                jAlert("Прошло недостаточно времени с последнего сообщения", 'Предупреждение');
                break;
            default:
                jAlert("Произошла ошибка, сообщите администратору", 'Предупреждение'); 
        }
    });
}

function ajaxAlerts(){
    $("a.ajaxalert").click(function(){
        $.get($(this).attr('href'), '', function(response){
            jAlert(response);
        })
        
        return false;
    });
}

function commentsManagement(){
    $(".COMMENTS_Block_Text a.buttonHide").click(function(){
        jAlert('Сообщение скрыто', 'Сообщение');
        
        $.get($(this).attr('href'), '', function(response){
            // jAlert(response);
        });
        
        refreshComments();
        
        return false;
    });

    $(".COMMENTS_Block_Text a.buttonShow").click(function(){
        jAlert('Сообщение опубликовано');
        
        $.get($(this).attr('href'), '', function(response){
            // jAlert(response);
        });
        
        refreshComments();
        
        return false;
    });
    
    $(".COMMENTS_Block_Text a.buttonEdit").click(function(){
        // jAlert('Редактируем комент');
        
        var edit_cont = $(this).parents(".COMMENTS_Block_Text:first").find(".text");
        var edit_bbcode = $(this).parents(".COMMENTS_Block_Text:first").find(".hidden_bbcode");
        var update_link = $(this).parents(".COMMENTS_Block_Text:first").find(".update_link");
        
        var edit_textarea = $("<textarea>");
        edit_textarea.blur(function(){
            $.post(update_link.val(), {text:$(this).val()}, function(){
                refreshComments();
            });
        });
        
        edit_textarea.val(edit_bbcode.text());
        edit_cont.html('').append(edit_textarea);
        $('textarea', edit_cont).markItUp(mySettings);
        
        return false;
    });
    
    $(".COMMENTS_Block_Text a.buttonDelete").click(function(){
        var this_link = $(this);
        
        jConfirm('Вы уверены что хотите удалить это сообщение?', null, function(confirm){
            if (confirm){
                $.get(this_link.attr('href'), '', function(response){
                    if (response == 'success'){
                        this_link.parents(".COMMENTS_Block:first").remove();
                        refreshComments();
                    } else {
                        jAlert(response, 'Сообщение');
                    }
                });
            } else {
                jAlert('Как хотите');
            }
        });
        
        return false;
    });
    
}

function toggleUserMenu(){
    $("#Spark_FRU_UserMenu_Button").toggleClass("button_down").toggleClass("button_up");
    $("#Spark_FRU_UserMenu_HidedMenu").toggleClass("menu_hide").toggleClass("menu_show");
        
    if ($("#Spark_FRU_UserMenu_HidedMenu").hasClass('menu_hide')) {
        $.cookie('FRU_UserMenu', 'hidden', {path:'/', expires: 30});
    } else {
        $.cookie('FRU_UserMenu', 'visible', {path:'/', expires: 30});
    }
}
 
$(document).ready(function(){
    makeUserMenusToggle();
    ajaxAlerts();
    
    $("#ajax_loading").bind("ajaxSend", function(){
        clearTimeout(window.ajaxTimeout);
        window.ajaxTimeout = setTimeout(function(){
            $("#ajax_loading").show();
        }, 300);
    }).bind("ajaxComplete", function(){
        clearTimeout(window.ajaxTimeout);
        window.ajaxTimeout = setTimeout(function(){
            $("#ajax_loading").hide();
        }, 300);
    });
    
    $("#box_auth_btn").click(function(){
        $("#box_auth_expand").slideToggle();
        return false;
    });  
    
    
    $("#Spark_FRU_UserMenu_Button").click(toggleUserMenu);
    if ($.cookie('FRU_UserMenu') == 'visible') {
        toggleUserMenu();
    }
    
    // форма поиска плагинов
    $(".frame_top_inner>form").ajaxForm({
        target: '#BOX_PlugSearch_RESULT',
        beforeSubmit: function(){
            $("#BOX_PlugSearch_REQS").hide();
            $("#BOX_PlugSearch_TAGS").hide();
        }
    });    
    
    $("#BOX_PlugSearch_REQS_btn").click(function(){
        $("#BOX_PlugSearch_REQS").toggle();
        $("#BOX_PlugSearch_TAGS").hide();
    });
    $("#BOX_PlugSearch_TAGS_btn").click(function(){
        $("#BOX_PlugSearch_TAGS").toggle();
        $("#BOX_PlugSearch_REQS").hide();
    });
    $(".frame_top_inner>form select[name=game]").change(function(){
        var show_class = 'game_' + $(this).val();
        
        $("#BOX_PlugSearch_REQS>span").hide();
        $("#BOX_PlugSearch_REQS>span."+show_class).show();
        $("#BOX_PlugSearch_REQS :checkbox").attr('checked', false);;
        $("#BOX_PlugSearch_TAGS>span").hide();
        $("#BOX_PlugSearch_TAGS>span."+show_class).show();
        $("#BOX_PlugSearch_TAGS :checkbox").attr('checked', false);;
    });
    //--форма поиска плагинов
    
    
    $("a.request_yes_or_no").click(function(){
        var link_href = $(this).attr('href');
        
        jConfirm('Вы уверены?', 'Подтверждение', function(r){
            if (r){
                window.location.href = link_href;
            } else {
                jAlert('Как хотите', 'Сообщение');
            }
        });
        
        return false;
    });
});