var IDs = {
    glayer             : "glayer",
    wait               : "wait",
    errorDialog        : "errorDialog",
    
    commentDialog      : "commentDialog",
    commentCompDialog  : "commentCompDialog",
    clipid             : "clipid",
    nickname           : "nickname",
    written_nickname   : "written_nickname",
    comment            : "comment",
    written_comment    : "written_comment",
    userCommentList    : "user_comment_list",
    userComentTmpl     : "user_coment_tmpl",
    commentBtn         : "commentBtn",
    okBtn_comment      : "okBtn_comment",
    cancelBtn_comment  : "cancelBtn_comment",
    okBtn_commentComp  : "okBtn_commentComp",
    
    commentDeleteDialog      : "commentDeleteDialog",
    commentDeleteCompDialog  : "commentDeleteCompDialog",
    delete_comment           : "delete_comment",
    delete_comment_id        : "delete_comment_id",
    okBtn_comment_delete     : "okBtn_comment_delete",
    cancelBtn_comment_delete : "cancelBtn_comment_delete",
    okBtn_comment_deleteComp : "okBtn_comment_deleteComp",
    
    friendDialog        : "friendDialog",
    friendCompDialog    : "friendCompDialog",
    mailaddress_friend  : "mailaddress_friend",
    sender_name_friend  : "sender_name_friend",
    sender_email_friend : "sender_email_friend",
    message_friend      : "message_friend",
    errorMsg_friend     : "errorMsg_friend",
    friendBtn           : "friendBtn",
    okBtn_friend        : "okBtn_friend",
    cancelBtn_friend    : "cancelBtn_friend",
    okBtn_friendComp    : "okBtn_friendComp"
}

/**
 * コメント
 */
