﻿//该JS对应View/Topic/Controls/Head.ascx
$(document).ready(function() {
    InitReName();  //主题重命名
    InitSuggestInfo(); //初始化建议功能
    InitFavorite(); //初始化关注此主题功能
    InitSpecialSetting(); //初始化管理列表
    InitAlsoKnownAsList(); //初始化别名信息

    //导航
    $('.topiclocation-border').find('.showmore').unbind().hover(function(event) {
        event.stopPropagation();
        var left = getRelativeLeft(this);
        $(this).parent().find('dl').css('left', left).show();
    }, function() {
        $(this).parent().find('dl').hide();
    });

    $('.topiclocation-border').find('dl').unbind().hover(function(event) {
        event.stopPropagation();
        $(this).show();
    }, function() {
        $(this).hide();
    });

    $('#btnShareToOtherWeb').unbind().hover(function(event) {
        event.stopPropagation();
        var top = getABSTop(this) + 15;
        var left = getABSLeft(this) - 21;
        $('#divShareToOtherWeb').css('top', top).css('left', left).show();
    }, function() {
        $('#divShareToOtherWeb').hide();
    });

    $('#divShareToOtherWeb').unbind().hover(function(event) {
        event.stopPropagation();
        $(this).show();
    }, function() {
        $(this).hide();
    });

    $('.viewimage-link').unbind().mouseover(function(event) {
        if ($(this).find('.icon').length == 0)
            $(this).append('<div class="icon"></div>');
    }).click(function() {
        ShowViewImage($(this).find('img'));
    });

    $('.friendinfo').unbind().hover(function() {
        $(this).find('.sendmessage-button,.addfriend-button').show();
    }, function() {
        $(this).find('.sendmessage-button,.addfriend-button').hide();
    });

    if (typeof (topicId) == undefined || topicId == null || topicId <= 0) return;

    //获取主题完善度
    $.ajax({
        type: "POST",
        url: '/Topic/GetTopicCompleteAjax/',
        data: { 'topicId': topicId },
        success: function(data) {
            $('#divTopicCompleteInfo').parent().attr('title', '完善属性：' + data);
            var dataArr = data.split('/');
            var propertyCount = parseInt(dataArr[0]);
            var havePropertyCount = parseInt(dataArr[1]);
            if (havePropertyCount > 0) {
                if (propertyCount == 0) {
                    $('#divTopicCompleteInfo').css('width', 1);
                } else {
                    var temp = propertyCount / havePropertyCount;
                    $('#divTopicCompleteInfo').css('width', temp * 50)
                    if (temp > 0.33) {
                        if (temp < 0.66)
                            $('#divTopicCompleteInfo').parent().addClass('middlegrade');
                        else
                            $('#divTopicCompleteInfo').parent().addClass('highgrade');
                    }
                }
            }
        },
        error: function(xmlHttpRequest, error) {
            //AlertTips("系统提示：", "对不起，系统故障，我们将尽快处理！", 2); 
        }
    });

});


//初始化别名列表信息
function InitAlsoKnownAsList() {

    //查看所有别名
    $('#showAllAlsoKnown').unbind().click(function() {
        $('#spanAlsoKnownAs').html($(this).attr('content'));
    });
}

