/*var win;

Ext.onReady(function(){
    var add_link = $('#add_comment');
    if(add_link){
        add_link = add_link[0];
        add_link.onclick = function(){
            url = add_link.href;
            form = new Ext.FormPanel({
                        labelWidth: 125,
                        frame: true,
                        //title: 'Date Range',
                        bodyStyle:'padding:5px 5px 0',
                        width: 350,
                        defaults: {width: 175},
                        items: [
                            { "id" : "message" , "height" : 20 , "style" :"padding-bottom:5px;" },
                            new Ext.form.TextField({"fieldLabel": "title", "msgTarget": "under", "minLength": 4, "blankText": "This field is required.", "allowBlank": false, "value": null, "maxLength": 100, "name": "title"}), 
                            {
                                width:306,
                                items:new Ext.form.TextArea({"fieldLabel": "description", "msgTarget": "under", "blankText": "This field is required.", "allowBlank": false, "value": null, "maxLength": 400, "name": "description" , "width":306})
                            }
                        ]
            });
            if(!win){
                win = new Ext.Window({
                    //applyTo     : 'hello-win',
                    title       : 'Add comment',
                    layout      : 'fit',
                    width       : 350,
                    height      : 230,
                    closeAction :'hide',
                    plain       : true,
                    items       : form,

                    buttons: [{
                        text     : 'Submit',
                        handler  : function(){
                            if(!form.form.isValid()){
                                $("#message").text("Please correct all the errors below");
                                return false;
                            } else {
                                var ar = form.form.getValues();
                                $.post(url , ar , function(data){
                                    if (data == "OK"){
                                        //$("#message").text("");
                                        win.hide();
                                    } else
                                        $("#message").text(data);
                                });
                            }
                            
                        }
                    },{
                        text     : 'Close',
                        handler  : function(){
                            win.hide();
                        }
                    }]
                });
            }
            win.show();
            
            return false;
        }
        //win.show(button);

    }
})*/

var opened_blog = null;
var loaded = [];
var opened = [];

function open_blog(blog_id){
    
    //Ask through ajax for the next children
    //If not loaded
    if (jQuery.inArray(blog_id , loaded) == -1){
        pre_open(blog_id);
        $('div#blog_' + blog_id + '>.in-text').show('slow');
        $.get("/comment/" , {"blog_id":blog_id} , function(data){
            if (data)
                $('div#blog_' + blog_id + '>.children').html(data).show('fast');
        });
        opened.push(blog_id);
        loaded.push(blog_id);
    } else if (jQuery.inArray(blog_id , opened) != -1){
        //Close
        $('div#blog_' + blog_id + '>.in-text').hide('fast');
        $('div#blog_' + blog_id + '>.children').hide('fast');
        opened = jQuery.grep(opened , function(el){el != blog_id});//(blog_id);
    } else {
        pre_open(blog_id);
        $('div#blog_' + blog_id + '>.in-text').show('fast');
        $('div#blog_' + blog_id + '>.children').show('fast');
        opened.push(blog_id);
    }
}
function pre_open(blog_id){
    return true;
}

function write_blog(blog_id){
    //Check if is open
    if (opened_blog == blog_id && blog_id){
        $("#add_comment").hide();
        opened_blog = null;
    } else {
        var inject_to = "#forum-new";
        if (blog_id)
            inject_to = 'div#blog_' + blog_id + ">.in-text";
        
        //console.log($(inject_to + ">.comment_spot").get(0));
        //console.log(inject_to + ">.comment_spot");
        $("#add_comment").appendTo(inject_to + ">.comment_spot").hide().show('fast');
        $("#parent").each(function(){
            this.value = blog_id;
        });
        opened_blog = blog_id;
    }
}