/**
 * Converts numeric value to string value ("expected" field)
 *
 * @param	value	Numeric value
 * @return	void
 */
function tx_calispenguinclub_expectedRenderer(value) {
	return (value == 0 ? tx_calispenguinclub_listStrings.expected_boy :
			(value == 1 ? tx_calispenguinclub_listStrings.expected_girl :
			(value == 2 ? tx_calispenguinclub_listStrings.expected_many :
				tx_calispenguinclub_listStrings.expected_unknown)));
}

/**
 * Creates a grid from the passed data
 *
 * @param	data	JSON data to show in the grid
 * @return	void
 */
function tx_calispenguinclub_createList(data) {

	Ext.get('tx-calispenguinclub-list').dom.innerHTML = '';

	// Load data
	var store = new Ext.data.SimpleStore({
		fields: [
			{ name: 'username' },
			{ name: 'birth_date', type: 'date', dateFormat: 'U' },
			{ name: 'expected' },
			{ name: 'comment' }
		]
	});
	store.loadData(data.data);

	// Create grid
	var grid = new Ext.grid.GridPanel({
		autoExpandColumn: 'comment',
		autoHeight: true,
		columns: [
			{ id:'username', header: tx_calispenguinclub_listStrings.username, width: 150, dataIndex: 'username' },
			{ header: tx_calispenguinclub_listStrings.birth_date, width: 155, renderer: Ext.util.Format.dateRenderer('d.m.Y'), dataIndex: 'birth_date' },
			{ header: tx_calispenguinclub_listStrings.expected, width: 65, renderer: tx_calispenguinclub_expectedRenderer, dataIndex: 'expected' },
			{ id: 'comment', header: tx_calispenguinclub_listStrings.comment, xwidth: 150, dataIndex: 'comment' }
		],
		enableHdMenu: false,
		renderTo: 'tx-calispenguinclub-list',
		stateful: false,
		store: store,
		stripeRows: true,
		title: tx_calispenguinclub_listStrings.list_title,
		width: 560
	});

	grid.render();
}

/**
 * Creates a form to add/edit/delete data
 *
 * @param	data	Initial form data
 * @return	void
 */
function tx_calispenguinclub_createForm(data) {
	var d = new Date();
	d.setTime(1000*data.birth_date);
	var form = new Ext.form.FormPanel({
		animCollapse: true,
		autoHeight:true,
		bodyBorder: true,
		bodyStyle: 'padding: 10px 7px 7px 7px',
		border: true,
		buttons: [{
			cls: 'x-btn-text-icon',
			handler: function() {
				var actionField = Ext.getCmp('tx-calispenguinclub-action');
				actionField.setValue('save');
				form.getForm().submit({
					waitMsg: tx_calispenguinclub_formStrings.sending_data,
					failure: tx_calispenguinclub_formFailure,
					success: tx_calispenguinclub_formSuccess
				});
			},
			icon: '/typo3conf/ext/calis_penguinclub/resources/template/tick.png',
			text: tx_calispenguinclub_formStrings.submitBtn
		}, {
			cls: 'x-btn-text-icon',
			handler: function() {
				var actionField = Ext.getCmp('tx-calispenguinclub-action');
				actionField.setValue('delete');
				form.getForm().submit({
					waitMsg: tx_calispenguinclub_formStrings.sending_data,
					failure: tx_calispenguinclub_formFailure,
					success: tx_calispenguinclub_formSuccess
				});
			},
			icon: '/typo3conf/ext/calis_penguinclub/resources/template/cross.png',
			text: tx_calispenguinclub_formStrings.deleteBtn
		}],
		collapseFirst: true,
		collapsed: true,
		collapsible: true,
		defaults: {
			bodyStyle: 'padding: 3px 5px'
		},
		frame: true, // frame encapsulates buttons too!
		items: [{
			xtype: 'hidden',
			name: 'eID',
			value: 'calis_penguinclub'
		}, {
			xtype: 'hidden',
			name: 'id',
			value: data.id
		}, {
			xtype: 'hidden',
			name: 'L',
			value: data.L
		}, {
			xtype: 'hidden',
			id: 'tx-calispenguinclub-action',
			name: 'action',
			value: 'save'
		}, {
			xtype: 'datefield',
			allowBlank: false,
			blankText: tx_calispenguinclub_formStrings.error_datenotblank,
			fieldLabel: tx_calispenguinclub_formStrings.birth_date,
			format: 'd.m.Y',
			id: 'tx-calispenguinclub-date',
			name: 'birth_date',
			value: d
		}, {
			xtype: 'combo',
			columns: 4,
			editable: false,
			fieldLabel: tx_calispenguinclub_formStrings.expected,
			hiddenName: 'expected',
			listWidth: 120,
			mode: 'local',
			store : [
					['0', tx_calispenguinclub_formStrings.expected_boy],
					['1', tx_calispenguinclub_formStrings.expected_girl],
					['2', tx_calispenguinclub_formStrings.expected_many],
					['3', tx_calispenguinclub_formStrings.expected_unknown]
			],
			value: data.expected,
			width: 120,
			triggerAction : 'all'
		}, {
			xtype: 'textfield',
			allowBlank: true,
			fieldLabel: tx_calispenguinclub_formStrings.comment,
			maxLength: 250,
			maxLengthText: tx_calispenguinclub_formStrings.error_texttoolong,
			name: 'comment',
			value: data.comment,
			width: 350
		}],
		labelWidth: 150,
		method: 'post',
		renderTo: 'tx-calispenguinclub-form',
		style: 'margin-bottom: 10px',
		title: tx_calispenguinclub_formStrings.form_title,
		titleCollapse: true,
		url: '/index.php',
		width: 563
	});
}

function tx_calispenguinclub_formSuccess(form, action) {
	try {
		var result = eval('(' + action.response.responseText + ')');
		if (result.error == '') {
			tx_calispenguinclub_createList(result.data);
		}
		else {
			Ext.Msg.alert(tx_calispenguinclub_formStrings.error, result.error);
		}
	}
	catch(e) {
		Ext.Msg.alert(tx_calispenguinclub_formStrings.error, e.message);
	}
}

function tx_calispenguinclub_formFailure() {
	Ext.Msg.alert(tx_calispenguinclub_formStrings.error, tx_calispenguinclub_formStrings.system_error);

}