var Comment = {
    init : function()
    {
        this.getCommentList();
        
        CommonLib.init(IDs.glayer);
        ErrorDialog.init(IDs.errorDialog);
        
        Event.observe($(IDs.commentBtn),        "click", this.openDialog.bindAsEventListener(this),      false);
        Event.observe($(IDs.okBtn_comment),     "click", this.okOnRelease.bindAsEventListener(this),     false);
        Event.observe($(IDs.cancelBtn_comment), "click", this.cancelOnRelease.bindAsEventListener(this), false);
        Event.observe($(IDs.okBtn_commentComp), "click", this.ok2OnRelease.bindAsEventListener(this),    false);
        
        Event.observe($(IDs.okBtn_comment_delete),     "click", this.okDeleteOnRelease.bindAsEventListener(this),     false);
        Event.observe($(IDs.cancelBtn_comment_delete), "click", this.cancelDeleteOnRelease.bindAsEventListener(this), false);
        Event.observe($(IDs.okBtn_comment_deleteComp), "click", this.ok2DeleteOnRelease.bindAsEventListener(this),    false);
    },
    
    openDialog : function()
    {    
        CommonLib.startWait(IDs.wait);
        var url    = '/clips/comment/regist.php';
        var method = 'post';
        var pars   = 'proc=index';
            pars  += '&' + 'clipid=' + $(IDs.clipid).value;
            pars  += '&' + 'comment=' + CommonLib.strToAJAX($(IDs.comment).value);
            pars  += '&' + 'nickname=' + CommonLib.strToAJAX($(IDs.nickname).value);
        var myAjax = new Ajax.Request(
                url, 
                {
                    method:     method,
                    parameters: pars, 
                    onSuccess:  function(json) {
                        var res = eval("(" + json.responseText + ")");
                        if (res.error_msg != "") {
                            ErrorDialog.show(res.error_msg);
                            CommonLib.stopWait(IDs.wait);
                        } else {
                            var nickname = $(IDs.nickname).value;
                                nickname = CommonLib.strToHtml(nickname);
                            var comment = $(IDs.comment).value;
                                comment = CommonLib.strToHtml(comment);
                                comment = comment.replace(/\x0D\x0A|\x0D|\x0A/g,'<br />');
                            $(IDs.written_comment).innerHTML  = comment;
                            $(IDs.written_nickname).innerHTML = nickname;
                            CommonLib.showDialog(IDs.commentDialog);
                            CommonLib.stopWait(IDs.wait);
                        }
                    }
                }
            );
    },
    
    okOnRelease : function()
    {
        CommonLib.startWait(IDs.wait);
        var url    = '/clips/comment/regist.php';
        var method = 'post';
        var pars   = 'proc=index';
            pars  += '&' + 'clipid=' + $(IDs.clipid).value;
            pars  += '&' + 'comment=' + CommonLib.strToAJAX($(IDs.comment).value);
            pars  += '&' + 'nickname=' + CommonLib.strToAJAX($(IDs.nickname).value);
            pars  += '&' + 'regist_flg=1';
        var myAjax = new Ajax.Request(
                url, 
                {
                    method:     method,
                    parameters: pars, 
                    onSuccess:  function(json) {
                        var res = eval("(" + json.responseText + ")");
                        if (res.error_msg != "") {
                            CommonLib.hideDialog(IDs.commentDialog);
                            ErrorDialog.show(res.error_msg);
                            CommonLib.stopWait(IDs.wait);
                        } else {
                            Comment.getCommentList();
                            //$(IDs.nickname).value             = "";
                            $(IDs.written_nickname).innerHTML = "";
                            $(IDs.comment).value              = "";
                            $(IDs.written_comment).innerHTML  = "";
                            CommonLib.hideDialog(IDs.commentDialog);
                            CommonLib.showDialog(IDs.commentCompDialog);
                            CommonLib.stopWait(IDs.wait);
                        }
                    }
                }
            );
    },
    
    cancelOnRelease : function()
    {
        CommonLib.hideDialog(IDs.commentDialog);
    },
    
    ok2OnRelease : function()
    {
        CommonLib.hideDialog(IDs.commentCompDialog);
    },
    
    //コメント一覧を取得
    getCommentList : function()
    {
        var url    = '/clips/comment/list.php';
        var method = 'post';
        var pars   = 'proc=index';
            pars  += '&' + 'clipid=' + $(IDs.clipid).value;
        var myAjax = new Ajax.Request(
                url, 
                {
                    method:     method,
                    parameters: pars, 
                    onSuccess:  function(json) {
                        var res = eval("(" + json.responseText + ")");
                        $(IDs.userCommentList).innerHTML = '';
                        for (var i = 0; i < res.comment_list.length ; i++) {
                            $(IDs.userCommentList).innerHTML += Comment.makeComment(res.comment_list[i].id, res.comment_list[i].name, res.comment_list[i].system_user_id, res.comment_list[i].date_year, res.comment_list[i].date_month, res.comment_list[i].date_day, res.comment_list[i].content, res.comment_list[i].btn_flg, res.comment_list[i].guest_flg, res.comment_list[i].rating);
                        }
                    }
                }
            );
    },
    
    //コメント行生成
    makeComment : function(id, name, system_user_id, year, month, day, content, btn_flg, guest_flg, rating)
    {
        var comment = $(IDs.userComentTmpl).innerHTML;
            if (btn_flg == 1) {
                comment = comment.replace('none',  'block');
            }
            comment = comment.replace('Comment.del(0)',    'Comment.del(' + id + ')');
            comment = comment.replace('user_comment0',        'user_comment' + id);
            if (guest_flg == 0) {
                comment = comment.replace('###name###',       '<a href="/clips/' + system_user_id + '/">' + name + '</a>');
                comment = comment.replace('###style1###',     '');
                comment = comment.replace('###style2###',     '');
                comment = comment.replace('###style3###',     '');
                comment = comment.replace('###style4###',     '');
                comment = comment.replace('###style5###',     '');
            } else {
                comment = comment.replace('###name###',       name);
                comment = comment.replace('###style1###',     'style="display: none;"');
                comment = comment.replace('###style2###',     'style="display: none;"');
                comment = comment.replace('###style3###',     'style="display: none;"');
                comment = comment.replace('###style4###',     'style="display: none;"');
                comment = comment.replace('###style5###',     'style="display: none;"');
            }
            comment = comment.replace('###year###',           year);
            comment = comment.replace('###month###',          month);
            comment = comment.replace('###day###',            day);
            comment = comment.replace('###content###',        content);
            for (i = 1; i <= parseInt(rating); i++) {
                comment = comment.replace('###star' + i.toString() + '###', 'full');
            }
            for (j = i; j <= 5; j++) {
                comment = comment.replace('###star' + j.toString() + '###', 'empty');
            }
        return comment;
    },
    
    //コメント削除
    del : function(id)
    {
        CommonLib.startWait(IDs.wait);
        $(IDs.delete_comment).innerHTML = $('user_comment'+id).innerHTML;
        $(IDs.delete_comment_id).value = id;
        CommonLib.showDialog(IDs.commentDeleteDialog);
        CommonLib.stopWait(IDs.wait);
    },
    
    okDeleteOnRelease : function()
    {
        CommonLib.startWait(IDs.wait);
        var url    = '/clips/comment/delete.php';
        var method = 'post';
        var pars   = 'proc=index';
            pars  += '&' + 'clipid=' + $(IDs.clipid).value;
            pars  += '&' + 'comment_id=' + $(IDs.delete_comment_id).value;
        var myAjax = new Ajax.Request(
                url, 
                {
                    method:     method,
                    parameters: pars, 
                    onSuccess:  function(json) {
                        var res = eval("(" + json.responseText + ")");
                        if (res.error_msg != "") {
                            CommonLib.hideDialog(IDs.commentDeleteDialog);
                            ErrorDialog.show(res.error_msg);
                            CommonLib.stopWait(IDs.wait);
                        } else {
                            Comment.getCommentList();
                            CommonLib.hideDialog(IDs.commentDeleteDialog);
                            CommonLib.showDialog(IDs.commentDeleteCompDialog);
                            CommonLib.stopWait(IDs.wait);
                        }
                    }
                }
            );
    },
    
    cancelDeleteOnRelease : function()
    {
        CommonLib.hideDialog(IDs.commentDeleteDialog);
    },
    
    ok2DeleteOnRelease : function()
    {
        CommonLib.hideDialog(IDs.commentDeleteCompDialog);
    }
}

