var NewsItemViewer = {
    ajaxUrl : '/entity.get.php',
    ajaxEnt : 'NewsItem',

    htmlnewsitem : 'newsitem',

    cache   : {},

    init    : function() {
        $$('.newslist .pad .preview a').each(function(obj){
            if (obj.rel) {
                obj.addEvent('click',function() {
                    NewsItemViewer.viewItemPreview(this.rel);
                    return false;
                });
            }
        });

        $$('.newslist a.up').each(function(obj) {
            obj.addEvent('click', function() {
                NewsItemViewer.scroll(-1);
                return false;
            });
        });

        $$('.newslist a.down').each(function(obj) {
            obj.addEvent('click', function() {
                NewsItemViewer.scroll(1);
                return false;
            });
        });
    },

    currentScrollElement : 0,
    scroll : function(dir) {
        if (dir != 1 && dir != -1)
            return;

        this.currentScrollElement += dir;

        if (this.currentScrollElement < 0)
            this.currentScrollElement = 0;
        else if (this.currentScrollElement >= $$('.newslist .preview').length) {
            this.currentScrollElement = $$('.newslist .preview').length - 1;
        }

        var totalscrollheight = 0;
        for(i = 0; i < this.currentScrollElement; i++) {
            totalscrollheight += $$('.newslist .preview')[i].getHeight() + 5;
        }

        new Fx.Tween($$('.newslist .pad')[0]).start('top', totalscrollheight * -1);
    },

    viewItemPreview : function(id) {

        if (typeof this.cache[ id ] != 'undefined') {
            object = this.cache[ id ];
            this.placeDataInHTML(object);
        } else {
            var jsonRequest = new Request.JSON({url: NewsItemViewer.ajaxUrl, onSuccess: function(newsitem){
                if (newsitem.error && newsitem.error[0]) {
                    alert(newsitem.error[0]);
                } else {
                    object = newsitem.objects[0];

                    NewsItemViewer.cache[ object.id ] = object;

                    NewsItemViewer.placeDataInHTML(object);
                }
            }}).get({'ent' : this.ajaxEnt, 'id' : id});
        }

    },

    weekdays : ['zondag', 'maandag', 'dinsdag', 'woensdag', 'donderdag', 'vrijdag', 'zaterdag'],
    months   : ['januari','februari','maart','april','mei','juni','juli','augustus','september','oktober','november','december'],

    placeDataInHTML : function(dataobject) {
        // title
        $$('#' + this.htmlnewsitem + ' h2')[0].innerHTML = dataobject['title'].replace("&","&amp;");

        // date
        var dt = new Date();
        dt.setTime(parseInt(dataobject['unix_date']) * 1000);

        $$('#' + this.htmlnewsitem + ' h4')[0].innerHTML = this.weekdays[ dt.getDay() ] + ' ' + dt.getDate() + ' ' + this.months[ dt.getMonth() ] + ' ' + dt.getFullYear();

        // contentsmall
        $$('#' + this.htmlnewsitem + ' p').each(function(obj, key) {
            if (key == 0)
                obj.innerHTML = dataobject.contentsmall.replace('<p>','').replace('</p>','<br />').replace('<p>','<br />').replace('</p>','<br />').replace('<p>','<br />').replace('</p>','<br />');
            else if (key > 1)
                obj.destroy();
        });

        // link
        $$('#' + this.htmlnewsitem + ' a').each(function(obj, key) {
            obj.title = 'Lees meer over ' + dataobject.title;
            obj.href  = '/nieuws/' + dataobject.id + '/' + escape(dataobject.title);
        });

        // image
        var img = $$('#' + this.htmlnewsitem + ' img')[0];

        img.src = '/image.php?ent=' + this.ajaxEnt + '&id=' + dataobject.id + '&w=100';
        img.alt = dataobject.title;
    },

    dummy : null
}

window.addEvent('domready', function() {
    NewsItemViewer.init();
});