﻿/// <reference path="jquery-1.2.6.js"/>
/// <reference path="json2.js"/>
/// <reference path="Plugins/jquery.facebox.js"/>
/// <reference path="Plugins/jquery.shoprpop.js"/>

//this file is included in most places. Add global code here.
jQuery(document).ready(function() {

    addCommentHoverClose();
    
});

function addCommentHoverClose() {
    jQuery('.closecomment').mouseover(function() { jQuery(this).addClass('closecommenton'); });
    jQuery('.closecomment').mouseout(function() { jQuery(this).removeClass('closecommenton'); });
    jQuery('.closereccomment').mouseover(function() { jQuery(this).addClass('closereccommenton'); });
    jQuery('.closereccomment').mouseout(function() { jQuery(this).removeClass('closereccommenton'); });
    jQuery('.removeanswer').mouseover(function() { jQuery(this).addClass('removeansweron'); });
    jQuery('.removeanswer').mouseout(function() { jQuery(this).removeClass('removeansweron'); });
}

function commentOnError(error) {
    alert("There was an error processing your request.\n"
                    + "[" + error.status + "]"
                    + " [" + error.statusText + "]");
}

function expandNewRecComment(comment, recid) {
    if (shoprisauth() == false) {
        jQuery(comment).blur();
        return false;
    }
    
    jQuery(comment).css('color', '#000000');
    jQuery('#saveRecComment_' + recid).css('display', '');
    jQuery(comment).elastic();

    if (comment.value.substr(0, 7) == "What do")
        comment.value = "";
        
}

function closeNewRecComment(comment, recid) {
    if (comment.value.length < 1) {
        comment.value = "What do you think about this recommendation?";
        jQuery(comment).css('color', '#666666');
        jQuery('#saveRecComment_' + recid).css('display', 'none');
        jQuery(comment).css('height', '20px');
    }
}

function recCommentClicked(recid, isproj) {
    var el = document.getElementById("addcommenttext_" + recid);
    if (el.value.length > 0) {
        createNewRecComment(recid, el.value, isproj);
        el.value = "";
        closeNewRecComment(el, recid);
    }
}

function expandNewProjectComment(comment, projid) {
    if (shoprisauth() == false) {
        jQuery(comment).blur();
        return false;
    }
    
    jQuery(comment).css('color', '#000000');
    jQuery('#saveProjectComment_' + projid).css('display', '');
    jQuery(comment).elastic();

    if (comment.value.substr(0, 13) == "Add a comment")
        comment.value = "";

}

function expandNewAnswer(answer, questionid) {
    if (!shoprisauth()) {
        jQuery(answer).blur();
        return false;
    }

    jQuery(answer).css('color', '#000000');
    jQuery('#saveAnswer_' + questionid).css('display', '');
    jQuery(answer).elastic();

    var defaultText = "Answer this question!";

    if (answer.value.substr(0, defaultText.length) == defaultText)
        answer.value = "";
}

function closeNewProjectComment(comment, projid) {
    if (comment.value.length < 1) {
        comment.value = "Add a comment to this list!";
        jQuery(comment).css('color', '#666666');
        jQuery('#saveProjectComment_' + projid).css('display', 'none');
        jQuery(comment).css('height', '20px');
    }
}

function closeNewAnswer(answer, questionid) {
    if (answer.value.length < 1) {
        answer.value = "Answer this question!";
        jQuery(answer).css('color', '#666666');
        jQuery('#saveAnswer_' + questionid).css('display', 'none');
        jQuery(answer).css('height', '20px');
    }
}

function projCommentClicked(projid) {
    var el = document.getElementById("addprojectcommenttext_" + projid);
    if (el.value.length > 0) {
        createNewProjectComment(projid, el.value);
        el.value = "";
        closeNewProjectComment(el, projid);
    }
}

function answerClicked(questionid) {
    var el = document.getElementById("addanswertext_" + questionid);
    if (el.value.length > 0) {
        createNewAnswer(questionid, el.value);
        el.value = "";
        closeNewAnswer(el, questionid);
    }
}

function createNewAnswer(questionid, answer) {
    jQuery.ajaxDotNet(window.ShoprService + "AddAnswer", {
        data: { questionid: questionid, answer: answer },
        success: function(o) {
            if (o != null) {
                var obj = eval('(' + o.d + ')');

                var newOuterEl = "<div id='answer_" + obj.CommentID + "' class='answersegment'></div>";
                var newEl =
                    "<div style='display:none;'  id='qAnswer_" + obj.CommentID + "' class='answerbar'>" +
                    "<div style='float: left; padding-left: 5px;'><img src='" + obj.UserImageURLMini + "' /></div>" +
                    "<div style='float: left; padding: 0px 0px 0px 10px;'><b>" + obj.UserName + "</b> <span class='approxDate'>Just added!</span><br />" +
                    "<div class='xxx'><p>" + parseAnswer(obj.CommentText) + "</p></div></div>" +
                    "<div class='removeanswer' onclick='deleteAnswer(" + obj.CommentID + ");'>" +
                    "<img src='/images/commentclose.png' /></div></div>"; //

                var jOuterEl = jQuery(newOuterEl);
                var jEl = jQuery(newEl);
                var jTextBox = jQuery('#answerNew_' + questionid);
                jEl.css('backgroundColor', '#CCCCFF');  // sort of a light blue as the lead-in color.
                jOuterEl.insertBefore(jTextBox);
                jOuterEl.append(jEl);
                //jEl.append('#projectanswer_' + obj.ProjectLineID);
                //jEl.insertBefore(jTextBox);
                jEl.slideDown(400, function() {
                    jEl.animate({ backgroundColor: "#FFDD77" });
                    addCommentHoverClose();
                });

            }
        },
        error: onCommonError
    });
}

