/******
 ** Using javascript to cascade into columns.
 ** The columns must be the names of DIVS. Only immediate text children will be split, CHILD NODES WILL NOT BE SPLIT. (e.g. <p> will not be split)
 ** NO Internal markup is allowed.
 ** Text will flow from columns until all columns are of about equal height.
 ** Delimiters are any characters in the delimiter block', unless empty, in which case every character is a delimiter
 ** The columns MUST have SOME text in them (to create the text nodes).  They will be replaced, so a space each is fine.
 **/
// newCols= array of IDS of div columns to cascade to, in order of cascading.
// newOriginText= ID of Div that contains the original text.
// newName= name of this instance of columns.
function newscolumns(newName,newOriginText,newCols) {
	this.name = newName;
	this.colNames = newCols;
	this.originText= document.getElementById(newOriginText);
	this.cols = new Array();
	this.flowright=true; //TODO: Implement Flowing LEFT
	
	this.init = function() {
		if(newCols instanceof Array) {
			this.colNames = newCols;
			for (k=0;k<this.colNames.length;k=k+1){
				this.cols[k]=document.getElementById(this.colNames[k]);
				//this.cols[i].style.height="100px"
			}
		} 
		if(this.cols.length>0) {
			for (j=0;j<this.cols.length;j=j+1){
				this.cols[j].style.height="auto";
				//clear data first.
				while(this.cols[j].hasChildNodes()) {
				this.cols[j].removeChild(this.cols[j].firstChild);
				}
				//add a blank node
				var newNode = document.createTextNode("");
				this.cols[j].appendChild(newNode);
			}
			//populate first text
			var originTextNumNodes = this.originText.childNodes.length;
			for(m=0;this.originText.hasChildNodes() && m<originTextNumNodes;m=m+1) {
			this.cols[0].appendChild(this.originText.childNodes[0]);	
		//	this.cols[0].appendChild(this.originText.childNodes[m].cloneNode(true));
			}
			//this.cols[0].lastChild.data = this.originText.lastChild.data;
		}
	};
		
		//Cascade nodes across columns.  Note that, the children divs MUST have height set to auto.
	this.cascadeNodes = function() {
		var changed = false;
		var initNumNodes;
		initNumNodes = this.cols[0].childNodes.length;
		//Make sure there are at least 2 columns
		if(this.cols.length>1){
			for(i=0;i<this.cols.length-1;i++) {
				curInitNumNodes = this.cols[i].childNodes.length;
				for(n=0;n<initNumNodes&&n<curInitNumNodes;n=n+1) {
					this.cols[i+1].insertBefore(this.cols[i].lastChild,this.cols[i+1].firstChild);
					changed=true;
					//If 2nd column is too big, reverse column changes.
				//	+this.cols[i].scrollHeight+"-"this.cols[i+1].scrollHeight);
					if(this.cols[i].scrollHeight<this.cols[i+1].scrollHeight ) {
						this.cols[i].appendChild(this.cols[i+1].firstChild);
						if(n==0) {
							// if no changes, leave.
							changed=false;
						}
						n=initNumNodes;
						if(this.cols[i].childNodes.length>1 && this.cols[i].childNodes[0].nodeType==3 && this.cols[i].childNodes[1].nodeType==3) {
							this.cols[i].childNodes[0].data=this.cols[i].childNodes[0].data+this.cols[i].childNodes[1].data;
							this.cols[i].removeChild(this.cols[i].childNodes[1]);
						}
					} else {
					// if change went through, and child has 2 adjacent texts at first, add 2nd text to first and remove
					// 2nd text.
						if(this.cols[i+1].childNodes.length>1 && this.cols[i+1].childNodes[0].nodeType==3 && this.cols[i+1].childNodes[1].nodeType==3) {
							this.cols[i+1].childNodes[0].data=this.cols[i+1].childNodes[0].data+this.cols[i+1].childNodes[1].data;
							this.cols[i+1].removeChild(this.cols[i+1].childNodes[1]);
						}
					}
				}
			}
		}
		return changed;
	}
	
	//cascades text.  Note that, the children divs MUST have height set to auto.
	this.cascadeText = function() {
		var initTextSize, originalTextSize, text1, text2, textChanged;
		//var textChangedTotal=0;
		//Make sure there are at least 2 columns
		
		if(this.cols.length>1){
			initTextSize=0;
				
			//Find the maximum amount of text that will be moved around, which is sum of movable text in each column,
				// not including the last one.
			for(h=0;h<this.cols.length-1;h=h+1) {
				if(this.cols[h].hasChildNodes() && this.cols[h].lastChild.nodeType==3) {
				initTextSize=initTextSize+this.cols[h].lastChild.data.length;
				}
			}
			
			textChanged=1;
			for(h=0;h<initTextSize&&textChanged>0;h=h+1) {
				//Every change could result in needed to reflow the text.
				//keep going if textChanged.
				textChanged=0;
				
				for(i=0;i<this.cols.length-1;i=i+1) {
				//if the last node of the first column is not text, skip this changing.
					if(this.cols[i].hasChildNodes() && this.cols[i].lastChild.nodeType==3) {
						text1=this.cols[i].lastChild.data
						originalTextSize=text1.length; //original text size for this itteration
						//if first node of 2nd col is no text, create text node and insert as first child in 2nd col
						if(this.cols[i+1].firstChild.nodeType!=3) {
							var newNode = document.createTextNode("");
							this.cols[i+1].insertBefore(newNode,this.cols[i+1].firstChild);
						}
						text2=this.cols[i+1].firstChild.data
						delimitIndex=text1.length-1;
						counter=0;
						//Don't run more than original text size. if you do, there was something wrong
						for(counter=0;counter<originalTextSize;counter=counter+1) {
							delimitIndex=findLastText(text1," \n-");
							if ( text1.length>2 && this.cols[i].scrollHeight>this.cols[i+1].scrollHeight) {
								//Change Parse text to reflect the new lines
								text2= text1.substr(delimitIndex)+text2;
								text1=text1.substr(0,delimitIndex);
								temp1=this.cols[i].lastChild.data;
								temp2=this.cols[i+1].firstChild.data;
								this.cols[i].lastChild.data=text1;
								this.cols[i+1].firstChild.data=text2;
								textChanged=textChanged+1;
								//If the last change made the 2nd column too big, reverse changes and exit.
								if(this.cols[i].scrollHeight<this.cols[i+1].scrollHeight ) {
									this.cols[i].lastChild.data=temp1;
									this.cols[i+1].firstChild.data=temp2;
									counter=originalTextSize;
									textChanged=textChanged-1;
								}
							}
							else {
							//exit this loop
								counter=originalTextSize;
							}
						}
					}
				}
			}
		}
	}//end class cascadetext
	
	//cascades All across the columns (nodes and text)
	this.cascadeAll= function() {
		var changed=true;
		var counter=0;
		var initNumNodes=this.cols[0].childNodes.length;
		// continue if there was a change, or a number of iterations 
		// over the number of nodes in the text is reached, +1 to allow for a final cascadeText.
		while(changed && counter<=initNumNodes) {
			if(changed) {
				this.cascadeText();
			}
			changed=false;
			changed = this.cascadeNodes();
			counter=counter+1;
		}
	}
	this.changeToColumns= function() {
		this.cascadeAll();
		var originNumNodes= this.originText.childNodes.length;
		for(m=0;this.originText.hasChildNodes() && m<originNumNodes;m=m+1) {
			this.originText.removeChild(this.originText.firstChild);
			}
	}
}//end class newscolumns

//Finds the last occurence of of a character in delimiters in text
function findLastText(text,delimiters) {
	maxDelimitIndex=-1;
	delimitIndex=0;
	for(l=0;l<delimiters.length;l=l+1) {
		delimitIndex=text.lastIndexOf(delimiters.charAt(l));
		if(delimitIndex>maxDelimitIndex) {
			maxDelimitIndex=delimitIndex;
		}
	}
	return maxDelimitIndex;
}

