/*
 * ** This is the standard bootstrap script **
 */ 
COMPRESSED=true;
(function() {
    window.SpotMain = {
        scriptName: 'SpotMain.js',
        scriptPath: 'shared/js/',
        contextPath: 'js/',
        libsToLoad: [
            'Libs/ext-2.2/adapter/jquery/jquery.js',
  //          'Libs/ext-2.2/adapter/jquery/ext-jquery-adapter.js',
            'Libs/ext-2.2/ext-custom.js',
            'Libs/elabel.js',
            'Libs/markermanager.js'
        ],
        scriptsToLoad: [
            'Spot.js',
            'Spot/BaseClass.js',
            'Spot/Util.js',
            'Spot/Util/Feed.js',
            'Spot/Util/FeedManager.js',
            'Spot/Ui.js',
            'Spot/Ui/AlertWindow.js',
            'Spot/Ui/SpotLogo.js',
            'Spot/Ui/MapRenderer.js',
            'Spot/Ui/LiveWidget.js'
        ],
        cssToLoad: [
            'Libs/ext-2.2/resources/css/ext-all.css',
            'Libs/ext-2.2/resources/css/xtheme-black.css',
            'Spot/Resources/Css/main.css'
        ],
        scriptLocation: null,
        rootLocation: null,
        contextLocation: null
    };

    SpotMain.getScriptLocation = function() {
        if (SpotMain.scriptLocation == null) {
            var scripts = document.getElementsByTagName('script');
            for (var i = 0; i < scripts.length; i++) {
                var src = scripts[i].getAttribute('src');
                if (src) {
                    var index = src.lastIndexOf(SpotMain.scriptName);
                    // set path length for src up to a query string
                    var pathLength = src.lastIndexOf('?');
                    if (pathLength < 0) {
                        pathLength = src.length;
                    }
                    // is it found, at the end of the URL?
                    if ((index > -1) && (index + SpotMain.scriptName.length == pathLength)) {
                        SpotMain.scriptLocation = src.slice(0, pathLength - SpotMain.scriptName.length);
                        break;
                    }
                }
            }
        }
        return SpotMain.scriptLocation;
    };

    SpotMain.getRootLocation = function() {
        if (SpotMain.rootLocation == null) {
            var scriptLoc = SpotMain.getScriptLocation();
            var index = scriptLoc.lastIndexOf(SpotMain.scriptPath);
            var pathLength = scriptLoc.length;
            if ((index > -1) && (index + SpotMain.scriptPath.length == pathLength)) {
                SpotMain.rootLocation = scriptLoc.slice(0, pathLength - SpotMain.scriptPath.length);
            } else {
                SpotMain.rootLocation = '';
            }
        }
        return SpotMain.rootLocation;
    };


    SpotMain.getContextLocation = function() {
        if (SpotMain.contextLocation == null) {
            var scriptLoc = SpotMain.getScriptLocation();
            var index = scriptLoc.lastIndexOf(SpotMain.contextPath);
            var pathLength = scriptLoc.length;
            if ((index > -1) && (index + SpotMain.contextPath.length == pathLength)) {
                SpotMain.contextLocation = scriptLoc.slice(0, pathLength - SpotMain.contextPath.length);
            } else {
                SpotMain.contextLocation = '';
            }
        }
        return SpotMain.contextLocation;
    };

    SpotMain.loadScripts = function(scriptLoc, scriptArray) {
        var agent = navigator.userAgent;
        var docWrite = (agent.match("MSIE") || agent.match("Safari"));
        if(docWrite) {
            var allScriptTags = new Array(scriptArray.length);
        }
        for (var i = 0; i < scriptArray.length; i++) {
            if (scriptArray[i].match("jquery.js") != null) {
                // jquery loading - check if it is already loaded
                if (typeof(jQuery) != 'undefined') {
                    // jquery is already loaded - so skip
                    continue;
                }
            }
            if (docWrite) {
                if (scriptArray[i].indexOf('http') == 0) {
                    allScriptTags[i] = '<script src="' + scriptArray[i] + '"></script>';
                } else {
                    allScriptTags[i] = '<script src="' + scriptLoc + scriptArray[i] + '"></script>';
                }
            } else {
                var scriptElem = document.createElement('script');
                var headElem = document.getElementsByTagName('head');
                if (scriptArray[i].indexOf('http') == 0) {
                    scriptElem.src = scriptArray[i];
                } else {
                    scriptElem.src = scriptLoc + scriptArray[i];
                }
                var insertPoint = headElem.length ? headElem[0] : document.body;
                insertPoint.appendChild(scriptElem);
            }
        }
        if (docWrite) {
            document.write(allScriptTags.join(''));
        }
    };
 
     SpotMain.loadCss = function(scriptLoc, cssArray) {
        var agent = navigator.userAgent;
        var docWrite = (agent.match("MSIE") || agent.match("Safari"));
        if(docWrite) {
            var allCssTags = new Array(cssArray.length);
        }
        for (var i = 0; i < cssArray.length; i++) {
            if (docWrite) {
                if (cssArray[i].indexOf('http') == 0) {
                    allCssTags[i] = '<link rel="stylesheet" type="text/css" href="' + cssArray[i] + '"></link>';
                } else {
                    allCssTags[i] = '<link rel="stylesheet" type="text/css" href="' + scriptLoc + cssArray[i] + '"></link>';
                }
            } else {
                var linkElem = document.createElement('link');
                var headElem = document.getElementsByTagName('head');
                linkElem.rel='stylesheet';
                linkElem.type='text/css';
                if (cssArray[i].indexOf('http') == 0) {
                    linkElem.href = cssArray[i];
                } else {
                    linkElem.href = scriptLoc + cssArray[i];
                }
                var insertPoint = headElem.length ? headElem[0] : document.body;
                insertPoint.appendChild(linkElem);
            }
        }
        if (docWrite) {
            document.write(allCssTags.join(''));
        }
    };

    SpotMain.startLoad = function(loadable) {
        var scriptLoc = SpotMain.getScriptLocation();
        SpotMain.loadCss(scriptLoc, SpotMain.cssToLoad);
        SpotMain.loadScripts(scriptLoc,loadable.libsToLoad);
        var isCompressed = (typeof COMPRESSED != 'undefined' && COMPRESSED == true);
        if (!isCompressed) {
            SpotMain.loadScripts(scriptLoc,loadable.scriptsToLoad);
        } else {
            var scriptArray = new Array();
            scriptArray.push(loadable.scriptsToLoad[0]);
            SpotMain.loadScripts(scriptLoc,scriptArray);
        }
    };
    
    SpotMain.startLoad(SpotMain);
})();
