Ext.namespace('DJIndexes.Press');
DJIndexes.Press = function(){

    var config = {
        proxy: '/DataService/v2/Press.cfc',
        max_display: 2,
        section: [{
            params: {
                method: 'getFeed',
                type: 'topic',
                value: 'press-release'
            },
            container: 'recent-press-releases',
            archiveURL: 'http://press.djindexes.com/index.php/topic/press-release/'
        }, {
            params: {
                method: 'getFeed',
                type: 'topic',
                value: 'component-change'
            },
            container: 'index-component-changes',
            archiveURL: 'http://press.djindexes.com/index.php/topic/component-change/'
        }]
    };
    
    var postTemplate = new Ext.Template('<p>', '<b>{date}</b><br/>', '{title} ', '<a href="{permalink}">more&nbsp;&raquo;</a>', '</p>');
    
    var archiveLinkTemplate = new Ext.Template('<p><a href="{url}">View archive &raquo;</a></p>');
    
    function _init(){
        Ext.each(config.section, function(s, idx){
            var pr = Ext.get(s.container);
            if (pr) {
                getData(s.params);
            }
        });
        
    }
    
    function getData(params){
        Ext.Ajax.request({
            method: 'GET',
            disableCaching: false,
            url: config.proxy,
            params: params,
            callback: handleData,
            scope: this
        });
    }
    
    function handleData(options, success, response){
    
        if (success) {
        
            var id = response.tId;
            var data;
            try {
                data = Ext.decode(response.responseText);
            } 
            catch (e) {
            }
            
            Ext.each(data, function(item, i){
                if (i <= config.max_display - 1) {
                    postTemplate.append(config.section[id].container, item);
                }
            });
            
            archiveLinkTemplate.append(config.section[id].container, {
                url: config.section[id].archiveURL
            });
            updateCss();
        }
        
    }
    function updateCss(){
        var pr = Ext.get(config.section[0].container), cc = Ext.get(config.section[1].container);
        if (pr && cc) {
            if (pr.dom.clientHeight < cc.dom.clientHeight) {
                cc.addClass('lborder');
                cc.removeClass('dl145');
                cc.addClass('dl144');
                pr.removeClass('rborder');
            }
            else {
                cc.removeClass('lborder');
                cc.removeClass('dl144');
                cc.addClass('dl145');
                pr.addClass('rborder');
            }
        }
    }
    return {
        init: _init
    }
    
}();
Ext.onReady(DJIndexes.Press.init, DJIndexes.Press);

