/**
 * Templates class provides reusedable HTML-snippets and marker replacement
 *
 * @author Karsten John Gerber (karsten.john.gerber[at]googlemail.com)
 */
LIB3m5.JQueryTemplates = (function($) {

    /** this pointer **/
    var base;

	var JQueryTemplates = function() {
        base = this;
		base.clazz = JQueryTemplates;
		base.$ = $(this);
    };

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // PRIVATE VARS
    /** the collection of template snippets **/
    var templates = {};
    /** regexp for removing javascripts **/
    var rscript = /<myscript\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi;
    /** fallback if template is not found **/
    var fallback = '<div class="unknown_template_${NAME}"></div>';
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // PUBLIC METHODS
    /**
     * parse the given HTML for all elements having a certain css class
     * @param {String} data the raw HTML data
     * @param {String} cssClassDefiningTheBlocks the css class defining the template blocks
     */
    JQueryTemplates.prototype.parse = function(data, cssClassDefiningTheBlocks){
        // bild a dummy div and look for all div with the class "cssClassDefiningTheBlocks" and create a template from its inner html
        var wrap = $('<script type="text/html"></script>');
        if ($.browser.msie) {
            var browserVersion = parseInt($.browser.version, 10);
            if (browserVersion < 9) {
                var wrap = $('<div></div>');
            }
        }
        wrap.append(data.replace(rscript, ""));

        wrap.find("." + cssClassDefiningTheBlocks).each(function(idx, e) {
            createTemplate(e);
        });
    };

    /**
     * Check if there is a template with the given name
     *
     * @param {String} name the name of template
     * @return {Boolean} true if template exists
     */
    JQueryTemplates.prototype.hasTemplate = function(name) {
        return templates.hasOwnProperty(name);
    };

    /**
     * Get a template and replace the markers.
     *
     * @param {String} name the name of the template
     * @param {Object} replacements the replacements as key value pairs, where key is the marker name and value the replacement
     *
     * @return {Object} a jQuery-Object containing the templates content, and if the template was not found a standard return value (@see fallback)
     */
    JQueryTemplates.prototype.get = function(name, replacements) {
        if (this.hasTemplate(name)) {
            var txt = templates[name];
            txt = LIB3m5.JQueryToolbox.replaceMarkersInString(txt, replacements);
            // detect pure text and wrap it into a <div>
            try {
                var $txt = $(txt);
                if ($txt.html() != null) {
                    return $txt;
                }
            }
            catch(e) {}

            return $("<div>" + txt + "</div>");
        }
        $.log("unknown template", name);
        return $(LIB3m5.JQueryToolbox.replaceMarkersInString(fallback, { NAME: name}));
    };
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // PRIVATE METHODS
    /**
     * creates a reusable template from a given dom-object
     *
     * @param {Object} value the html snippet to create the template from
     */
    var createTemplate = function(value) {
        var txt = $(value).html();
        try {
            txt = txt.replace(/\%7B/gi, "{");
            txt = txt.replace(/\%7D/gi, "}");
        }
        catch (e) {}
        // create the reuseable template
        templates[value.id] = txt;
    };

    return JQueryTemplates;
})(jQuery);
