//namespace Axisto
if(typeof(Axisto) == 'undefined')
    Axisto = {};

Axisto.Loader = (function() {
    // private fields
    var jqPath = 'js/jquery/jquery-1.2.6.js';
    var jqVersionRequired = '1.2.6';
    var axp = 'axp.js';
    var axpLoaderPath = 'axp-loader.js';
    var basePath = null;
    var baseQueryString = '';
    //private methods

    return {
        //public fields
        // public methods
        getCSS: function(url) {
            document.write('<link rel="stylesheet" type="text/css" href="' + url + '">');
        },
        getScript: function(url) {
            document.write(unescape('%3Cscript src="' + url + '" type="text/javascript"%3E%3C/script%3E'));
        },
        getScriptUrl: function(scriptName) {
            var u = null;
            var s = document.getElementsByTagName('SCRIPT');
            for (var i = 0; i < s.length; i++) {
                if (s[i].src != null && s[i].src.indexOf(scriptName) > 0) {
                    u = s[i].src;
                    break;
                }
            }
            return u;
        },
        getBasePath: function(scriptName) {
            var bp = { path: '', qs: '' };
            if (basePath && (scriptName == axp || scriptName == null)) {
                //return cached base path
                bp = basePath;
            }
            else {
                var regexp = new RegExp(scriptName);
                bp.path = this.getScriptUrl(scriptName).replace(regexp, '');
                if (bp.path.indexOf('?') > -1) {
                    var x = bp.path.split('?');
                    bp.path = x[0];
                    bp.qs = '?' + x[1];
                }

                if (!basePath && (scriptName == axp || scriptName == null)) {
                    basePath = bp;
                }
            }
            
            return bp;
        },
        parseVersion: function(version) {
            var v = { major: 0, minor: 0, release: 0, value: 0 };
            if (typeof (version) == 'string' && version.length > 0) {
                var r = version.split('.');
                if (version.length > 2) {
                    v.major = parseInt(r[0]);
                    v.minor = parseInt(r[1]);
                    v.release = parseInt(r[2]);
                    v.value = parseInt(r[0]) * 100000 + parseInt(r[1]) * 1000 + parseInt(r[2])
                }
            }
            return v;
        },
        loadPlayer: function() {
            //load axp
            var bp = this.getBasePath(axp);
            if (bp.path) {
                //check jquery presence
                if (typeof (jQuery) != 'function') {
                    this.getScript(bp.path + jqPath);
                }
                else if (this.parseVersion(jQuery.prototype.jquery).value < this.parseVersion(jqVersionRequired).value) {
                    this.getScript(bp.path + jqPath);
                }

                //load axp-loader.js
                this.getScript(bp.path + axpLoaderPath + bp.qs);
            }
        }
    };
})();

Axisto.Loader.loadPlayer();
