$(document).ready(init);
jQuery.fn.selectOptions = function(value) {
	this.each(
		function()	{
			if(this.nodeName.toLowerCase() != "select") return;
			
			// get number of options
			var optionsLength = this.options.length;
			
			
			for(var i = 0; i<optionsLength; i++) {
				if (this.options[i].value == value) {
					this.options[i].selected = true;
				};
			}
		}
	)
	return this;
}
function init() {
    saved_posts = {};
    comment_form = $("#comment-form");
    input_reply = $("#id_reply_to", comment_form);
    comment_link = $("a.comment-link");
    // actions
    if (!comment_form.children(".errorlist").length) {
        comment_form.hide();
        var show_comment_link = true;
    }
    else {
        var id = input_reply.val();
        var show_comment_link = id;
        move_form(id);
        $('html').animate({scrollTop: comment_form.offset().top}, 1);
    }
    if (show_comment_link) {
        comment_link.show();
    }
    $("a.edit-comment").css("display", "inline");

    // spinner
    $('#ajaxload').ajaxStart(function(){$(this).show()}).ajaxStop(function(){$(this).hide()});
};

function get_json(data) {
    return eval('(' + data + ')');
};

function move_form(id) {
    if (id) {
        $("#c" + id).append(comment_form);
    }
    else {
        $("#null_reply").append(comment_form);
    }
    document.getElementById("body_errors").innerHTML = "";
    document.getElementById("captcha_errors").innerHTML = "";
    if (document.forms['comment-form-itself'].captcha) document.forms['comment-form-itself'].captcha.value = "";
    document.forms['comment-form-itself'].body.value = "";
}

function replyto(id) {
    cancel_comment();
    input_reply.val(id);
    move_form(id);
    comment_form.show();
    $("#reply-link-" + id).hide();
    return false;
};

function comment() {
    cancel_comment();
    input_reply.val('');
    move_form(null);
    comment_form.show();
    comment_link.hide();
    return false;
};

function cancel_comment() {
    id = input_reply.val();
    if (id) {
        $("#reply-link-" + id).show();
    }
    else {
        comment_link.show();
    }
    comment_form.hide();
    return false;
};

function edit_comment(id, url) {
    comment_div = $("#c" + id + " .text");
    saved_posts[id] = comment_div.html()
    $.post(url,
           {'get_body': ''},
           function (data) {
               json = get_json(data);
               comment_div.html('<textarea class="comment-edit-area">' + json.body + '</textarea>');
               $("#reply-link-" + id).hide();
               $("#edit-comment-" + id).hide();
               $("#cancel-edit-" + id).css("display", "inline");
               $("#edit-submit-" + id).css("display", "inline");
           });
    return false;
};

function cancel_edit(id) {
    comment_div = $("#c" + id + " .text");
    comment_div.html(saved_posts[id]);
    $("#cancel-edit-" + id).hide();
    $("#edit-submit-" + id).hide();
    $("#edit-comment-" + id).css("display", "inline");
    return false;
};

function submit_edit(id, url) {
    comment_div = $("#c" + id + " .text");
    text = $("#c" + id + " .text textarea.comment-edit-area").val();
    if (text!= ""){
        $.post(url,
               {'body': text},
               function (data) {
                   json = get_json(data);
                   comment_div.html(json.body_html);
                   $("#cancel-edit-" + id).hide();
                   $("#edit-submit-" + id).hide();
                   $("#edit-comment-" + id).css("display", "inline");
               });
        return false;
    }
};

function delete_comment(id, url) {
    if (confirm("Are you sure to delete this comment?"))
        {
            $.post(url,
                   {'delete': true},
                   function (data) {
                       json = get_json(data);
                       $('#c' + json.id).remove();
                   });
        };
    return false;
};

function preview_comment(url) {
    text = $("#id_body", comment_form).val();
    preview_div = $("#comment-preview", comment_form);
    $.post(url,
          {'body': text},
          function (data) {
              json = get_json(data);
              preview_div.html(json.body_preview);
          });
    return false;
};
function get_json(data) {
    return eval('(' + data + ')');
};
function post_comment(entry_id,captcha_id,captcha,comment,reply_to,name){
	document.getElementById("body_errors").innerHTML = "";
	document.getElementById("captcha_errors").innerHTML = "";
	document.getElementById("name_errors").innerHTML = "";
	$.ajax({ 
			type: "POST", 
			url: "/comments/comment-add/"+entry_id+'/', 
			data: {"captcha_id":captcha_id, "captcha":captcha,"body":comment,"reply_to":reply_to,"name":name}, 
			success: function(responce){ 
				json = get_json(responce);
				if (json.errors!="ok"){
					if (json.errors.body) document.getElementById("body_errors").innerHTML = json.errors.body;
					if (json.errors.captcha) document.getElementById("captcha_errors").innerHTML = json.errors.captcha;
					if (json.errors.name) document.getElementById("name_errors").innerHTML = json.errors.name;
				}else{
					document.location.reload();
				}
			}
	});
}
function post_comment_registered(entry_id,comment,reply_to){
	document.getElementById("body_errors").innerHTML = "";
	$.ajax({ 
			type: "POST", 
			url: "/comments/comment-add/"+entry_id+'/', 
			data: {"body":comment,"reply_to":reply_to}, 
			success: function(responce){ 
				json = get_json(responce);
				if (json.errors!="ok"){
					if (json.errors.body) document.getElementById("body_errors").innerHTML = json.errors.body;
				}else{
					document.location.reload();
				}
			}
	});
}