﻿var SimpleReview = {
    BoardID : '58BF4A26-A922-4126-9281-32004A57654D',
    ObjectID : null,
    CheckFlag : false,
    PageNo : 1,
    HaveWish : 'None',
    NextFunction : '',
    WriterWidth : 0,
    EditorTopFix : 120,
    CheckHaveWish : function(havewish) {
        var userid = User.UserUID();
        
        if(userid != null) {
            if(this.CheckFlag) $('HaveWish').value = 'None'; // 아이템 페이지용
        
            if(havewish == "Have") {
                if($('HaveWish').value == 'Have') {
                    havewish = 'None';
                }
                else {
                    if($('HaveWish').value == 'Wish') {
                        $('imgIWish').src = '/images/item/btn_iwish_off.gif';
                        $('divHeartPoint').setStyle({display:'none'});
                        $('HeartPoint').value = 0;
                    }
                    $('imgIHave').src = '/images/item/btn_ihave_on.gif';
                    $('divStarPoint').setStyle({display:'inline'});
               }
            }
            else if(havewish == 'Wish') {
                if($('HaveWish').value == 'Wish') {
                    havewish = 'None';
                }
                else {
                    if($('HaveWish').value == 'Have') {
                        $('imgIHave').src = '/images/item/btn_ihave_off.gif';
                        $('divStarPoint').setStyle({display:'none'});
                        $('StarPoint').value = 0;
                    }
                    $('imgIWish').src = '/images/item/btn_iwish_on.gif';
                    $('divHeartPoint').setStyle({display:'inline'});
                }
            }
            
            if(havewish == 'None') {
                $('imgIHave').src = '/images/item/btn_ihave_off.gif';
                $('imgIWish').src = '/images/item/btn_iwish_off.gif';
                $('divStarPoint').setStyle({display:'none'});
                $('divHeartPoint').setStyle({display:'none'});
                $('StarPoint').value = 0;
                $('HeartPoint').value = 0;
            }
        
            $('HaveWish').value = havewish;
            this.CheckFlag = false;
            Score.HOTrating_init();
        }
        else
	        User.ShowLogin('SimpleReview.CheckHaveWish(\'' + havewish + '\')');
    },
    ShowWriter : function(width) {
        var userid = User.UserUID();
        this.WriterWidth = width;
        
        if(userid != null) {
            //if(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;
	        }
	        
            smalleditor.htmleditor.style.borderTop = 'solid 1px #8b8b8b';
            smalleditor.htmleditor.style.borderLeft = 'solid 1px #8b8b8b';	        
	    }
	    else
	        User.ShowLogin('SimpleReview.GetWriter(' + width + ')');
    },
    ShowWriter_Theme : function(width) {
        var userid = User.UserUID();
        this.WriterWidth = width;
        
        if(userid != null) {
            //if(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;
	        //}
	        
            smalleditor.htmleditor.style.borderTop = 'solid 1px #8b8b8b';
            smalleditor.htmleditor.style.borderLeft = 'solid 1px #8b8b8b';	        
	    }
	    else
	        User.ShowLogin('SimpleReview.GetWriter_Theme(' + width + ')');
    },
    GetWriter : function() {
        if(this.BoardID != null && this.WriterWidth > 0)
            CtlSimpleReviewWriteForm.GetSimpleReviewWriter(this.WriterWidth, 'SimpleReviewWriter', SimpleReview.GetWriterCallback);
    },
    GetWriter_Theme : function() {
        if(this.BoardID != null && this.WriterWidth > 0)
            CtlSimpleReviewThemeWriteForm.GetSimpleReviewWriter(this.WriterWidth, 'SimpleReviewWriter', SimpleReview.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);
                SimpleReview.ShowWriter(SimpleReview.WriterWidth);
            }
        }
    },    
    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)'});
        }
    },
    Save : function() {
        var userid = User.UserUID();
        
        if(userid == null || $('dummy').style.display != 'none') {
            $('dummy').onclick();
        }
        else {
            var point = 0;
            var havewish = $('HaveWish').value;
            var content = smalleditor.doc.body.innerHTML;
            
            if(this.ObjectID == null || this.ObjectID == '') {
                alert('아이템을 선택해주세요');
                return;
            }

            if(Opinity.Ajax.Common.RemoveTags(content).value.ReturnValue.trim() == '') {
                alert('내용을 입력해주세요');
                return;
            }
            
            if(havewish == "Have") {
                point = $('StarPoint').value;
            }
            else if(havewish == "Wish") {
                point = $('HeartPoint').value;
            }
            
            var postid = Opinity.Ajax.AjaxBoard.SaveSimpleReview(this.ObjectID, userid, content, '', havewish, point).value.ReturnValue;
            
            if(postid != '') {
                if(User.Me2day) 
                    if(Opinity.Ajax.AjaxBoard.Send2Me2day(postid, objectid, userid, this.BoardID, 0).value.ResultCode == 0) alert("미투전송 실패");

                CloseEdit();                
                if(this.NextFunction != '')
                    eval(this.NextFunction);
                return postid;
            }
            else
                alert('간단평가 입력시 에러발생');
        }
    },
    SaveReply : function(parentid, objectid) {
        var userid = User.UserUID();
        var content = $('txtReply' + parentid).value;
        
        if(userid != null) {
            /*if($('LOGINUSER-' + parentid).innerHTML == '') {
                $('LOGINUSER-' + parentid).update(User.UserNickname());
            }*/
            
            if(content.trim() == '') {
                alert('내용을 입력해주세요');
                return;
            }
            var postid = Opinity.Ajax.AjaxBoard.SaveSimpleReview(objectid, userid, content, parentid, 'None', 0).value.ReturnValue;
            
            if(postid != '') {
                if(this.NextFunction != '')
                    eval(this.NextFunction);
                else
                    document.location.reload();
                return postid;
            }
            else
                alert('입력시 에러발생');
        }
        else {
            User.ShowLogin("SimpleReview.SaveReply('" + parentid + "','" + objectid + "')");
            return;
        }
    },
    Delete : function(postid, itemid) {
        var userid = User.UserUID();
        
        if(confirm("삭제하시겠습니까?")) {
            if(Opinity.Ajax.AjaxBoard.DeleteBoardPost(this.BoardID, postid, itemid, userid).value.ReturnValue) {
                if(this.NextFunction != '')
                    eval(this.NextFunction);
                else
                    document.location.reload();
                return true;
            }
        }
        return false;
    },
    Edit : function(id, width) {
        var pos = Util.GetAbsolutePos($(id));
        var pos2 = Util.GetAbsolutePos($('imgReply' + id));
        var h = pos2.y - pos.y;
        if(h < 100) h = 100;

        if(isIE7 || isChrome) 
            $('PostEditor').setStyle({display:'inline',top:(pos.y - this.EditorTopFix - 11) + 'px',height:h + 'px'});
        else        
            $('PostEditor').setStyle({display:'inline',top:(pos.y - this.EditorTopFix) + 'px',height:h + 'px'});
        CtlSimpleReviewEditForm.getSimpleReviewEditor(id, width, h - 55, 'PostEditor', Util.AjaxCallback);
    },
    ClickReplyForm : function(parentid) {
        var nickname = User.UserNickname();
        
        if(nickname == null)
            User.ShowLogin("SimpleReview.ClickReplyForm('" + parentid + "')");
    },
    Init : function() {
        $('imgIHave').src = "/images/makeItem/btn_ihave_off.gif";
        $('imgIWish').src = "/images/makeItem/btn_iwish_off.gif";
        $('StarPoint').value = 0;
        $('HeartPoint').value = 0;
        $('StarPointImg').src = "/images/score/ico_star00.gif";
    }
}


