dojo.require("dijit.layout.ContentPane");

/**
 * @author DREDroot
 *
 */
dojo.declare("dojom.widgets.Grid",null, 
{	
	id: null,
	domNode: null,
	_contentPane: null,
	
	
	model : null,
	_columnsList:[],
	
	//_header: '<div class="contentDiv"><div class="contentDivTable"><table class="contentTable">',
	_header: '<div class="contentDivTable"><table class="contentTable">',
	_thead: '',
	_tbody: '',
	_footer: '</table></div></div>',
	
	previousItem: null,
		
	constructor: function(arguments)
	{
		var obj = this;
		this.model = new dojom.widgets.GridModel();	
		dojo.connect(this.model,'allChange',this,'refresh')
		
		this._contentPane = new dijit.layout.ContentPane(arguments);
		this.id = this._contentPane.id;
		this.domNode = this._contentPane.domNode;
		
		window[this.id+'_onRowClick'] = function(inIndex,trItem){obj.onRowClick(inIndex,trItem);};
		window[this.id+'_onCellClick'] = function(inIndex,trItem){obj.onCellClick(inIndex,trItem);};
		window[this.id+'_onRowDblClick'] = function(inIndex,trItem){obj.onRowDblClick(inIndex,trItem);};
		window[this.id+'_onCellDblClick'] = function(inIndex,trItem){obj.onCellDblClick(inIndex,trItem);};
		
		this.startup();
	},
		
	destroy: function()
	{
		this._contentPane.destroy();
	},
	
	setColumnList: function(columns)
	{
		this._columnsList = columns;
		this._createTHead();
		this.refresh();
	},
	
	_createTHead: function()
	{
		var cols = this._columnsList;
		if(!cols)
			return;
				
		var thead = '<thead class="contentTableHeader">';		
		for (var x = 0; x<cols.length; x++){ 
			if(cols[x]["name"]&&cols[x]["name"]!="")
			{
				thead +='<th ';
				thead += (cols[x]["width"] ? 'width="'+cols[x]["width"]+'"' : '');	
				thead += (cols[x]["align"] ? 'align="'+cols[x]["align"]+'"' : '');		
				thead += (cols[x]["style"] ? 'style="'+cols[x]["style"]+'"' : '');	
				thead += (cols[x]["label"] ? '>'+cols[x]["label"]+'</th>' : '></th>');
			}
		}				
		thead +='</thead>';
				
		this._thead = thead;	
		return thead;
	},
	
	setData: function(data)
	{
		this.model.setData(data);
		this.refresh();
	},
	
	_createTBody: function()
	{	
		var rows = this.model.data;
		var cols = this._columnsList;
		
		if(!cols || !rows)
			return;
		
		var tbody = '<tbody class="contentTableBody">';
		for (var x = 0; x<rows.length; x++){ 
			tbody +='<tr class="rowNormal" rowIndex='+x+' onclick="'+this.id+'_onRowClick({\'rowIndex\':'+x+',\'trItem\':this});" '
				+' ondblclick="'+this.id+'_onRowDblClick({\'rowIndex\':'+x+',\'trItem\':this});">';

			for (var y = 0; y<cols.length; y++)
			{ 
				tbody +='<td onclick="'+this.id+'_onCellClick({\'colIndex\':'+y+',\'rowIndex\':'+x+',\'tdItem\':this});" '
					+'ondblclick="'+this.id+'_onCellDblClick({\'colIndex\':'+y+',\'rowIndex\':'+x+',\'tdItem\':this});"';
				
				if(cols[y]["style"]!= null)
					tbody += 'style="'+cols[y]["style"]+'"';
				
				tbody +='>';
				
				if(cols[y]["get"])
					tbody +=cols[y]["get"]({'rowIndex':x,'grid':this});
				else if(cols[y]["value"])
		   			tbody +=cols[y]["value"];
				else if(cols[y]["field"]&&cols[y]["field"]!="")
			   		tbody +=rows[x][cols[y]["field"]];		   		
		   		else
			   		tbody +=rows[x][y];
		   		
		   		tbody +='</td>';
		   	}		   	
		   	tbody +='</tr>';
		}
		tbody +='</tbody>';
		this._tbody = tbody;
		
		return tbody;
	},
	
	startup: function()
	{			
		this._createTHead();		
		this.refresh();
	},
	
	refresh: function()
	{
		this._createTBody();
		this._contentPane.setContent(this._header+this._thead+this._tbody+this._footer);
	},
	
	onRowClick: function(e)
	{
		
		if(null!=this.previousItem)
			this.previousItem.className = "rowNormal";
			
		if(e.trItem.className == "rowNormal"){
			e.trItem.className = "rowClicked";
		}else{
			e.trItem.className = "rowNormal";
		}
		
		this.previousItem = e.trItem;
		
		return true;
	},
	
	onRowDblClick: function(e)
	{
		
		if(null!=this.previousItem)
			this.previousItem.className = "rowNormal";
			
		if(e.trItem.className == "rowNormal"){
			e.trItem.className = "rowClicked";
		}else{
			e.trItem.className = "rowNormal";
		}
		
		this.previousItem = e.trItem;
		
		return true;
	},
	
	onCellClick: function(e)
	{
		return true;
	},	
	
	onCellDblClick: function(e)
	{
		return true;
	}	
	
});

dojo.declare("dojom.widgets.GridModel", null, 
{
	data: [],
	
	constructor: function ()
	{
	},
	
	setData: function(data)
	{
		this.data = data;
		this.allChange();
	},
	
	insert: function(row)
	{
		this.data.push(row)
		this.allChange();
	},
	
	remove: function(rows)
	{		
		if(!rows || rows.length <1)
			return;
			
		for(var j=rows.length-1;j>=0;j--)
			this.data.splice(rows[j],1);
		
		
		this.allChange();
	},
	
	allChange: function()
	{
	}
		
});
