$package("js.ui.list");

js.ui.list.AbstractListView= function (){
	if(js.ui.list.Columns)
		this.columns =  new js.ui.list.Columns();
}
js.ui.list.AbstractListView.prototype = new js.ui.list.AbstractListBox();

js.ui.list.AbstractListView.prototype.getNativeClass = function (){
	return "js.ui.list.AbstractListView";
}

/*js.ui.list.AbstractListView.prototype.bindUI = function (){
	this.__superBindUI = js.ui.list.AbstractListBox.prototype.bindUI;
	this.__superBindUI();
	this.container = this.ui.getElementsByTagName("table")[0];
}*/
js.ui.list.AbstractListView.prototype.columns = null;

js.ui.list.AbstractListView.prototype.multiSelected = true;

js.ui.list.AbstractListView.ViewType = {DETAILINFO:0, BIGICON:1, SMALLICON:2, ICON:3, LIST:4, LARGE:5};

js.ui.list.AbstractListView.Arrangement = {TOP:0, LEFT:1};

js.ui.list.AbstractListView.prototype.arrangement = js.ui.list.AbstractListView.Arrangement.TOP;

js.ui.list.AbstractListView.prototype.viewType = 1;//js.ui.list.AbstractListView.ViewType.BIGICON;

js.ui.list.AbstractListView.prototype.cols = 5;

js.ui.list.AbstractListView.prototype.setViewType = function(value){
	this.viewType = value;
	this.paintHeader();
	this.clear();
	if(this.refresh) this.refresh();
}
js.ui.list.AbstractListView.prototype.setOpenType = function(value){
	this.openType = value;
}

js.ui.list.AbstractListView.prototype.createRow = function(){
	if(!this.cols)
		this.cols = 5;
	var row,cell;
	row = this.body.insertRow(-1);
	row.vAlign = "top";
	for(var i=0; i<this.cols; i++){
		cell = row.insertCell(-1);
	}
}

js.ui.list.AbstractListView.prototype.createCol = function(){
	if(!this.cols)
		this.cols = 5;
	var row,cell;
	if(this.body.rows.length<=0){
		for(var i=0; i<this.cols; i++){
			row = this.body.insertRow(-1);
			row.vAlign = "top";
		}	
	}
	for(var i=0; i<this.cols; i++){
		row = this.body.rows[i];
		cell = row.insertCell(-1);
	}
}

js.ui.list.AbstractListView.prototype.addItem = function(item){
	this.getModel().addElement(item);
	var table = this.getContainer();
	var renderer = this.createCellRenderer();
	if(this.viewType==0){
		table.appendChild(renderer.getUI());
	}
	else{
		if(renderer.size<=16) renderer.size = 32;
		var rowIdx = Math.floor((this.getModel().size() / this.cols)-0.0001);
		var colIdx = (this.getModel().size()-1) % this.cols;
		if(!this.body){
			this.body = document.createElement("tbody");
			this.container.appendChild(this.body);
		}
		if(this.arrangement==0 && rowIdx>=this.body.rows.length)
			this.createRow();
		else if(this.arrangement==1 && (this.body.rows.length<=0 || rowIdx>=this.body.rows[0].cells.length)){
			this.createCol();
		}
		var cell;
		if(this.arrangement==0)
			cell = this.body.rows[rowIdx].cells[colIdx];
		else
			cell = this.body.rows[colIdx].cells[rowIdx];
		cell.appendChild(renderer.getUI());
		cell.renderer = renderer;
	}
	renderer.setValue(item);
	this.renderers.addItem(renderer);
}

js.ui.list.AbstractListView.prototype.addColumn = function(column){
	if(!this.thead){
		this.thead = this.getUI().getElementsByTagName("tbody")[0];
		this.theadrow = this.thead.rows[0];
	}
	var col = this.theadrow.insertCell(-1);
	if(this.columns.size()==0 && !column.colSpan){
		col.colSpan = 3;
	}
	if(column.colSpan)
		col.colSpan = column.colSpan;
	if(column.width)
		col.width = column.width;
	var	renderer = column.createCellRenderer();
	col.appendChild(renderer.getUI());
	renderer.setValue(column.caption);
	this.columns.add(column);
}

js.ui.list.AbstractListView.prototype.createCellRenderer = function(){
	if(!this.renderClass) this.renderClass = js.ui.list.AbstractListItemRenderer;
	var renderer = new this.renderClass(this);
	renderer.style = this.viewType;
	return renderer;
}
js.ui.list.AbstractListView.prototype.clear = function(){
	this.getModel().clear();
	this.renderers.clear();
	this.body = null;
	var tb = this.getContainer();
    for(var i=tb.childNodes.length-1; i>1; i--){   
       tb.removeChild(tb.childNodes[i]);   
    }   
}
js.ui.list.AbstractListView.prototype.paintHeader = function(){
	if(this.viewType==1 && this.thead){
		this.thead.style.display = "none";
	}
	else if(this.thead){
		this.thead.style.display = "";
	}
}
js.ui.list.AbstractListView.prototype.paint = function(){
}


