var comments_page = '';
var pageNum = 1;

jQuery(function() {
    var re = /(\w+)\.shtml/;
    var filename = location.href.split('/');
    filename = filename[filename.length-1];
    comments_page = filename.replace(re, "$1");

    switch (comments_page) {
        case 'photocontest':
        case 'photonoteseight':
        case 'photonotesfive':
        case 'photonotesfour':
        case 'photonotesnine':
        case 'photonotesone':
        case 'photonotesseven':
        case 'photonotessix':
        case 'photonotesten':
        case 'photonotesthree':
        case 'photonotestwo':
            
            initCommentsForm();
            break;
    }
});

function initCommentsForm() {
    jQuery('#content').append(
        '<div id="comment_form_container">'
        + '<a class="coloredlinkunderlined" href="javascript:void();" id="add_comment_link" rel="nofollow">Add Comment</a>'
        + '<div id="comment_form">'
        + '</div>'
        + '<div id="comment_list">'
        + '</div>'
        + '</div>'
    );
    
    jQuery("a#add_comment_link").click(function(e) {
        e.preventDefault();
        
        jQuery(this).hide();
        
        jQuery("#comment_form")
            .html('<img src="images/loading.gif" alt="loading" />')
            .load('comments-admin/ajax/form.php', {}, startCommentsForm);
        
        return false;
    });
    
    listComments();
}

function listComments() {
    jQuery("#comment_list")
        .html('<img src="images/loading.gif" alt="loading" />')
        .load('comments-admin/ajax/list.php', {page_num: pageNum, page: comments_page }, function() {
            jQuery('#comment_list').trigger("focus");
            
            jQuery('#paging a').click(function(e) {
                e.preventDefault();
                
                pageNum = jQuery(this).attr("rel");
                listComments();
                
                return false;
            });
        });
}

function startCommentsForm() {
    jQuery("#comment_form_page").val(comments_page);
    
    jQuery("#comment_form_submit").click(function() {
        if (!jQuery("#comment_form_name").val()) {
            jQuery("#comment_form_error").html("Please enter your name!");
            return;
        }
        
        if (!jQuery("#comment_form_email").val()) {
            jQuery("#comment_form_error").html("Please enter your email!");
            return;
        }
        
        if (!/\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,5}$/.test(jQuery("#comment_form_email").val())) {
            jQuery("#comment_form_error").html("Please enter valid email!");
            return;
        }
        
        if (!jQuery("#comment_form_comment").val()) {
            jQuery("#comment_form_error").html("Please enter comment!");
            return;
        }
        
        var params = {};
        jQuery("#comment_form_form :input").each(function () {
            params[this.name] = this.value;
        });
        
        jQuery("#comment_form").html('<img src="images/loading.gif" alt="loading" />');
        
        jQuery.ajax({
            url:  "comments-admin/ajax/form-submit.php",
            type: "POST",
            data: params,
            success: function(html){
                jQuery("#add_comment_link").html("Add Another Comment").show();
                jQuery("#comment_form").html("Your comment has been posted.");

                pageNum = 1;
                listComments();
            }
        });
    });
}

