$(document).ready(init);

jQuery.fn.extend({
    scrollBottom: function(speed) {
        return this.each(function() {
            var bottomOffset = $(this).offset().top + $(this).height();
            $('html').animate({scrollTop: bottomOffset - $(window).height()},
                              speed);
            });
        },
    scrollBottomCond: function(speed) {
        return this.each(function() {
            var bottomOffset = $(this).offset().top + $(this).height();
            if (bottomOffset > $(window).scrollTop() + $(window).height()) {
                $('html').animate({scrollTop: bottomOffset - $(window).height()},
                                  speed);
            }});
        }
    });


function get_json(data) {
    return eval('(' + data + ')');
};


function init() {
    saved_posts = {};
    post_form = $("#post-form");
    input_reply = $("#id_reply_to", post_form);
    post_link = $("a.post-link");
    // actions
		//		if (!post_form.find(".errorlist").length) {
		//		post_form.hide();
				//var show_post_link = true;
		//}
//     else {
//         var id = input_reply.val();
//         var show_post_link = id;
//         if (id) {$("#p" + id).append(post_form);}
//         post_form.scrollBottomCond(1);
//     }
//     if (show_post_link) {
//         post_link.show();
//     }
    $("a.edit-post").css("display", "inline");

    // spinner
    $('#ajaxload').ajaxStart(function(){$(this).show()}).ajaxStop(function(){$(this).hide()});
};

function preview_post(url) {
    text = $("#id_body", post_form).val();
    preview_div = $("#post-preview", post_form);
    $.post(url,
          {'body': text},
          function (data) {
              json = get_json(data);
              preview_div.html(json.body_preview);
              preview_div.css("display", "block");
          });
    return false;
};

function cancel_postedit() {
    //post_div = $("#c" + id + " .text");
		post_div = $("#post-form-container");
		post_div.html();
    $("#post-form").hide();
    //$("#edit-submit-post").hide();
    //$("#post-form").css("display", "inline");
    return false;
};


function submit_postedit(id, url) {
    post_div = $("#c" + id + " .text");
    text = $("#c" + id + " .text textarea.post-edit-area").val();
    $.post(url,
           {'body': text},
           function (data) {
               json = get_json(data);
               post_div.html(json.body_html);
               $("#cancel-edit-" + id).hide();
               $("#edit-submit-" + id).hide();
               $("#edit-post-" + id).css("display", "inline");
           });
    return false;
};

function getnewpost(){
    if (!location) {
        var location = "#post-form-container";
    }
		$.post('/forms/postform',{},
					 function(data){
							 json = get_json(data);
							 $(location).append(json.html);
					 });
		return false;
}

function newpost() {
    cancel_postedit();
    input_reply.val('');
    if (!location) {
        var location = "#post-form-container";
    }
    //if (!after) {
		$(location).append(post_form);
    //} else {
    //    $(location).after(post_form);
		// }
    post_form.show();
    //post_link.hide();
    $('#id_name', post_form).focus();
    post_form.scrollBottomCond(1);
    return false;
};

