var CourPanel = function(config) {
	this.config = {
		autoScroll: true,
		title: ''
	};
	CourPanel.superclass.constructor.call(this, this.config);
	
	/* Magic */

	var th = this;
	this.cubes_store = new Ext.data.JsonStore({
		url: 'actions/panel.cubes.get.json.php',
		fields: ['id', 'title', 'html_short'],
		autoLoad: false,
		listeners: {
			load: {
				fn: th.cubes_store_onload,
				scope: th
			}
		}
	});
}

Ext.extend(CourPanel, Ext.Panel, {
	cubes_cache: new Array(),
	cubes_shown: new Array(),
	req_line_cubes: new Array(),

	cubes_store_onload: function (th, records, options) {
		for (var i in records) {
			if (!(records[i] instanceof Ext.data.Record)) {
				continue;
			}
			this.cubes_cache[records[i].get('id')] = records[i];
		}
		this.resort_panel(options.req_line_cubes, options.scroll_to_cube_id, options.params['c[]']);
	},

	resort_panel: function(req_line_cubes, scroll_to_cube_id, new_cubes) {
		var leave_scroll = false;
		if (!req_line_cubes)
		{
			if (!leave_scroll) {
				leave_scroll = true;
			}
			if (this.req_line_cubes) {
				req_line_cubes = this.req_line_cubes
			} else {
				msg_error('First call of the resort_panel() has got blank req_line_cubes param');
			}
		}
		
		this.req_line_cubes = req_line_cubes;
		this.cubes_shown = new Array();
		for (var i in req_line_cubes) {
			if (this.cubes_cache[req_line_cubes[i]]) {
				this.cubes_shown[this.cubes_shown.length] = this.cubes_cache[req_line_cubes[i]].data;
			}
		}
		var old_scroll = Ext.get(this.body).getScroll();
		Templates.cube_tpl.overwrite(this.body, this.cubes_shown);
		for (var i in this.cubes_shown) {
			if (this.cubes_shown[i].id) {
				Ext.get('cube_id_' + this.cubes_shown[i].id).addClassOnOver('x-view-over');
			}
		}
		if (leave_scroll) {
			Ext.get(this.body).scrollTo('top', old_scroll.top);
			Ext.get(this.body).scrollTo('left', old_scroll.left);
		} else if (!scroll_to_cube_id) {
			Ext.get(this.body).scrollTo('top', 0);
		} else {
			// jQuery scrollTo plugin in work
			$('#' + Ext.getDom(this.body).id).scrollTo($('#cube_id_' + scroll_to_cube_id), 700);
		}
	}
});