//===主题名称重命名 开始
var oldTopickeyword = '';
function InitReName() {
    //点击重命名
    $('#btnReName').unbind().click(function() {
        //判断是否登录 
        if (userID <= 0) //已登录
        {
            ShowLogin();
            return;
        }
        if ($('#fontTopicKeyword').length > 0)
            oldTopickeyword = $('#fontTopicKeyword').html();
        ShowLoading();
        $.ajax({
            type: "POST",
            url: topicControllerUrl + '/GetTopicNameAjax/',
            data: { 'topicID': topicId, 'missionUserID': userID }, //missionUserID
            success: function(data) {
                if (data.indexOf('|') > 0) {//主题关键词
                    var results = data.split('|');
                    oldTopicName = results[0];
                    if (results[1] == 'admin' && results.length > 2)
                        $('#spanKeywordDescription').html(results[2]);
                    else if (results[1] != 'admin')
                        $('#spanKeywordDescription').html(results[1]);
                    else
                        $('#spanKeywordDescription').html('');

                    $('#txtTopicName').val(results[0]);
                    $('#txtTopicKeyword').val(oldTopickeyword).parent().show();

                    //主题关键词规则描述
                    if ($('#spanKeywordDescription').html() != '') {
                        $('#txtTopicKeyword').unbind().focus(function() {
                            $('#divKeywordDescription').show();
                        }).blur(function() {
                            $('#divKeywordDescription').hide();
                        });
                    }
                }
                else {
                    oldTopicName = data;
                    $('#txtTopicName').val(oldTopicName);
                    $('#txtTopicKeyword').parent().hide();
                }
                $('#divTopicName,#divTopicNameAction').hide();
                $('#divTopicNameEditor').show();
                HideLoading();
            },
            error: function(xmlHttpRequest, error) { HideLoading(); AlertTips("系统提示：", "对不起，系统故障，我们将尽快处理！", 2); }
        });
    });

    //取消重命名
    $('#btnCancelReName').unbind().click(function() {
        $('#divTopicNameEditor').hide();
        $('#divTopicName,#divTopicNameAction').show();
    });

    //保存重命名
    $('#btnSaveReName').unbind().click(function() {
        var oldkeyword = '';
        var keyword = '';
        if ($('#txtTopicKeyword').length > 0)
            keyword = $.trim($('#txtTopicKeyword').val());
        var newName = $.trim($('#txtTopicName').val());
        if (newName == oldTopicName && keyword == oldTopickeyword) {
            $('#btnCancelReName').click();
            return;
        }
        var fullName = newName;
        if(keyword!='')
            fullName += ' ('+keyword+')';
        ConfirmTips("系统提示：", "确定将该主题重命名为<br />《<font color=red>" + fullName + "</font>》吗？", "ConfirmReName()");
    });
    
    $('#btnTopicSetPass').unbind().click(function() {
        ConfirmTips("系统提示：", "确定审核<font color=red>通过</font>该主题吗？", "ConfirmTopicSetIsPass(true)");
    });
    
    $('#btnTopicSetNotPass').unbind().click(function() {
        ConfirmTips("系统提示：", "确定审核<font color=red>不通过</font>该主题吗？", "ConfirmTopicSetIsPass(false)");
    });    
}

//审核是否通过
function ConfirmTopicSetIsPass(isPass)
{
    ShowLoading();
    $.ajax({
            type: "POST",
            url: '/Topic/SetTopicIsPassAjax/',
            data: { 'topicId': topicId,'isPass':isPass },
            success: function(data) {
                HideLoading();
                if(data=='True')
                    location.reload();
                else if(data=='NotAdmin')
                    AlertTips("系统提示：", "对不起，非管理员不能审核主题信息！", 2);
                else
                    AlertTips("系统提示：", "对不起，主题审核失败！", 2);
            },
            error: function(xmlHttpRequest, error) { HideLoading(); AlertTips("系统提示：", "对不起，系统故障，我们将尽快处理！", 2); }
        });
}

//确认修改主题名称
function ConfirmReName() {
    ShowLoading();
    var newName = $.trim($('#txtTopicName').val());
    var topicTypeID = $('#hidTopicTypeID').val();
    if(topicTypeID=='')
        topicTypeID = 0;
    if ($('#txtTopicKeyword').length > 0)
        oldTopickeyword = $.trim($('#txtTopicKeyword').val());
    else
        oldTopickeyword = '';
    $.ajax({
        type: "POST",
        url: topicControllerUrl + '/UpdateTopicNameAjax/',
        data: { 'topicID': topicId, 'topicName': escape(newName), 'keyword': escape(oldTopickeyword),'typeID':topicTypeID },
        success: ReNameSuccess,
        error: function(xmlHttpRequest, error) { HideLoading(); AlertTips("系统提示：", "对不起，系统故障，我们将尽快处理！", 2); }
    });
}

