var CComment = Class.create({
    // Comment Variables
    entry:      '',
    div:        null,

    // Class Variables
    box:        null,
    form:       null,
    tpl:        null,
    main:       false,
    login:      false,

    /**
     * Constructor
     *
     * @return CComment
     */
    initialize: function(obj) {
        this.box    = $('commentBox');
        this.form   = this.box.down('form');
        this.main   = obj.main  || false;
        this.login  = obj.login || false;
        this.entry  = obj.entry || '';

        s   = this.form.select('.submit')[0];
        s.observe('click', this.submit.bindAsEventListener(this));

        if(!this.main) {
            b           = document.body.innerHTML;
            this.tpl    = b.match(/\[comment\]([\S\s]+)\[\/comment\]/);
        }
    },

    show: function(obj) {
        if(Object.isString(obj.entry)) {
            this.form.e.value   = obj.entry;
            this.form.p.value   = '';
        } else if(Object.isString(obj.parent)) {
            this.form.e.value   = this.entry;
            this.form.p.value   = obj.parent;
        } else {
            this.form.e.value   = this.entry;
            this.form.p.value   = '';
        }

        this.div    = $(obj.id);

        this.form.t.value   =
            (!obj.title) ?
            '' : ((obj.title.match(/^re:/i)) ?
            obj.title : 'Re: ' + obj.title);
        this.form.txt.value = '';

        $(this.div).insert({after: this.box});
        this.box.show();

        if(this.login)  this.form.txt.focus();
        else            this.form.u.focus();
    },

    submit: function(ev) {
        ev.stop();

        if(this.form.txt.value.blank()) {
            alert('comment can not be empty.');
            this.form.txt.focus();
            return;
        }

        $('commentLoader').show();

        new Ajax.Request('/ajax/comment.php', {
            parameters: this.form.serialize(),
            onSuccess:  this.validateSubmit.bind(this)
        });
    },

    validateSubmit: function(t) {
        try {
            js	= t.responseText.evalJSON(true);
        } catch(e) {
            alert('Received invalid JSON:\n' + t.responseText);
            return;
        }

        $('commentLoader').hide();

        if(js.m == 'comment' && js.r) {
            if(this.main)   document.location   = js.d.url;
            else            this.write(js.d);
        } else {
            if(js.m == -1) {
                alert(js.r);
            } else if(js.m == 'login') {
                document.location.reload(true);
            } else if(!js.r) {
                alert(js.d);
            }
        }
    },

    write: function(d) {
        this.box    = this.box.remove();

        if(this.tpl != null) {
            h   = this.tpl[1].interpolate({
                id:     d.id,
                entry:  d.entry,
                user:   d.user,
                date:   d.ts,
                title:  d.title,
                text:   d.text
            }, syntax);

            if(d.parent != '') {
                subs    = this.div.up().select('div.sub')[0];
                subs.insert({bottom: h});
            } else {
                $('comments').insert({bottom: h});
            }
        }
    }
});
