/**
 * @lib:	NYN
 * 
 * @desc:	alleviate extra overhead from common tasks
 * 
 * @site:	All
 * @author:	Michael Pope
 * 			nyndesigns.com
 *
 * @file:	nyn.js
 * @date:	22.10.2013 - 16:46:12  [Michael Pope]
 */


/**
 * Check Value
 * 
 * @desc	check the length of a textarea (on mouseup) and resize if required
 * 
 * @param	element
 * @param	int		max length
 * @param	string	optional id
 * @param	string	optional error class name
 * @param	string	optional error timeout length
 */
function checkValue( e, maxlength, id, error_class, error_timeout )
{
	field_length = $( e ).val().length;
	
	if( field_length > maxlength ) {
		
		$( e ).val( $( e ).val().substr( 0, maxlength ) );
		
	}
	
	if( id && field_length >= maxlength ) {
			
		flashError( id, error_class, error_timeout ); 
		
	}
}


/**
 * Update Value
 * 
 * @desc	update textarea character count notification msg
 * 
 * @param	element
 * @param	int		max length
 * @param	string	id
 */
function updateValue( e, maxlength, id, message )
{
	message	= message || '{length} of {maxlength}';
	
	$( '#'+id ).html( message.replace( '{length}', $( e ).val().length ).replace( '{maxlength}', maxlength ) );
}


/**
 * Check Words
 * 
 * @desc	check the number of "words" of a textarea (on mouseup) and resize if required
 * 
 * @param	element
 * @param	int		max length
 * @param	string	optional id
 * @param	string	optional error class name
 * @param	string	optional error timeout length
 */
function checkWords( e, maxlength, id, error_class, error_timeout )
{
	var word_count = wordCount( $( e ).val() );
	
	if( word_count > maxlength ) {
		
		words_list = $( e ).val().match( /\b\S+\b/g );
		concat_word = '';
		
		$.each( $( e ).val().split( /\b\S+\b/g, maxlength ), function( i, word_delimiter ) {
    
			concat_word += word_delimiter + words_list[ i ];
			
		} );
		
		$( e ).val( concat_word ).blur();
	}
	
	if( id && word_count >= maxlength ) {
			
		flashError( id, error_class, error_timeout ); 
			
	}
}


/**
 * Update Words
 * 
 * @desc	update textarea "word" count notification msg
 * 
 * @param	element
 * @param	int		max length
 * @param	string	id
 * @param	string	custom message
 */
function updateWords( e, maxlength, id, message )
{
	message	= message || '{length} of {maxlength}';
	
	$( '#' + id ).html( message.replace( '{length}', wordCount( $( e ).val() ) ).replace( '{maxlength}', maxlength ) );
}


/**
 * Update Words
 * 
 * @desc	count the number of "words" in a string
 * 
 * @note	this expects that sentences separated by a period, also include
 * 			a space -- it's too difficult to detect all cases:
 * 			
 * 			ex 1. me@domain.com (expected = 1; actual = 1)
 * 			ex 2. Hello.World (expected = 2; actual = 1)
 * 			ex 3. Hello. World (expected = 2; actual = 2)
 * 
 * @param	string	string / sentence
 */
function wordCount( s )
{
	m = s.length ? s.match(/\b\S+\b/g) : '';
	
	return m ? m.length : 0;
}


/**
 * Flash Error
 * 
 * @desc	temp change the class on an element to warn, notify, mark error
 * 
 * @param	string	id
 * @param	string	optional error class name
 * @param	string	optional error timeout length (if equal to or less than 0, will leave class on)
 */
function flashError( id, error_class, error_timeout )
{
	error_class = error_class || 'error_text';
	error_timeout = error_timeout || 1000;
	
	$( '#' + id ).addClass( error_class );
	
	if( error_timeout > 0 ) {
	
		setTimeout( 
			function() { 
				$( '#' + id ).removeClass( error_class );
			}, 
			error_timeout 
		);
	
	}
}


/**
 * Update Selected Message
 * 
 * @desc	update # of selected checkboxes notification msg
 * 
 * @param	string	class
 * @param	string	id
 */
function updateSelectedMsg( c, id )
{
	$( '#' + id ).html( '(' + $('.' + c + ':checkbox:checked' ).length + ' of ' + $( '.' + c + ':checkbox' ).length + ' selected)' );
}


/**
 * Check Box Select Valid
 * 
 * @desc	performs quick validation: at least one checkbox must be selected
 * 
 * @param	string	class
 * @return	bool
 */
function checkboxSelectValid( c )
{
	return $( '.' + c + ':checkbox:checked' ).length > 0;
}


/**
 * Data Tables Checkbox Select Valid
 * 
 * @desc	performs quick validation: at least one checkbox must be selected
 * 
 * @param	string	class
 * @return	bool
 */
function dataTablesCheckboxSelectValid( c )
{
	return $( 'input:checkbox:checked', $.data( document.body, c ).fnGetNodes() ).length > 0;
}


/**
 * Checkbox Select
 * 
 * @desc	performs quick validation: at least one checkbox must be selected
 * 			update notification msg on fail and open it
 * 
 * @param	string	class
 * @param	string
 * @return	bool
 */
