
document.registeredColumnLayouts = new Array();

function registerColumnLayout(id, cols, newClassName, maxHeight, minHeight){
	if(cols==null || cols<1){
		alert("registerColumnLayout invalid cols argument: "+cols);
		return;
	}
	document.registeredColumnLayouts.push({id: id, cols: cols, newClassName: newClassName, maxHeight: maxHeight, minHeight: minHeight});
}


function layoutColumns () {
	for (var i in document.registeredColumnLayouts) {
		var config = document.registeredColumnLayouts[i];
		var obj = document.getElementById(config.id);
		if(obj!=null){
			//May we create an element
			if (document.createElement (obj.nodeName.toUpperCase ()) == null) {
				return;
			}
			var w = obj.scrollWidth;
			var newClassName = (config.newClassName!=null && config.newClassName.length>0) ? config.newClassName : null;
			var initWidth = obj.style.width;
			var initVisibility = obj.style.visibility;
			var initClassName = obj.className;
			var agent = navigator.userAgent.toLowerCase ();
			var isSafari = (agent.indexOf('safari') != -1);

			if(!isSafari)
				obj.style.visibility = 'hidden';
						
			w = Math.floor((w*1)/config.cols);
			
			if(newClassName!=null)
				obj.className = newClassName;
			else
				obj.style.width = w+"px";

			h = obj.scrollHeight;
			if(config.minHeight!=null){
				if(h<config.minHeight)
					return;
			}
			
			if(config.maxHeight!=null){
				if((config.maxHeight*config.cols)<h){
					obj.style.width = initWidth;
					obj.style.visibility = initVisibility;
					obj.className = initClassName;
					return;
				}
			}
								
			//Enclose #text by font
			var children = obj.childNodes;
			for(var j=0;j<children.length;j++){
				var c = children[j];
				if(c.nodeName=="#text"){
					var f = document.createElement("FONT");
					f.name = "_layouter_";
					if(f.innerText!=null)
						f.innerText = c.nodeValue;
					else
						f.innerHTML = c.nodeValue;
					AJS.insertAfter(f,c);
				}
			}
			//Delete all #text
			var children = obj.childNodes;
			for(var j=0;j<children.length;j++){
				var c = children[j];
				if(c.nodeName=="#text"){
					obj.removeChild(c);
				}
			}
			
			//Create cols
			var cols = new Array();
			cols.push(obj);
			
			for(var j=1;j<config.cols;j++){
				var newObj = document.createElement(obj.nodeName.toUpperCase());
				if(newClassName!=null){
					newObj.className = newClassName;
				}
				else {
					newObj.style.width = w+"px";
					newObj.className = obj.className;
				}
				if(!isSafari)
					newObj.style.visibility = 'hidden';
				cols.push(newObj);
				AJS.insertAfter(newObj,cols[j-1]);
			}
			
			//Arrange
			var height = config.maxHeight;
			if(height==null)
				height = obj.scrollHeight/config.cols;
			
			for(var j=1;j<cols.length;j++){
				var prevObj = cols[j-1];
				var top = AJS.absolutePosition(prevObj).y;
				var children = prevObj.childNodes;
				for(var k=children.length-1;k>=0;k--){
					var c = children[k];
					var y = AJS.absolutePosition(c).y-top;
					var move = false;
					if((y+c.scrollHeight)>height){
						if(y<height && c.name=="_layouter_"){
							var prop = c.innerText!=null?"innerText":"innerHTML";
							var txt = c[prop];
							var pos = txt.length;
							while(pos>=0){
								var pos = txt.lastIndexOf(" ",pos);
								if(pos!=-1){
									c[prop] = txt.substring(0,pos+1);
									//Fits
									if((y+c.scrollHeight)<height){
										break;
									}
								}
								pos--;
							}
							if(pos>=0){
								var f = document.createElement("FONT");
								f.name = "_layouter_";
								f[prop] = txt.substring(pos+1,txt.length);
								var fc = cols[j].firstChild;
								if(fc!=null)
									AJS.insertBefore(f,fc);
								else
									cols[j].appendChild(f);
							}
							else {
								c[prop] = txt;
								move = true;
							}
						}
						else {
							move = true;
						}
					}
					if(move){
						prevObj.removeChild(c);
						var fc = cols[j].firstChild;
						if(fc!=null)
							AJS.insertBefore(c,fc);
						else
							cols[j].appendChild(c);
					}
				}
			}
			
			//Show all
			for(var j=0;j<cols.length;j++){
				cols[j].style.visibility = '';
				// hide empty columns
				if (cols[j][cols[j].innerText != null ? 'innerText' : 'innerHTML'] == '') {
					cols[j].style.display = 'none';
				}
			}
		}
	}
}

//AJS.AEV (window, 'load', layoutColumns);