//成功修改主题名称
function ReNameSuccess(data) {
    HideLoading();
    if(data=='keywordError')
        AlertTips("系统提示：", $('#spanKeywordDescription').html(), data, 1);
    else if (data != 'true')
        AlertTips("系统提示：", "主题重命名失败！", data, 1);
    else {//判断是否重定向URL
        var newName = $.trim($('#txtTopicName').val());
        ShowLoading();
        $.ajax({
            type: "POST",
            url: topicControllerUrl + '/GetTopicNameAjax/',
            data: { 'topicID': topicId, 'missionUserID': missionUserID },
            success: function(result) {
                result = result.split('|');
                if (newName == result[0]) {//更新页面新主题名称
                    if ($('#txtTopicKeyword').length > 0 && $.trim($('#txtTopicKeyword').val()) != '')
                        $('#divTopicName').html(newName+'&nbsp;<font id="fontTopicKeyword">' + oldTopickeyword + '</font>');
                    else
                        $('#divTopicName').html(newName);
                }
                else
                    $('#divTopicName').html(newName);
                topicName = newName;
                $('#btnCancelReName').click();
                HideLoading();
            },
            error: function(xmlHttpRequest, error) { HideLoading(); AlertTips("系统提示：", "对不起，系统故障，我们将尽快处理！", 2); }
        });
    }
}
//===主题名称重命名结束

///刷新本页
function reloadPage() {
    location.reload();
}

//初始化建议功能
function InitSuggestInfo() {
    $('.btnTypeSuggestion,.btnAddSuggest').unbind('click').click(function() {
        if (userID == 0) //未登录
        {
            ShowLogin();
            return;
        }
        $.ajax({
            type: "POST",
            url: commonControllerUrl + '/GetControlHtmlAjax/',
            data: { 'viewName': '..\\Topic\\Controls\\ShowLayer\\Suggest' },
            success: function(data) {
                HideLoading();
                $('body').append(data);
                var typeName = $(this).attr('typename');
                var objectName = topicName;
                if (typeName == undefined) {
                    $('#divSuggest').find('.downdl > dd').hide();
                    $('#divSuggest').find('.downdl > dt').show();
                } else {
                    $('#divSuggest').find('.downdl > dt').hide();
                    $('#divSuggest').find('.downdl > dd').show();
                    objectName = objectName + '(' + typeName + ')';
                }
                $('#divSuggestObject').html(objectName);
                ShowTransparentBg();
                $('#divSuggest').show();
            },
            error: function(xmlHttpRequest, error) {
                HideLoading();
                AlertTips("系统提示：", "对不起，系统故障，我们将尽快处理！", 2);
            }
        });
    });
}

//初始化关注此主题
function InitFavorite() {
    $('.btnAddFavorite').unbind('click').click(function() {
        //判断是否登录 
        if (userID == 0) //未登录
        {
            currEventObj = $(this);
            ShowLogin();
            return;
        }
        else {
            currFavoriteTopicID = $(this).attr('topicid');
            //加载收藏控件
            if ($('#divAddFavorite').length > 0)
                GetTopicFavoriteTagsName();
            else {
                $.ajax({
                    type: "POST",
                    url: '/Common/GetControlHtmlAjax',
                    data: { 'viewName': '..\\Topic\\Controls\\ShowLayer\\AddFavorite' },
                    success: function(data) {
                        $('body').append(data);
                        InitFavorite();
                        GetTopicFavoriteTagsName();
                    },
                    error: function(xmlHttpRequest, error) { HideLoading(); AlertTips("系统提示：", "对不起，系统故障，我们将尽快处理！", 2); }
                });
            }
        }
    });
}
//初始化管理列表
function InitSpecialSetting() {
    $('#btnSpecialSetting').unbind().hover(function(event) {
        event.stopPropagation();
        var top = getABSTop(this) + 15;
        var left = getABSLeft(this) - 64;
        if ($('#divTopicSetting').length == 0) {
            $.ajax({
                type: "POST",
                url: topicControllerUrl + '/GetManageMenuAjax/',
                data: { 'topicId': topicId, 'missionUserID': missionUserID, 'userID': userID, 'missionState': missionState, 'missionID': missionID, 'isLock': isLock },
                success: function(data) {
                    $('body').append(data);
                    $('#divTopicSetting').css('top', top).css('left', left).show();
                },
                error: function(xmlHttpRequest, error) {
                    HideLoading();
                    AlertTips("系统提示：", "对不起，系统故障，我们将尽快处理！", 2);
                }
            });
        }
        else {
            $('#divTopicSetting').css('top', top).css('left', left).show();
        }
    }, function() {
        $('#divTopicSetting').hide();
    });
}

