﻿function PokeMe(poker, poked)
{
    var ipoke = new Poke(poker, poked);
    ipoke.show();
 }

 function ignorePoke(recordid,obj)
 {
     $.get('/WS/poke.ashx', { 'recordid': recordid,
         'Action': 'Ignore',
         'id': Math.random()
     }, function(data)
     {
         $(obj).parent("div").remove();
     });
 }


 Poke = function(poker, poked) {
     this.pageSize = 6;
     this.userId = poker;
     this.friendId = poked;
     this.dialogId = "D_Poke_Dialog";
     this.dialogTitleId = "D_Poke_Title";
     this.dialogIdContentId = "D_Poke_Content";
     this.init();
     this.CurrentType = 0;

 };

 Poke.prototype.init = function() {
    var sBox = '\
            <table class="pop_dialog_table" style="display: none" id="' + this.dialogId + '">\
                <tr>\
                    <td class="pop_topleft">\
                    </td>\
                    <td class="pop_border">\
                    </td>\
                    <td class="pop_topright">\
                    </td>\
                </tr>\
                <tr>\
                    <td class="pop_border">\
                    </td>\
                    <td class="pop_content">\
                        <div class="divMsgFrameCss">\
                            <div class="divMsgTitleCss" id="' + this.dialogTitleId + '">\
                                </div>\
                            <div class="divMsgMainCss" id="' + this.dialogIdContentId + '">\
                            </div>\
                        </div>\
                    </td>\
                    <td class="pop_border">\
                    </td>\
                </tr>\
                <tr>\
                    <td class="pop_bottomleft">\
                    </td>\
                    <td class="pop_border">\
                    </td>\
                    <td class="pop_bottomright">\
                    </td>\
                </tr>\
            </table>\  ';
            $("body").append(sBox);
};


Poke.prototype.show = function() {
    $('#' + this.dialogId).css({ position: 'absolute', zIndex: '101' });
    $('#' + this.dialogId).css({ width: '500px' });
    $('#' + this.dialogId).show();
    this.setBG();
    this.Loading();
//    $(window).resize(this.center());
//    $(window).scroll(this.center());
    this.check();

};

Poke.prototype.check = function()
{
    var self = this;

    if (this.userId == 0)
    {
        self.showMSG("-3");
        return false;
    }

    if (this.friendId == 0)
    {
        self.showMSG("-4");
        return false;
    }

    if (this.userId == this.friendId)
    {
        self.showMSG("-6");
        return false;
    }
    
    $.get('/WS/poke.ashx', { 'UserId': this.userId,
        'FriendId': this.friendId,
        'Action': 'Check',
        'id': Math.random()
    },
    function(data)
    {
        if (parseInt(data) > 0)
            self.buildContentAJAX();
        else
            self.showMSG(data);
    });
};

Poke.prototype.buildContentAJAX = function() {
    var self = this;
    $.get('/WS/poke.ashx', { 'UserId': this.userId,
        'FriendId': this.friendId,
        'id': Math.random()
    },
    function(data) {
        self.innerContent(data);
        var friend = $('#hiddenFriendName').val();
        self.title('你想怎么招惹' + friend + '呢?');
        self.bindBehaviors();
    });
};

Poke.prototype.Loading=function(){
    this.innerContent('<span class="msgBigError">正在加载数据，请等待...</span>');
    this.title('等待');
    this.center();
};

Poke.prototype.innerContent = function(content) {
    $('#' + this.dialogIdContentId).html(content);
    this.center();
};

Poke.prototype.title=function(msg){
    $('#'+this.dialogTitleId).html(msg);
    
};

