/*
jQuery Two Column Plugin
	* Version 1.0
	* November 3, 2009
	* URL: http://www.eljamez.com
	* Description: 
	* Author: James A. Hall
	* Copyright: Copyright (c) 2009 James A. Hall
	* Licence: 
*/

(function($) {
	$.fn.elTwoColumn = function(options){
		// Defaults are added as a property below (outside of the main function), or can be added in controling .js
		options = $.extend({}, $.fn.elTwoColumn.defaults, options);
		
		// Check to see if element exhists
		this.each(function() {  
			var $element = $(this);// Set this element variable (the element in which this plugin effects);
			var contents = $element.html();// Creates string from the contents of the element 
			//console.log(contents);
			var words = contents.split(" ");// Creates an array of all the words	
			//contents.html().replace(".", )
			var sentence = contents.split(".");// Creates an array of the sentences
			for(var e=0; e < sentence.length; e++){// This for loop brings that period back
				sentence[e] = sentence[e]+".";
				//console.log(sentence[e]);
			};
			var fullCountOfWords = 0;// keeps track of word count to know where to split paragraph into 2
			var p1 = "";// contents of first paragraph string
			var p2 = "";// contents of second paragraph string
			var firstonetop1 = true;// boolean to put first sentence of 2nd paragraph into end of 1st paragraph
			
			//This loop places the sentences into the larger strings that will be the contents of the 1st and 2nd paragraphs. It check the current character count (fullCountOfWords) against half the original character count (charCount).  If the original character count is lower, then the sentence goes into the first paragraph. If the current count(fullCountOfWords) is higher, then the sentence goes into the sencond paragraph.  One exception, the first sentence of the second paragraph is moved back to the end of the first paragraph so the first paragraph will always be longer than the second paragraph.
			var regexp = /<("[^"]*"|'[^']*'|[^'">])*>/gi;// Matching all html tags using regex
			$element.html(  
			    $element.html().replace(regexp,"") // Removing the html tags to get accurate word count.
			);
			var charCount = $element.html().length;// Get character count of entire element (to be used for comparison)
			//console.log($element.html().length);
			for(var e=0; e < sentence.length-1; e++){
				fullCountOfWords = fullCountOfWords + sentence[e].length;
				if (fullCountOfWords < charCount/2){
					p1 += sentence[e];
				}else{
					if (firstonetop1 == true){
						p1 += sentence[e];
						firstonetop1 = false;
					}else{
						p2 += sentence[e];
					};
				};
			};
			//place strings into new paragraphs.
			$("<"+options.tagType+" class='"+options.firstClass+"'>"+p1+"</"+options.tagType+">").insertBefore($element);
			$("<"+options.tagType+" class='"+options.secondClass+"'>"+p2+"</"+options.tagType+">").insertAfter(options.tagType + "." + options.firstClass);
			$element.remove();// NOW WE REMOVE THE ORIGINAL ELEMENT (thanks for the memories ;)
		});
		return $(this);
	};
	
	// Defaults property for default settings, will be called above
	$.fn.elTwoColumn.defaults = {
		tagType : 'p',
		firstClass : 'left',
		secondClass : 'right'
	};
})(jQuery);