if (typeof(UofC) === "undefined") { UofC = {}; }

UofC.Browser = {

    userAgent: function() {
        return navigator.userAgent.toLowerCase();
    },
    
    isInternetExplorer: function() {
        var ua = this.userAgent();
        
        return !!ua.match(/msie/i);
    }

};

UofC.Scripting = {
  
  addJavaScript: function(scriptURL) {
    var js = document.createElement("script");
    
    js.setAttribute("type", "text/javascript");
    js.setAttribute("src", scriptURL);
    
    document.getElementsByTagName("head")[0].appendChild(js);
  }
  
};

UofC.IEStyleSheets = {
    
    prepareStyleSheets: function() {
        var styleSheets = document.styleSheets;

        for (var i = 0; i < styleSheets.length; i++) {
            this.importJavaScriptsForStyleSheet(styleSheets[i]);
        }
        
        UofC.Scripting.addJavaScript("https://static.ucalgary.ca/libraries/ie7-js/IE8_uofc.js");
    },
    
    importJavaScriptsForStyleSheet: function(styleSheet, path, level) {
        if (!level) { level = 0; }
        
        if (level < 3) {
            for (var i = 0; i < styleSheet.imports.length; i++) {
                this.importJavaScriptsForStyleSheet(styleSheet.imports[i], this.getPath(styleSheet.href, path), level + 1);
            }
        }

        var href = this.makePath(styleSheet.href, path);
        var pathExtensionIndex = href.length - 4;
        
        if (href.length > 26 && href.substr(0, 26) == "https://static.ucalgary.ca/" && href.substr(pathExtensionIndex, 4) == ".css") {
            href = href.substr(0, pathExtensionIndex) + ".iecss";

            var js = document.createElement("script");

            js.setAttribute("type", "text/javascript");
            js.setAttribute("src", href);

            document.getElementsByTagName("head")[0].appendChild(js);
        }
    },
    
    getPath: function(href, path) {
        href = this.makePath(href, path);
        
        return href.slice(0, href.lastIndexOf("/") + 1);
    },
    
    makePath: function(href, path) {
        if (this.RELATIVE.test(href)) href = (path || "") + href;
        
        return href;
    },
    
    RELATIVE: /^[\w\.]+[^:]*$/
    
};

(function() {
    
if (UofC.Browser.isInternetExplorer()) {
    UofC.IEStyleSheets.prepareStyleSheets();
}

UofC.Scripting.addJavaScript("https://static.ucalgary.ca/global/scripts/css_browser_selector.js");
    
})();