var Score = {
    HOTrating_init : function() {
        $('imgStarPoint').src = "/images/score/ico_star" + $('StarPoint').value + ".gif";
        $('imgHeartPoint').src = "/images/score/ico_heart" + $('HeartPoint').value + ".gif";
    },
    HOTrating_mouseout : function(elem) {
        this.HOTrating_init();
    },			
    HOTrating_mouseover : function(flag) {
        if($('HaveWish').value == "None") return;
            
        if($('HaveWish').value == "Have") {
            imgElem = $('imgStarPoint');
            
            if (10 != flag)
	            imgElem.src = "/images/score/ico_star" + flag + "0.gif";
	        else
		        imgElem.src = "/images/score/ico_star100.gif";
	    }
        else if($('HaveWish').value == "Wish") {
            imgElem = $('imgHeartPoint');
            
            if (10 != flag)
	            imgElem.src = "/images/score/ico_heart" + flag + "0.gif";
	        else
		        imgElem.src = "/images/score/ico_heart100.gif";
        }
    },
    HOTrating_mouseclick : function(flag) {
        if($('HaveWish').value == "Have")                  
            $('StarPoint').value = flag * 10;
        else if($('HaveWish').value == "Wish") 
            $('HeartPoint').value = flag * 10;
    }
}


var SREditor = {
    Editor : null,
    PostID : '',
    ContentID : '',
    Close : function() {
        $('PostEditor').update('');
        $('PostEditor').setStyle({display:'none'});
    },
    Save : function() {
        var userid = User.UserUID();
        var point = 0;
        var havewish = $('HAVEWISH-' + this.PostID).value;
        var content = this.Editor.doc.body.innerHTML;
        
        if(content.trim() == '') {
            alert('내용을 입력해주세요');
            return;
        }
        
        if(havewish == 'Have') {
            point = $('STARPOINT-' + this.PostID).value;
        }
        else if(havewish == 'Wish') {
            point = $('HEARTPOINT-' + this.PostID).value;
        }

        if(Opinity.Ajax.AjaxBoard.UpdateSimpleReview(this.PostID, this.ContentID, userid, content, havewish, point).value.ReturnValue) {
            if(SimpleReview.NextFunction != null) eval(SimpleReview.NextFunction);
            this.Close();
        }        
    },
    CheckHaveWish : function(havewish) {
        if(havewish == 'Have') {
            if($('HAVEWISH-' + this.PostID).value == 'Have') {
                havewish = 'None';
            }
            else {
                if($('HAVEWISH-' + this.PostID).value == 'Wish') {
                    $('IMG-WISH-' + this.PostID).src = '/images/item/btn_iwish_off.gif';
                    $('DIV-HEART-' + this.PostID).setStyle({display:'none'});
                    $('HEARTPOINT-' + this.PostID).value = 0;
                }
                $('IMG-HAVE-' + this.PostID).src = '/images/item/btn_ihave_on.gif';
                $('DIV-STAR-' + this.PostID).setStyle({display:'inline'});
           }
        }
        else if(havewish == 'Wish') {
            if($('HAVEWISH-' + this.PostID).value == 'Wish') {
                havewish = 'None';
            }
            else {
                if($('HAVEWISH-' + this.PostID).value == 'Have') {
                    $('IMG-HAVE-' + this.PostID).src = '/images/item/btn_ihave_off.gif';
                    $('DIV-STAR-' + this.PostID).setStyle({display:'none'});
                    $('STARPOINT-' + this.PostID).value = 0;
                }
                $('IMG-WISH-' + this.PostID).src = '/images/item/btn_iwish_on.gif';
                $('DIV-HEART-' + this.PostID).setStyle({display:'inline'});
            }
        }
        
        if(havewish == 'None') {
            $('IMG-HAVE-' + this.PostID).src = '/images/item/btn_ihave_off.gif';
            $('IMG-WISH-' + this.PostID).src = '/images/item/btn_iwish_off.gif';
            $('DIV-STAR-' + this.PostID).setStyle({display:'none'});
            $('DIV-HEART-' + this.PostID).setStyle({display:'none'});
            $('STARPOINT-' + this.PostID).value = 0;
            $('HEARTPOINT-' + this.PostID).value = 0;
        }
    
        $('HAVEWISH-' + this.PostID).value = havewish;
        this.CheckFlag = false;
        this.HOTrating_init();
    },
    HOTrating_init : function() {
        $('IMG-STAR-' + this.PostID).src = '/images/score/ico_star' + $('STARPOINT-' + this.PostID).value + '.gif';
        $('IMG-HEART-' + this.PostID).src = '/images/score/ico_heart' + $('HEARTPOINT-' + this.PostID).value + '.gif';
    },
    HOTrating_mouseout : function(elem) {
        this.HOTrating_init();
    },			
    HOTrating_mouseover : function(flag) {
        if($('HAVEWISH-' + this.PostID).value == 'None') return;
            
        if($('HAVEWISH-' + this.PostID).value == 'Have') {
            imgElem = $('IMG-STAR-' + this.PostID);
            
            if (10 != flag)
	            imgElem.src = '/images/score/ico_star' + flag + '0.gif';
	        else
		        imgElem.src = '/images/score/ico_star100.gif';
	    }
        else if($('HAVEWISH-' + this.PostID).value == 'Wish') {
            imgElem = $('IMG-HEART-' + this.PostID);
            
            if (10 != flag)
	            imgElem.src = '/images/score/ico_heart' + flag + '0.gif';
	        else
		        imgElem.src = '/images/score/ico_heart100.gif';
        }
    },
    HOTrating_mouseclick : function(flag) {
        if($('HAVEWISH-' + this.PostID).value == 'Have')                  
            $('STARPOINT-' + this.PostID).value = flag * 10;
        else if($('HAVEWISH-' + this.PostID).value == 'Wish') 
            $('HEARTPOINT-' + this.PostID).value = flag * 10;
    }
}
