
var Comments =
{
    commentTemplate: null,
    commentRequests: {},
    editingPostId: null,
    submittingPostId: null,
    moderateComments: false,

    monthNames: null,
    dayNames: null,

    init: function()
    {
        this.commentTemplate = new Template("commentTemplate");

        // Define my monthNames array
        this.monthNames = new Array();
        this.monthNames[this.monthNames.length] = 'January';
        this.monthNames[this.monthNames.length] = 'February';
        this.monthNames[this.monthNames.length] = 'March';
        this.monthNames[this.monthNames.length] = 'April';
        this.monthNames[this.monthNames.length] = 'May';
        this.monthNames[this.monthNames.length] = 'June';
        this.monthNames[this.monthNames.length] = 'July';
        this.monthNames[this.monthNames.length] = 'August';
        this.monthNames[this.monthNames.length] = 'September';
        this.monthNames[this.monthNames.length] = 'October';
        this.monthNames[this.monthNames.length] = 'November';
        this.monthNames[this.monthNames.length] = 'December';

        // Define my dayNames array
        this.dayNames = new Array();
        this.dayNames[this.dayNames.length] = 'Sunday';
        this.dayNames[this.dayNames.length] = 'Monday';
        this.dayNames[this.dayNames.length] = 'Tuesday';
        this.dayNames[this.dayNames.length] = 'Wednesday';
        this.dayNames[this.dayNames.length] = 'Thursday';
        this.dayNames[this.dayNames.length] = 'Friday';
        this.dayNames[this.dayNames.length] = 'Saturday';

        if (window.location.hash == "#comments")
        {
            var firstBlogPost = getElementByClass(document.documentElement, "post-wrapper");
            var postId = firstBlogPost.id.substr(4);
            this.toggle(postId);
        }
    },

    onURLChange: function()
    {
    },

    onClick: function(postId, event)
    {
        preventDefault(event);
    
        Comments.toggle(postId);
    },

    ////////////////////////////////////////////////////////////////////////////////////////////////

    toggle: function(postId)
    {
        var postElt = $("post" + postId);
        var commentLinkBox = getElementByClass(postElt, "post-footer");
    
        if (hasClass(commentLinkBox, "toggled"))
            this.show(postId, true);
        else
            this.show(postId);
    },
    
    show: function(postId, hide)
    {
        var postElt = $("post" + postId);
        var commentLinkBox = getElementByClass(postElt, "post-footer");
    
        if (hide)
        {
            removeClass(commentLinkBox, "toggled");
            removeClass(commentLinkBox, "ready");
            this.showEditor(postId, true);
            $("post" + postId + "Comments").style.display = "none";
        }
        else
        {
            addClass(commentLinkBox, "toggled");
            this.load(postId);
        }
    },
    
    showEditor: function(postId, hide)
    {
        if (!hide && this.editingPostId)
            this.showEditor(this.editingPostId, true);
    
        if (!postId)
            postId = this.editingPostId;
        
        if (!postId)
            return;
            
        var postElt = $("post" + postId);
        var commentLinkBox = getElementByClass(postElt, "post-footer");
        if (!hide && hasClass(commentLinkBox, "editing"))
            return;
    
        var commentEditorBox = $("commentEditorBox");
        
        if (hide)
        {
            commentEditorBox.style.display = "none";
            removeClass(commentLinkBox, "editing");
            this.editingPostId = null;
        }
        else
        {
            this.editingPostId = postId;
            $("commentEntryId").value = Number(postId);
            
            addClass(commentLinkBox, "editing");
    
            commentEditorBox.style.display = "block";
            $("commentEditor").value = "";
    
            var commentsElt = $(postElt.id + "Comments");
            commentsElt.parentNode.insertBefore(commentEditorBox, commentsElt);
            
            var emptyName = LabeledInput.apply($("commentName"), "Your Name");
            var emptyEmail = LabeledInput.apply($("commentEmailURL"), "Your URL");
            $("commentEditor").focus();
            
            smoothScrollIntoView(commentEditorBox);
        }
    },
    
    ////////////////////////////////////////////////////////////////////////////////////////////////

    load: function(postId)
    {
        var postElt = $("post" + postId);
        var commentLinkBox = getElementByClass(postElt, "post-footer");
        addClass(commentLinkBox, "loading");
        
        var url = "/blog/comments/commentsrss.php?eid=" + postId;
    
        // Add the current time to the url to make it unique so that we skip the cache 
        var args = new Date().getTime();
        
        var req = loadXMLDocument(url, args, function(doc) { Comments.onLoad(doc, postId); });
        this.commentRequests[postId] = req;
    },
    
    cancelLoad: function(postId)
    {
        if (postId in this.commentRequests)
        {
            this.commentRequests[postId].abort();
            delete this.commentRequests[postId];
        }
    
        Comments.show(postId, true);
    },
    
    onLoad: function(doc, postId)
    {
        delete this.commentRequests[postId];
    
        var postElt = $("post" + postId);
    
        var commentLinkBox = getElementByClass(postElt, "post-footer");
        removeClass(commentLinkBox, "loading");
        addClass(commentLinkBox, "ready");
    
        var commentsBox = $("post" + postId + "Comments");
        clearChildren(commentsBox);
            
        // Create and insert the comment elements
        var commentCount = 0;
        var items = doc.documentElement.childNodes;       
        for (var i = 0; i < items.length; ++i)
        {
            if (items[i].nodeType == 1)
            {
                ++commentCount;
                var id = items[i].getAttribute("id");
                var author = Comments.readXMLNodeValue(items[i], "author");
                var email = Comments.readXMLNodeValue(items[i], "email");
                var url = Comments.readXMLNodeValue(items[i], "url");
                var timestamp = Comments.readXMLNodeValue(items[i], "timestamp");
                var body = Comments.readXMLNodeValue(items[i], "text");

                // Make sure the author isn't blank
                if (author.length == 0)
                    author = "Anonymous";
                
                //alert('Email: ' + email + ' URL: ' + url);
                Comments.insertCommentBox(commentsBox, id, author, email, url, timestamp, body);
            }
        }
    
        commentsBox.style.display = "block";

        if (commentCount == 0)
            this.showEditor(postId);
        else
            smoothScrollIntoView(commentsBox);
    },
    
    insertCommentBox: function(commentsBox, id, author, email, url, timestamp, body)
    {
        var commentElt = this.commentTemplate.createTemplate();
        commentElt.id = id;
        
        if (email && !url)
            url = "mailto:" + email;
        
        // If no url/email is provided then don't put a link around the author name
        if (url)
        {
            var html = '<a href="' + url + '">' + author + '</a>';
            this.commentTemplate.setHTMLValue(commentElt, "author", html);
        }
        else
            this.commentTemplate.setTextValue(commentElt, "author", author);
    
        // Format the date to look pretty (no offense to all you ISO members out there)
        var theDate = new Date(timestamp);
        var min = theDate.getMinutes();
        if (min < 10) min = "0" + min;
        var hr = theDate.getHours();
        if (hr< 10) hr = "0" + hr;
        var dateFormatted = hr + ':' + min +
            ' on ' + this.dayNames[theDate.getDay()] + ', ' +
            this.monthNames[theDate.getMonth()] + ' ' + theDate.getDate();
        this.commentTemplate.setTextValue(commentElt, "timestamp", dateFormatted);
        
        this.commentTemplate.setHTMLValue(commentElt, "content", body);
    
        commentsBox.insertBefore(commentElt, commentsBox.firstChild);
        
        if (commentsBox.childNodes.length % 2)
            setClass(commentElt, "commentBoxOdd");
    
        return commentElt;
    },
        
    ////////////////////////////////////////////////////////////////////////////////////////////////

    onSubmit: function(event)
    {
        var commentName = $("commentName");
        var commentEditor = $("commentEditor");
        var commentEmailURL = $("commentEmailURL");    
    
        if (!commentName.value || commentName.value == commentName.defaultValue)
        {
            alert("You need a name to comment on my website.  Feel free to make one up.");
            commentName.focus();
            event.preventDefault();
        }
        else if (!commentEditor.value)
        {
            alert("Are you going to write something or not?");
            commentEditor.focus();
            event.preventDefault();
        }
        else
        {
            this.submittingPostId = $("commentEntryId").value;
    
            var body = Comments.textToHTML(commentEditor.value);
    
            var commentsBox = $("post" + this.editingPostId + "Comments");

            var url = "";
            var email = "";
            var emailRegex = /(.*?)\@(.*?)\.(.*)/i;
            var urlRegex = /^http:\/\//i;

            //alert('Value: ' + commentEmailURL.value + ' Default: ' + commentEmailURL.defaultValue);
            if (commentEmailURL.value != commentEmailURL.defaultValue) {
                if (emailRegex.test(commentEmailURL.value)) {
                    commentEmailURL.name = "email";
                    email = commentEmailURL.value;
                    //$('commentEmail').value = email;
                }
                else {
                    commentEmailURL.name = "url";
                    url = commentEmailURL.value;
                    if (!urlRegex.test(url))
                        url = 'http://' + url;
                    //$('commentURL').value = url;
                }
            } else
                commentEmailURL.value = "";

            //alert('Default: "' + commentEmailURL.defaultValue + '" Email: "' + email + '" URL: "' + url + '"');
            
            var commentBox = Comments.insertCommentBox(commentsBox, "", commentName.value,
                email, url, new Date(), body);
        
            this.showEditor(this.editingPostId, true);
        
            var commentProgress = getElementByClass(commentBox, "progressBox");
            commentProgress.style.display = "block";
            //commentBox.appendChild(commentProgress);
        
        }
    },
    
    onPosted: function(event)
    {
        if (this.submittingPostId)
        {
            var postElt = $("post" + this.submittingPostId);

            if (this.moderateComments == true) {
                var modBox = getElementByClass(postElt, "moderationBox");
                modBox.style.display = "block";
            } else {
                // Fix the comment count
                var commentCount = getElementByClass(postElt, "commentLink");
                commentCount.innerHTML = this.incrementCommentCount(commentCount.innerHTML);
            }

            var commentProgress = getElementByClass(postElt, "progressBox");
            commentProgress.style.display = "none";

            this.submittingPostId = null;
            $("formSubmitter").src = "about:blank";
      }
    },
    
    ////////////////////////////////////////////////////////////////////////////////////////////////
    incrementCommentCount: function(text) {
        if (text == 'Add A Comment')
            return '1 Comment';
        else if (text == '1 Comment')
            return '2 Comments';
        else {
            var cnt = text.split(' ');
            cnt = parseInt(cnt[0])+1;
            return cnt+' Comments';
        }
    },

    textToHTML: function(text)
    {
        var lines = text.split("\n");
        
        var html = "";
        for (var i in lines)
            html += "<p>" + lines[i] + "</p>";
        
        return html;
    },

    readXMLNodeValue: function(node, sourceNodeName)
    {
        var sourceNode = node.getElementsByTagName(sourceNodeName)[0];
        if (sourceNode.childNodes.length)
            return sourceNode.childNodes[0].nodeValue;
        else
            return "";
    }    
};

////////////////////////////////////////////////////////////////////////////////////////////////////

var LabeledInput = 
{
    apply: function(inputElt, defaultValue)
    {
        addListener(inputElt, "focus", this.onFocusInput, true);
        addListener(inputElt, "blur", this.onBlurInput, true);

        inputElt.defaultValue = defaultValue;
    
        if (inputElt.value == "" || inputElt.value == defaultValue)
        {
            addClass(inputElt, "empty");
            inputElt.value = defaultValue;
            return true;
        }  
    
        return false;
    },
    
    onFocusInput: function(event)
    {
        if (getEventTarget(event).nodeName == "INPUT" && hasClass(getEventTarget(event), "empty"))
        {
            getEventTarget(event).defaultValue = getEventTarget(event).value;
            getEventTarget(event).value = ""
            removeClass(getEventTarget(event), "empty");
        }
    },
    
    onBlurInput: function(event)
    {
        if (getEventTarget(event).nodeName == "INPUT" && getEventTarget(event).value == "")
        {
            getEventTarget(event).value = getEventTarget(event).defaultValue;
            addClass(getEventTarget(event), "empty");
        }
    }
};
