﻿
        
function loadCommentsData()
{
    var params = "{idMedia:'" + mediaObject.idmedia + "'}";
    
    jQuery.ajax({
           url: "/services/mediaservice/mediaservice.asmx/GetCommentsForMedia",
           type: "POST",
           data: params,
           contentType: "application/json; charset=utf-8",
           dataType: "json",
           complete: function(response,stat){
              if(stat=="success") {
                 if (response.responseText != '""')
                 { 
                     var commentsData = clearJSONInput(response.responseText);
                     $('#commentPanel').empty();
                     var counter = 0;
                     $.each(commentsData, function(index, value) { 
                        var btnFunction = 'javascript:btnSendCommentResponse_click("' + mediaObject.idmedia + '","' + infoUser.userid + '","' + infoUser.username + '",$("#newComment_' + counter + '").text(),"' + value.idcomment + '","");'
                     
                        var newCommentHtml = "<div class='wdgShowComments_comment' style='height:auto;margin-bottom:10px;' ><div class='wdgShowComments_header' >";
                        newCommentHtml += value.commentdate;
                        newCommentHtml += " - " + value.username;
                        newCommentHtml += ":</div><div class='wdgShowCommentsText' style='height:auto;margin-bottom:5px;text-align:justify;' >" + value.comment + "</div>";
                        if (value.childComments != "")
                        {
                            $.each(value.childComments, function(index, value) { 
                                newCommentHtml += "<div style='height:auto;' ><div class='wdgShowComments_header' style='padding-left:30px;border-left:1px #cccccc solid;height:auto;'>";
                                newCommentHtml += value.commentdate;
                                newCommentHtml += " - " + value.username;
                                newCommentHtml += ":</div><div class='wdgShowCommentsText' style='padding-left:30px;border-left:1px #cccccc solid;height:auto;margin-bottom:5px;text-align:justify;' >" + value.comment + "</div></div>";
                            });
                        }
                        
                        
                        if (infoUser.logged)
                        {
                            newCommentHtml += "<br /><span style='text-align:right;width:100%;height:auto;'>";
                            newCommentHtml += "<button class='wdgShowComments_button' id='btnShowResponeComment_" + counter + "'>" + wdgShowCommentResponseOpen + "</button></span>";
                            newCommentHtml += "<div id='responseCommentContainer_" + counter + "' style='height:auto;' ><fieldset><textarea name='commento' id='newComment_" + counter + "'";
                            newCommentHtml += " style='width: 500px; height: 100px; font-size: 11px' class='extendedInput' >" + wdgShowCommentResponse + "</textarea>";
                            newCommentHtml += "<button class='wdgShowComments_button' id='btnSendComment_" + counter + "' onclick='" + btnFunction + "' >" + wdgShowCommentResponseSend + "</button>";
                            newCommentHtml += "</fieldset></div>";
                        }
                        
                        newCommentHtml += "</div>";
                        
                        $('#commentPanel').append(newCommentHtml);
                        
                        if (infoUser.logged)
                        {
                            $("#responseCommentContainer_" + counter).hide();
                            $("#btnShowResponeComment_" + counter).button();
                            $("#btnShowResponeComment_" + counter).click(function() {
	                            $("#responseCommentContainer_" + $(this)[0].id.substr($(this)[0].id.indexOf('_')+1)).toggle('slow');
	                            return false;
                             });
                            $("#btnSendComment_" + counter).button();
                            $("#newComment_" + counter).jqEasyCounter({
                                maxChars: 500,
                                maxCharsWarning: 400,
                                msgFontSize: '10px',
                                msgFontColor: '#000',
                                msgWarningColor: '#F00'             
                            });
                            
                            $("#newComment_" + counter).keydown(function(event) {
                                if (event.keyCode == '13') {
                                    event.preventDefault();
                                    $("#btnSendComment_" + $(this)[0].id.substr($(this)[0].id.indexOf('_')+1)).click();
                                }
                            });
                        }
                        
                        counter++;
                     });
                     
                     $('#commentPanelTitle').show();
                     
                     return true;
                 }
                 else
                 {
                    $('#commentPanelTitle').hide();
                 }
              }
           }
        });
}

$(function() {
    loadCommentsData();
    
    $('#newComment').jqEasyCounter({
        maxChars: 500,
        maxCharsWarning: 400,
        msgFontSize: '10px',
        msgFontColor: '#000',
        msgWarningColor: '#F00'             
    });
    
    $('#newComment').keydown(function(event) {
        if (event.keyCode == '13') {
            event.preventDefault();
            $('#btnSendComment').click();
        }
    });
});

function btnSendComment_click(idMedia, idUser, userName, 
                                comment, idCommentLinked, bpComment)
{
    var params = "{idMedia:'" + idMedia + "',";
    params += "idUser:'" + idUser + "',";
    params += "userName:'" + userName + "',";
    params += "comment:'" + comment + "',";
    params += "idCommentLinked:'" + idCommentLinked + "',";
    params += "bpComment:'" + bpComment + "'}";

    jQuery.ajax({
       url: "/services/mediaservice/mediaservice.asmx/SetCommentForMedia",
       type: "POST",
       data: params,
       contentType: "application/json; charset=utf-8",
       dataType: "json",
       complete: function(response,stat){
          if(stat=="success") {
             loadCommentsData();
             $('#bpComment').toggle('slow');
             $('#bpCommentButton').toggleClass("active");
             return true;
          }
       }
    });
}

function btnSendCommentResponse_click(idMedia, idUser, userName, 
            comment, idCommentLinked, bpComment)
{
    var params = "{idMedia:'" + idMedia + "',";
    params += "idUser:'" + idUser + "',";
    params += "userName:'" + userName + "',";
    params += "comment:'" + comment + "',";
    params += "idCommentLinked:'" + idCommentLinked + "',";
    params += "bpComment:'" + bpComment + "'}";

    jQuery.ajax({
       url: "/services/mediaservice/mediaservice.asmx/SetCommentForMedia",
       type: "POST",
       data: params,
       contentType: "application/json; charset=utf-8",
       dataType: "json",
       complete: function(response,stat){
          if(stat=="success") {
             loadCommentsData();
             return true;
          }
       }
    });
}

