var jsCore = jsCore || {};

jsCore.Base = new Class({

    Implements: Options,

    helperInstance: null,

    initialize: function (options, iOptions, iObjects) {
        iOptions !== false && this.setOptions(options);
        iObjects !== false && this.grabObjects(this.elements, this.options.objects);
        this.helperInstance = new jsCore.Helpers();
    },

    load: function () {
        jsCore.Class = [];

        Array.from(arguments).each(function (argument) {
            if (typeOf(window[argument[0]]) == 'class') {
                // Callback to class executer
                var extension = function () {
                    jsCore.Class[argument[0]] = new window[argument[0]](argument[1] || {});
                };
                // If need call class after domready event
                if (argument[1] && argument[1].domready === true) {
                    window.addEvent('domready', extension);
                } else {
                    // Call at once
                    extension.call();
                }
            } else {
                throw new Error('Class: ' + argument[0] + ' not exist');
            }
        });
        
        return this;
    },

    iGet: function () {
        var section = arguments[0].match(/^(.*?)::(.*)$/);

        if (!section) {
            throw new Error('Incorrect call base getter method');
        }
        
        switch (section[1]) {
            case 'text':
                return typeof(arguments[1]) == 'object' ? this.options.texts[section[2]].substitute(arguments[2]) : this.options.text[section[2]];
                break;
            case 'class':
                return this.options.classes[section[2]];
                break;
            case 'object':
                return this.options.objects[section[2]];
                break;
            case 'lang':
                return this.parceOption(key, arguments[2] || globalDynamicLang || {});
                break;
            case 'option':
                return this.parceOption(key, arguments[2] || coreOptions || {});
                break;
            case 'helper':
                if (typeOf(this.helperInstance[section[2]]) != 'function') {
                    throw new Error('Helper: ' + section[2] + ' not exist');
                }
                return this.helperInstance[section[2]].apply(this.helperInstance, Array.from(arguments).slice(1).flatten());
                break;
        }
    },

    parceOption: function (key, store) {
        var propertyName,
        keyArray = key.split("::");
        for (var segment in keyArray) {
            if (keyArray.hasOwnProperty(segment)) {
                propertyName = propertyName == null ? store[keyArray[segment]] : propertyName[keyArray[segment]];
                if (propertyName === undefined) {
                    throw new Error('Error getting section name: ' + keyArray[segment]);
                }
            }
        }
        return propertyName;
    }

});

jsCore.Helpers = new Class({

    Extends: jsCore.Base,

    elements: {},

    initialize: function () {
        return this;
    },

    go: function (param) {
        window.location.href = param;
    },

    isValidUrl: function (str) {
        return str.test(/(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/)
    }
});
