

/********************************************************************************/
/* Initialise Rollover SubNav
/********************************************************************************/


(function ($) {
	$.fn.formatTextArea = function () {
		$.fn.formatTextArea.initFormat('.AutoFormatMarkup');
	}
		
	//----------------------------------------------------------------------
	
	
	$.fn.formatTextArea.initFormat = function (_target) {
		
		$(_target).change($.fn.formatTextArea.formatMarkup);
		//$(_target).keyup($.fn.formatTextArea.formatMarkup);
	}
	
	//----------------------------------------------------------------------
	
	
	$.fn.formatTextArea.formatMarkup = function () {
		// Store each line of content in an array list
		var textContentLines = $(this).val().split('\n');
		
		// Set the initialTab to the number of tabs on the first line of content
		var initialTabs = textContentLines[0].split('\t').length-1;
		
		//$(this).val(textContentLines[0].split('\t')[initialTabs]);
		// Create a variable to store the edited content
		var newContent = textContentLines[0].split('\t')[initialTabs];
		
		// Cycle through all the remaining lines of content to find the lowest amount of tabs on a line
		for(var i=1; i<textContentLines.length; i++){
			var tabCount = textContentLines[i].split('\t').length-1;
			initialTabs = Math.min(initialTabs, tabCount);
			//$(this).val($(this).val()+'\n'+textContentLines[i]);
			newContent += '\n'+textContentLines[i];
		}
		
		// Create a tabString to store the escape code for the initialTabs
		var tabString = "\n";
		for(var i=0; i<initialTabs; i++){
			tabString += "\t";
		}
		
		// Convert the tabString into a regular Expression
		var re = RegExp(tabString, "g");
		
		$(this).val(newContent);
		
		// Remove all the initialTabs from each line
		//$(this).val($(this).val().replace(/(\n\t\t\t)/g,'\n'));
		$(this).val($(this).val().replace(re,'\n'));
		
		// Set the character spacing for each tab
		$(this).val($(this).val().replace(/\t/g,'    '));
	}
		
	//----------------------------------------------------------------------
	
	$(document).ready(function() {
		$.fn.formatTextArea();
	});
	
	
})(jQuery);




