﻿/// <reference path="jquery-1.3.2-vsdoc.js" />

function doPoke(pokeder,fn) {
    Poke.init(pokeder,fn);
    Poke.show();
}

var Poke = {
    friendId: null,
    pageSize: 6,
    func: null,
    init: function (pokeder, fn) {
        fb_dialog.init().load();
        this.friendId = pokeder;
        this.func = fn;

    },
    show: function () {
        var self = this;

        $.get("/Poke/CheckPoke",
        {
            friendId: this.friendId,
            render: Math.random()
        },
         function (result) {
             if (parseInt(result) > 0) {
                 self.bulidHtml();
             } else
                 self.showMessage(result);
         });
    },
    showMessage: function (result) {
        var titlemsg = null;
        var xBox = null;
        var self = this;
        switch (result) {
            case '-1':
                titlemsg = "错误";
                xBox = '<span class="msgBigError">招惹失败，您的F币不够！</span>';
                break;
            case '-2':
                titlemsg = "错误";
                xBox = '<span class="msgBigError">对方没作答，不能招惹！</span>';
                break;
            case '-3':
                titlemsg = "错误";
                xBox = '<span class="msgBigError">你尚未登录，不能使用此功能！</span>';
                break;
            case '-4':
                titlemsg = "错误";
                xBox = '<span class="msgBigError">招惹对象不明确！</span>';
                break;
            case '-5':
                titlemsg = "错误";
                xBox = '<span class="msgBigError">只有个人用户可以使用此功能！</span>';
                break;
            case '-6':
                titlemsg = "错误";
                xBox = '<span class="msgBigError">自己不能招惹自己！</span>';
                break;
            default:
                titlemsg = "完成";
                xBox = '<span class="msgBigError">' + result + '</span>';
        }

        fb_dialog.title(titlemsg);

        var mBox = '';
        mBox = '<div class=\"dialog_buttons\">\
                  <input type="button" id="btnPoke11Close" value="关闭" class="inputCancelbuttonsmall" onclick="fb_dialog.close();"/>\
                   </div>';

        fb_dialog.insertHtml("<div id='divpokelist'>" + xBox + "</div>" + mBox);


        if (isNaN(result)) {
            window.setTimeout(function () { fb_dialog.close(); }, 1500);
        }
    },
    bulidHtml: function () {
        var self = this;

        $.get("/Poke/PokeList",
        {
            friendId: this.friendId,
            render: Math.random()
        },
         function (result) {
             var pokfr = $(result).find("#friendUserNickName_hidden").val();
             fb_dialog.title("你想怎么招惹" + pokfr + "呢？");
             fb_dialog.insertHtml(result);
             self.bindBehavious();
         });
    },
    bindBehavious: function () {
        var self = this;
        $('#D_Poke_page a').bind("click", function () {
            self.onPageChange(parseInt($(this).html()));
            $('#D_Poke_page a').removeClass('checked');
            $(this).addClass('checked');
        });

        $('#PokePopbtnCancel').click(function () {
            fb_dialog.close();
        });

        $('#btnPoke').click(function () {
            //alert(self.CurrentType);
            if ($("#tablepokelist input[name='acttype']:checked").val() != null) {
                $(this).attr('disabled', 'disabled');
                $(this).addClass('inputCancelbuttonsmall');
                $(this).val('正在处理');
                self.save();
            }
            else
            { alert('请选择一个动作'); }
        });

        $('#txtPokeLeaveWord').keyup(function (event) {
            var self = this;
            if (event.keyCode == 13) {
                $('#btnPoke').click();
                event.preventDefault();
                if (event.stopPropagation) event.stopPropagation();
                return false;
            }
        });
    },
    onPageChange: function (index) {
        var self = this;
        if ($('#D_Poke_page a').filter('.checked').html() == index)
            return false;

        $('#tablepokelist input:radio').removeAttr('checked');
        $('#tablepokelist tbody tr').each(function (i) {
            if ((i + 1) > (index - 1) * self.pageSize && (i + 1) <= index * self.pageSize) {
                $(this).removeClass('hiddentr');
            }
            else {
                $(this).addClass('hiddentr');
            };
        });
    },
    save: function () {
        var self = this;
        var leaveWord = $('#txtPokeLeaveWord').val();
        var pokeId = -1;
        pokeId = $("#tablepokelist input[name='acttype']:checked").val();

        $.post('/Poke/Post', {
            'friendId': this.friendId,
            'leaveWord': encodeURIComponent(leaveWord),
            'pokeId': pokeId,
            'render': Math.random()
        }, function (data) {
            self.showMessage(data);
            if ($.isFunction(self.func)) {
                self.func();
            }
        });
    }
}


function DelPokeLine(obj) {
    $("#" + obj).fadeOut("fast", function () {
        $(this).remove();
        if ($("#pokelistUl>li").size() <=1) { 
            $("#pokelistUl").html("<li style='margin-top: 8px; color: #808080;'>最近没有招惹</li>");
        }
    });
}

function HideAllPoke(obj) {
    $.post('/Poke/HideALlPoke', {
        'render': Math.random()
    }, function (data) {
        $(obj).parent("li").parent("ul").html("<li style=\"margin-top: 8px; color: #808080;\">最近没有招惹</li>");
    });
}
