Event.observe(window, "load", function() {
    
    $$(".inp-vote-past").each(function(item) {
        Event.observe(item, "click", voteOnClick.bindAsEventListener(item, "past"));
    });
    $$(".inp-vote-present").each(function(item) {
        Event.observe(item, "click", voteOnClick.bindAsEventListener(item, "present"));
    });
    $$(".inp-vote-future").each(function(item) {
        Event.observe(item, "click", voteOnClick.bindAsEventListener(item, "future"));
    });
    
    $$(".inp-myvote-past").each(function(item) {
        Event.observe(item, "click", myvoteOnClick.bindAsEventListener(item, "past"));
    });
    $$(".inp-myvote-present").each(function(item) {
        Event.observe(item, "click", myvoteOnClick.bindAsEventListener(item, "present"));
    });
    $$(".inp-myvote-future").each(function(item) {
        Event.observe(item, "click", myvoteOnClick.bindAsEventListener(item, "future"));
    });
    
    if($("btn-highlight")) {
        Event.observe("btn-highlight", "click", function() {
            highlightSearchTerms($F('highlight-term'));
        });
        Event.observe("highlight-term", "keyup", function(e) {
            if(e.keyCode == 13) {
                highlightSearchTerms($F('highlight-term'));
            }
        });
    }
    
    if($("btn-highlight-reset")) {
        Event.observe("btn-highlight-reset", "click", resetHighlight);
    }
    
    if($("btn-toggle-comments")) {
        Event.observe("btn-toggle-comments", "click", function() {
            $$(".comments-container").each(function(item) {
                item.toggle();
            });
        });
    }
    
    $$(".frm-comment").each(function(item) {
        Event.observe(item, "submit", commentFormOnSubmit.bindAsEventListener(item));
    });
    
});

function commentFormOnSubmit(e) {
    var id = this.id.substr(13);
    var title = $F("inp-comment-title-" + id);
    var text = $F("inp-comment-text-" + id);
    new Ajax.Request("ajax.php?p=comment", {
        parameters: {
            "wid": id,
            "title": title,
            "text": text
        },
        onSuccess: function(tr) {
            $("new-comment-container-" + id).insert(tr.responseText);
            $("inp-comment-title-" + id).value = '';
            $("inp-comment-text-" + id).value = '';
        }
    });
    Event.stop(e);
}

function voteOnClick(e, w) {    
    var id = this.id.substr(w.length + 10);
    new Ajax.Updater("votes-" + w + "-" + id, "ajax.php?p=vote/" + id + "/" + w, {});
}

function myvoteOnClick(e, w) {
    var id = this.id.substring(w.length + 12, this.id.lastIndexOf("-"));
    var catId = this.id.substr(this.id.lastIndexOf("-")+1);
    new Ajax.Updater("myvotes-" + w + "-" + catId, "ajax.php?p=myvote/" + id + "/" + w, {});
}

function doHighlight(bodyText, searchTerm) {
    highlightStartTag = '<span class="highlight">';
    highlightEndTag = '</span>';
    
    var newText = "";
    var i = -1;
    var lcSearchTerm = searchTerm.toLowerCase();
    var lcBodyText = bodyText.toLowerCase();
    
    while(bodyText.length > 0 ) {
        i = lcBodyText.indexOf(lcSearchTerm, i+1);
        if(i < 0) {
            newText += bodyText;
            bodyText = "";
        } else {
            if(bodyText.lastIndexOf(">", i) >= bodyText.lastIndexOf("<", i)) {
                newText += bodyText.substring(0, i) + highlightStartTag + bodyText.substr(i, searchTerm.length) + highlightEndTag;
                bodyText = bodyText.substr(i + searchTerm.length);
                lcBodyText = bodyText.toLowerCase();
                i = -1;
            }
        }
    }
    return newText;
}

function highlightSearchTerms(searchText) {
    //resetHighlight();
    if(!document.body || typeof(document.body.innerHTML) == "undefined") {
        return false;
    }
    $$(".highlight-container").each(function(item) {
        var bodyText = item.innerHTML;
        bodyText = doHighlight(bodyText, searchText);
        item.innerHTML = bodyText;
    });
    return true;
}

function resetHighlight() {
    $$(".highlight-container").each(function(item) {
        var res = item.innerHTML.replace(/<span class="highlight">([^<]*)<\/span>/g, "$1");
        item.innerHTML = res;
    });
}