/**
 * 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 = {

    upper : function(str) { return str.toUpperCase(); },
    lower : function(str) { return str.toLowerCase(); },

    ucfirst : function(str) {
        return str.substring(0, 1).toUpperCase() + str.substring(1, str.length);
    },
    lcfirst : function(str) {
        return str.substring(0, 1).toLowerCase() + str.substring(1, str.length);
    },

    trim : function(str) {
        return str.replace(/^\s+/, '').replace(/\s+$/, '');
    },

    collapse : function(str) {
        return str.replace(/^\s+/, '').replace(/\s+$/, '').replace(/\s+/g, ' ');
    },

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

    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.replace(/\n+/g, '<p>');
    },

    html_break : function(str) {
        return str.replace(/\n+/g, '<br><br>');
    },

    html_line_break : function(str) {
        return str.replace(/\n/g, '<br>');
    },

    uri : function(str) {
    /*Note that URI escaping isn't always enough when generating hyperlinks in an HTML document.
    The '&' character, for example, is valid in a URI and will not be escaped by the URI filter.
    In this case you should also filter the text through the 'html' filter.

    <a href="${filename|uri|html}">click here</a>
    */
        return "URI: not implemented. ";
    },

    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].replace(/^/g, pad);
        }
        return lines.join("\n");
    },

    truncate : function(str, length) {
        if(!length) length = 32;
        if(str.length <= length) return str;
        return str.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.replace(regex, '');
    },

    replace : function(str, regpattern, replace) {
        if(!regpattern) return str;
        if(!replace) replace = '';
        var regex = new RegExp(regpattern, 'g');
        return str.replace(regex, replace);
    },

    alert : function(str) {
        alert(str);
        return "";
    },

    highlight : function(str, regpattern) {
        if(!regpattern) return str;
        var regex = new RegExp('(' + regpattern + ')', 'gi');
        return str.replace(regex, '<em>$1</em>');
    },

    amzimg : function(str) {
        var s = '_SCTHUMBZZZ_';
        var p = str.indexOf(s);
        if (0 <= p) {
            var mr = 12;
            var r = mr - (Math.random() * mr * 2);
            // return str.substr(0, p) + '_P' + (0 <= r ? 'B' : 'C') + '_PU' + r + s;
            // return str.substr(0, p) + '_PC_PU' + r + s;
            return str.substr(0, p) + '_PC_PU' + r + '_SH50_TZZZZZZZ_' + str.substr(p + s.length);
        }
        else {
            return str;
        }
    },

    amzimg_shadow : function(str) {
        var s = '_SCTHUMBZZZ_';
        var p = str.indexOf(s);
        if (0 <= p) {
            return str.substr(0, p) + '_PA0,1,1,2_TZZZZZZZ_' + str.substr(p + s.length);
        }
        else {
            return str;
        }
    }

}
