naviki.main.portlets.LayoutColumns = function(config) {

	/* call the super constructor */
	this.constructor.superclass.constructor.call(this, config);

	this.columns = this.config['columns']; 
	if (this.columns == null) {
		this.columns = 4;
	}
	
	eventMgr.onWindowResize.subscribe(this.onResize, this);

	this.initialize();			
};

/* update the positions of tall portlets */
naviki.main.portlets.LayoutColumns.prototype.updatePortletPositions = function() {
	var container = this.config['container'];
	var portlet = null;
	for (var i=0;i<container.portlets.length;i++) {
		portlet = container.portlets[i];
		this.updatePortletPosition(portlet);	
	}
};

/* update the position of a specific portlet */
naviki.main.portlets.LayoutColumns.prototype.updatePortletPosition = function(portlet) {
	var panelelementid = portlet.getPanelContainer().id;
	var div = null;
	var children = null;
	for (var i=0;i<this.columns;i++) {
		div = this.divs[i];
		children = div.childNodes;
		for (var j=0;j<children.length;j++) {
			if (children[j].id == panelelementid) {
				portlet.updatePositionInLayout(i,j);
			}
		}
	}
};

/* returns the width in pixel for one div */
naviki.main.portlets.LayoutColumns.prototype.getPortletWidth = function() {
	return this.onediv;
};

/* return the id of the container div for adding a new portlet */
naviki.main.portlets.LayoutColumns.prototype.getContainerID = function() {
	return this.divs[0].id;
};

/* add a  new portlet to the layout */
naviki.main.portlets.LayoutColumns.prototype.addNewPortlet = function(portlet) {
	
	//portlet.render(this.getContainerID());
	var x = 0;
	var y = 0;
	/* search the best new position for the portlet */
	var region;
	var minbottom = 9999999;
	for (var i=0;i<this.divs.length;i++) {
		region = YAHOO.util.Dom.getRegion(this.divs[i].id);
		if (region.bottom < minbottom) {
			x = i;
			minbottom = region.bottom;
		}
	}
	
	y = this.divs[x].childNodes.length;
	portlet.render(this.divs[x].id);
	portlet.updatePositionInLayout(x,y);
	
};


/* place a portlet in the layout */
naviki.main.portlets.LayoutColumns.prototype.placePortlet = function(portlet) {
	var x = portlet.getX() + (Math.floor(this.onediv/2));
	var y = portlet.getY();
	
	/* find the right column */	
	var index = -1;
	var region = YAHOO.util.Dom.getRegion(this.divs[0]);
	for (i=0;i<this.columns;i++) {
		var xdiv = region.left + (i*this.onediv);
		var wxdiv = region.left + (i*this.onediv) + this.onediv;
		if ((x >= xdiv) && (x<=wxdiv)) {
			index = i;
		}
	}
	if (index < 0) {
		if (x < xdiv) {
			index = 0;
		} else {
			index = this.columns-1;
		}
	}
	
	/* find the right row */
	var rowindex = 0;
	var correction = 0;
	if (this.divs[index].hasChildNodes()) {
		/* the divs has some portlets, so look at them */
		var children = this.divs[index].childNodes;
		for (i=0;i<children.length;i++) {
			var node = children[i];
			if (node.style.position == "static") {
				var noderegion = YAHOO.util.Dom.getRegion(node);
				if (noderegion.top < y) {
					rowindex = i+1-correction;
				}
			} else {
				correction++;
			}
		}
	}
	/* render to the right div */
	portlet.render(this.divs[index].id);
	/* now we must move the portletnode to the correct row */
	if (this.divs[index].childNodes.length > 1) {
		/* more than one element in div */
		if (rowindex != this.divs[index].childNodes.length-1) {
			/* i have to move the node */
			this.divs[index].insertBefore(
				this.divs[index].childNodes[this.divs[index].childNodes.length-1],
				this.divs[index].childNodes[rowindex]
			);
		}
	}
	this.updatePortletPositions();
};

/* place all portlets of the container */
naviki.main.portlets.LayoutColumns.prototype.placeAllPortlets = function() {
	var portlet = null;
	for (var i=0;i<this.config['container'].portlets.length;i++) {
		portlet = this.config['container'].portlets[i];
		portlet.render(this.divs[portlet.getXMatrixPosition()]);
	}
};

/* init */
naviki.main.portlets.LayoutColumns.prototype.initialize = function() {
	naviki.main.portlets.LayoutColumns.superclass.initialize.call(this);

	this.containerregion = YAHOO.util.Dom.getRegion(this.config['containerid']);

	this.width  = this.containerregion.right - this.containerregion.left;
	this.onediv = Math.floor((this.width - this.columns*20) / this.columns);

	/* create table with 3 columns */
	var table = document.createElement("table");
	var thead = document.createElement("thead");
	var tbody = document.createElement("tbody");
	var tr = document.createElement("tr");
	var th = document.createElement("th");
	var td = document.createElement("td");

	YAHOO.util.Dom.setStyle(table, 'width', "100%"); 
	
	var i=0;
	for (i=0;i<this.columns;i++) {
		tr.appendChild(th);
	}	
	thead.appendChild(tr);
	table.appendChild(thead);
	
	tr = document.createElement("tr");
	
	this.divs = [];
	for (i=0;i<this.columns;i++) {
		td = document.createElement("td");

		if(i==0){
			YAHOO.util.Dom.setAttribute(td, 'align', "left"); 
		}else if((i+1)==this.columns){
			YAHOO.util.Dom.setAttribute(td, 'align', "right");
		}else{
			YAHOO.util.Dom.setAttribute(td, 'align', "center");
		}
		YAHOO.util.Dom.setAttribute(td, 'valign', "top");
		
		this.divs[i] = document.createElement("div");
		this.divs[i].id = this.config['containerid']+"_"+i;
		this.divs[i].style.position = "static";
		this.divs[i].style.left = ""+(i*this.onediv)+"px";
		this.divs[i].style.top = "0px";
		this.divs[i].style.width = ""+(this.onediv+5)+"px";
		td.appendChild(this.divs[i]);
		tr.appendChild(td);
	}
	tbody.appendChild(tr);
	table.appendChild(tbody);
	document.getElementById(this.config['containerid']).appendChild(table);
};

naviki.main.portlets.LayoutColumns.prototype.onResize = function(type, e, me){

	var containerregion = YAHOO.util.Dom.getRegion(me.config['containerid']);
	var width           = containerregion.right - containerregion.left;
	var onediv          = Math.floor((width - me.columns*20) / me.columns);
	me.onediv           = onediv;

	for (i=0;i<me.columns;i++) {
		document.getElementById(me.config['containerid']+"_"+i).style.width = (onediv+5) + "px";
		document.getElementById(me.config['containerid']+"_"+i).style.left  = (i*onediv) + "px";
	}
	
	eventMgr.updateWidth.fire(onediv);
};
