var JadeStrings = {
	_initial: function()
	{
		var body = document.getElementById('body');
		var span = document.createElement('span');
		span.id='ruler';
		span.className = 'text-size';
		span.style.visibility='hidden';
		span.style.whiteSpace='nowrap';
		body.appendChild(span);
	},
	Capitalize: function(value)
	{
		return value.substr(0,1).toUpperCase()+value.substr(1).toLowerCase();
	},
	LowerCase: function(value)
	{
		return value.toLowerCase();
	},
	PadLeft: function(value,chr,length)
	{
		for(var i=0;i<length;i++) { value = chr+value; }
		return value.substring(value.length-length);
	},
	PadRight: function(value,chr,length)
	{
		for(var i=0;i<length;i++){ value = value+chr; }
		return value.substring(value.lenght-length);
	},
	UpperCase: function(value)
	{
		return value.toUpperCase();
	},
	Format: function()
	{
		var num = arguments.length; 
		var str = arguments[0];   
		for (var i = 1; i < num; i++) { 
			var pattern = "\\{" + (i-1) + "\\}"; 
			var reg = new RegExp(pattern, "g"); 
			str = str.replace(reg, arguments[i]); 
		} 
		return str; 
	},
	ToDate: function(value){
		if(value.length==8)	{
			return value.substr(6,2)+"/"+value.substr(4,2)+"/"+value.substr(0,4);
		}
		else { return value; }
	},
	ToTime: function(value,seconds){
		if(typeof(seconds)=="undefined") { seconds=true; }
		if(value.length==6 && seconds==true) {
			return value.substr(0,2)+":"+value.substr(2,2)+":"+value.substr(4,2);
		}
		else if(value.length>=4 || (seconds==false && value.length>=4))	{
			return value.substr(0,2)+":"+value.substr(2,2);		
		}
		else{ return value; }
	},
	Trim: function(string) 
	{
		var whitespace = ' \n\r\t\v\f\u00a0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000';
		for (var i = 0; i < string.length; i++) {
			if (whitespace.indexOf(string.charAt(i)) === -1) {
				string = string.substring(i);
				break;
			}
		}
		for (i = string.length; i > 0; i--) {
			if (whitespace.indexOf(string.charAt(i)) === -1) {
				string = string.substring(0, i+1);
				break;
			}
		}
		return whitespace.indexOf(string.charAt(0)) === -1 ? string : '';
	},
	Asc: function(value,position){
		if(typeof(position)=="undefined"){ position=0; }
		return value.charCodeAt(position); 
	},
	Chr: function(value){
		return String.fromCharCode(value);
	},
	ToBytes: function( str ) {
		var ch, st, re = [];
		for (var i = 0; i < str.length; i++ ) {
			ch = str.charCodeAt(i);  // get char 
			st = [];                 // set up "stack"
			do {
				st.push( ch & 0xFF );  // push byte to stack
				ch = ch >> 8;          // shift value down by 1 byte
			}  
			while ( ch );
			re = re.concat( st.reverse() );
		}
		return re;
	},
	ToIntArray: function(str){
		var re=[];
		for(var i=0; i<str.length;i++){
			re.push(parseInt(str.substr(i,1),10));
		}
		return re;
	},
	FromCharArray: function(value){
		var str='';
		for(var i=0;i<value.length;i++){
			str += value[i];
		}
		return str;
	},
	Measure: function(value){
		var ruler = document.getElementById('ruler');
		ruler.innerHTML=value;
		return ruler.offsetWidth;
	},
	VisualTrim: function(value,size){
		if(Jade.Strings.Measure(value)>size){
			var position=parseInt(size/12,10);
			var total_size=size-19;
			while(Jade.Strings.Measure(value.substring(0,position))<total_size){ position++; }
			return value.substring(0,position)+'...';
		}
		else{ return value; }
	},
	StripAccents: function(value){
		var map=['àáâãäçèéêëìíîïñòóôõöùúûüýÿÀÁÂÃÄÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝ'
				,'aaaaaceeeeiiiinooooouuuuyyAAAAACEEEEIIIINOOOOOUUUUY'];
		for(var i=0, iLen=map[0].length;i<iLen;i++){
			value=value.replace(map[0][i], map[1][i]);
		}
		return value;
	}
};
Jade.LoadLibrary('Strings',JadeStrings);