$(function() {
    //add new comment stuff
    append_comment_preview();
    register_comment_preview_events();
    init_comment_preview();
    register_comment_reset();
    add_comment_validator();
});

function append_comment_preview() {
    $(".addcomment").after(
        '<div class="commentboxnew" id="comment">' +
            '<span id="text">content?</span>' +
            '<div class="commentinfo">' +
                '<span id="name">:o</span> on <span id="date">' +
                    curr_month + ' ' + curr_day + ', ' +
                    curr_year + ' ' + curr_hours + ':' + curr_min +
                    '</span>' +
            '</div>' +
        '</div>'
    );
}
function register_comment_preview_events() {
    $("#commentform #site").change(update_name_site);
    $("#commentform #site").keydown(update_name_site);
    $("#commentform #site").keyup(update_name_site);

    $("#commentform .name").change(update_name_site);
    $("#commentform .name").keydown(update_name_site);
    $("#commentform .name").keyup(update_name_site);

    $("#commentform .together textarea.comment").change(update_comment_content);
    $("#commentform .together textarea.comment").keydown(update_comment_content);
    $("#commentform .together textarea.comment").keyup(update_comment_content);
}
function init_comment_preview() {
    update_name_site();
    update_comment_content();
}
function register_comment_reset() {
    $("#commentform").find(":reset").click(function(e) {
        e.preventDefault();

        $("#commentform #site").val("");
        $("#commentform #name").not(":hidden").val("");
        $("#commentform .together textarea.comment").val("");
        update_name_site();
        update_comment_content();
    });
}
function add_comment_validator() {
    $("#commentform").find(":submit").livequery('click', function(e) {
        if(!is_commentform_valid()) {
            $("#commentform").find(".content_shouter").html(
                ' You must enter a message'
            );
            e.preventDefault();
        }
    });
}

//compose name with a possible link for comments
function compose_name_site(name, site) {
    if(site.length && site.substring(0,7) != "http://") {
        site = "http://" + site;
    }
    if(name == undefined || !name.length) {
        name = 'Anonymous';
    }
    if(site.length) {
        var composed = '<a href="' + site + '">' + name + '</a>';
    }
    else {
        var composed = name;
    }
    return composed;
}

//sets name and link for the comment preview
function update_name_site() {
    if(!$("#commentform").find("#site").length) {
        var site = "";
    } else {
        var site = $("#commentform").find("#site").val();
    }
    var name = $("#commentform").find("#name").val();
    var composed = compose_name_site(name, site);
    $(".addcomment").next().find("#name").html(composed);
}

//updates preview comments content
function update_comment_content() {
    if(!$("#commentform .together textarea.comment").length) return;

    var textarea = $("#commentform .together textarea.comment");

    var text = textarea.val();
    var text = comment_markdown(text);
    if(!text.length) {
        text = '-content-';
        textarea.addClass("badcomment");
    }
    else {
        textarea.removeClass("badcomment");
    }
    $(".addcomment").next().find("#text").html(text);
}

function is_commentform_valid() {
    var content = comment_markdown($("#commentform .together textarea.comment").val());
    if(!content.length) {
        return false;
    }
    else {
        return true;
    }
}

//comment header is the header on the far top of all the comments
//showing how many comments there are
function incr_comments_header() {
    var header = $(".commentstitle a").html();
    if(header === null) return;
    if(header.substr(0,2) == "No") {
        $("#.commentstitle a").html("One comment");
    }
    else if(header.substr(0,3) == "One") {
        $("#.commentstitle a").html("2 comments");
    }
    else {
        var num = parseInt(header.match(/[0-9]+/));
        ++num;
        $("#.commentstitle a").html(num + " comments");
    }
}

function decr_comments_header() {
    var header = $(".commentstitle a").html();
    if(header === null) return;

    if(header.substr(0,3) == "One") {
        $("#.commentstitle a").html("No comments yet :(");
    }
    else if(header.substr(0,2) != "No") {
        var num = parseInt(header.match(/[0-9]+/));
        --num;
        if(num > 1) {
            $("#.commentstitle a").html(num + " comments");
        }
        else {
            $("#.commentstitle a").html("One comment");
        }
    }
}

//update edit a comment
function update_editcomment() {
    update_editcomment_content();
    update_editcomment_name_site();
}

function update_editcomment_content() {
    var text = $(this).val();
    text = comment_markdown(text);
    if(!text.length) {
        text = '-content-';
        $(this).addClass("badcomment");
    }
    else {
        $(this).removeClass("badcomment");
    }
    $(this).parent().prev().find("#text").html(text);
}

function update_editcomment_name_site() {
    var name = $(this).parent().find("#name").val();
    var site = $(this).parent().find("#site").val();
    var composed = compose_name_site(name, site);
    $(this).parent().prev().find("#name").html(composed);
}

//current time stuff
var month = new Array(12);
month[0] = "January";
month[1] = "February";
month[2] = "March";
month[3] = "April";
month[4] = "May";
month[5] = "June";
month[6] = "July";
month[7] = "August";
month[8] = "September";
month[9] = "October";
month[10] = "November";
month[11] = "December";

var date = new Date();
var curr_month = month[date.getUTCMonth()];
var curr_day = date.getDate();
var curr_year = date.getFullYear();
var curr_hours = date.getHours();
var curr_min = date.getMinutes();
if(curr_min < 10) {
    curr_min = "0" + curr_min.toString();
}

function markdown(text) {
    var converter = new Showdown.converter();
    var html = converter.makeHtml(text);
    return parse_embed_youtube(html);
    //return html;
}

function comment_markdown(text) {
    var converter = new SimpleMarkdown.converter();
    var allowed_tags = 'a em strong p i b br';
    return strip_tags(converter.makeHtml(text), allowed_tags);
}

function parse_embed_youtube(text) {
    return text.replace(/(:?<p>http:\/\/www.youtube.com\/watch\?v=([^ <>=&"]+))/gi,
        "<iframe class=\"youtube-player\" type=\"text/html\" width=\"600\" height=\"385\" src=\"http://www.youtube.com/embed/$2\" frameborder=\"0\"></iframe>");
}

String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
    return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
    return this.replace(/\s+$/,"");
}


