﻿function gsPosts()
{
    this.oCurrentCommentBox = null;
    this.oCurrentCommentBoxOwnerPost = null;
    this.oLastCommentBoxPlaceHolder = null;
}

gsPosts.prototype =
{
    playMiniVideo: function (oBtn, sVid, nBrand) {
        if ((oBtn != null && typeof (oBtn) == "object") && (sVid != null && sVid != "") && (isNaN(nBrand) == false)) {
            var oVidPrev = oBtn.previousSibling;

            if (oVidPrev != null && typeof (oVidPrev) != undefined) {
                if (oVidPrev.nodeType == 1) {
                    if (oVidPrev.className == "postvideo") {
                        var sVideoHtml = "";

                        // Youtube
                        if (nBrand == 1) {
                            sVideoHtml += "  <object width=\"460\" height=\"259\">";
                            sVideoHtml += "     <param name=\"wmode\" value=\"opaque\" />";
                            sVideoHtml += "     <param name=\"movie\" value=\"http://www.youtube-nocookie.com/v/" + sVid + "&amp;autoplay=1&amp;hl=en_US&amp;fs=1?rel=0\"></param>";
                            sVideoHtml += "     <param name=\"allowFullScreen\" value=\"true\"></param>";
                            sVideoHtml += "     <param name=\"allowscriptaccess\" value=\"always\"></param>";
                            sVideoHtml += "     <embed src=\"http://www.youtube-nocookie.com/v/" + sVid + "&amp;autoplay=1&amp;hl=en_US&amp;fs=1?rel=0\" type=\"application/x-shockwave-flash\" wmode=\"opaque\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"460\" height=\"260\"></embed>";
                            sVideoHtml += "  </object>";
                        }

                        // Vimeo
                        if (nBrand == 2) {
                            sVideoHtml += "  <object width=\"460\" height=\"259\">";
                            sVideoHtml += "     <param name=\"wmode\" value=\"opaque\" />";
                            sVideoHtml += "     <param name=\"allowfullscreen\" value=\"true\" />";
                            sVideoHtml += "     <param name=\"allowscriptaccess\" value=\"always\" />";
                            sVideoHtml += "     <param name=\"movie\" value=\"http://vimeo.com/moogaloop.swf?clip_id=" + sVid + "&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=00adef&amp;fullscreen=1\" />";
                            sVideoHtml += "     <embed src=\"http://vimeo.com/moogaloop.swf?clip_id=" + sVid + "&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=00adef&amp;fullscreen=1\" type=\"application/x-shockwave-flash\" wmode=\"opaque\" allowfullscreen=\"true\" allowscriptaccess=\"always\" width=\"460\" height=\"259\"></embed>";
                            sVideoHtml += "  </object>";
                        }

                        oBtn.style.display = "none";
                        oVidPrev.innerHTML = sVideoHtml;
                    }
                }
            }
        }
    },

    getItemLikeInfo: function (nContentID, nContentType) {
        var sLikeInfo = "";


        {
            var params = new Array();
            params[0] = "ContentID=" + nContentID;
            params[1] = "ContentType=" + nContentType;

            sLikeInfo = oAjaxTools.getData(23, params, false, null);
        }

        return sLikeInfo;
    },

    likeItem: function (oBtn, nContentID, nContentType) {
        var oMe = this;

        var oBtnParent = oBtn.parentNode;
        if (typeof (oBtn) != undefined && typeof (oBtn.parentNode) != undefined) {
            oBtn.innerHTML = "&nbsp; &nbsp; &nbsp;";
            oBtn.className = "miniloading";

            var params = new Array();
            params[0] = "ContentID=" + nContentID;
            params[1] = "ContentType=" + nContentType;

            var func = function () {
                if (arguments[0] != null) {
                    if (arguments[0].readyState == 4) {
                        if (arguments[0].status == 200) {
                            if (arguments[0].responseText == "success") {
                                var sLikeInfo = oMe.getItemLikeInfo(nContentID, nContentType);

                                if (typeof (oBtn) != undefined) {
                                    oBtnParent.innerHTML = "<span class = \"link\" onclick = \"oPosts.unlikeItem(this, " + nContentID + ", " + nContentType + ");\">Unlike</span>";
                                }

                                var nPostID = oBtnParent.id.replace("_post_", "").replace("_likebtn", "");

                                if (document.getElementById("_post_" + nPostID + "_likes") != null) {
                                    document.getElementById("_post_" + nPostID + "_likes").style.display = "block";
                                    document.getElementById("_post_" + nPostID + "_likes").innerHTML = "&nbsp; &nbsp; &nbsp; &nbsp; " + sLikeInfo;
                                }
                            }
                            else {
                                if (typeof (oBtn) != undefined) {
                                    oBtnParent.innerHTML = "<span class = \"link\" onclick = \"oPosts.likeItem(this, " + nContentID + ", " + nContentType + ");\">Like</span>";
                                }

                                alert("An error occured, please try again later.\n\n" + arguments[0].responseText);
                            }
                        }
                    }
                    else {
                        oBtnParent.innerHTML = "<span class = \"link\" onclick = \"oPosts.likeItem(this, " + nContentID + ", " + nContentType + ");\">Like</span>";
                    }
                }
            };

            var oReq = oAjaxTools.getData(21, params, true, func);
        }
    },

    unlikeItem: function (oBtn, nContentID, nContentType) {
        var oMe = this;
        var oBtnParent = oBtn.parentNode;

        if (typeof (oBtn) != undefined && typeof (oBtn.parentNode) != undefined) {
            oBtn.innerHTML = "&nbsp; &nbsp; &nbsp;";
            oBtn.className = "miniloading";

            var params = new Array();
            params[0] = "ContentID=" + nContentID;
            params[1] = "ContentType=" + nContentType;

            var func = function () {
                if (arguments[0] != null) {
                    if (arguments[0].readyState == 4) {
                        if (arguments[0].status == 200) {
                            if (arguments[0].responseText == "success") {
                                var sLikeInfo = oMe.getItemLikeInfo(nContentID, nContentType)

                                if (typeof (oBtn) != undefined) {
                                    oBtnParent.innerHTML = "<span class = \"link\" onclick = \"oPosts.likeItem(this, " + nContentID + ", " + nContentType + ");\">Like</span>";
                                }

                                var nPostID = oBtnParent.id.replace("_post_", "").replace("_likebtn", "");

                                if (document.getElementById("_post_" + nPostID + "_likes") != null && sLikeInfo != "") {
                                    document.getElementById("_post_" + nPostID + "_likes").style.display = "block";
                                    document.getElementById("_post_" + nPostID + "_likes").innerHTML = "&nbsp; &nbsp; &nbsp; &nbsp; " + sLikeInfo;
                                }
                                else {
                                    document.getElementById("_post_" + nPostID + "_likes").style.display = "none";
                                }

                            }
                            else {
                                if (typeof (oBtn) != undefined) {
                                    oBtnParent.innerHTML = "<span class = \"link\" onclick = \"oPosts.unlikeItem(this, " + nContentID + ", " + nContentType + ");\">Unlike</span>";
                                }

                                alert("An error occured, please try again later.\n\n" + arguments[0].responseText);
                            }
                        }
                    }
                    else {
                        oBtnParent.innerHTML = "<span class = \"link\" onclick = \"oPosts.unlikeItem(this, " + nContentID + ", " + nContentType + ");\">Unlike</span>";
                    }
                }
            };

            var oReq = oAjaxTools.getData(22, params, true, func);
        }
    },

    showHideCommentBoxByCommentBtn: function (oBtn) {

    },

    addCommentBtnOnClick: function (postid) {
        var oMe = this;

        oMe.showPostCommentArea(postid);
        oMe.switchToCommentBoxById(postid);
    },

    showPostCommentArea: function (postid) 
    {
        if (document.getElementById("_post_" + postid + "_comments").style.display != "block") 
        {
            document.getElementById("_post_" + postid + "_comments").style.display = "block";
        }
    },

    switchToCommentBoxById: function (postid) 
    {
        var oMe = this;

        if (document.getElementById("_post_" + postid + "_comments_writecomment").style.display != "block") 
        {
            document.getElementById("_post_" + postid + "_comments_writecomment").style.display = "block";
        }

        oMe.switchToCommentBox(document.getElementById("writecomment_phr_" + postid));
    },

    switchToCommentBox: function (oField) 
    {
        var oMe = this;

        if (oMe.oLastCommentBoxPlaceHolder != null) {
            if (oMe.oLastCommentBoxPlaceHolder.nodeType == 1) {
                oMe.oLastCommentBoxPlaceHolder.style.display = "";
            }
        }

        if (oField != null) {
            if (oField.nodeType == 1) {
                var oCommentBox = document.getElementById("CommentBox");
                oCommentBox.style.display = "block";

                var oCommentBoxTextBox = document.getElementById("CommentBox_Textarea");
                    oCommentBoxTextBox.value = "";

                oMe.oLastCommentBoxPlaceHolder = oField;
                oField.style.display = "none";

                if (oCommentBox.parentNode != oField.parentNode)
                {
                    oField.parentNode.appendChild(oCommentBox);
                }

                oCommentBoxTextBox.select();

                oMe.oCurrentCommentBoxOwnerPost = oField.id.replace("writecomment_phr_", "");
            }
        }
    },

    CommentBoxBlur: function ()
    {
        var oMe = this;

        oMe.onCommentBoxBlur();
    },

    onCommentBoxBlur: function () 
    {
        var oMe = this;

        var oCommentBoxTextBox = document.getElementById("CommentBox_Textarea");

        if (oCommentBoxTextBox != null) 
        {
            if (oCommentBoxTextBox.value == "") 
            {
                oMe.ResetCommentBox();
            }
        }
    },

    ResetCommentBox: function () {
        var oMe = this;

        var oCommentBox = document.getElementById("CommentBox");

        if (oCommentBox != null) {
            oMe.oLastCommentBoxPlaceHolder.style.display = "block";
            oCommentBox.style.display = "none";
            oMe.oCurrentCommentBoxOwnerPost = null;
        }
    },

    getItemComments: function (nContentID, nContentType, nPostID)
    {
        var sComments = "";

        if (nContentID > 0 && nContentType >= 0 && nPostID > 0) 
        {
            var params = new Array();
            params[0] = "ContentID=" + nContentID;
            params[1] = "ContentType=" + nContentType;
            params[2] = "PostID=" + nPostID;

            sComments = oAjaxTools.getData(26, params, false, null);
        }

        return sComments;
    },

    PostComment: function (nContentID, nContentType, nPostID) {
        var oMe = this;

        if (document.getElementById("CommentBox_Textarea").value != "" && nContentID > 0 && nContentType >= 0) 
        {
            var oSubmitBtn = document.getElementById("CommentBox_CommentBtn");

            oSubmitBtn.disabled = true;

            document.getElementById("CommentBox_LoadStatus").style.display = "block";

            var params = new Array();
            params[0] = "Comment=" + document.getElementById("CommentBox_Textarea").value;
            params[1] = "ContentID=" + nContentID;
            params[2] = "ContentType=" + nContentType;
            params[3] = "PostID=" + nPostID;

            var func = function ()
            {
                if (arguments[0] != null) 
                {
                    if (arguments[0].readyState == 4)
                    {
                        if (arguments[0].status == 200)
                        {
                            if (arguments[0].responseText.indexOf("True") == 0)
                            {
                                document.body.appendChild(document.getElementById("CommentBox"));
                                document.getElementById("CommentBox").style.display = "none";

                                var sComments = oMe.getItemComments(nContentID, nContentType, nPostID);

                                oMe.CommentBoxBlur();

                                if (sComments != "")
                                {
                                    if (document.getElementById("_post_" + nPostID + "_comments") != null)
                                    {
                                        document.getElementById("_post_" + nPostID + "_comments").innerHTML = sComments;
                                    }
                                }

                                oMe.showPostCommentArea(nPostID);

                                document.getElementById("CommentBox_LoadStatus").style.display = "none";
                                oSubmitBtn.disabled = false;
                            }
                            else 
                            {
                                alert("An error occured, please try again later.\n\n");

                                document.getElementById("CommentBox_LoadStatus").style.display = "none";
                                oSubmitBtn.disabled = false;
                            }
                        }
                        else 
                        {
                            alert("An error occured, please try again later.\n\n");
                        }
                    }
                    else 
                    {
                    }
                }
            };
        }
        else 
        {
            alert("An error occured, please try again later.");

            document.getElementById("CommentBox_LoadStatus").style.display = "none";
            oSubmitBtn.disabled = false;
        }

        var oReq = oAjaxTools.getData(24, params, false, func);
    },

    SubmitComment: function () {
        var oMe = this;

        if (oMe.oCurrentCommentBoxOwnerPost != null) {
            var arrCommentData = document.getElementById("_post_" + oMe.oCurrentCommentBoxOwnerPost + "_comments_data").value.split(',');

            oMe.PostComment(arrCommentData[0], arrCommentData[1], oMe.oCurrentCommentBoxOwnerPost);
        }
    },

    RemovePostComment: function (commentid, postid) 
    {
        var oMe = oPoster;

        if (commentid != null && commentid != "" && isNaN(commentid) == false && parseInt(commentid) > 0) 
        {
            if (confirm("Are you sure you want to remove this comment?") == true) 
            {
                var params = new Array();
                params[0] = "cid=" + parseInt(commentid);

                var func = function () 
                {
                    if (arguments[0] != null) 
                    {
                        if (arguments[0].readyState == 4) 
                        {
                            if (arguments[0].status == 200) 
                            {
                                if (arguments[0].responseText == "True") 
                                {
                                    setTimeout(function () 
                                    {
                                        if (document.getElementById("_post_" + postid + "_" + commentid) != null)
                                        {
                                            document.getElementById("_post_" + postid + "_" + commentid).style.display = "none";
                                        }
                                        //alert("Post Removed");                          

                                    }, 0);
                                }
                                else 
                                {
                                    setTimeout(function () 
                                    {
                                        alert("This comment could not be removed at this time. Please try again a little bit later.");
                                    }, 0);
                                }
                            }
                        }
                        else {
                        }
                    }
                };

                var oRemovePostCommentReq = oAjaxTools.getData(32, params, true, func);
            }
        }
    }
}

// Global Instance
oPosts = new gsPosts();
