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',
				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){
			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
			});
			
		}
		
	}
	
	return {
		init: _init
	}
	
}();
Ext.onReady(DJIndexes.Press.init, DJIndexes.Press);
