if (!window.GBResources) {
(function() {


var baseClass = function(base, attrs) {
    var prototype = base;
    if (attrs === undefined) {
        for (var attr in attrs) {
            prototype[attr] = attrs[attr];
        }
    }
    var constructor = (prototype && prototype.__init__) || function() { };
    constructor.prototype = prototype;
    return constructor;
};



//

window.GBResources = new baseClass({
    __init__: function(p) {
        var self = this;
        self.strings  = {};
        self.images   = {};
        self.features = {};
        self.preload_img_div = null;
        if (p == null) {
            return;
        }
        if (p.strings) {
            self.registerStrings (p.strings);
        }
        if (p.images) {
            self.registerImages (p.images);
        }
        if (p.features) {
            self.registerFeatures (p.features);
        }
    },
    
    registerStrings: function (stringsHash) {
        var self = this;
        self.registerResources (self.strings, stringsHash);
    },
    
    
    
    
    getString: function (stringId, hashGet) {
        var self = this;
        
        
        if (!(stringId in self.strings)) {
            self.log ('!!! ERROR: StringId "' + stringId +
                      '" does not exist in the strings repository.');
            return '';
        }
        var str = '' + self.strings[stringId];
        
        
        
        if (hashGet == null) {
            return str;
        }
        
        
        for (var key in hashGet) {
            var regex = new RegExp("\\${" + key + "}", "g");
            str = str.replace(regex, hashGet[key]);
        }
        return str;
    },
    
    
    
    registerImages: function (imagesHash) {
        var self = this;
        self.registerResources (self.images, imagesHash);
        
        for (var imageId in self.imagesHash) {
            self.preloadImage (self.images[imageId]);
        }
    },
    
    
    
    preloadImage: function (imageSrc) {
        var self = this;
        if (!self.preload_img_div) {
            self.preload_img_div = self.div ({'style':'display:none'});
        }
        self.preload_img_div.appendChild (self.img({src:imageSrc}));
    },
    
    getImage: function (imageId) {
        var self = this;
        return self.images[imageId];
    },
    
    registerFeatures: function (featuresHash) {
        var self = this;
        self.registerResources (self.features, featuresHash);
    },
    
    getFeature: function (featureId) {
        var self = this;
        return self.features[featureId];
    },
    
    
    
    
    registerResources: function (resourceStore, resourcesHash) {
        var self = this;
        if (resourcesHash === null || resourceStore === null) {
            return;
        }
        for (var resourceId in resourcesHash) {
            resourceStore[resourceId] = resourcesHash[resourceId];
        }
    },
    
    
    
    
    img: function(attrs) {
        var self = this;
        attrs = attrs || {};
        attrs.border = attrs.border || 0;
        return self.el('img', attrs);
    },
    div: function(attrs, children) {
        var self = this;
        return self.el('div', attrs, children);
    },
    el: function(type, attrs, children) {
        var self = this;
        var el = document.createElement(type);
        if (attrs) {
            self.set_attributes(el, attrs);
        }
        if (children) {
            self.appendChildren(el, children);
        }
        return el;
    },
    set_attributes: function(el, attrs) {
        for (var attr in attrs) {
            if (attr == 'style') {
                el.style.cssText = attrs[attr];
            } else {
                var new_attr = {
                    "class": "className",
                    "checked": "defaultChecked",
                    "usemap": "useMap",
                    "for": "htmlFor",
                    "readonly": "readOnly",
                    "colspan": "colSpan",
                    "bgcolor": "bgColor",
                    "cellspacing": "cellSpacing",
                    "cellpadding": "cellPadding",
                    "valign":"vAlign",
                    "nowrap":"noWrap"
                }[attr] || attr;
                el[new_attr] = attrs[attr];
            }
        }
    },
    log: function (msg) {
        var self = this;
        msg = new Date() + ': ' + msg;
        if (window.console) {
            window.console.log (msg);
        }
    }
});
})();}