/**
 * お友達に教える
 */
var Friend = {
    init : function()
    {
        CommonLib.init(IDs.glayer);
        ErrorDialog.init(IDs.errorDialog);
        
        Event.observe($(IDs.friendBtn),        "click", this.openDialog.bindAsEventListener(this),      false);
        Event.observe($(IDs.okBtn_friend),     "click", this.okOnRelease.bindAsEventListener(this),     false);
        Event.observe($(IDs.cancelBtn_friend), "click", this.cancelOnRelease.bindAsEventListener(this), false);
        Event.observe($(IDs.okBtn_friendComp), "click", this.ok2OnRelease.bindAsEventListener(this),    false);
    },
    
    openDialog : function()
    {
        CommonLib.startWait(IDs.wait);
        $(IDs.errorMsg_friend).innerHTML = '';
        $(IDs.mailaddress_friend).value  = '';
        $(IDs.message_friend).value      = '';
        CommonLib.showDialog(IDs.friendDialog);
        CommonLib.stopWait(IDs.wait);
    },
    
    okOnRelease : function()
    {
        CommonLib.startWait(IDs.wait);
        var url    = '/clips/friend.php';
        var method = 'post';
        var pars   = 'proc=index';
            pars  += '&' + 'clipid=' + $(IDs.clipid).value;
            pars  += '&' + 'mailaddress=' + CommonLib.strToAJAX($(IDs.mailaddress_friend).value);
            pars  += '&' + 'sender_name=' + CommonLib.strToAJAX($(IDs.sender_name_friend).value);
            pars  += '&' + 'sender_email=' + CommonLib.strToAJAX($(IDs.sender_email_friend).value);
            pars  += '&' + 'message=' + CommonLib.strToAJAX($(IDs.message_friend).value);
        var myAjax = new Ajax.Request(
                url, 
                {
                    method:     method,
                    parameters: pars, 
                    onSuccess:  function(json) {
                        var res = eval("(" + json.responseText + ")");
                        if (res.error_msg != "") {
                            $(IDs.errorMsg_friend).innerHTML = res.error_msg;
                            CommonLib.stopWait(IDs.wait);
                        } else {
                            CommonLib.hideDialog(IDs.friendDialog);
                            CommonLib.showDialog(IDs.friendCompDialog);
                            CommonLib.stopWait(IDs.wait);
                        }
                    }
                }
            );
    },
    
    cancelOnRelease : function()
    {
        CommonLib.hideDialog(IDs.friendDialog);
    },
    
    ok2OnRelease : function()
    {
        CommonLib.hideDialog(IDs.friendCompDialog);
    }
}

