// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

 // ****** START  ******** Code to truncate the string 
    
     // Removes leading whitespaces
    function LTrim( value ) 
    {
    	var re = /\s*((\S+\s*)*)/;
    	return value.replace(re, "$1");
    }
    
    // Removes ending whitespaces
    function RTrim( value ) 
    {
    	var re = /((\s*\S+)*)\s*/;
    	return value.replace(re, "$1");
    }
    
    // Removes leading and ending whitespaces
    function trim( value ) 
    {
    	return LTrim(RTrim(value));
    } 
  // ***** END ******** Code to truncate the string 
  
  
  // ********* START ************ Code to add <WBR>
  
      // For the given 'text',
    function tokenize_text(text,index)
    {
    	  // Tokenize with " "
          var _tokens = text.split(" ");
		  text = "";
		  // Iterate through all the tokens,
		  for ( var i = 0; i < _tokens.length; i++ )
		  {
		  	// Find any of the token is too long,
		  	if( _tokens[ i ].length > index )
		  	{
		  		// Tokenize with "<br/>"
		  		var sub_tokens = _tokens[ i ].split("<br/>");
		  		var sub_text   = "";
		  		// Iterate through all the tokens,
			    for ( var j = 0; j < sub_tokens.length; j++ )
			    {	
			    	// Find any of the token is too long,if so, add <WBR> s to that token
				    if( sub_tokens[ j ].length > index )
			  		{
			  			sub_text = sub_text + "<br/>" + getTokens_With_WBR( sub_tokens[ j ] , index );
			  		} 
			  		else //keep as it is,
			  		{
			  			sub_text = sub_text + "<br/>" + sub_tokens[ j ];
			  		} 		
		  		}
		  		text = text + " " + sub_text;
		  	}
		  	else //keep as it is,
		  	{
		  		text = text + " " + _tokens[ i ];
		  	}
		  }
		  return text;
    }
    
  
    //Returns the short text.
    function getShortChirp(text)
    {
    	var short_text = text;
    	if( short_text.length > 60 )
    	{
    		short_text = "";
	    	var _tokens = text.split("<wbr>");

			  for ( var i = 0; i < _tokens.length; i++ )
			  {
			  	if( (short_text.length + _tokens[i].length) > 60 )
			  	{	 
			  		return short_text;		
			    }
			  	else
			  	{
			  		short_text = short_text + "<wbr>" + _tokens[ i ];
			  	}
			  }
		  }
		  return short_text;
    }
    
     // ********* END ************ Code to add <WBR>