Ext.namespace('DJIndexes.Press');
DJIndexes.Press = function () {
    var config = {
        proxy: '/DataService/v2/Press.cfc',
        max_display: 3,
        section: [{
            params: {
                method: 'getFeed',
                type: 'topic',
                value: 'press-release'
            },
            container: 'recent-press-releases',
			title: 'Press Releases',
            archiveURL: 'http://press.djindexes.com/index.php/topic/press-release/'
        }, {
            params: {
                method: 'getFeed',
                type: 'topic',
                value: 'component-change'
            },
            container: 'index-component-changes',
			title: 'Component Changes',
            archiveURL: 'http://press.djindexes.com/index.php/topic/component-change/'
        }]
    };
    var postTemplate = new Ext.Template('<p><b>{date}</b>&ndash;<a href="{permalink}">{title}</a></p>');
    var archiveLinkTemplate = new Ext.Template('<h3><a href="{url}" title="{title}" alt="{title}">{title}</a></h3>');
    var marketReportsLinkTemplate = new Ext.Template('<h3><a href="http://press.djindexes.com/index.php/market-reports/" title="Market Reports" alt="Market Reports">Market Reports &raquo;</a></h3><p></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 ? response.tId : 0;
            var data;
            try {
                data = Ext.decode(response.responseText);
            } catch (e) {}
            archiveLinkTemplate.append(config.section[id].container, {
				title: config.section[id].title,
                url: config.section[id].archiveURL
            });
            Ext.each(data, function (item, i) {
                if (i <= config.max_display - 1) {
                    postTemplate.append(config.section[id].container, item);
                }
            });
			marketReportsLinkTemplate.append(config.section[id].container,{});
//            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);