/**
 * 投票
 */
var Rating = {
    init : function()
    {
        this.getRating();
        
        Event.observe($('my_rating1'), "click",  this.doRating.bindAsEventListener(this), false);
        Event.observe($('my_rating2'), "click",  this.doRating.bindAsEventListener(this), false);
        Event.observe($('my_rating3'), "click",  this.doRating.bindAsEventListener(this), false);
        Event.observe($('my_rating4'), "click",  this.doRating.bindAsEventListener(this), false);
        Event.observe($('my_rating5'), "click",  this.doRating.bindAsEventListener(this), false);

        Event.observe($('my_rating1'), "mouseout",  this.changeMyRating0.bindAsEventListener(this), false);
        Event.observe($('my_rating2'), "mouseout",  this.changeMyRating0.bindAsEventListener(this), false);
        Event.observe($('my_rating3'), "mouseout",  this.changeMyRating0.bindAsEventListener(this), false);
        Event.observe($('my_rating4'), "mouseout",  this.changeMyRating0.bindAsEventListener(this), false);
        Event.observe($('my_rating5'), "mouseout",  this.changeMyRating0.bindAsEventListener(this), false);

        Event.observe($('my_rating1'), "mouseover", this.changeMyRating1.bindAsEventListener(this), false);
        Event.observe($('my_rating2'), "mouseover", this.changeMyRating2.bindAsEventListener(this), false);
        Event.observe($('my_rating3'), "mouseover", this.changeMyRating3.bindAsEventListener(this), false);
        Event.observe($('my_rating4'), "mouseover", this.changeMyRating4.bindAsEventListener(this), false);
        Event.observe($('my_rating5'), "mouseover", this.changeMyRating5.bindAsEventListener(this), false);
    },
    
    getRating : function()
    {
        var url    = '/clips/rating/get_avg.php';
        var method = 'post';
        var pars   = 'proc=index';
            pars  += '&' + 'clipid=' + $(IDs.clipid).value;
        var myAjax = new Ajax.Request(
                url, 
                {
                    method:     method,
                    parameters: pars, 
                    onSuccess:  function(json) {
                        var res = eval("(" + json.responseText + ")");
                        if (res.error_msg != "") {
                            ErrorDialog.show(res.error_msg);
                        } else {

                            Rating.resetAvgRating();

                            var rating_num = 5;
                            var rating1 = Math.floor(res.avg_rating);
                            var rating2 = 0;
                            if ((""+res.avg_rating).indexOf(".") > -1) {
                                rating2 = 1;
                            }
                            var rating3 = rating_num - rating1 - rating2;
                            
                            var no = 1
                            for (var i=1; i <= rating1; i++) {
                                $('avg_rating'+no).addClassName("full");
                                no++;
                            }
                            for (var i=1; i <= rating2; i++) {
                                $('avg_rating'+no).addClassName("half");
                                no++;
                            }
                            for (var i=1; i <= rating3; i++) {
                                $('avg_rating'+no).addClassName("empty");
                                no++;
                            }
                        }
                    }
                }
            );
    },

    resetAvgRating : function()
    {
        $('avg_rating1').removeClassName("full");
        $('avg_rating2').removeClassName("full");
        $('avg_rating3').removeClassName("full");
        $('avg_rating4').removeClassName("full");
        $('avg_rating5').removeClassName("full");

        $('avg_rating1').removeClassName("half");
        $('avg_rating2').removeClassName("half");
        $('avg_rating3').removeClassName("half");
        $('avg_rating4').removeClassName("half");
        $('avg_rating5').removeClassName("half");

        $('avg_rating1').removeClassName("empty");
        $('avg_rating2').removeClassName("empty");
        $('avg_rating3').removeClassName("empty");
        $('avg_rating4').removeClassName("empty");
        $('avg_rating5').removeClassName("empty");
    },
    
    changeMyRating0 : function()
    {

        $('my_rating1').removeClassName("full");
        $('my_rating2').removeClassName("full");
        $('my_rating3').removeClassName("full");
        $('my_rating4').removeClassName("full");
        $('my_rating5').removeClassName("full");
        $('rating').value = '0';
    },
    
    changeMyRating1 : function()
    {
        $('my_rating1').addClassName("full");
        $('my_rating2').addClassName("empty");
        $('my_rating3').addClassName("empty");
        $('my_rating4').addClassName("empty");
        $('my_rating5').addClassName("empty");
        $('rating').value = '1';
    },
    
    changeMyRating2 : function()
    {
        $('my_rating1').addClassName("full");
        $('my_rating2').addClassName("full");
        $('my_rating3').addClassName("empty");
        $('my_rating4').addClassName("empty");
        $('my_rating5').addClassName("empty");
        $('rating').value = '2';
    },
    
    changeMyRating3 : function()
    {
        $('my_rating1').addClassName("full");
        $('my_rating2').addClassName("full");
        $('my_rating3').addClassName("full");
        $('my_rating4').addClassName("empty");
        $('my_rating5').addClassName("empty");
        $('rating').value = '3';
    },
    
    changeMyRating4 : function()
    {
        $('my_rating1').addClassName("full");
        $('my_rating2').addClassName("full");
        $('my_rating3').addClassName("full");
        $('my_rating4').addClassName("full");
        $('my_rating5').addClassName("empty");
        $('rating').value = '4';
    },
    
    changeMyRating5 : function()
    {
        $('my_rating1').addClassName("full");
        $('my_rating2').addClassName("full");
        $('my_rating3').addClassName("full");
        $('my_rating4').addClassName("full");
        $('my_rating5').addClassName("full");
        $('rating').value = '5';
    },
    
    //評価を行う
    doRating : function()
    {
        var url    = '/clips/rating/regist.php';
        var method = 'post';
        var pars   = 'proc=index';
            pars  += '&' + 'clipid=' + $(IDs.clipid).value;
            pars  += '&' + 'rating=' + $('rating').value;
        var message = "★×" + $('rating').value + " で評価しますか？";
        if (confirm(message) == true) {
            var myAjax = new Ajax.Request(
                url, 
                {
                    method:     method,
                    parameters: pars, 
                    onSuccess:  function(json) {
                        var res = eval("(" + json.responseText + ")");
                        if (res.error_msg != "") {
                            ErrorDialog.show(res.error_msg);
                        } else {
                            var youElements = document.getElementsByClassName("you", "evalStarWrapper");
                            var length = youElements.length;
                            for (var i = 0; i < length; i++) {
                                CommonLib.hideDialog(youElements[i]);
                            }
                            $("evalStarWrapper").addClassName("after");
                            Rating.getRating();
                            Comment.getCommentList();
                        }
                    }
                }
            );
        }
    }
}