function createNewProjectComment(projid, comment) {
    jQuery.ajaxDotNet(window.ShoprService + "AddProjComment", {
        data: { projid: projid, comment: comment },
        success: function(o) {
            if (o != null) {
                var obj = eval('(' + o.d + ')');

                var newOuterEl = "<div id='projectcomment_" + obj.ProjectLineID + "' class='projectcomment'></div>";
                var newEl =
                    "<div style='display:none;'  id='commentproj_" + obj.CommentID + "' class='commentproj'>" +
                    "<div style='float: left; padding-left: 5px;'><img src='" + obj.UserImageURLMini + "' /></div>" +
                    "<div style='float: left; padding: 0px 0px 0px 10px;'><b>" + obj.UserName + "</b> <span class='approxDate'>Just added!</span><br />" +
                    "<div class='commenttproj'><p>" + parseComment(obj.CommentText) + "</p></div></div>" +
                    "<div class='left' style='width:10px;'><div class='closecomment left' onclick='deleteProjComment(" + obj.CommentID + ");'>" +
                    "<img src='/images/commentclose.png' /></div></div></div>"; //

                var jOuterEl = jQuery(newOuterEl);
                var jEl = jQuery(newEl);
                var jTextBox = jQuery('#commentprojnew_' + projid);
                jEl.css('backgroundColor', '#FFCCFF');  // sort of a light fuchsia as the lead-in color.
                jOuterEl.insertBefore(jTextBox);
                jOuterEl.append(jEl);
                //jEl.append('#projectcomment_' + obj.ProjectLineID);
                //jEl.insertBefore(jTextBox);
                jEl.slideDown(400, function() {
                    jEl.animate({ backgroundColor: "#BFF56B" });
                    addCommentHoverClose();
                });

            }
        },
        error: onCommonError
    });
}

function createNewRecComment(recid, comment, isproj) {

    var proj = "";
    if(isproj == true)
        proj = "proj";

    jQuery.ajaxDotNet(window.ShoprService + "AddRecComment",
        {
            data: { recid: recid, comment: comment },
            success: function(o) {
                if (o != null) {
                    var obj = eval('(' + o.d + ')');

                    var newEl = "<div style='display:none;' id='commentrec_" + obj.CommentID + "' class='commentrec" + proj + "'>" +
                        "<div style='float: left; padding-left: 5px;'><img src='" + obj.UserImageURLMini + "' width='25px' height='25px' /></div>" +
                        "<div style='float: left; padding: 0px 0px 0px 10px;'><b>" + obj.UserName + "</b><br />" +
                        "<div class='commentt" + proj + "'><p>" + parseComment(obj.CommentText) + "</p></div></div>" +
                        "<div class='left' style='width:10px;'><div class='closereccomment left' onclick='deleteRecComment(" + obj.CommentID + ");'>" +
                        "<img src='/images/commentclose.png' /></div></div></div>";

                    var jEl = jQuery(newEl);
                    var jTextBox = jQuery('#commentrecnew_' + recid);
                    jEl.css('backgroundColor', '#FAFAD2');
                    jEl.insertAfter(jTextBox);
                    jEl.slideDown(400, function() {
                        jEl.animate({ backgroundColor: "#DDEDFF" });
                        addCommentHoverClose();
                    });

                }
                else {
                }
            },
            error: onCommonError
        });

    }

    function deleteAnswer(commentid) {
        var str = "<div style='font-size:20px;padding:10px;'>Do you really want to delete this answer?</div>";
        str += "<div style='width: 350px; text-align: center;'><a href='javascript:void(0);' onclick='jQuery.facebox.closeWithData();' ><img src='/images/yes45.gif' /></a>&nbsp;&nbsp;<a href='javascript:void(0);' onclick='jQuery.facebox.close();'><img src='/images/no45.gif' /></a></div>";
        jQuery.facebox(str);
        jQuery().unbind('closeWithData.facebox');
        jQuery().bind('closeWithData.facebox', {}, function(e, data) {
            jQuery.facebox.closeNoEvent();
            jQuery.ajaxDotNet(window.ShoprService + "DeleteAnswer",
            {
                data: { commentid: commentid },
                success: function(o) {
                    if (o != null) {
                        jQuery('#qAnswer_' + commentid).parent().slideUp(700, function() {
                            jQuery('#qAnswer_' + commentid).parent().remove();
                        });

                    }
                    else {
                    }
                },
                error: onCommonError
            });
        });
    }
    