Poke.prototype.bindBehaviors = function() {
    var self = this;

    $("#feed-poke-tab li a").click(function() {
        $("#feed-poke-tab li a").removeClass("selected");
        $("#feed-poke-tab li").removeClass("selected");
        $(this).parent("li").addClass("selected");
        $(this).addClass("selected");
        if (this.id == "action_1") {
            $("#divpokelist").show();
            $("#divworklist").hide();
            self.CurrentType = 0;
        }
        else {
            $("#divpokelist").hide();
            $("#divworklist").show();
            self.CurrentType = 1;
        }
    });


    $('#D_Poke_page a').bind("click", function() {
        self.onPageChange(parseInt($(this).html()));
        $('#D_Poke_page a').removeClass('checked');
        $(this).addClass('checked');
    });

    $('#D_Work_page a').bind("click", function() {
        self.onPageChange_Work(parseInt($(this).html()));
        $('#D_Work_page a').removeClass('checked');
        $(this).addClass('checked');
    });



    $('#' + this.dialogId + ' #btnCancel').click(function() {
        self.close();
    });

    $('#' + this.dialogId + ' #btnPoke').click(function() {
        //alert(self.CurrentType);
        if ((self.CurrentType == 0 && $('#tablepokelist input:radio[@checked]').val() != null) || (self.CurrentType == 1 && $('#tableworklist input:radio[@checked]').val() != null)) {
            $(this).attr('disabled', 'disabled');
            $(this).addClass('inputaux');
            $(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;
        }
    });

    //    $('#tablepokelist tbody tr:gt(1) td img,#tablepokelist tbody tr:gt(1) input:radio').live("mouseover",function(e) { self.showToolTip(e); });
    //    $('#tablepokelist tbody tr:gt(1) td img,#tablepokelist tbody tr:gt(1) input:radio').live("mouseout",function() { self.hideToolTip(); });

    //    $('#tableworklist tbody tr:gt(1) td img,#tablepokelist tbody tr:gt(1) input:radio').live("mouseover", function(e) { self.showToolTip(e); });
    //    $('#tableworklist tbody tr:gt(1) td img,#tablepokelist tbody tr:gt(1) input:radio').live("mouseout", function() { self.hideToolTip(); });

};

Poke.prototype.save = function() {
    var self = this;
    var clientUserId = 0;
    var leaveWord = $('#txtPokeLeaveWord').attr("value");
    var pokeId=-1;
    if (self.CurrentType ==0)
        pokeId=$('#tablepokelist input:radio[@checked]').val();
    else
        pokeId=$('#tableworklist input:radio[@checked]').val();
        
    $.get('/WS/poke.ashx', { 'UserId': this.userId,
        'FriendId': this.friendId,
        'ClientUserId': clientUserId,
        'leaveWord': leaveWord,
        'Action': 'Save',
        'PokeId': pokeId,
        'id': Math.random()
    }, function(data) {
        self.showMSG(data);
    });

};

Poke.prototype.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');
        };
    });
};


Poke.prototype.onPageChange_Work = function(index) {
    var self = this;
    if ($('#D_Work_page a').filter('.checked').html() == index)
        return false;

    $('#tableworklist input:radio').removeAttr('checked');
    $('#tableworklist tbody tr').each(function(i) {
        if ((i + 1) > (index - 1) * self.pageSize && (i + 1) <= index * self.pageSize) {
            $(this).removeClass('hiddentr');
        }
        else {
            $(this).addClass('hiddentr');
        };
    });
};

Poke.prototype.showMSG = 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>';
    }

    var mBox = '';
    mBox = '<div class=\"divMsgBottomCss\">\
                  <input type="button" id="btnPoke11Close" value="关闭" class="inputaux"/>\
                   </div>';
    this.innerContent(xBox + mBox);
    this.title(titlemsg);

    if (parseInt(result) >= 0)
    {
        window.setTimeout(function() { self.close(); }, 1500);
    }
    else
        $('#btnPoke11Close').click(function()
        {
            self.close();
        });

};

Poke.prototype.setBG = function() {
    if (document.getElementById("TB_HideSelect") == null) {
        $("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div>");
    }
    if (document.getElementById("TBtooltip") == null) {
        $('<div id="TBtooltip" class="tooltip"></div>').appendTo(document.body).hide();
    }
    this.overlaySize();

};

Poke.prototype.showToolTip = function(e) {
    var mouseX = e.pageX || (e.clientX ? e.clientX + document.body.scrollLeft : 0);
    var mouseY = e.pageY || (e.clientY ? e.clientY + document.body.scrollTop : 0);
    mouseX += 10;
    mouseY += 10;
    $('#TBtooltip').css({ left: mouseX + 'px', top: mouseY + 'px' });

    $('#TBtooltip').html($(e.toElement ? e.toElement : e.target).parent().find('span').html());
    $('#TBtooltip').fadeIn('slow');
};

Poke.prototype.hideToolTip = function() {
    $('#TBtooltip').hide();
};

Poke.prototype.overlaySize=function(){
	if (window.innerHeight && window.scrollMaxY) {	
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		yScroll = document.body.offsetHeight;
  	}
	$("#TB_overlay").css("height",yScroll +"px");
};


Poke.prototype.center = function() {
    var pagesize = this.getPageSize();
    var arrayPageScroll = this.getPageScrollTop();
    var TB_WIDTH = $('#' + this.dialogId).width();
    var TB_HEIGHT = $('#' + this.dialogId).height();
    $('#' + this.dialogId).css({ left: ((pagesize[0] - TB_WIDTH) / 2) + "px", top: (arrayPageScroll[1] + ((pagesize[1] - TB_HEIGHT) / 2)) + "px" });
    this.overlaySize();

};


Poke.prototype.getPageSize=function(){
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
	
	arrayPageSize = new Array(w,h) 
	return arrayPageSize;
};


Poke.prototype.getPageScrollTop=function(){
	var yScrolltop;
	if (self.pageYOffset) {
		yScrolltop = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScrolltop = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScrolltop = document.body.scrollTop;
	}
	arrayPageScroll = new Array('',yScrolltop) 
	return arrayPageScroll;
};

Poke.prototype.close = function() {
    this.hideToolTip();
    $('#' + this.dialogId).fadeOut("fast", function() { $('#' + this.dialogId + ',#TB_overlay,#TB_HideSelect').remove(); });
    return false;
};