/**
 * オススメ
 */
var recommendation_registed = new Array(9);
recommendation_registed[1] = false;
recommendation_registed[2] = false;
recommendation_registed[3] = false;
recommendation_registed[4] = false;
recommendation_registed[5] = false;
recommendation_registed[6] = false;
recommendation_registed[7] = false;
recommendation_registed[8] = false;
recommendation_registed[9] = false;

var Recommendation = {
    init : function(isMember)
    {
        CommonLib.init(IDs.glayer);
        ErrorDialog.init(IDs.errorDialog);
        
        this.getNum();
        
        if (isMember) {
        Event.observe($('recommendation1'), "mouseover", this.changeBg.bind(this,'',1), false);
        Event.observe($('recommendation2'), "mouseover", this.changeBg.bind(this,'',2), false);
        Event.observe($('recommendation3'), "mouseover", this.changeBg.bind(this,'',3), false);
        Event.observe($('recommendation4'), "mouseover", this.changeBg.bind(this,'',4), false);
        Event.observe($('recommendation5'), "mouseover", this.changeBg.bind(this,'',5), false);
        Event.observe($('recommendation6'), "mouseover", this.changeBg.bind(this,'',6), false);
        Event.observe($('recommendation7'), "mouseover", this.changeBg.bind(this,'',7), false);
        Event.observe($('recommendation8'), "mouseover", this.changeBg.bind(this,'',8), false);
        Event.observe($('recommendation9'), "mouseover", this.changeBg.bind(this,'',9), false);
        
        Event.observe($('recommendation1'), "mouseout", this.clearBg.bind(this,'',1), false);
        Event.observe($('recommendation2'), "mouseout", this.clearBg.bind(this,'',2), false);
        Event.observe($('recommendation3'), "mouseout", this.clearBg.bind(this,'',3), false);
        Event.observe($('recommendation4'), "mouseout", this.clearBg.bind(this,'',4), false);
        Event.observe($('recommendation5'), "mouseout", this.clearBg.bind(this,'',5), false);
        Event.observe($('recommendation6'), "mouseout", this.clearBg.bind(this,'',6), false);
        Event.observe($('recommendation7'), "mouseout", this.clearBg.bind(this,'',7), false);
        Event.observe($('recommendation8'), "mouseout", this.clearBg.bind(this,'',8), false);
        Event.observe($('recommendation9'), "mouseout", this.clearBg.bind(this,'',9), false);
        
        Event.observe($('recommendation1'), "click", this.regist.bind(this,'',1),      false);
        Event.observe($('recommendation2'), "click", this.regist.bind(this,'',2),      false);
        Event.observe($('recommendation3'), "click", this.regist.bind(this,'',3),      false);
        Event.observe($('recommendation4'), "click", this.regist.bind(this,'',4),      false);
        Event.observe($('recommendation5'), "click", this.regist.bind(this,'',5),      false);
        Event.observe($('recommendation6'), "click", this.regist.bind(this,'',6),      false);
        Event.observe($('recommendation7'), "click", this.regist.bind(this,'',7),      false);
        Event.observe($('recommendation8'), "click", this.regist.bind(this,'',8),      false);
        Event.observe($('recommendation9'), "click", this.regist.bind(this,'',9),      false);
        }
    },
    
    changeBg : function(obj ,i)
    {
        if (!recommendation_registed[i]) {
            $('recommendation'+i).addClassName("over");
            $('recommendation'+i).style.cursor = 'pointer';
        }
    },
    
    clearBg : function(obj, i)
    {
        if (!recommendation_registed[i]) {
            $('recommendation'+i).removeClassName("over");
        }
    },
    
    getNum : function()
    {
        var url    = '/clips/recommendation/get_num.php';
        var method = 'post';
        var pars   = 'proc=index';
            pars  += '&' + 'clipid=' + $(IDs.clipid).value;
        var myAjax = new Ajax.Request(
                url, 
                {
                    method:     method,
                    parameters: pars, 
                    onSuccess:  function(json) {
                        var res = eval("(" + json.responseText + ")");
                        if (res.error_msg != "") {
                            ErrorDialog.show(res.error_msg);
                        } else {
                            for (var i = 0; i < res.recommentdations.length ; i++) {
                                $('recommendation' + res.recommentdations[i].id + '_num').innerHTML = res.recommentdations[i].num;
                                if (res.recommentdations[i].registed == 1) {
                                    recommendation_registed[res.recommentdations[i].id] = true;
                                    $('recommendation'+res.recommentdations[i].id).addClassName("after");
                                    $('recommendation'+res.recommentdations[i].id).style.cursor = 'default';
                                }
                            }
                        }
                    }
                }
           );
    },
    
    regist : function(obj, i)
    {
        if (!recommendation_registed[i]) {
            var url    = '/clips/recommendation/regist.php';
            var method = 'post';
            var pars   = 'proc=index';
                pars  += '&' + 'clipid=' + $(IDs.clipid).value;
                pars  += '&' + 'recommendation_id=' + i;
            var myAjax = new Ajax.Request(
                    url, 
                    {
                        method:     method,
                        parameters: pars, 
                        onSuccess:  function(json) {
                            var res = eval("(" + json.responseText + ")");
                            if (res.error_msg != "") {
                                ErrorDialog.show(res.error_msg);
                            } else {
                                Recommendation.getNum();
                            }
                        }
                    }
               );
        }
    }
}
