preload = ['commentgood.jpg', 'commentdiscard.jpg', 'good_text.gif', 'discard_text.gif'];
for (var i = 0; i < preload.length; ++i) {
    img = new Image();
    img.src = 'http://bitcast-a.bitgravity.com/theory11/images/media/' + preload[i];
}

function crover(obj, e) {
    if (!e) e = window.event;
    if (e.pageX)		x = e.pageX;
    else if (e.clientX)	x = e.clientX + document.body.scrollLeft;
    
    x -= gl(obj);

    var id = obj.id.substr(5);
    var text = $('ratetext_' + id);
    if (x < obj.width/2) {
        obj.src = 'http://bitcast-a.bitgravity.com/theory11/images/media/commentgood.jpg';
        obj.rating = 'good';
        if (text) {
            text.src = 'http://bitcast-a.bitgravity.com/theory11/images/media/good_text.gif';
        }
    } else {
        obj.src = 'http://bitcast-a.bitgravity.com/theory11/images/media/commentdiscard.jpg';
        obj.rating = 'discard';
        if (text) {
            text.src = 'http://bitcast-a.bitgravity.com/theory11/images/media/discard_text.gif';
        }
    }

    obj.onmouseout = function() {
        this.src = 'http://bitcast-a.bitgravity.com/theory11/images/media/commentrate.jpg';
        
        var id = this.id.substr(5);
        var text = $('ratetext_' + id);
        if (text) {
            text.src = 'http://bitcast-a.bitgravity.com/theory11/images/media/rate_text.gif';
        }
    }

    obj.onclick = function() {
        if (this.rating) {
            this.onmousemove = function() {}
            this.onmouseout = function() {}
            
            var id = this.id.substr(5);
            var text = $('ratetext_' + id);
            if (text) {
                text.style.display = 'none';
            }
            
            this.style.cursor = 'default';

            new Ajax.Request(
                '/rate_comment.php?id=' + id + '&rating=' + this.rating,
                {
                    asynchronous:true,
                    evalScripts:true,
                    onComplete:function(request){eval(request.responseText);}
                }
            );
        }
        return false;
    }
}

function ShowComment(index, ownreply)
{
    if (ownreply) {
        var sparereply = $('sparereply_' + ownreply);
        if (sparereply) {
            sparereply.style.display = 'none';
        }
    }

    var csays = $('csays_' + index);
    if (csays) {
        csays.innerHTML = 'says:'
    }
    
    var creplies = $('creplies_' + index);
    if (creplies) {
        creplies.innerHTML = 'replies:'
    }
    
    var comview = $('combody_' + index);
    if (comview) {
        comview.style.display = '';
    }
}

function QuickReply(id) {
    $('reply' + id).style.display = '';
    $('comment_text_' + id).focus();
    return false;
}

function SubmitComment(content_id) {
    if (!ValidateLength('commenttextarea', 'Comment', 10, 1000)) { return false; }
    
    new Ajax.Updater('commentList', '/comment.php',
        {
            asynchronous: true,
            evalScripts: true,
            onLoading: function (request) {
                elem = $('submit_comment');
                elem.value = 'Posting...';
                elem.disabled = true;
            },
            onComplete: function (request) {
                elem = $('submit_comment');
                elem.value = 'Submit';
                elem.disabled = false;
                $('commenttextarea').value = '';
            },
            parameters: Form.serialize('commentform')
        }
    );
    return false;
}

function SubmitReply(id) {
    if (!ValidateLength('comment_text_' + id, 'Comment', 10, 1000)) { return false; }
    
    new Ajax.Updater('commentList', '/comment',
        {
           asynchronous: true,
           evalScripts: true,
           onLoading: function(request) {
               elem = $('submit_comment_' + id);
               elem.value = 'Posting...';
               elem.disabled = true
           },
           onComplete: function(request) {
               elem = $('submit_comment_' + id);
               elem.value = 'Submit';
               elem.disabled = false;
               $('comment_text_' + id).value='';
           },
           parameters: Form.serialize('replyform' + id)
        }
    );
    return false;
}

function DeleteComment(id) {
    if (confirm('Are you sure you want to delete this comment?')) {
        new Ajax.Request('/admin/delete_comment.php?id=' + id,
            {
                asynchronous: true,
                evalScripts: true,
                onComplete: function(request) {
                    new Element.remove('comment' + id);
                }
            }
        );
    }
    return false;
}
function RestoreComment(id) {
    if (confirm('Are you sure you want to restore this comment?')) {
        new Ajax.Request('/admin/restore_comment.php?id=' + id,
            {
                asynchronous: true,
                evalScripts: true,
                onComplete: function(request) {
                    alert('Coolio.');
                    ShowComment(id);
                }
            }
        );
    }
    return false;
}
function EditComment(id) {
    text = $('commenttext' + id);
    if (text) {
        text.originalhtml = text.innerHTML;
        temp = text.innerHTML.replace(/^\s+/g, '');
        temp = temp.replace(/\s+$/g, '')
        temp = temp.replace(/<br \/>/gi, "\n");
        temp = temp.replace(/<br>/gi, "\n");
        text.innerHTML = '<form id="editform'+id+'" method="post" action="/edit_comment" onsubmit="return SaveEdit('+id+')">'
            + '<input type="hidden" name="id" value="'+id+'" />'
            + '<textarea id="edit_' + id + '" name="comment" style="width:90%;height:100px">'
            + temp
            + '</textarea><br />'
            + '<input type="submit" value="Save Changes" />'
            + ' <input type="reset" value="Cancel" onclick="CancelEdit('+id+')" />';
    }
    return false;
}
function CancelEdit(id) {
    text = $('commenttext' + id);
    if (text) {
        text.innerHTML = text.originalhtml;
    }
    return false;
}
function SaveEdit(id) {
    edit = $('edit_' + id);

    if (edit) {
        new Ajax.Request(
            '/edit_comment',
            {
                parameters: Form.serialize('editform' + id),
                onFailure: function(request) {
                    alert(request.responseText);
                },
                onSuccess: function(request) {
                    text = $('commenttext' + id);
                    if (text) {
                        text.innerHTML = request.responseText.replace(/\\'/g, '\'').replace(/\\"/g, '"').replace(/\\\\/g, '\\');
                        time = $('time' + id);
                        if (time) {
                            time.innerHTML = '0 seconds ago';
                        }
                    }
                }
            }
        );
    }

    return false;
}