function deleteProjComment(commentid) {
    var str = "<div style='font-size:20px;padding:10px;'>Do you really want to delete this comment?</div>";
    str += "<div style='width: 350px; text-align: center;'><a href='javascript:void(0);' onclick='jQuery.facebox.closeWithData();' ><img src='/images/yes45.gif' /></a>&nbsp;&nbsp;<a href='javascript:void(0);' onclick='jQuery.facebox.close();'><img src='/images/no45.gif' /></a></div>";
    jQuery.facebox(str);
    jQuery().unbind('closeWithData.facebox');
    jQuery().bind('closeWithData.facebox', {}, function(e, data) {
        jQuery.facebox.closeNoEvent();
        jQuery.ajaxDotNet(window.ShoprService + "DeleteComment",
            {
                data: { commentid: commentid },
                success: function(o) {
                    if (o != null) {
                        jQuery('#commentproj_' + commentid).parent().slideUp(700, function() {
                            jQuery('#commentproj_' + commentid).parent().remove();
                        });

                    }
                    else {
                    }
                },
                error: onCommonError
            });
    });
}

function flagProjComment(commentid) {
    return flagComment(commentid, '#commentproj_');
}

function flagRecComment(commentid) {
    return flagComment(commentid, '#commentrec_');
}

function flagAnswer(commentid) {
    return flagComment(commentid, '#qAnswer_', "answer");
}

function flagComment(commentid, identifier, text) {
    if (!text) text = "comment";
    var str = "<div style='font-size:20px;padding:10px;'>Do you really want to flag this " + text + " as inappropriate?</div>";
    str += "<div style='width: 350px; text-align: center;'><a href='javascript:void(0);' onclick='jQuery.facebox.closeWithData();' ><img src='/images/yes45.gif' /></a>&nbsp;&nbsp;<a href='javascript:void(0);' onclick='jQuery.facebox.close();'><img src='/images/no45.gif' /></a></div>";
    jQuery.facebox(str);
    jQuery().unbind('closeWithData.facebox');
    jQuery().bind('closeWithData.facebox', {}, function(e, data) {
        jQuery.facebox.closeNoEvent();
        jQuery.ajaxDotNet(window.ShoprService + "FlagComment",
            {
                data: { commentid: commentid },
                success: function(o) {
                    if (o != null) {
                        //                        // Remove the comment, or replace with a comment that says, "Under editorial review"?  Going with the former for now.
                        //                        jQuery('#commentrec_' + commentid).slideUp(700, function() {
                        //                            jQuery('#commentrec_' + commentid).remove();
                        //                        });
                        jQuery(identifier + commentid).find('.reviewState').text('Under review.');

                    }
                    else {
                    }
                },
                error: onCommonError
            });
    });
}

function deleteRecComment(commentid) {

    var str = "<div style='font-size:20px;padding:10px;'>Do you really want to delete this comment?</div>";
    str += "<div style='width: 350px; text-align: center;'><a href='javascript:void(0);' onclick='jQuery.facebox.closeWithData();' ><img src='/images/yes45.gif' /></a>&nbsp;&nbsp;<a href='javascript:void(0);' onclick='jQuery.facebox.close();'><img src='/images/no45.gif' /></a></div>";
    jQuery.facebox(str);
    jQuery().unbind('closeWithData.facebox');
    jQuery().bind('closeWithData.facebox', {}, function(e, data) {
        jQuery.facebox.closeNoEvent();
        jQuery.ajaxDotNet(window.ShoprService + "DeleteComment",
            {
                data: { commentid: commentid },
                success: function(o) {
                    if (o != null) {
                        jQuery('#commentrec_' + commentid).slideUp(700, function() {
                            jQuery('#commentrec_' + commentid).remove();
                        });

                    }
                    else {
                    }
                },
                error: onCommonError
            });
    });
}

function parseAnswer(input) {
    return parseComment(input);  // Just use the comment parsing rule, for now.
}

function parseComment(input) {
    var converted = input;
    //disable
    converted = converted.replace(/</gim, '&lt;').replace(/>/g, '&gt;');
    
    //convert http:// to anchor tags
    converted = converted.replace(/(?:^|[^''""])(http|https):\/\/[\S]+(\b|$)/gim, function() {
        var base = arguments[0]
        var first = "";
        if (base.length > 0 && base.charAt(0) != 'h') {
            first = base.charAt(0);
            base = base.substr(1);
        }

        var cut = base;
        if (base.length > 30)
            cut = base.substr(0, 26) + "..."

        return first + "<a href='" + base + "'>" + cut + "</a>"
    });

    return converted.replace(/\n/gim, '<br/>');
}

