Ext.namespace("dlr.cmp.console");
dlr.cmp.console.consolePanel = function(cfg){
	// ------------------------------------------------
	// private properties
	// ------------------------------------------------
	var that = this;
    var archiveStore = new Ext.data.Store({
		reader: new Ext.data.JsonReader({
				root: "archiveitems",
				totalProperty: "total"
			},
			Ext.data.Record.create([
				{name: "name"},
				{name: "size"},
				{name: "compressedsize"},
				{name: "type"},
				{name: "datelastmodified"}
			])
		)
    });
	var archiveWin = new Ext.Window({
		title: "&nbsp;",
		layout: "anchor",
		width: 400,
		height: 300,
		closeAction: "hide",
		items: [{
			xtype: "grid",
			anchor: "100% 0",
			autoScroll: true,
			store: archiveStore,
			cm: new Ext.grid.ColumnModel([
				{name: "name", header: "Name", sortable: true},
				{name: "size", header: "Size", renderer: Ext.util.Format.fileSize, align: "right", sortable: true},
				{name: "compressedsize", header: "Compressed Size", renderer: Ext.util.Format.fileSize, align: "right", sortable: true},
				{name: "type", header: "Type", sortable: true},
				{name: "datelastmodified", header: "Last Modified", sortable: true}
			])
		}],
		buttons:[{text: "Close", handler: function(){archiveWin.hide();}}]
	});
	var panelCfg = {
		layout: "anchor",
		frame: false,
		border: false,
		collapsible: true
	};
	// ------------------------------------------------
	// private functions
	// ------------------------------------------------
	function getCmps(){
		return {
		};
	}; // end getCmps
	function setEvents(){
		that.getUpdater().on("update", (function(){
			var deleteLnks = Ext.DomQuery.select(".archiveItemDelete", that.getEl().dom);
			var viewLnks = Ext.DomQuery.select(".archiveItemView", that.getEl().dom);
			Ext.each(deleteLnks, (function(itm, idx, len){
				Ext.get(itm).on("click", deleteArchive);
			}));
			Ext.each(viewLnks, (function(itm, idx, len){
				Ext.get(itm).on("click", viewArchive);
			}));
		}));
	}; // end setEvents
	function deleteArchive(){
		var dom = this.dom;
		Ext.Msg.confirm("Delete Archive", "Are you sure you want to do this?", (function(btn){
			if (btn != 'yes'){return;}
			Ext.Ajax.request({
				url: 'queueHelper.cfc',
				params: {
					method: "deleteArchive",
					data: '{archiveId: "'+dom.getAttribute("archiveId")+'"}'
				},
				success: (function(response, options){
					that.load({
						url: "console.cfm?which=archives&r=" + Math.random()
					});
				}),
				failure: (function(response, options){
					alert("");
				})
			});
		}));
	}; // end deleteArchive
	function viewArchive(){
		archiveWin.show();
		Ext.Ajax.request({
			url: 'queueHelper.cfc',
			params: {method: "archiveItems", data: "{archiveId: "+this.dom.getAttribute("archiveId")+"}"},
			success: (function(response, options){
				var response = Ext.decode(response.responseText);
				//_d(response)
				archiveStore.loadData(response);
				archiveWin.setTitle(response.archivename);
			}),
			failure: (function(r){})
		});
	}; // end viewArchive
	// ------------------------------------------------
	// public properties
	// ------------------------------------------------
	// ------------------------------------------------
	// public methods
	// ------------------------------------------------
	// ------------------------------------------------
	// constructor code
	// ------------------------------------------------
	Ext.apply(this, cfg);
	// apply private cfgs
	Ext.apply(this, panelCfg);
	dlr.cmp.console.consolePanel.superclass.constructor.call(this, cfg);
	// events
	that.on("afterlayout", setEvents);
};
Ext.extend(dlr.cmp.console.consolePanel, Ext.Panel);
Ext.reg("consolePanel", dlr.cmp.console.consolePanel);