/*
 * TODO: Copyright (c) 2007 Denis Morozov <dmorozov@exadel.com>
 *
 * ...
 */
ClientUILib.declarePackage("ClientUI.controls.grid.GridFooter");

ClientUILib.requireClass("ClientUI.common.box.Box");

/*
/* GridHeader.js - Grid control header pane
 * TODO: add comments
 */
ClientUI.controls.grid.GridFooter = Class.create({
	CLASSDEF: {
		name: 'ClientUI.controls.grid.GridFooter',
		parent: ClientUI.common.box.Box
	}
	
});

Object.extend(ClientUI.controls.grid.GridFooter.prototype, {
	initialize: function(template, grid) {
		this.grid = grid;
		ClientUI.controls.grid.GridFooter.parentClass.constructor().call(this, template);
		this.createControl(template);
	},
	createControl: function(template) {
		var errMsg = "";
		if(!template) {
			errMsg = "Invalid template specified for GridFooter.";
			ClientUILib.log(ClientUILogger.ERROR, errMsg);
			throw(errMsg);
		}
		
		if(!this.parseTemplate(template)) {
			errMsg = "Unable to parse template. GridFooter requires template specified over table element with one row.";
			ClientUILib.log(ClientUILogger.ERROR, errMsg);
			throw(errMsg);
		}

		// Set dimensions
		this.setHeight(this.defaultHeight);
		this.setWidth(this.defaultWidth);
		this.controlCreated = true;
	},
	parseTemplate: function(template) {
		if(!template) {
			return false;
		}

		var childs = template.childNodes;
		for(var i=0; i<childs.length; i++) {
			if(childs[i].tagName && childs[i].tagName.toLowerCase() == "div") {
				this.container = new ClientUI.common.box.Box(childs[i], null, true);
				this.container.setStyle({"z-index": 100});
				break;
			}
		}
		
		var normal = null, frozen = null;
		var childs = this.container.getElement().childNodes;
		for(var i=0; i<childs.length; i++) {
			if(childs[i].id && childs[i].id.indexOf("FrozenBox")>=0) {
				frozen = childs[i];
			}
			else if(childs[i].id && childs[i].id.indexOf("NormalBox")>=0){
				normal = childs[i];
			}
		}

		if(!normal || !frozen) {
			errMsg = "Unable to parse template for GridFooter. Unable to find FrozenBox or NormalBox.";
			ClientUILib.log(ClientUILogger.ERROR, errMsg);
			throw(errMsg);
		}
		this.contentBox = new ClientUI.common.box.Box(normal);
		this.contentBox.makeAbsolute();
		this.frozenContentBox = new ClientUI.common.box.Box(frozen);
		this.frozenContentBox.makeAbsolute();

		// create row template
		var ch = this.contentBox.getElement().firstChild;
		while(ch) {
			if(ch.tagName && ch.tagName.toLowerCase()=="table") {
				this.headerRow = new ClientUI.common.box.Box($(ch), null, true);
				break;
			}
			ch = ch.nextSibling;
		}
		ch = this.frozenContentBox.getElement().firstChild;
		while(ch) {
			if(ch.tagName && ch.tagName.toLowerCase()=="table") {
				this.headerFrozenRow = new ClientUI.common.box.Box($(ch), null, true);
				break;
			}
			ch = ch.nextSibling;
		}		

		var columns = this.grid.getHeader().getColumns();
		var i=0;
		var cols = $A(this.headerFrozenRow.getElement().getElementsByTagName("col"));
		cols.each(function(col) {
			columns[i].footerCol = $(col);
			i++;
		});
		cols = $A(this.headerRow.getElement().getElementsByTagName("col"));
		cols.each(function(col) {
			columns[i].footerCol = $(col);
			i++;
		});

		this.helpObj = new ClientUI.common.box.Box(this.frozenContentBox.getElement(), null, true);
		this.defaultWidth = this.grid.getHeader().defaultWidth;
		this.defaultHeight = Element.getHeight(this.headerRow.getElement().rows[0].cells[0]);
		if(ClientUILib.isGecko) {
			this.defaultHeight -= this.getBorderWidth("tb") + this.getPadding("tb");
			//this.defaultWidth -= this.getBorderWidth("lr") + this.getPadding("lr");
		}
				
		this.frozenSubstrate = new ClientUI.common.box.Box(this.grid.getElement().id + ":fs", this.getElement());
		this.frozenSubstrate.getElement().name = this.getElement().id + "FRFrm";
		this.frozenSubstrate.setHeight(this.defaultHeight);			
		return true;
	},
	updateSize: function() {
		this.defaultWidth = this.grid.getHeader().defaultWidth;
		this.defaultHeight = Element.getHeight(this.headerRow.getElement().rows[0].cells[0]);
		if(ClientUILib.isGecko) {
			this.defaultHeight -= this.getBorderWidth("tb") + this.getPadding("tb");
			//this.defaultWidth -= this.getBorderWidth("lr") + this.getPadding("lr");
		}
		this.setHeight(this.defaultHeight);
		this.setWidth(this.defaultWidth);
	},
	updateLayout: function() {
		if(!this.controlCreated || !this.grid.controlCreated) {
			return;
		}
		ClientUI.controls.grid.GridFooter.parentClass.method("updateLayout").call(this);
	
		var height = this.getHeight();
		var totalWidth = this.grid.getColumnsTotalWidth();
		var frozenContentWidth = this.grid.getColumnsFrozenWidth();

		this.contentBox.setWidth(Math.max(this.getWidth(), totalWidth));
		this.contentBox.setHeight(height);
		this.contentBox.moveTo(frozenContentWidth - this.grid.getScrollOffset(), 0);
		this.frozenContentBox.setWidth(frozenContentWidth);
		this.frozenContentBox.setHeight(height);
		this.frozenContentBox.moveTo(0, 0);
		var frozenContentWidth = this.grid.getBody().frozenContentBox.getWidth();
		
		var viewWidth = this.grid.getBody().scrollBox.getViewportWidth();
		this.container.setWidth(viewWidth);
		this.setWidth(viewWidth);
		this.frozenSubstrate.setWidth(frozenContentWidth);
	},
	adjustScrollPosition: function(pos) {
		this.contentBox.moveToX(this.grid.getColumnsFrozenWidth()-pos);	
	},
	adjustColumnWidth: function(column, width) {
		var columns = this.grid.getHeader().getColumns();
		columns[column].footerCol.width = width>0 ? width : 1;
	},
	
	setFakeColumnWidth: function() {
		var columns = this.grid.getHeader().getColumns();
		columns.last().footerCol.width = 1;
		var width = this.grid.getElement().offsetWidth - this.headerRow.getElement().offsetWidth;
		if (width < 1) {
			width = 1;
		}
		columns.last().footerCol.width = width;
	},
	
	hideColumn: function(index, frozen) {
		var rows;
		if(frozen) {
			rows = this.headerFrozenRow.getElement().rows;
		} else {
			rows = this.headerRow.getElement().rows;
		}
		for(var i=0; i<rows.length; i++) {
			rows[i].removeChild(rows[i].cells[index]);
		}
	}	
});
