/*
    All of the items this file dynamically adds
    get combined and compressed on deployment.
    
    The following line is used to tag this file as a combiner file
    @rivet[2.0]
    
    1. Change the combinerFileName to be the same as this javascript file
	   (This can be a string or a regular expression)
    2. Add includes.push("javascriptfile.js") line for each file you want to include
    
	After copying to a deployment directory run the Rivet.Console.exe
	pointing to it.
	eg: Rivet.Console.exe "c:\path\to\jsdirecory\"
    
    Full documentation can be found at http://github.com/davetayls/rivet
---------------------------------------------------*/
(function () {

    // UPDATE THE FOLLOWING
    var combinerFileName = "lib.js";
    var includes = [];
    includes.push("jquery-1.5.2.min.js");
    includes.push("jquery-plugins/jquery.easing.js");
    includes.push("debug.js");
    includes.push("jquery-ui-1.8.10.min.js");
    includes.push("jquery-plugins/jquery.hint.js");
    includes.push("jquery-plugins/jquery.enterSubmit.js");
    includes.push("jquery-plugins/jquery.cycle.all.min.js");
    includes.push("jquery-plugins/jquery.tools.scrollable.min.js");
    includes.push("jquery-plugins/jquery-ui-accordian.min.js");
    includes.push("jquery-plugins/jquery.windowViewer.js");
    includes.push("jquery-plugins/tweets/jquery.tweet.js");
    // maps
//    includes.push("jquery-maps/maps-v3infoWindow.js");
//    includes.push("jquery-maps/maps-v3pin.js");
//    includes.push("jquery-maps/jquery.maps.js");


    /* ********* DON'T CHANGE ANYTHING BELOW THIS LINE ******************************/
    var dirSeparator;
    function getJsLocation(jsFileName) {
        var location;
        if (typeof jsFileName === 'string') {
            jsFileName = new RegExp(jsFileName.toLowerCase());
        }
        // If used within a browser
        if (typeof document !== 'undefined' && typeof document.getElementsByTagName !== 'undefined') {
            var scriptFiles = document.getElementsByTagName("script");
            var found = false;
            for (var i = 0; i < scriptFiles.length; i += 1) {
                var scriptTag = scriptFiles[i];
                var scriptFileName = scriptTag.src.substring(scriptTag.src.lastIndexOf("/") + 1).toLowerCase();
                if (jsFileName.test(scriptFileName)) {
                    location = scriptTag.src.substring(0, scriptTag.src.lastIndexOf("/") + 1);
                    found = true;
                }
            }
            if (!found) { throw 'Rivet Combiner could not match ' + jsFileName + ' to any embeded script references'; }
        }
        // is used with rhino
        else if (typeof java !== 'undefined') {
            location = new java.io.File(environment['user.dir']).toString();
        }
        // if used with cscript
        else if (WScript) {
            location = WScript.CreateObject("WScript.Shell").CurrentDirectory;
        } else {
            throw 'Juxtapo Combiner could not retrieve the working directory';
        }
        dirSeparator = location.indexOf('\\') === -1 ? '/' : '\\';
        location = location.substr(location.length - 1, 1) === dirSeparator ? location : location + dirSeparator;
        return location;
    };
    var combinerJsLocation = getJsLocation(combinerFileName);
    function resolveAbsoluteUrl(baseUrl, relativeUrl) {
        try {
            if (relativeUrl.substr(0, 1) === '/') {
                return baseUrl.substring(0, baseUrl.indexOf('/', baseUrl.indexOf('//') + 2)) + relativeUrl;
            }
            else {
                relativeUrl = dirSeparator === '/' ? relativeUrl : relativeUrl.replace(/\//g, '\\');
                var Loc = baseUrl;
                Loc = Loc.substring(0, Loc.lastIndexOf(dirSeparator));
                while (/^\.\./.test(relativeUrl)) {
                    Loc = Loc.substring(0, Loc.lastIndexOf(dirSeparator));
                    relativeUrl = relativeUrl.substring(3);
                }
                return Loc + dirSeparator + relativeUrl;
            }
        }
        catch (ex) {
            print('d:' + ex);
        }
    }
    function includeJs() {
        for (var i = 0; i < includes.length; i++) {
            var src = resolveAbsoluteUrl(combinerJsLocation, includes[i]);

            // If used within a browser
            if (typeof document !== 'undefined' && typeof document.getElementsByTagName !== 'undefined') {
                document.write("<script type=\"text/javascript\" src=\"" + src + "\"></script>");
            }
            // is used with rhino
            else if (typeof load !== 'undefined') {
                load(src);
            }
            // if used with cscript
            else if (typeof ActiveXObject !== 'undefined') {
                eval(new ActiveXObject("Scripting.FileSystemObject").OpenTextFile(src, 1).ReadAll());
            }
            else {
                throw ('combiner is not compatible with this JavaScript engine');
            }
        }
        return null;
    };
    var getQuery = function (ji) {
        var hu = window.location.search.substring(1);
        var gy = hu.split("&");
        for (var i = 0; i < gy.length; i += 1) {
            var ft = gy[i].split("=");
            if (ft[0] === ji) {
                return ft[1];
            }
        }
        return null;
    };
    if (getQuery('rivet') !== 'nojs') {
        includeJs();
    }
})();

