/*
@name: /htdocs/js/lib/oggetti/gridwindow.js
@desc: Componente javascript gridwindow
@authors: Marco Biondi
@lastauthor: Marco Biondi
@require: Prototype 1.4.0, moo 1.2.2
*/
var GridWindow = Class.create();
GridWindow.prototype = {
	initialize: function(divID, globalName, options) {
		this._container = $(divID);
		this._gName = globalName;
		this.options = {
				gridStyle:						'',
				gridWidth:						'150px',
				titleStyle:						'',
				titleContent:					'gridWindow Title ['+divID+' '+globalName+']',
				cellStyle:						'',
				cellWidth:						'50px',
				cellHeight:						'50px',
				numRows:							3,
				numCols:							3,
				bodyOpen:							true,
				openSpeed:						400,
				footerStyle:					'',
				footerContent:				'gridWindow footer',
				renderingFunction:		this._defCellRenderer.bind(this)
				};
		Object.extend(this.options, options || {});

		this._firstRecord = 0;
		this._totCells = this.options.numRows*this.options.numCols;
		this._cells = new Array();

		this._container.innerHTML = "";
		this.dataSource = null;
		this._transition = null;
	},

	destroy: function()
	{
		this.releaseDataSource();
	},

	releaseDataSource: function() {
		if(this.dataSource)
			this.dataSource.delListener("onDataChange", this);

		this.dataSource = null;
	},

	bindDataSource: function(ds) {
		if(this.dataSource)
			this.dataSource.delListener("onDataChange", this);

		this.dataSource = ds;
		this._firstRecord = 0;
		this.dataSource.addListener("onDataChange", this);
	},

	getOffset: function() {
		return(this._firstRecord);
	},

	setOffset: function(ofs, force) {
		if(this.dataSource)
		{
			if(force)
				this._firstRecord = ofs;
			else
				this._firstRecord = Math.min(this.dataSource.getLength()-this._totCells,Math.max(0,parseInt(ofs)));
		}
		else
			this._firstRecord = 0;

		return(this._firstRecord);
	},

	onDataChange: function(objEvt) {
		switch(objEvt.dEvType)
		{
			case "update":
				if(objEvt.from >= this._firstRecord && objEvt.from < (this._firstRecord+_totCells))
				{
					var dset = this.dataSource.getItemAt(objEvt.from);
					var rIndex = objEvt.from-this._firstRecord;
					this.options.renderingFunction(dset, this._cells[rIndex], objEvt.from);
				}
				break;
			default:
				this.draw();
				break;
		}
	},

	create: function() {
		var foo, bar, main, cRow, cell, i, j, spacer;

		var baseULstyle = 'display: block; margin: 0px; padding: 0px; list-style-type: none;';
		var innerULstyle = baseULstyle +' height: '+this.options.cellHeight+';';
		var rowLIstyle = 'clear: left; margin: 0px; padding: 0px; display: inline;';
		var cellLIstyle = 'display: block; float: left; width: '+this.options.cellWidth+'; height: '+this.options.cellHeight+'; overflow: hidden;';

		this._container.className = this.options.gridStyle;
		this._container.style.width = this.options.gridWidth;

		//Creo il titolo
		foo = document.createElement('div');
		foo.className = this.options.titleStyle;
		if(typeof(this.options.titleContent) == 'string')
			foo.innerHTML = this.options.titleContent;
		else
			foo.appendChild(this.options.titleContent);
		this._container.appendChild(foo);

//		spacer = document.createTextNode('\n');
//		this._container.appendChild(spacer);

		//Creo il contenitore delle celle
		main = document.createElement('ul');
//		main.className = 'ulBase';
		main.style.cssText = baseULstyle; // + ' background-color: #00FF00';

		for(i=0; i<this.options.numRows; i++)
		{
			//Creo la riga
			cRow = document.createElement('li');
//			cRow.className = 'liBase';
			cRow.style.cssText = rowLIstyle;
			foo = document.createElement('ul');
//			foo.className = 'ulBase';
			foo.style.cssText = innerULstyle;

			for(j=0; j<this.options.numCols; j++)
			{
				//Creo le celle
				bar = document.createElement('li');
//				bar.className = 'liCella';
				bar.style.cssText = cellLIstyle;
				cell = document.createElement('div');
				cell.className = this.options.cellStyle;

				this._cells.push(cell);

				bar.appendChild(cell);

//				spacer = document.createTextNode('\n\t\t\t');
//				foo.appendChild(spacer);

				foo.appendChild(bar);
			}

//			spacer = document.createTextNode('\n\t\t');
//			cRow.appendChild(spacer);

			cRow.appendChild(foo);

//			spacer = document.createTextNode('\n\t');
//			main.appendChild(spacer);

			main.appendChild(cRow);
		}

		this._transition = new fx.Height(main, {duration: this.options.openSpeed});
		if(!this.options.bodyOpen)
			this._transition.hide();

		this._container.appendChild(main);

//		spacer = document.createTextNode('\n');
//		this._container.appendChild(spacer);

		//Creo il footer
		foo = document.createElement('div');
		foo.className = this.options.footerStyle;
//		foo.style.cssText = 'clear: left;';
		if(typeof(this.options.footerContent) == 'string')
			foo.innerHTML = this.options.footerContent;
		else
			foo.appendChild(this.options.footerContent);
		this._container.appendChild(foo);

//		spacer = document.createTextNode('\n');
//		this._container.appendChild(spacer);
	},

	draw: function() {
		var i, res, dset;
		for(i=0; i<this._totCells; i++)
		{
			dset = this.dataSource.getItemAt(i+this._firstRecord);
			res = this.options.renderingFunction(dset, this._cells[i], i+this._firstRecord);
			if(typeof(res) != 'undefined')
			{
				res = String(res);
				this._cells[i].innerHTML = res;
			}
		}
	},

	_defCellRenderer: function(dati, cell, recordIndex) {
		var res = '['+recordIndex+']';
		var i;

		if(dati)
		{
			res += "<br />";
			for(i in dati)
				res += i+": "+String(dati[i]).escapeHTML()+"<br />";
		}

		return res;
	},

	bodyOpen: function(status) {
		if(status == -1)
			this._transition.toggle();
		else if(status)
			this._transition.custom(0,this._transition.el.iniWidth);
		else
			this._transition.hide();
	}
}
