function Columns( name ) {
	this.name = name;
	this.columnCount = 1;
	this.paragraphs = null;
	this.paragraphsPages = null;
	this.dataSource = null;
	this.table = null;
	this.delimiter = null;
	this.minColumnWidth = 200;
	this.paginaActual = 1;
	this.cantidadParrafos = null;
	this.cantidadPorPagina = null;
	
	return this;
}

Columns.prototype.bindTo = function( tableID ) {
	var table = document.getElementById( tableID );
	
	if( table ) {
		if( table.attachEvent ) {
			table.attachEvent( 'onresize', this.onWindowResize );
		}
		else {
			table.onresize = this.onWindowResize;
		}
		
		table._columns = this;
		
		this.table = table;
	}
	else {
		this.table = null;
	}
}

Columns.prototype.load = function() {
	var src = this.dataSource;
	
	if( src ) {
		this.paragraphs = null;
		this.paragraphsPages = null;
		this.paragraphs = new Array();
		this.paragraphsPages = new Array();
		
		var parrafo = (this.cantidadPorPagina * this.paginaActual) - this.cantidadPorPagina;
		if( src.hasChildNodes ) {
			node = src.firstChild;
			while( node ) {
				this.paragraphsPages.push( node );	
				node = node.nextSibling;
			}
			this.cantidadParrafos = this.paragraphsPages.length;
			for (; parrafo < this.cantidadPorPagina * this.paginaActual && parrafo < this.cantidadParrafos; parrafo++)
			{
				this.paragraphs.push( this.paragraphsPages[parrafo] );
			}
		}
	}
	else {
		//alert( 'No se especific� el dataSource.' );
	}
}

Columns.prototype.onWindowResize = function( e ) {

}

Columns.prototype.draw = function( columnCount ) {
	if( columnCount == undefined ) {
		columnCount = this.columnCount;
	}
	
	var length = this.paragraphs.length;
	var perColumn = Math.ceil( length / columnCount );
	
	this.clear();
	
	row = document.createElement( 'tr' );
	
	this.table.tBody.appendChild( row );
	
	if( this.delimiter ) {
		delimiter = this.delimiter.toUpperCase();
		
		for( var i = 0; i < length; i++ ) {
			isDelimiterTag = this.paragraphs[i].nodeName.toUpperCase() == delimiter;
			
			if( i == 0 || isDelimiterTag ) {
				column = document.createElement( 'td' );
				
				row.appendChild( column );
			}
			
			if( !isDelimiterTag ) {
				column.appendChild( this.paragraphs[i].cloneNode( true ) );
			}
		}
	}
	else {
		var sumatoria = 0;
		var sumatoria2 = 0;
		
		for(i = 0; i < length; i++ ) {
			if(this.paragraphs[i].innerText.length)
				sumatoria += this.paragraphs[i].innerText.length;
		}
		var media = Math.ceil(sumatoria/columnCount);

		row = this.table.tBody.firstChild;
		column = document.createElement( 'td' );
		row.appendChild( column );
		cantidadColumnas = 0;

		for(i = 0; i < length; i++ ) {
			
			if (this.paragraphs[i].innerText.length)
				sumatoria2 += this.paragraphs[i].innerText.length;
					
			if (sumatoria2 >= media && cantidadColumnas < 1) {
//			if( i % perColumn == 0 ) {
				
				mePaso = sumatoria2 - media;
				if (this.paragraphs[i].innerText.length - mePaso < 40)
				{
					mePaso = this.paragraphs[i].innerText.length;
				}
				else
				{
					while (this.paragraphs[i].innerText.substring(this.paragraphs[i].innerText.length - mePaso, this.paragraphs[i].innerText.length - mePaso+1) != ' ')
					{
						mePaso++;
					}
					mePaso--;
				}
				this.paragraphs[i+1].innerText = this.paragraphs[i].innerText.substr(this.paragraphs[i].innerText.length - mePaso, mePaso) + this.paragraphs[i+1].innerText;
				this.paragraphs[i].innerText = this.paragraphs[i].innerText.substr(0,this.paragraphs[i].innerText.length - mePaso);
			
				column.appendChild( this.paragraphs[i].cloneNode( true ) );

				row = this.table.tBody.firstChild;
				cantidadColumnas++;
				column = document.createElement( 'td' );
				
				row.appendChild( column );
				sumatoria2 = 0;
			}
			else
			{
				column.appendChild( this.paragraphs[i].cloneNode( true ) );
			}
		}
	}
	
	this.dataSource.style.display = 'none';
}

Columns.prototype.clear = function() {
	if( this.table ) {
		tBody = this.table.tBody;
		
		/*
		if( tBody ) {
			if( document.removeNode ) {
				tBody.removeNode( true );
			}
			else {
				this.table.removeChild( tBody );
			}
			
			tBody = null;
			this.table.tBody = null;
		}
		*/
		
		if( !tBody ) {
			tBody = document.createElement( 'tbody' );
			this.table.tBody = tBody;
			this.table.appendChild( tBody );
		}
		
		/*
		if( !this.table.firstChild ) {
			this.table.appendChild( document.createElement( 'tbody' ) );
		}
		*/
		
		if( tBody.firstChild ) {
			if( document.removeNode ) {
				tBody.firstChild.removeNode( true );
			}
			else {
				tBody.removeChild( tBody.firstChild );
			}
		}
	}
}

Columns.prototype.setColumnCount = function( count ) {
	this.columnCount = count;
	//this.draw();
}