﻿
mywallBoard = function(objectid, type, userid, IsMyObject)
{
    this.ObjectId = objectid;
    this.ObjectType = type;
    this.UserId = userid;
    this.Me = IsMyObject;
    this.domDoc = null;
    this.smileyImages = ['0.gif', '1.gif', '2.gif', '3.gif', '4.gif', '5.gif', '6.gif', '7.gif', '8.gif', '9.gif', '10.gif', '11.gif', '12.gif', '13.gif', '14.gif', '15.gif', '16.gif', '17.gif', '18.gif', '19.gif', '20.gif', '21.gif', '22.gif', '23.gif', '24.gif', '25.gif', '26.gif', '27.gif', '28.gif', '29.gif', '30.gif', '31.gif', '32.gif', '33.gif', '34.gif', '36.gif', '38.gif', '39.gif', '41.gif', '43.gif', '44.gif', '45.gif', '46.gif', '47.gif', '48.gif', '49.gif', '50.gif', '51.gif', '52.gif', '53.gif', '54.gif', '55.gif', '56.gif', '60.gif', '61.gif', '62.gif', '63.gif', '64.gif', '65.gif', '66.gif', '67.gif', '68.gif', '69.gif', '70.gif', '71.gif', '74.gif', '75.gif', '76.gif', '77.gif', '79.gif', '80.gif', '81.gif', '82.gif', '83.gif', '84.gif', '85.gif', '86.gif', '87.gif', '88.gif', '89.gif', '90.gif', '100.gif', '101.gif', '103.gif', '104.gif', '105.gif', '106.gif', '107.gif', '108.gif', '109.gif'];
    this.smileyPath = '/fckeditor/editor/images/smiley/msn/';
    this.instance();
};

mywallBoard.prototype.instance = function() {
    var self = this;
    this.EditorEnable();
    $('#wallsmileImg').click(function(e) {
        if (document.getElementById("wallsmilelayer") == null) {
            $('<div id="wallsmilelayer"></div>').appendTo(document.body).hide();
        }
        $(this).addClass('clicked');
        var offset = $(e.toElement ? e.toElement : e.target).offset();

        var mouseX = offset.left;
        var mouseY = offset.top + 25;
        $('#wallsmilelayer').css({ border: 'solid 1px #d7d7d7', width: '470px' });
        $('#wallsmilelayer').css({ position: 'absolute', zIndex: '102' });
        $('#wallsmilelayer').css({ left: mouseX + 'px', top: mouseY + 'px' });
        $('#wallsmilelayer').css({ background: '#fff' });
        $('#wallsmilelayer').css({ overflow: 'hidden' })
        $('#wallsmilelayer').show();
        self.loadSmileImg();
    });

    //$('#wallsmilelayer').focus(function() { alert("abc"); $(this).show(); });
    $('#wallsmileImg').blur(function() {
        setTimeout(function() { $('#wallsmilelayer').hide(); $("#wallsmileImg").removeClass('clicked'); }, 200);
    });
    
    $("#wallBoardly").html("正在加载数据...");
    this.List(5);
    this.bind();
};

mywallBoard.prototype.EditorEnable = function() {
    var self = this;
    this.domDoc = $('#wallEditor').attr('contentWindow').document;
    this.domDoc.designMode = 'on';
    this.domDoc.open();
    this.domDoc.write($('#wallEditorValue').val());
    this.domDoc.close();
    this.domDoc.body.contentEditable = true;
    $(this.domDoc.body).css({ margin: '0px', padding: '3px', fontSize: '10.5pt' });
//    $(this.domDoc).keydown(function(e) { self.quickSave(e); });
//    $(this.domDoc.body).focus();
};

mywallBoard.prototype.bind = function() {
    var self = this;
    $("#wallbtnSent").click(function() {
        var content = $(self.domDoc.body).html();
        if ($.trim(content) == '') {
            alert("留言内容不能为空");
            return false;
        }

        if (parseInt(self.UserId) <= 0) {
            alert("你没有登录不能留言");
            return false;
        }
        
        self.disable();
        $.post('/WS/wallBoard.ashx', { 'UserId': self.UserId,
            'objectDetailId': self.ObjectId,
            'Action': 'save',
            'Objecttype': self.ObjectType,
            'txt': content,
            'id': Math.random()
        },
    function(data) {
        if (data > 0) {
            $(self.domDoc.body).html('').focus();
            self.List(40);
        } else {
            alert("留言失败,请稍候再试");
        }
        self.enable();
    });
    });


    $("#wallboardlistall").toggle(function() {
        $(this).html("查看最新40条留言");
        self.List(999);
    }, function() {
        $(this).html("查看所有留言");
        self.List(5);
    });
};

mywallBoard.prototype.disable = function() {
    $('#wallbtnSent').attr('disabled', 'disabled');
    $('#wallbtnSent').addClass('inputaux');
    $('#wallbtnSent').val('正在发送...');

};

mywallBoard.prototype.changeobjectid = function(objid) {
    this.ObjectId = objid;
    this.List(5);
};

mywallBoard.prototype.enable = function() {

    $('#wallbtnSent').removeAttr('disabled');
    $('#wallbtnSent').removeClass('inputaux');
    $('#wallbtnSent').val('留言');
};

mywallBoard.prototype.loadSmileImg = function()
{
    var self = this;
    if ($('#wallsmilelayer').html() == '')
    {
        $(this.smileyImages).each(function(index)
        {
            $('#wallsmilelayer').append('<div><img src="' + self.smileyPath + this + '"></div');
        });

        $('#wallsmilelayer div').addClass('imgdiv');
        $('#wallsmilelayer div').hover(function() { $(this).addClass('imgdivHover') }, function() { $(this).removeClass('imgdivHover') });
        $('#wallsmilelayer div img').click(function(e)
        {
            $(self.domDoc.body).append('<img src=" ' + $(this).attr('src') + '"/>');
            setTimeout(function() { $('#wallsmilelayer').hide(); $("#wallsmileImg").removeClass('clicked'); }, 200);
        });
    }
};


mywallBoard.prototype.List = function(i) {
    var self = this;
    $.get('/WS/wallBoard.ashx', { 'UserId': this.UserId,
        'objectDetailId': this.ObjectId,
        'Action': 'List',
        'Objecttype': this.ObjectType,
        'isme': this.Me,
        'rec': i,
        'id': Math.random()
    }, function(data) {
        $("#wallBoardly").html($(data).find(".remove a").click(function() {
            if (confirm("确定删除吗？")) {
                $.get('/WS/wallBoard.ashx', { 'UserId': self.UserId,
                    'objectDetailId': this.id,
                    'Action': 'del',
                    'Objecttype': self.ObjectType,
                    'id': Math.random()
                }, function(data) {
                    if (data > 0) {
                        self.List(40);
                    }
                });
            }
        }).end());
    });
};