
var gridProcCmd_none = 0;

var gridProcCmd_layout = 105;

// head

var gridProcCmd_rowCaptionHeading = 1;
var gridProcCmd_colContainer = 101;
var gridProcCmd_colGroupCaption = 2;
var gridProcCmd_colSelect = 3;
var gridProcCmd_colComment = 4;
var gridProcCmd_colCaption = 5;
var gridProcCmd_colSubCaption = 6;
var gridProcCmd_headPadding = 7;

// body

var gridProcCmd_bodyContainer = 102;
var gridProcCmd_cellContainer = 103;
var gridProcCmd_rowContainer = 104;

var gridProcCmd_rowGroupCaption = 8;
var gridProcCmd_rowSelect = 9;
var gridProcCmd_rowComment = 10;
var gridProcCmd_rowCaption = 11;
var gridProcCmd_funcIcon = 12;
var gridProcCmd_cellContent = 13;
var gridProcCmd_vertScrollBar = 14;
var gridProcCmd_cell = 19;

// foot

var gridProcCmd_rowCaptionPadding = 15;
var gridProcCmd_horzScrollBar = 16;
var gridProcCmd_footPadding = 17;
var gridProcCmd_controlBar = 18;



function GridProcessPipe( id, gridInfo, gridRef, grid, debugLog )
{


	this.construct = function( id, gridInfo, gridRef, grid, debugLog )
	{
		this._id = id;
		this._cmd = new Queue();
		this._dbg = debugLog;

		this._gridRef = gridRef;
		this._gridInfo = gridInfo;
		this._grid = grid;

	}

	this._started = false;

	this._id = '';
	this._dbg = null;

	this._stopCallback = null;
	this._doneCallback = null;
	this._timer = null;
	this._cmd = null;
	this._lastCmd = null;

	this._atomSwitch = false;
	this._lastAtomAdded = false;

	this._gridRef = null;
	this._gridInfo = null;

	this._grid = null;

	this.getId = function()
	{
		return this._id;
	}

	this.queueCmd = function( cmd, opt )
	{
		this._cmd.queue({ 'cmd': cmd, 'opt': opt, atom: this._lastAtomAdded });

		this._lastAtomAdded = !this._lastAtomAdded;
	}


	this.start = function()
	{


		if( this._started == false ) {

			//this.queueCmd( sfCmd_stop, null, null ); 
/*
			// --- DEBUG
			if( this._dbg ) {
				var dbg = '';
				this._dbg.message( this.getId(), 'start', 'Started' + dbg, 'heading' );
				this._dbg.indent();
			}
			// --- DEBUG	
*/
			if( this._cmd.count() > 0 ) {

				var self = this;

				this._timer = $.timer( 1, function( timer ) {
					if( self._started == false ) {
						self._started = true;
						self._timer = timer;
					}

					self.tick( self, timer );
				});
			}
			else {
				if( this._doneCallback != null ) {
					this._doneCallback( this );
					this._doneCallback = null;
				}
			}

		 }

	}

	this.stop = function()
	{

/*
		if( this._dbg ) {
			// --- DEBUG
			this._dbg.undent();
			this._dbg.message( this.getId(), 'process', 'Stopped.', 'priority' );
			// --- DEBUG
		}
*/

		if( this._started == true ) {
			//window.status += ( ' STOP ' + this.getId() );


			this._started = false;

			this._timer.stop();
			this._timer = null;

			if( this._stopCallback != null ) {
				this._stopCallback( this );
				this._stopCallback = null;
			}

			if( this._cmd.count() == 0 ) {

				if( this._doneCallback != null ) {
					this._doneCallback( this );
					this._doneCallback = null;
				}
			}

		}
	}

	this.onStop = function( callback )
	{
		this._stopCallback = callback;
	}

	this.onDone = function( callback )
	{
		this._doneCallback = callback;
	}

	this.process = function( self )
	{	
		var cmd = self._cmd.peek( 0 );

		if( cmd ) {

			if( cmd.atom == self._atomSwitch ) {

		
				try {
					// --- table ---

					if( cmd.cmd == gridProcCmd_layout ) {
						self._grid._procLayout( self._gridInfo, self._gridRef );
					}
			


					// --- head ---

					//alert( cmd.obj.getId() );

					else if( cmd.cmd == gridProcCmd_rowCaptionHeading ) {
						self._grid._procRowCaptionHeading( self._gridInfo, self._gridRef );
					}

					else if( cmd.cmd == gridProcCmd_colContainer ) {
						self._grid._procColContainer( self._gridInfo, self._gridRef );
					}


					else if( cmd.cmd == gridProcCmd_colGroupCaption ) {
						self._grid._procColGroupCaption( self._gridInfo, self._gridRef, cmd.opt.colGrpIndex );
					}

					else if( cmd.cmd == gridProcCmd_colSelect ) {
						self._grid._procColSelect( self._gridInfo, self._gridRef, cmd.opt.colGrpIndex, cmd.opt.colIndex );
					}

					else if( cmd.cmd == gridProcCmd_colComment ) {
						self._grid._procColComment( self._gridInfo, self._gridRef, cmd.opt.colGrpIndex, cmd.opt.colIndex  );
					}

					else if( cmd.cmd == gridProcCmd_colCaption) {
						self._grid._procColCaption( self._gridInfo, self._gridRef, cmd.opt.colGrpIndex, cmd.opt.colIndex  );
					}

					else if( cmd.cmd == gridProcCmd_colSubCaption ) {
						self._grid._procColSubCaption( self._gridInfo, self._gridRef, cmd.opt.colGrpIndex, cmd.opt.colIndex  );
					}

					else if( cmd.cmd == gridProcCmd_headPadding ) {
						self._grid._procHeadPadding( self._gridInfo, self._gridRef );
					}

					// --- body ---

					else if( cmd.cmd == gridProcCmd_bodyContainer ) {
						self._grid._procBodyContainer( self._gridInfo, self._gridRef );
					}

					else if( cmd.cmd == gridProcCmd_rowGroupCaption ) {
						self._grid._procRowGroupCaption( self._gridInfo, self._gridRef, cmd.opt.rowGrpIndex );
					}

					else if( cmd.cmd == gridProcCmd_cellContainer ) {
						self._grid._procCellContainer( self._gridInfo, self._gridRef, cmd.opt.rowGrpIndex );
					}

					else if( cmd.cmd == gridProcCmd_rowSelect ) {
						self._grid._procRowSelect( self._gridInfo, self._gridRef, cmd.opt.rowGrpIndex, cmd.opt.rowIndex );
					}

					else if( cmd.cmd == gridProcCmd_rowComment ) {
						self._grid._procRowComment( self._gridInfo, self._gridRef, cmd.opt.rowGrpIndex, cmd.opt.rowIndex );
					}

					else if( cmd.cmd == gridProcCmd_rowCaption ) {
						self._grid._procRowCaption( self._gridInfo, self._gridRef, cmd.opt.rowGrpIndex, cmd.opt.rowIndex );
					}

			

					else if( cmd.cmd == gridProcCmd_funcIcon ) {

						self._grid._procFuncIcon(
							self._gridInfo,
							self._gridRef,
							cmd.opt.colGrpIndex,
							cmd.opt.colIndex,
							cmd.opt.rowGrpIndex,
							cmd.opt.rowIndex,
							cmd.opt.iconIndex
						);

					}

					else if( cmd.cmd == gridProcCmd_cellContent ) {

							self._grid._procCellContent(
							self._gridInfo,
							self._gridRef,
							cmd.opt.colGrpIndex,
							cmd.opt.colIndex,
							cmd.opt.rowGrpIndex,
							cmd.opt.rowIndex
						);

					}

					else if( cmd.cmd == gridProcCmd_cell ) {

						self._grid._procCell(
							self._gridInfo,
							self._gridRef,
							cmd.opt.cellIndex,
							cmd.opt.colGrpIndex,
							cmd.opt.colIndex,
							cmd.opt.rowGrpIndex,
							cmd.opt.rowIndex
						);

					}


					else if( cmd.cmd == gridProcCmd_rowContainer ) {
						self._grid._procRowContainer( self._gridInfo, self._gridRef );
					}

					else if( cmd.cmd == gridProcCmd_vertScrollBar ) {

						self._grid._procVertScrollBar( self._gridInfo, self._gridRef );

					}

					// --- foot ---

					else if( cmd.cmd == gridProcCmd_rowCaptionPadding ) {
						self._grid._procRowCaptionPadding( self._gridInfo, self._gridRef );
				}	

					else if( cmd.cmd == gridProcCmd_horzScrollBar ) {
						self._grid._procHorzScrollBar( self._gridInfo, self._gridRef );
					}

					else if( cmd.cmd == gridProcCmd_footPadding ) {
						self._grid._procFootPadding( self._gridInfo, self._gridRef );
					}

					else if( cmd.cmd == gridProcCmd_controlBar ) {
						self._grid._procControlBar( self._gridInfo, self._gridRef );
					}

				}
				catch( ex ) {
				

/*				
					if( this._dbg ) {
						this._dbg.message( cmd.obj.getId(), 'process','<p /> ' + ex, 'error' );
					}
*/
					self.stop();
					throw ex + ' [ from GridProcessPipe.process() ]';
				}
				self._atomSwitch = !self._atomSwitch;

				self._cmd.advance();
			}
		
			return cmd;
		}

	}

	this.tick = function( self, timer )
	{

        	if( self._started == true ) {
	        	if( self._cmd.count() > 0 ) {

				var lastCmd = gridProcCmd_none;

				//while( ( self._started == true ) && ( self._cmd.count() > 0 ) && ( lastCmd != sfCmd_update ) ) {
				for( var tmp = 0; tmp < 2; tmp++ ) {
					lastCmd = self.process( self )
				}
				//}
			}
			else {
			    self.stop();
			}

		}


	}

	this.construct( id, gridInfo, gridRef, grid, debugLog );

}


