/**
 * TrimPath TT Emulation Modifiers. Release 0.1.
 * Copyright (C) 2005 Brian Bittman.
 * 
 * This code is licensed under the GNU General Public License
 * and the Apache License, Version 2.0, as follows:
 *
 * This program is free software; you can redistribute it and/or 
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed WITHOUT ANY WARRANTY; without even the 
 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
var TT_Filters = {
   
	round: function(num) {
		return Math.round(num);
	},
	floor: function(num) {
		return Math.floor(num);
	},
	ceil: function(num) {
		return Math.ceil(num);
	},


   add_commas : function(num) {
     num += "";
     num = num.replace(/(\d)(\d{3,3})?(\d{3,3})?(\d{3,3})?(\d{3,3})?(\d{3,3})?(\d{3,3})$/,"$1,$2,$3,$4,$5,$6,$7");
     num = num.replace(/,{2,}/,",");
     return num;
   },
   
    upper : function(str) { return str.toUpperCase(); },
    lower : function(str) { return str.toLowerCase(); },
    
    ucfirst : function(str) { 
        return str.toString().substring(0, 1).toUpperCase() + str.substring(1, str.length); 
    },
    lcfirst : function(str) { 
        return str.toString().substring(0, 1).toLowerCase() + str.substring(1, str.length); 
    },

		titleCase: function(str) {
			if( !str || str == "" ) {
				return "";
			}
			var original = str;
			var o_split = original.split(" ");
			var special_words = new Array('and', 'the', 'to', 'for', 'is', 'in', 'a', 'at', 'an', 'from', 'by', 'if', 'of');
			for (i=0;i<o_split.length;i++) {
				if (i == 0) {
					//always capitalize the first word
					o_split[i] = (o_split[i].substring(0,1)).toUpperCase() + o_split[i].substring(1);
				} else if(special_words.indexOf(o_split[i]) < 0) { 
				  o_split[i] = (o_split[i].substring(0,1)).toUpperCase() + o_split[i].substring(1);
				}
			}
			retval = o_split.join(' ');
			return retval;
		},

		quoteCase: function(str) {
			if( !str || str == "" ) {
				return "";
			}
			var new_str = str.toLowerCase();
			// new_str = new_str.replace(/(&ldquo;|&rdquo;)/,'"');
			new_str = new_str.replace(/^("*)/,'');
			new_str = new_str.replace(/("*)$/,'');
			return '"' + this.ucfirst(new_str) + '"';
		},

    trim : function(str) {
        return str.toString().replace(/^\s+/, '').replace(/\s+$/, '');
    },
    
    collapse : function(str) {
        return str.toString().replace(/^\s+/, '').replace(/\s+$/, '').replace(/\s+/g, ' ');
    },
    
    html : function(str) { 
    	if(str) {      
        	return str.toString().replace(/&/g, '&amp;').replace(/\</g, '&lt;').replace(/\>/g, '&gt;');
    	}
    },

    link : function(str) { 
    	if(str) {      
        	return str.toString().replace(/&/g, '&amp;').replace(/\</g, '&lt;').replace(/\>/g, '&gt;').replace(/"/g, '').replace(/\s/g, '%20');
    	}
    },


    js : function(str) { 
    	if(str) {      
        	return str.toString().replace(/'/g, '').replace(/\n|\r/g, '')
    	}
    },
        
    html_entity : function(str) {
        return "HTML_ENTITY: not implemented.  see http://www.w3.org/TR/REC-html40/sgml/entities.html";
    },
    
    html_para : function(str) {
        return str.toString().replace(/\n+/g, '<p>');
    },
    
    html_break : function(str) {
        return str.toString().replace(/\n+/g, '<br><br>');
    },
    
    html_line_break : function(str) {
        return str.toString().replace(/\n/g, '<br>');
    },
    
    uri : function(str) {
        return escape(str); 
    },

    unescape_uri : function(str) {
    	str = str.replace(/\+/g, ' ');
        return unescape(str); 
    },
        
    indent : function(str, pad) {
        if(!pad) pad = '    ';
        var lines = str.split(/\n/);
        var ret = "";
        for(var i=0; i<lines.length; i++) {
            lines[i] = lines[i].toString().replace(/^/g, pad);
        }
        return lines.join("\n");
    },
    
    truncate : function(str, length) {
    	if(str) {
	        if(!length) length = 32;
	        if(str.length <= length) return str;
	        return str.toString().substring(0, length - 3) + "...";
    	}
    },
    
    repeat : function(str, iterations) {
        if(!iterations) iterations = 1;
        var ret = str;
        for(var i=iterations - 1; i>0; i--) {
            ret += str;
        }
        return ret;
    },
    
    remove : function(str, regpattern) {
        if(!regpattern) return str;        
        var regex = new RegExp(regpattern, 'g');
        return str.toString().replace(regex, '');
    },
    
    replace : function(str, regpattern, replace) {
    	if(str) {
	        if(!regpattern) return str;
	        if(!replace) replace = '';
	        var regex = new RegExp(regpattern, 'g');
	        str = str.toString().replace(regex, replace);
    	}
    	return str;
    },
    
    
    strftime : function (time, fmt) {
    	var t = new Date();
    	var mili_time = time;
    	// check if unix seconds, if so, add millis
    	if(time < 10000000000) {
    		mili_time = time + "" + "000";
    	}
    	t.setTime(mili_time);
    	for (var s in TT_strftime_funks) {
	        if (s.length == 1 ) {
	            fmt = fmt.replace('%' + s, TT_strftime_funks[s](t));
	    	}
    	}
    	return fmt;
    },
    
    alert : function(str) {
        alert(str);
        return "";
    },
    
    clean_entities : function(str) {
    	if(str) {
	        return TT_cleanEntities(str);
    	}
        //return TT_cleanEntities(str);
    },

    html_noentities : function(str) {
    	if(str) {
        	return str.toString().replace(/\</g, '&lt;').replace(/\>/g, '&gt;').replace(/"/g, '&quot;');
    	}
    },

    html_entities : function(str) {
        return TT_cleanEntities(str).toString().replace(/\</g, '&lt;').replace(/\>/g, '&gt;');
    }
    
}


/* other support functions -- thanks, ecmanaut! */
TT_strftime_funks = {
  zeropad: function( n ){ return n>9 ? n : '0'+n; },
  a: function(t) { return ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'][t.getDay()] },
  A: function(t) { return ['Sunday','Monday','Tuedsay','Wednesday','Thursday','Friday','Saturday'][t.getDay()] },
  b: function(t) { return ['Jan','Feb','Mar','Apr','May','Jun', 'Jul','Aug','Sep','Oct','Nov','Dec'][t.getMonth()] },
  B: function(t) { return ['January','February','March','April','May','June', 'July','August',
      'September','October','November','December'][t.getMonth()] },
  c: function(t) { return t.toString() },
  d: function(t) { return this.zeropad(t.getDate()) },
  H: function(t) { return this.zeropad(t.getHours()) },
  I: function(t) { return (t.getHours()==12||t.getHours()==0) ? 12 : this.zeropad((t.getHours() + 12) % 12) },
  i: function(t) { return (t.getHours()==12||t.getHours()==0) ? 12 : (t.getHours() + 12) % 12 },
  m: function(t) { return this.zeropad(t.getMonth()+1) }, // month-1
  M: function(t) { return this.zeropad(t.getMinutes()) },
  p: function(t) { return this.H(t) < 12 ? 'am' : 'pm'; },
  S: function(t) { return this.zeropad(t.getSeconds()) },
  w: function(t) { return t.getDay() }, // 0..6 == sun..sat
  y: function(t) { return this.zeropad(this.Y(t) % 100); },
  Y: function(t) { return t.getFullYear() },
  '%': function(t) { return '%' }
}


// thanks chris and text mate!
function TT_cleanEntities(str) {
  var ENTITY_MAP = { /* 34: 'quot', 39: 'apos', 38: 'amp', 60: 'lt', 62: 'gt',*/ 160: 'nbsp', 161: 'iexcl', 164: 'curren', 162: 'cent', 163: 'pound', 165: 'yen', 166: 'brvbar', 167: 'sect', 168: 'uml', 169: 'copy', 170: 'ordf', 171: 'laquo', 172: 'not', 173: 'shy', 174: 'reg', 8482: 'trade', 175: 'macr', 176: 'deg', 177: 'plusmn', 178: 'sup2', 179: 'sup3', 180: 'acute', 181: 'micro', 182: 'para', 183: 'middot', 184: 'cedil', 185: 'sup1', 186: 'ordm', 187: 'raquo', 188: 'frac14', 189: 'frac12', 190: 'frac34', 191: 'iquest', 215: 'times', 247: 'divide', 192: 'Agrave', 193: 'Aacute', 194: 'Acirc', 195: 'Atilde', 196: 'Auml', 197: 'Aring', 198: 'AElig', 199: 'Ccedil', 200: 'Egrave', 201: 'Eacute', 202: 'Ecirc', 203: 'Euml', 204: 'Igrave', 205: 'Iacute', 206: 'Icirc', 207: 'Iuml', 208: 'ETH', 209: 'Ntilde', 210: 'Ograve', 211: 'Oacute', 212: 'Ocirc', 213: 'Otilde', 214: 'Ouml', 216: 'Oslash', 217: 'Ugrave', 218: 'Uacute', 219: 'Ucirc', 220: 'Uuml', 221: 'Yacute', 222: 'THORN', 223: 'szlig', 224: 'agrave', 225: 'aacute', 226: 'acirc', 227: 'atilde', 228: 'auml', 229: 'aring', 230: 'aelig', 231: 'ccedil', 232: 'egrave', 233: 'eacute', 234: 'ecirc', 235: 'euml', 236: 'igrave', 237: 'iacute', 238: 'icirc', 239: 'iuml', 240: 'eth', 241: 'ntilde', 242: 'ograve', 243: 'oacute', 244: 'ocirc', 245: 'otilde', 246: 'ouml', 248: 'oslash', 249: 'ugrave', 250: 'uacute', 251: 'ucirc', 252: 'uuml', 253: 'yacute', 254: 'thorn', 255: 'yuml', 338: 'OElig', 339: 'oelig', 352: 'Scaron', 353: 'scaron', 376: 'Yuml', 710: 'circ', 732: 'tilde', 8194: 'ensp', 8195: 'emsp', 8201: 'thinsp', 8204: 'zwnj', 8205: 'zwj', 8206: 'lrm', 8207: 'rlm', 8211: 'ndash', 8212: 'mdash', 8216: 'lsquo', 8217: 'rsquo', 8218: 'sbquo', 8220: 'ldquo', 8221: 'rdquo', 8222: 'bdquo', 8224: 'dagger', 8225: 'Dagger', 8230: 'hellip', 8240: 'permil', 8249: 'lsaquo', 8250: 'rsaquo', 8364: 'euro' };
 
	var chars = str.split("");
	var newtext = '';
	for(i = 0; i < chars.length; i++) {
  	var chr = chars[i].charCodeAt(0);
    if( ENTITY_MAP[chr] ) 
      newtext += '&'+ENTITY_MAP[chr]+';';
    else if( chr > 127 ) 
      newtext += '&#'+chr+';';
    else 
      newtext += chars[i];
  } 
    
  return newtext;
}
