if (typeof(Jesper) == 'undefined')
    var Jesper = {};

if (typeof(Jesper.utils) == 'undefined')
    Jesper.utils = {};

Jesper.utils.Comments = new Class({
    initialize: function (container, form, type, id) {
        var self = this;
        this.type = type;
        this.content_id = id;
        this.form = document.id(form);
        this.el = document.id(container);
        this.el.setStyle('position', 'relative');

        this.controls = new Element('div', {
              'html': '<a style="float:right" href="javascript:;">N&auml;sta sida &raquo;</a>',
              'style': {'overflow': 'hidden'}
            });
        this.controls.getFirst('a').addEvent('click', function () {
            self.reload(self.page+1);
        });

        this.msg = new Element('span', {'html': '<em>Inga fler inl&auml;gg hittades</em>'});
        this.req = new Request.JSON({
                method: "get",
                url: "/_ajax/comments-get.php",
                onSuccess: function (d) {
                    if ($defined(self.loading) && self.loading != null)  {
                        self.loading.dispose();
                        self.loading = null;
                    }
                    if  (d.length) {
                        self.el.set('html', "");
                        d.each(function (i) {self.insert(i)});
                        if (d.length == 20)
                            self.controls.inject(self.el);
                    } else 
                        self.msg.inject(self.controls);
                    self.el.fade('in');
                }
            });
        this.post = new Request.JSON({
                method: "post",
                url: "/_ajax/comments-post.php",
                onSuccess: function (d) {
                    self.post_loading.dispose();
                    self.reload(1);
                }
            });
        this.del = new Request({
                method: "post",
                url: "/_ajax/admin/comments-del.php"
            });
        this.reload(1);

        this.form.addEvent('submit', function () {
            var txt = this.getFirst('textarea');
            var val = txt.get('value').toString();
            txt.set('value', '');
            self.post_loading = new Element('span', {'html': 'Postar inl&auml;gg...'});
            self.post_loading.inject(self.form);
            self.post.send({data: {text: val, id: self.content_id, type: self.type}});
            return false;
        });
    },

    reload: function (page) {
        this.el.setStyle('opacity', 0.5);
        this.loading = new Element('img', {src: 'http://static.jesper.nu/images/lightbox-loading.gif'});
        this.loading.setStyles({'z-index': 300, 'position': 'absolute', 'top': Math.max(this.el.getHeight()/2-50, 0), 'left': this.el.getWidth()/2-50});
        this.loading.inject(this.el);
        this.req.send({data: {'type': this.type, 'id': this.content_id, 'page': page}});
        this.page = page;
    },

    insert: function (d) {
        var self = this;
        if (typeof(d.image) != "string" || !d.image.length)
            d.image = "http://www.jesper.nu/images/rattan2.gif";
        d.image_full = d.image.replace("/minithumbs/", "/snyggast/");

        if (typeof(d.extra) != 'undefined' && d.extra != null) {
            if (d.extra.indexOf('d') !== false)
                d.extra = "&nbsp; <a href='javascript:;' id='comment-"+d.id+"-del'>Tabort</a>";
        } else d.extra = '';
        if (d.num != null) {
            d.extra +=' #'+d.num;
        }

        var post = new Element('div', {
                'id': 'comment-'+d.id,
                'class': 'shout-post'+(this.odd=!this.odd?" odd":""),
                'html': this.post_tpl.substitute(d)
            });

        if (typeof(Jesper.link) != 'undefined' && typeof(Jesper.link.Bubbles) != 'undefined')
            new Jesper.link.Bubbles(post);
        if (typeof(lightbox_list) != 'undefined')
            lightbox_list.add(post.getChildren('.shout-left').getFirst());

        post.inject(this.el);

        var x;
        if ((x = document.id('comment-'+d.id+'-del'))) {
            x.addEvent('click', function () {
                var id = this.getProperty('id').split('-');
                self.remove(id[1]);
            });
        }
    },

    remove: function (id) {
        var el = document.id('comment-'+id);
        el.dispose();
        this.del.send({data: {id: id, type: this.type}});
    },

    post_tpl: '<div class="shout-content"><a href="/{poster_name}/" title="{poster_name} -- {status}">{poster_name}</a><br />{text}</div>'
                  +'<div class="shout-left"><a href="{image_full}" class="lightbox"><img src="{image}" alt="Bild p&aring; {poster_name}" /></a></div>'
                  +'<div class="shout-right">{date}{extra}</div>',
    controls_tpl: '',
    page: 1,
    odd: false
});