function checkboxSelect( c, type )
{
	if( ! checkboxSelectValid( c ) ) {
		
		$( '#dialog-message' ).html( 'You must select <span style="text-decoration: underline;">at least one</span> ' + type + ' first.' ).dialog( 'open' );

		return false;
		
	}

	return true;
}


/**
 * Data Tables Checkbox Select
 * 
 * @desc	performs quick validation: at least one checkbox must be selected
 * 			update notification msg on fail and open it
 * 
 * @param	string	class
 * @param	string
 * @return	bool
 */
function dataTablesCheckboxSelect( c, type )
{
	if( ! dataTablesCheckboxSelectValid( c ) ) {
		
		$( '#dialog-message' ).html( 'You must select <span style="text-decoration: underline;">at least one</span> ' + type + ' first.').dialog( 'open' );

		return false;

	}

	return true;
}


/**
 * Checkbox Unique Valid
 * 
 * @desc	performs quick validation: only one checkbox can be selected
 * 
 * @param	string	class
 * @return	bool
 */
function checkboxUniqueValid( c )
{
	return $( '.' + c + ':checkbox:checked' ).length == 1;
}


/**
 * Checkbox Unique Valid
 * 
 * @desc	performs quick validation: only one checkbox can be selected
 * 
 * @param	string	class
 * @return	bool
 */
function dataTablesCheckboxUniqueValid( c )
{
	return $( 'input:checkbox:checked', $.data( document.body, c ).fnGetNodes() ).length == 1;
}


/**
 * Checkbox Unique Valid
 * 
 * @desc	performs quick validation: only one checkbox can be selected
 * 			update notification msg on fail and open it
 * 
 * @param	string	class
 * @param	string
 * @return	bool
 */
function checkboxUnique( c, type )
{
	if( ! checkboxUniqueValid( c ) ) {
		
		$( '#dialog-message' ).html( 'You must select <span style="text-decoration: underline;">exactly one</span> ' + type + ' first.' ).dialog( 'open' );
		
		return false;
		
	}
	
	return true;
}


/**
 * Checkbox Unique Valid
 * 
 * @desc	performs quick validation: only one checkbox can be selected
 * 			update notification msg on fail and open it
 * 
 * @param	string	class
 * @param	string	type
 * @return	bool
 */
function dataTablesCheckboxUnique( c, type )
{
	if( ! dataTablesCheckboxUniqueValid( c ) ) {

		$( '#dialog-message' ).html( 'You must select <span style="text-decoration: underline;">exactly one</span> ' + type + ' first.' ).dialog( 'open' );

		return false;

	}

	return true;
}


/**
 * Checkbox Unique Valid
 * 
 * @desc	toggle checkbox with given class against checkbox 'e'
 * 
 * @param	element
 * @param	string	class
 * @return	bool
 */
function checkboxToggleSelect( e, c )
{
	return $( '.' + c + ':checkbox' ).attr( 'checked', $(e).attr( 'checked' ) );
}


/**
 * Checkbox Unique Valid
 * 
 * @desc	toggle checkbox with given class against checkbox 'e'
 * 
 * @param	element
 * @param	string	class
 * @return	bool
 */
function dataTablesCheckboxToggleSelect( e, c )
{
	return $( 'input:checkbox', $.data( document.body,c).fnGetNodes() ).attr( 'checked', $(e).attr( 'checked' ) );
}


/**
 * Remove Tooltip
 * 
 * @param	mixed	object or identifier
 * @return	bool
 */
function removeTooltip( id ) 
{
	var api 		= $( id ).data( 'tooltip' ); 

	if( api != undefined ) {
			
		var tip		= api.getTip();
			
		if( tip != undefined ) {
		
			api.getTip().remove();
			
			return true;
			
		}
		
	}
	
	return false;	
}


/**
 * Is Greater Than OR Equal To jQuery Version
 * 
 * @param	string	version to test (ie. 1.9.1)
 * @return	bool
 */ 
function is_greater_or_equal_to_jquery_ui_version( test_version )
{
	var current_jquery_version = $.fn.version || $.fn.jquery;
	return is_greater_or_equal_to_version( current_jquery_version, test_version );
}


/**
 * Is Greater Than OR Equal To Version
 * 
 * @param	string	version to test against (ie. 1.9.1)
 * @param	string	version to test (ie. 1.9.1)
 * @return	bool
 */ 
function is_greater_or_equal_to_version( version, test_version ) 
{
	version 		= version || '1.0.0';
	test_version 	= test_version || '1.0.0';
	
	version			= version.split( '.' );
	test_version	= test_version.split( '.' );
	
	for( var i = 0; i <= 2; i++ ) {
	
		if( ! test_version[ i ] ) {

			return true;
			
		}
		
		if( ! version[ i ] ) {

			return false;
			
		}
		
		var i_version = parseInt( version[ i ] );
		var i_test_version = parseInt( test_version[ i ] );
		
		if( i_version > i_test_version ) {

			return true;
			
		}
		
		if( i_version < i_test_version ) {

			return false;
			
		}
	
	}
	
	return true;	
}
