/**
 *  Simplified BSD License
	Copyright 2010 Omskep. All rights reserved.

	Redistribution and use in source and binary forms, with or without modification, are 
	permitted provided that the following conditions are met:

	1.	Redistributions of source code must retain the above copyright notice, this list of 
		conditions and the following disclaimer.

	2.	Redistributions in binary form must reproduce the above copyright notice, this list 
		of conditions and the following disclaimer in the documentation and/or other materials 
		provided with the distribution.

	THIS SOFTWARE IS PROVIDED BY Omskep ''AS IS'' AND ANY EXPRESS OR IMPLIED 
	WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Omskep OR 
	CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
	CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
	SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 
	ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
	NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
	ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

	The views and conclusions contained in the software and documentation are those of the 
	authors and should not be interpreted as representing official policies, either expressed 
	or implied, of Omskep. 
 */

function WordCount() {
	//Private Members & Menthods
	var target;
	var outputTo;

	//Function Custom Trim
	var customTrim = function ( value )	{
	    return value.replace( /^\s+|\s+$/g, '' );
	};
	
	//Function Update Output
	var updateOutput = function ( value ) {
		outputTo.html( value );
	};
	//Remove Repeating Spaces
	var removeSpaces = function( value ) {
		while ( value.indexOf('  ') > -1 )
		{
			value = value.split('  ').join(' ');
		}
		return value;
	};
	//Public Members & Menthods
	return {
		//Function Set Target Div
		setTarget: function(newTarget) {
			target = newTarget;
		},
		//Function Set Output Div/Span
		setOutput: function(newOutput) {
			outputTo = newOutput;
		},
		//Function Get Word Count
		getWordCount: function() {
			var trimmed = customTrim(target.val());
			trimmed = removeSpaces( trimmed );
			if( trimmed.length < 1 ){
		    	updateOutput( 0 );			
		    }
		    else {
		    	updateOutput( trimmed.split(' ').length );
		    }
		},
		returnCount: function( element )
		{
            var trimmed = customTrim($(element).val());
            trimmed = removeSpaces( trimmed );
            if( trimmed.length < 1 ){
                return 0;          
            }
            else {
                return trimmed.split(' ').length;
            }
		},
		//Function Get Character Count
		getCharCount: function( noSpaces ) {
			
			var spaces = 0;
			var trimmed = target.val();
			if( noSpaces === true )
			{
				trimmed = customTrim(target.val());
				trimmed = removeSpaces( trimmed );
				spaces = trimmed.split(' ').length-1;
			}
		    if( trimmed.length < 1 ){
		    	updateOutput( 0 );			
		    }
		    else {
		    	updateOutput( trimmed.length - spaces );
		    }
		},
		
		//Function Get Character Countdown
		getCharCountdown: function( initialCount ) {
			var trimmed = target.val();
		    if( trimmed.length <= 0 ){
		    	updateOutput( initialCount );			
		    }
		    else {
		    	updateOutput( initialCount - trimmed.length );
		    }
			return ( initialCount - trimmed.length );
		},
		
		//Function Call Word Count
		callWordCount: function( target, output ) {
			if ( target.length > 0) {
				this.setTarget( target );
				this.setOutput( output );
				this.getWordCount();
			}
		}
	};
}