﻿var Talk = { 
    InitFunction : null,
    NextFunciton : null,
    PageNo : 1,
    BoardID : null,
    ObjectID : null,
    WriterWidth : 0,
    EditorWidth : 0,
    ShowWriter : function(width) {
        var userid = User.UserUID();
        this.WriterWidth = width;
        
        if(userid != null) {
            //if(this.BoardID == '7071107C-C37B-4C4A-8ACD-5BDA98C20ABA' && Opinity.Ajax.Common.CheckMe2day().value.ReturnValue)
	        //    $('chkMe2day').setStyle({display:'inline'});
            //else
	        //    $('chkMe2day').setStyle({display:'none'});           

	        $('dummy').setStyle({display:'none'});
	        $('SmallEditor').setStyle({display:'inline'});
	        if (editor_load == 0) {
                smalleditor = new editor2('txtComment', width, 52);
	            smalleditor.init('txtComment', width);
	            editor_load = 1;
                if(this.InitFunction != null) eval(this.InitFunction);
	        }
	        
            smalleditor.htmleditor.style.borderTop = 'solid 1px #8b8b8b';
            smalleditor.htmleditor.style.borderLeft = 'solid 1px #8b8b8b';
	    }
	    else
            User.ShowLogin('Talk.GetWriter()');	    
    },
    GetWriter : function() {
        if(this.BoardID != null && this.ObjectID != null && this.WriterWidth > 0)
            CtlTalkWriter.GetTalkWriter(this.BoardID, this.ObjectID, this.WriterWidth, 'TalkWriter', Talk.GetWriterCallback);
    },
    GetWriterCallback : function(res) {
        if(res && res.value)
        {
            if(res.value.ResultCode == 1) { 
                $(res.value.ID).setStyle({display:'inline'}); 
                $(res.value.ID).update(res.value.HTML);
                Talk.ShowWriter(Talk.WriterWidth);
            }
        }
    },    
    Save : function(objectid) {
        var userid = User.UserUID();       
        
        if(userid == null) {
            $('dummy').onclick();
        }
        else if($('dummy').style.display != 'none') {
            if (this.ObjectID != "76326914-EE62-4264-8BE7-5F207DE068B7")
            {
                alert('내용을 입력해주세요');
                this.ShowEdit(551);
            }
        }
        else {
            if(this.ObjectID == null || this.ObjectID == '') {
                alert('대상을 선택해주세요');
                return;
            }
        
            var content = smalleditor.doc.body.innerHTML;
            
            if (this.ObjectID == "76326914-EE62-4264-8BE7-5F207DE068B7")
            {
                content = "WOW SEOUL";
            }            
            
            if(content.trim() == '' || Opinity.Ajax.Common.RemoveTags(content).value.ReturnValue.trim() == '') {
                alert('내용을 입력해주세요');
                return;
            }
                    
            var postid = Opinity.Ajax.AjaxBoard.SaveBoardPost(this.BoardID, this.ObjectID, userid, content, '').value.ReturnValue;
            
            if(postid != '') {
                if(User.Me2day) 
                    if(Opinity.Ajax.AjaxBoard.Send2Me2day(postid, this.ObjectID, userid, this.BoardID, Collection.FlashSkinNo).value.ResultCode == 0) alert("미투전송 실패");        
            
                CloseEdit();
                if(this.NextFunction != null) eval(this.NextFunction);
                else return true;
            }
            else
                alert('입력시 에러발생');
        }
    },
    Delete : function(postid, objectid) {
        var userid = User.UserUID();

        if(userid != null) {
            if(confirm("삭제하시겠습니까?")) {
                if(Opinity.Ajax.AjaxBoard.DeleteBoardPost(this.BoardID, postid, objectid, userid).value.ReturnValue) {
                    if(this.NextFunction != null) eval(this.NextFunction);              
                    else return true;
                }
            }
            else
                return false;
        }
        
        return false;
    },
    Delete2 : function(boardid, postid, objectid) {
        var userid = User.UserUID();
        
        if(userid != null) {
            if(confirm("삭제하시겠습니까?")) {
                if(Opinity.Ajax.AjaxBoard.DeleteBoardPost(boardid, postid, objectid, userid).value.ReturnValue) {
                    if(this.NextFunction != null) eval(this.NextFunction);              
                    else return true;
                }
            }
            else
                return false;
        }
        
        return false;
    },    
    Edit : function(id, width) {
        var pos = Util.GetAbsolutePos($(id));
        var pos2 = Util.GetAbsolutePos($('imgReply' + id));
        var h = pos2.y - pos.y + 4;
        if(h < 95) h = 95;
        
        if(isIE7 || isChrome) 
            $('PostEditor').setStyle({display:'inline',top:(pos.y - 3) + 'px',height:h + 'px'});
        else
            $('PostEditor').setStyle({display:'inline',top:(pos.y + 7) + 'px',height:h + 'px'});
        CtlTalkEditForm.GetTalkEditor(id, width, h - 55, 'PostEditor', Util.AjaxCallback);
    },
    ShowReply : function(seq) {
        if($("REPLIES-" + seq).style.display != "none") {
            $("REPLIES-" + seq).style.display = "none";
            $('imgReply'+seq).setStyle({backgroundImage:'url(/images/editor/ico_reply_off.gif)'});
        }
        else {
            $("REPLIES-" + seq).style.display = "inline";     
            $('imgReply'+seq).setStyle({backgroundImage:'url(/images/editor/ico_reply_on.gif)'});
        }
    },
    SaveReply : function(objectid, parentid) {
        var userid = User.UserUID();
        
        if(userid != null) {
            /*if($('LOGINUSER-' + parentid).innerHTML == '') {
                $('LOGINUSER-' + parentid).update(User.UserNickname());
            }*/

            var content = $('txtReply' + parentid).value;
            
            if(content.trim() == '') {
                alert('내용을 입력해주세요');
                return;
            }

            var postid = Opinity.Ajax.AjaxBoard.SaveBoardPost(this.BoardID, objectid, userid, content, parentid).value.ReturnValue;
            
            if(postid != '') {
                if(this.NextFunction != null) eval(this.NextFunction);
                else return true;
            }
            else
                alert('입력시 에러발생');
        }
        else
            User.ShowLogin("Talk.SaveReply('" + objectid + "','" + parentid + "')");
    },
    ClickReplyForm : function(parentid) {
        var nickname = User.UserNickname();
        
        if(nickname == null)
            User.ShowLogin("Talk.ClickReplyForm('" + parentid + "')");
    }
}




var TEditor = {
    Editor : null,
    PostID : '',
    ContentID : '',
    Close : function() {
        $('PostEditor').update('');
        $('PostEditor').setStyle({display:'none'});
    },
    Save : function() {
        var userid = User.UserUID();
        var content = this.Editor.doc.body.innerHTML;
        
        if(content.trim() == '') {
            alert('내용을 입력해주세요');
            return;
        }
        
        if(Opinity.Ajax.AjaxBoard.UpdateBoardPost(Talk.BoardID, this.PostID, this.ContentID, userid, content).value.ReturnValue) {
            if(Talk.NextFunction != null) eval(Talk.NextFunction);
            this.Close();
        }        
    }   
}