﻿var Dcc_CommentsAPI = null;
if (!window.jqsDCCJMG) jqsDCCJMG = jQuery;
(function(jQuery) {
    MediaGallery = {
        DeleteMedia: function(moduleid, mediaid) {
            DCCJMG_SetConstants(moduleid);
            DCCJMG.iMediaIdToMod = mediaid;
            var dialogDeleteMedia = jQuery('#' + moduleid + '_dialogDeleteMedia');
            dialogDeleteMedia.dialog('open');
            dialogDeleteMedia.height(55);
        },
        EditMedia: function(moduleid, mediaid) {
            DCCJMG_SetConstants(moduleid);
            DCCJMG.iMediaIdToMod = mediaid;
            WEBGetMedia(DCCJMG.PortalId, moduleid, mediaid);
            var dialogEditMedia = jQuery("#" + moduleid + "_dialogEditMedia");
            dialogEditMedia.dialog('open');
            dialogEditMedia.height('auto');
        },
        LoadSubGalleries: function(moduleId, galleryId, folderId) {
            DCCJMG_SetConstants(moduleId);
            if (!DCCJMG.enableSubGallery) return;
            if (galleryId != -1) {
                WEBGetSubGalleriesList(DCCJMG.PortalId, moduleId, galleryId, folderId);
            }
            else {
                var parentDiv = jQuery("div[id*=" + moduleId + "_ViewMediaGallery_dvGallaryMain]");
                //no gallery found in settings
                jQuery('div#' + moduleId + '_MediaGallery').hide();
                parentDiv.find('div.dcc_gallery_container').find(".EmptyMessage").css("color", '#' + DCCJMG.strTextColor).show();
            }
        },
        LoadMedia: function(moduleId, galleryId, folderId, mediaPageNo) {
            DCCJMG_SetConstants(moduleId);
            if (galleryId != -1) {
                WEBGetMediaList(DCCJMG.TabId, DCCJMG.PortalId, moduleId, galleryId, folderId, DCCJMG.UserId, mediaPageNo, DCCJMG.pageSize, DCCJMG.thumbSortBy, DCCJMG.specificuserdesc, DCCJMG.specificuserflag);
            }
            else {
                var parentDiv = jQuery("div[id*=" + moduleId + "_ViewMediaGallery_dvGallaryMain]");
                //no gallery found in settings
                handlePaging(moduleId, 1, 1, 1);
                jQuery('div#' + moduleId + '_MediaGallery').hide();
                parentDiv.find('div.dcc_gallery_container').find(".EmptyMessage").css("color", '#' + DCCJMG.strTextColor).show();
            }
        },
        ValidateAddComment: function(obj) {
            var parentDiv = jQuery(obj).parents("div[class*=pp_pic_holder]");
            var flag = true;
            var txtComment = parentDiv.find("*[id*=txtComment]")[0].value;
            if (jQuery.trim(txtComment) == "") {
                flag = false;
            }
            else if (MediaGallery.LoggedInUserId == -1) {
                var txtAnonymousEmail = parentDiv.find("*[id*=txtAnonymousEmail]")[0];
                var captchaCode = parentDiv.find("*[id*=uword]")[0].value;
                if (jQuery.trim(captchaCode) == '') {
                    parentDiv.find("*[id*=lblMessage]").html(strJcapEmptyErrorMessage).css("color", "red");
                    setTimeout("Dcc_CommentsAPI.ClearMessage();", 5000);
                    flag = false;
                }
                var txtAnonymousName = parentDiv.find("*[id*=txtAnonymousName]")[0];
                if (txtAnonymousName.value == "" || txtAnonymousName.value == strAnonymousText) {
                    parentDiv.find("*[id*=lblAnonymousName]")[0].innerHTML = '*';
                    flag = false;
                }
                else {
                    parentDiv.find("*[id*=lblAnonymousName]")[0].innerHTML = '';
                }
                if (txtAnonymousEmail.value == "" || !MediaGallery.ValidateEmail(txtAnonymousEmail.value)) {
                    parentDiv.find("*[id*=lblAnonymousEmail]")[0].innerHTML = strAnonymousEmailErrorMessage;
                    flag = false;
                }
                else {
                    parentDiv.find("*[id*=lblAnonymousEmail]")[0].innerHTML = '';
                }
            }
            return flag;
        },
        AddComment: function(e) {
            var ev = e || window.event;
            var obj = ev.target || ev.srcElement;
            MediaGallery.AddComment.obj = obj;
            var picHolderDiv = jQuery(obj).parents("div[class*=pp_pic_holder]");
            var parentDiv = jQuery("div[id*=" + DCCJMG.ModuleId + "_ViewMediaGallery_dvGallaryMain]");
            if (MediaGallery.ValidateAddComment(obj)) {
                picHolderDiv.find("*[id*=btnAddComment]")[0].disabled = true;
                picHolderDiv.find("*[id*=lblMessage]")[0].innerHTML = strAddingCommentMessage;
                var userName = null;
                var emailAddress = null;
                if (picHolderDiv.find("*[id*=rowCaptcha]").css("display") != "none") {
                    userName = picHolderDiv.find("*[id*=txtAnonymousName]")[0].value
                    emailAddress = picHolderDiv.find("*[id*=txtAnonymousEmail]")[0].value;
                }
                else {
                    userName = parentDiv.find("*[id*=hdnUserName]")[0].value
                    emailAddress = parentDiv.find("*[id*=hdnUserEmail]")[0].value;
                }
                var comment = {
                    CommentID: -1,
                    MediaID: parseInt(mediaIds[window.currentindex]),
                    CommentText: picHolderDiv.find("*[id*=txtComment]")[0].value.substring(0, 150),
                    UserID: parseInt(parentDiv.find("*[id*=hdnUserId]")[0].value),
                    UserName: userName,
                    UserEmail: emailAddress,
                    PostingDateTime: '',
                    StatusFlag: 1,
                    IsApproved: 1
                };
                DNNCentric.JMediaGallery.MediaGalleryController.AddComment(comment, jQuery.trim(document.getElementById("uword").value), DCCJMG.ModuleId, Dcc_CommentsAPI.AddCommentSuccessCallBack, Dcc_CommentsAPI.AddCommentFailureCallBack);
            }
            return false;
        },
        ShowCharCount: function(e) {
            var ev = e || window.event;
            var obj = ev.target || ev.srcElement;
            var parentDiv = jQuery(obj).parents("div[aria-labelledby*=_dialogEditMedia]");
            var lblDeleteBoxCharCount = parentDiv.find("*[id*=lblDeleteBoxCharCount]")[0];
            if (DCCJMG.maxCharCount < obj.value.length) {
                obj.value = obj.value.toString().substring(0, DCCJMG.maxCharCount);
                return false;
            }
            lblDeleteBoxCharCount.innerHTML = DCCJMG.maxCharCount - obj.value.length;
            return true;
        },
        ResetBackGround: function(e) {
            var ev = e || window.event;
            var obj = ev.target || ev.srcElement;
            if (obj.value.length > 0) {
                jQuery(obj).css('background-color', 'white');
            }
        },
        ValidateEmail: function(email) {
            var reg = /^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$/;
            return reg.test(email);
        },
        SetCommentsHeight: function(obj, height, increaseHeight) {
            var parentDiv = jQuery(obj).parents("div[id*=dvGallaryMain]");
            var parent = jqsDCCJMG("#pp_comments").parent();
            if (increaseHeight) {
                while (parent.attr("class") != "pp_right") {
                    parent.height(parent.height() + height);
                    parent = parent.parent();
                }
            }
            else {
                while (parent.attr("class") != "pp_right") {
                    parent.height(parent.height() - height);
                    parent = parent.parent();
                }
            }
        },
        ToggleComments: function(e) {
            var ev = e || window.event;
            var obj = ev.target || ev.srcElement;
            MediaGallery.ClearCommentsOnLoad(obj);
            var parentDiv = jQuery(obj).parents("div[class*=pp_pic_holder]");
            var parent = jQuery(obj).parent();
            var tblComments = parent.find("table");
            //code to set height and width of comment's area.
            var pp_comments = parentDiv.find("*[id*=pp_comments]");
            var pp_pic_holder = parentDiv;
            //code to show hide captcha            
            if (MediaGallery.LoggedInUserId != -1) {
                parentDiv.find("*[id*=rowCaptcha]").css("display", "none");
            }
            else {
                parentDiv.find("*[id*=rowCaptcha]").css("display", "inline-block");
                //var captchaImage = parent.find("img:first").attr("src", strBaseURLforIMG + 'js/jcap/' + decodeURIComponent(imgdir) + '/' + imgid + '.jpg');
                var captchaImage = parent.find("img:first").attr("src", DCCJMG.strModulePath + 'Thumbnail.ashx?ot=3&mid=' + DCCJMG.ModuleId + '&rnd=' + Math.random());
                parent.find("img:nth-child(2)").attr("src", DCCJMG.strBaseURLforIMG + 'images/refresh.jpg');
                if (navigator.appName == "Microsoft Internet Explorer") {
                    captchaImage.parent().find("input:first").css("top", "-5px");
                }

                //code to show hide for comments area in case of anonymous comments are disabled.
                if (DCCJMG.IsAnynomousCommentsEnabled) {
                    parentDiv.find("*[id*=rowCommentArea]").css("display", "inline-block");
                    parentDiv.find("*[id*=btnAddComment]").css("display", "inline-block");
                }
                else {
                    parentDiv.find("*[id*=rowCaptcha]").css("display", "none");
                    parentDiv.find("*[id*=rowCommentArea]").css("display", "none");
                    parentDiv.find("*[id*=btnAddComment]").css("display", "none");
                }
            }
            parent = jQuery(obj).parent();
            if (tblComments.css("display") == "none") {
                this.doresizeValue = doresize;
                doresize = false;
                tblComments.css("display", "");
                if (MediaGallery.LoggedInUserId == -1) {
                    MediaGallery.tblCommentsHeight = parentDiv.find("*[class*=pp_commentsTable]").height();
                }
                else {
                    MediaGallery.tblCommentsHeight = tblComments.height(); //520; //
                }
                Dcc_CommentsAPI.TableComments.width(pp_contentWidth);
                Dcc_CommentsAPI.TableComments.find("#dvCommentContainer").width(pp_contentWidth - 20);
                MediaGallery.SetCommentsHeight(obj, MediaGallery.tblCommentsHeight, true);
                this.Position = parentDiv.position();
            }
            else {
                doresize = this.doresizeValue;
                MediaGallery.SetCommentsHeight(obj, MediaGallery.tblCommentsHeight, false);
                tblComments.css("display", "none");
            }
            return false;
        },
        ClearCommentsOnLoad: function(obj) {
            var parentDiv = jQuery(obj).parents("div[class*=pp_pic_holder]");
            parentDiv.find("*[id*=txtComment]")[0].value = '';
            parentDiv.find("*[id*=txtAnonymousName]")[0].value = strAnonymousText;
            parentDiv.find("*[id*=txtAnonymousEmail]")[0].value = strAnonymousEmailText;
            parentDiv.find("*[id*=uword]")[0].value = '';
            parentDiv.find("*[id*=lblAnonymousEmail]")[0].innerHTML = '';
            parentDiv.find("*[id*=lblAnonymousName]")[0].innerHTML = '';
        }
    }

    Dcc_CommentsAPI = {
        StopEventBubbeling: function(e) {
            var ev = e || window.event;
            var obj = ev.target || ev.srcElement;
            if (ev.keyCode == 37 || ev.keyCode == 39) {
                ev.cancelBubble = true;
            }
        },
        CommentLengthCkecker: function(e) {
            var ev = e || window.event;
            var obj = ev.target || ev.srcElement;
            if (obj.value.length > 150) {
                obj.value = obj.value.substring(0, 150);
            }
        },
        RefreshCaptchaImage: function(e) {
            var ev = e || window.event;
            var obj = ev.target || ev.srcElement;
            var parent = jQuery(obj).parent();
            var captchaImage = parent.find("img:first")
            captchaImage.attr("src", DCCJMG.strModulePath + 'Thumbnail.ashx?ot=3&mid=' + DCCJMG.ModuleId + '&rnd=' + Math.random());

            return false;
        },
        GetAllComments: function() {
            DNNCentric.JMediaGallery.MediaGalleryController.GetComments(parseInt(mediaIds[window.currentindex]), 1, Dcc_CommentsAPI.GetCommentSuccessCallBack, Dcc_CommentsAPI.GetCommentFailureCallBack);
        },
        ClearMessage: function() {
            //    document.getElementById('lblMessage').innerHTML = '';
            jQuery('#lblMessage').html("").css("color", "black");
        },
        AddCommentSuccessCallBack: function(result, eventArgs) {
            document.getElementById('btnAddComment').disabled = false;
            if (result == -2) {
                jQuery('#imgCaptcha').attr("src", DCCJMG.strModulePath + 'Thumbnail.ashx?ot=3&mid=' + DCCJMG.ModuleId + '&rnd=' + Math.random());
                jQuery('#lblMessage').html(strJcapErrorMessage).css("color", "red");
                setTimeout("Dcc_CommentsAPI.ClearMessage();", 5000);
                return;
            }
            if (MediaGallery.LoggedInUserId == -1 && MediaGallery.IsModerationEnabled) {
                jQuery('#lblMessage').html(strAnonymousCommentMessage).css("color", "red");
                setTimeout("Dcc_CommentsAPI.ClearMessage();", 5000);
            }
            else {
                document.getElementById('lblMessage').innerHTML = "";
            }
            document.getElementById('txtComment').value = "";
            document.getElementById('uword').value = "";
            document.getElementById('txtAnonymousName').value = strAnonymousText;
            document.getElementById('txtAnonymousEmail').value = strAnonymousEmailText;
            if (result == 0) {
                Dcc_CommentsAPI.GetAllComments();
            }
            if (MediaGallery.LoggedInUserId == -1) {
                jQuery("#rowCaptcha").css("display", "inline-block");
                var parent = jQuery("#pp_comments");
                document.getElementById('uword').value = "";
                var captchaImage = parent.find("img:first")
                captchaImage.attr("src", DCCJMG.strModulePath + 'Thumbnail.ashx?ot=3&mid=' + DCCJMG.ModuleId + '&rnd=' + Math.random());
            }
        },
        AddCommentFailureCallBack: function(error) {
        },
        NextPage: function(e) {
            var ev = e || window.event;
            var obj = jQuery(ev.srcElement || ev.target);
            var currentPageNumber = parseInt(obj.attr("CurrentPageNumber"));
            var mediaid = parseInt(obj.attr("mediaid"));
            document.getElementById("txtAnonymousName").value = "";
            document.getElementById("uword").value = "";
            Dcc_CommentsAPI.NextClicked = true;
            DNNCentric.JMediaGallery.MediaGalleryController.GetComments(mediaid, currentPageNumber + 1, Dcc_CommentsAPI.GetCommentSuccessCallBack, Dcc_CommentsAPI.GetCommentFailureCallBack);
            return false;
        },
        PreviousPage: function(e) {
            var ev = e || window.event;
            var obj = jQuery(ev.srcElement || ev.target);
            var currentPageNumber = parseInt(obj.attr("CurrentPageNumber"));
            var mediaid = parseInt(obj.attr("mediaid"));
            document.getElementById("txtAnonymousName").value = "";
            document.getElementById("uword").value = "";
            Dcc_CommentsAPI.NextClicked = true;
            DNNCentric.JMediaGallery.MediaGalleryController.GetComments(mediaid, currentPageNumber - 1, Dcc_CommentsAPI.GetCommentSuccessCallBack, Dcc_CommentsAPI.GetCommentFailureCallBack);
            return false;
        },
        TableComments: null,
        GetCommentSuccessCallBack: function(result, eventArgs) {
            if (Dcc_CommentsAPI.TableComments == null) {
                Dcc_CommentsAPI.TableComments = jQuery('#tblComments');
            }
            Dcc_CommentsAPI.TableComments.find("tr").remove();
            if (result != null || result != "-1") {
                jQuery("#pp_comments").find("#lnkComments").html(strMediaCommentsLinkText + "(" + result.TotalComments + ")");
                var arrComments = result.Comments;
                if (arrComments.length > 0) {
                    var row = null;
                    var cell = null;
                    for (var i = 0; i < arrComments.length; i++) {
                        var objComment = arrComments[i];
                        row = document.createElement("tr");
                        cell = document.createElement("td");
                        cell.innerHTML = '<div id="dvCommentContainer"><table style="width:100%"><tr><td style="width:77.8%" class="dcc_gallery_arial_14">' + objComment.UserName + ':</td><td style="width:20%" class="dcc_gallery_arial_111" rowspan="2" valign="middle">' + objComment.PostingDate + '<br />' + objComment.PostingTime + ' </td><td class="arial_11" style="width:10%" rowspan="2" >' + (MediaGallery.LoggedInUserId != -1 ? ((MediaGallery.IsAdminLogin || MediaGallery.LoggedInUserId == objComment.UserID) ? '<img onclick="return Dcc_CommentsAPI.DeleteComment(event);" src="' + DCCJMG.strBaseURLforIMG + 'images/delete_button.png" commentid="' + objComment.CommentID + '"  mediaid="' + result.MediaId + '" CurrentPageNumber="' + result.CurrentPageNumber + '" class="dcc_gallery_delete_button"/>' : ' ') : ' ') + '</td></tr><tr><td ><span class="dcc_gallery_arial_122">' + objComment.CommentText + '</span></td></tr></table></div>';
                        jQuery(cell).attr("width", "70%");
                        row.appendChild(cell);
                        Dcc_CommentsAPI.TableComments.append(row);
                    }
                    row = document.createElement("tr");
                    cell = null;
                    cell = document.createElement("td");
                    jQuery(cell).width("100%").attr("valign", "top").attr("align", "right");
                    if (result.CurrentPageNumber < result.TotalPages) {
                        if (result.CurrentPageNumber == 1) {
                            cell.innerHTML = "<table border='0' align='left' cellpadding='0' cellspacing='0'><tr><td>&nbsp;</td><td><img src='" + DCCJMG.strBaseURLforIMG + "images/next1.jpg' onclick='return Dcc_CommentsAPI.NextPage(event);' mediaid='" + result.MediaId + "' CurrentPageNumber='" + result.CurrentPageNumber + "' style='border-width:0px;' alt='' /></td></tr></table>";
                            row.appendChild(cell);
                        }
                        if (result.CurrentPageNumber > 1) {
                            //Comments Paging Previous button
                            cell.innerHTML = "<table border='0' align='left' cellpadding='0' cellspacing='0'><tr><td><img src='" + DCCJMG.strBaseURLforIMG + "images/prev1.jpg' onclick='return Dcc_CommentsAPI.PreviousPage(event);' mediaid='" + result.MediaId + "' CurrentPageNumber='" + result.CurrentPageNumber + "' style='border-width:0px;' alt='' />&nbsp;</td><td>&nbsp;<img src='" + DCCJMG.strBaseURLforIMG + "images/next1.jpg' onclick='return Dcc_CommentsAPI.NextPage(event);' mediaid='" + result.MediaId + "' CurrentPageNumber='" + result.CurrentPageNumber + "' style='border-width:0px;' alt='' /></td></tr></table>";
                            row.appendChild(cell);
                        }
                    }
                    else {
                        if (result.CurrentPageNumber > 1) {
                            //Comments Paging Next button                            
                            cell.innerHTML = "<table border='0' align='left' cellpadding='0' cellspacing='0'><tr><td><img src='" + DCCJMG.strBaseURLforIMG + "images/prev1.jpg' onclick='return Dcc_CommentsAPI.PreviousPage(event);' mediaid='" + result.MediaId + "' CurrentPageNumber='" + result.CurrentPageNumber + "' style='border-width:0px;' alt='' /></td></tr></table>";
                            row.appendChild(cell);
                        }
                    }
                    Dcc_CommentsAPI.TableComments.append(row);
                    Dcc_CommentsAPI.TableComments.width(pp_contentWidth);
                    Dcc_CommentsAPI.TableComments.find("#dvCommentContainer").width(pp_contentWidth - 20);
                    var strDisplay = jQuery("#tblComments").css("display");
                    if (strDisplay == "block" || strDisplay == "table" || strDisplay == "inline-block") {
                        MediaGallery.SetCommentsHeight(MediaGallery.AddComment.obj, MediaGallery.tblCommentsHeight, false);
                        MediaGallery.tblCommentsHeight = jQuery(".pp_commentsTable").height();
                        MediaGallery.SetCommentsHeight(MediaGallery.AddComment.obj, MediaGallery.tblCommentsHeight, true);
                    }
                }
            }
            jQuery("#pp_comments").css("display", "inline-block");
        },
        GetCommentFailureCallBack: function(error) {
        },
        DeleteComment: function(e) {
            if (confirm(strDeleteMsgText)) {
                var ev = e || window.event;
                var obj = ev.srcElement || ev.target;
                Dcc_CommentsAPI.DeleteComment.row = jQuery(obj).parent().parent().get()[0];
                var commentID = parseInt(jQuery(obj).attr("commentid"));
                Dcc_CommentsAPI.mediaid = parseInt(jQuery(obj).attr("mediaid"));
                Dcc_CommentsAPI.CurrentPageNumber = parseInt(jQuery(obj).attr("CurrentPageNumber"));
                DNNCentric.JMediaGallery.MediaGalleryController.DeleteComment(commentID, Dcc_CommentsAPI.DeleteCommentSuccessCallback, Dcc_CommentsAPI.DeleteCommentFailureCallback);
                return false;
            }
            else {
                return false;
            }
        },
        DeleteCommentSuccessCallback: function(result, eventArgs) {
            if (result == 0) {
                DNNCentric.JMediaGallery.MediaGalleryController.GetComments(Dcc_CommentsAPI.mediaid, Dcc_CommentsAPI.CurrentPageNumber, Dcc_CommentsAPI.GetCommentSuccessCallBack, Dcc_CommentsAPI.GetCommentFailureCallBack);
            }
        },
        DeleteCommentFailureCallback: function(error)
        { },
        ShowHideEmailText: function(e) {
            var ev = e || window.event;
            var obj = ev.target || ev.srcElement;
            obj.value = (jQuery.trim(obj.value) == "" ? strAnonymousEmailText : obj.value);
        },
        ShowHideNameText: function(e) {
            var ev = e || window.event;
            var obj = ev.target || ev.srcElement;
            obj.value = (jQuery.trim(obj.value) == "" ? strAnonymousText : obj.value);
        },
        ClearText: function(e) {
            var ev = e || window.event;
            var obj = ev.target || ev.srcElement;
            if (obj.id == 'txtAnonymousEmail') {
                if (obj.value == strAnonymousEmailText) {
                    obj.value = '';
                }
            }
            else {
                if (obj.value == strAnonymousText) {
                    obj.value = '';
                }
            }
        }
    };
})(jqsDCCJMG);

;
