/* Comment system */

function nl2br(str) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Philip Peterson
    // *     example 1: nl2br('Kevin\nvan\nZonneveld');
    // *     returns 1: 'Kevin<br/>\nvan<br/>\nZonneveld'
 
    if (str == '')
        return false;
    else
        return str.replace(/([^>])\n/g, '$1<br />\n');
}

function commentPage(page, cate, tid, items) {
    $('#commentDiv').html('<div class="loadingPage"><img src="/img/loading_3.gif"><p>Loading, please wait</p></div>');
    $.ajax({
        type: 'POST',
        url: '/ajax/ajax_comment',
        data: 'action=get&page='+page+'&cate='+cate+'&tid='+tid+'&itemsPerPage='+items,
        success: function(msg){
            $('#commentDiv').html(msg);
        }
    })
}

function userCommentPage(page, cate, uid, items) {
    $('#commentDiv').html('<div class="loadingPage"><img src="/img/loading_3.gif"><p>Loading, please wait</p></div>');
    $.ajax({
        type: 'POST',
        url: '/ajax/ajax_comment',
        data: 'action=user&page='+page+'&cate='+cate+'&uid='+uid+'&itemsPerPage='+items,
        success: function(msg){
            $('#commentDiv').html(msg);
        }
    })
}

$(document).ready(function() {

    $('#commentButton').click(function(){
        
            var commentNew = $('#comment').val();
            var cate = $('#cate').val();

            if (commentNew == '') {
                alert('Sorry, please input your comment');
            } else {
                // proceed to comment
                
                var formData = $('#formComment').serialize();
                
                $('#commentTable').html('<div class="loadingPage"><img src="/img/loading_3.gif"><p>Loading, please wait</p></div>');
                
                $.ajax({
                    type: 'POST',
                    url: '/ajax/ajax_comment',
                    data: 'action=post&'+formData,
                    success: function(msg){
                            if (msg == 'OK') {
                                if (cate != 'users')
                                    realmsg = genMsg('Your comment has been submitted successfully!<br />The new comment has been added.', 'Success');
                                else
                                    realmsg = genMsg('Your message has been submitted successfully!<br />The new message has been added.', 'Success');
                                $('#commentTable').html(realmsg);
                                $('#newCommentDisplay').html('<li class="newComment"><p class="NumberNew">@</p><p><span class="commentTime">Just now</span> <span class="commentAuthor">You</span></p><p>'+nl2br(commentNew)+'</p></li>');
                                $('#noComment').hide();
                            } else {
                                switch (msg)
                                {
                                    case 'login' :
                                        realmsg = genMsg('Sorry, but you are not signed in, please <a href="/user_signup">sign in or sign up as a kooplayer</a>.', 'Error');
                                        break;
                                    case 'database' :
                                        realmsg = genMsg('Sorry, there is an error in the database, please try again later.', 'Error');
                                        break;
                                    case 'data' :
                                        realmsg = genMsg('Sorry, there is an error in the data format, please try again later.', 'Error');
                                        break;
                                    case 'read' :
                                        realmsg = genMsg('Sorry, there is an error in the data reading, please try again later.', 'Error');
                                        break;
                                }
                                $('#commentTable').html(realmsg);
                            }
                        }
                });
            }
        });
        
})