/* Comment lib */

$(document).ready(function()
{
    var re = /\w+_(\d+)/;

    $('#new_comment').click(function()
    {
        $('#comment_form').slideToggle(200);

        editor_init("comment[content]");
    });

    $('.comment_answer').live("click", function()
    {
        $(".answer_form").empty();
        var comment_id = $(this).attr("id");
        cid = comment_id.replace(/answer_(\d+)/, "$1");
        answerComment(cid);
        editor_init("comment[content]");
    });
//TODO: bad comment
/* Comment with negative rating
	$(".bad_comment").click(function(){
		var comment_id = $(this).attr("id");
		cid = comment_id.replace(/showcomment_(\d+)/, "$1");
		$("#badcomment_"+cid).show();
		$(this).empty();
	});
*/
//TODO: karma
/* no karma engine yet

    var re = /\w?karma_(\w+)_(\w+)_(\d+)_(\d+)$/;
    $('.comment_karma_min_b').live("click", function()
    {
        var u_id = $(this).attr("id");
        obj_id = u_id.replace(re, "$4");
        owner_id = u_id.replace(re, "$3");
        module = u_id.replace(re, "$2");
        owner = u_id.replace(re, "$1");
        comment_karma (obj_id, owner_id, owner, module, -1);
    });

    $('.comment_karma_plus_b').live("click", function()
    {
        var u_id = $(this).attr("id");
        obj_id = u_id.replace(re, "$4");
        owner_id = u_id.replace(re, "$3");
        module = u_id.replace(re, "$2");
        owner = u_id.replace(re, "$1");
        comment_karma (obj_id, owner_id, owner, module, 1);
    });
*/
});

function editor_init(element)
{
	$('textarea[name="'+element+'"]').markItUp(myBbcodeSettings);
}

editor_init("comment[content]");
//TODO: Grade and Karma
/** Оценка 
function comment_karma(obj_id, owner_id, owner_type, module, rate)
{
	var req = new JsHttpRequest();

	req.onreadystatechange = function()
	{
		if (req.readyState == 4)
        {
            if(req.responseText)
            {
                 alert(req.responseText);
            }
            if (req.responseJS)
            {
                if (req.responseJS.status == 'ok')
                {
                    var karma = req.responseJS.karma;

                    $('#comment_karma_'+obj_id).html(karma);

                    new_class = 'null';
                    if (parseInt(karma) > '0')
                    {
                        new_class= 'plus';
                    }
                    if (parseInt(karma) < '0')
                    {
                        new_class = 'min';
                    }

                    $('#comment_karma_'+obj_id).attr("class", new_class+' karma_voted');
                    $('#pkarma_'+owner_type+'_'+module+'_'+owner_id+'_'+obj_id).attr("class", 'comment_dis_karma_plus_b').attr("src", i_url+'comment_dis_plus.gif');
                    $('#mkarma_'+owner_type+'_'+module+'_'+owner_id+'_'+obj_id).attr('class', 'comment_dis_karma_min_b').attr("src", i_url+'comment_dis_min.gif');
                }
            }

            $("body").css("cursor", "auto");
		}
    }
    $("body").css("cursor", "wait");
    var what = 'karma';
    req.open(null, '/js/karma.php', true);
	req.send({action:what, obj_id:obj_id, owner_id:owner_id, owner_type:owner_type,module:module, rate:rate});

}
**/

/** Управление комментариями **/
function answerComment(id) {
    $('#answer_form_'+id).html('<form id="anscom_'+id+'" method="post" action="/comment.php"><input type="hidden" name="do" value="answerComment"><input type="hidden" name="comment[parent]" value="'+id+'"><input type="hidden" name="comment[obj_id]" value="'+$('#comment_obj_id').val()+'"><input type="hidden" name="comment[module]" value="'+$('#comment_module').val()+'"><input type="hidden" name="comment[source]" value="'+$('#comment_source').val()+'" /><textarea name="comment[content]" cols="50" rows="5" ></textarea><br/><input type="submit" name="send_comment" value="Добавить" class="submit"></form>');
}

/** answerQuoteComment(ID)
 * create comment form into #answer_form_ID with filled fields, taken from backend
 * @var id
 */
function answerQuoteComment(id) {
    // Create new JsHttpRequest object.
    var req = new JsHttpRequest();

    // Code automatically called on load finishing.
    req.onreadystatechange = function() {
        if (req.readyState == 4) {

            if(req.responseText)
            {
                byId('alert_'+id).innerHTML = req.responseText;
            }

            if (req.responseJS)
            {
                comment = req.responseJS;
                byId('answer_form_'+id).innerHTML = '<form id="edcom_'+id+'" method="post" action="/js/comment.php"><input type="hidden" name="do" value="answerComment"><input type="hidden" name="parent" value="'+id+'"><input type="hidden" name="obj_id" value="'+byId('comment_obj_id').value+'"><input type="hidden" name="module" value="'+byId('comment_module').value+'"><input type="hidden" name="return" value="'+byId('comment_return').value+'" /><textarea name="answer" cols="50" rows="5" ><blockquote class="comment_quote">'+comment['author']['print_login']+': '+comment['comment']+'</blockquote><p></p></textarea><br><div align="center"><input type="submit" name="asnwerQuoteComment" value="Добавить" class="small_orange_button"></div></form>';

                $('textarea[name="comment[content]"]').markItUp(myBbcodeSettings);
            }
		}
    }


    byId('answer_form_'+id).innerHTML = '<img src="/loader.gif" width="44" height="44" alt="Подождите идет загрузка.">';
    var what = 'getComment';
    // Prepare request object (automatically choose GET or POST).
    req.open(null, '/js/comment.php', true);
	req.send({action:what, id:id});
}
