/**
 * PST.cmp.RandomPic
 * @desc Shows a random picture
 * @author Brandon Grady
 */
Ext.ns('PST.cmp');

PST.cmp.News = Ext.extend(Ext.grid.GridPanel, {
    constructor: function(config){
		if (!config) {
			config = {};
		}

		// Definitions
		this.weeksBack = 8;
		if (config.weeksBack && config.weeksBack !== null) {
			this.weeksBack = config.weeksBack;
		}
		this.newsUrl = 'ctrl.php?c=post&verb=list&weeks_back=' + this.weeksBack;
		
        // Defaults
		Ext.applyIf(config, {
			title: "Latest News (click a row for details)",
			id: "newsGrid",
			autoHeight: false,
			height: 300
		})
		
        PST.cmp.News.superclass.constructor.call(this, config);
    },
    
    initComponent: function() {
		Ext.apply(this, {
			loadMask: true,
			store: {
				xtype: 'jsonstore',
				storeId: 'newsStore',
				autoDestroy: true,
				autoLoad: true,
				url: this.newsUrl,
				root: '',
				fields: ['category','summary','author','body','date','thumbnail','picture']
			},
			cm: new Ext.grid.ColumnModel([
				{id: 'category', header: 'Category', dataIndex: 'category', width: 55},
				{id: 'summary', header: 'Summary', dataIndex: 'summary'}
			]) ,
			autoExpandColumn: "summary"
		});
		
        // Call the superclass.
        PST.cmp.News.superclass.initComponent.call(this);
		
		// Tie in the rowclick
		this.on('rowclick', function(grid, rowIndex, e) {
			var row = grid.store.getAt(rowIndex);
			var summary = row.get("summary");
			var author = row.get("author");
			var category = row.get("category");
			var body = row.get("body");
			var dt = row.get("date").us_dt;
			var pdir = row.get("picture").directory;
			var thumbnail = "";
			
			if (row.get("thumbnail") != "") {
				thumbnail = 
					"<a target='_blank' href='viewPictures.php?pdir=content/pictures/" + pdir + "'><img style='float:right;' src='" + row.get("thumbnail") + "'/></a>";
			}
			
			var message = 
				thumbnail + 
				'Posted in <i>' + category + '</i> by <i>' + author + '</i> on <i>' + dt + '</i>' + 
				'<br/><br/>' +
				body;
			
			Ext.Msg.show({
				title: 'Post Details',
				width: 400,
				buttons: Ext.MessageBox.OK,
				msg: message 
			});
		});
    }
});
