/*-----------------------------------------------------------------------------+
| Product:  index.js - Ajile's default auto-loader for shared dependencies.
|+-----------------------------------------------------------------------------+
| Author:   Michael A. I. Lee [ http://ajile.iskitz.com/ ]
|
| Created:  Friday,    November   2, 2006    [2006.06.02 - 19:44:40 EDT]
| Modified: Saturday   December  16, 2006    [2006.12.16 - 13:00:00 EST]
|+-----------------------------------------------------------------------------+
|
| README:
|
| If you need a way to import/load a common set of scripts for every page on
| your site or in your web application, this is the file you should use.
|
| This index.js file can be used to define scripting dependencies for a page,
| site, or application in a single place for use in many places.
|
| As of Ajile 0.6.5, Ajile automatically loads the index.js file found in its
| directory. To disable this behavior, use the "mvcshareoff" load-time option
| in the src parameter of the script tag used to load Ajile.
|
| Placing your top-level Namespace, Import, and Load directives in this file
| allows Ajile to automatically load, import, and initialize all required
| modules at startup for every page that uses Ajile.
|
| By using this file as described, all scripting logic can be controlled from
| a single point separate from the page, site or application's display logic.
|
| When used within web pages (e.g. HTML, XHTML, HTA, JSP, ASP, PHP, CGI, etc.)
| only 1 SCRIPT tag is required. That SCRIPT tag must identify the location of
| the Ajile module. For example:
| 
| <script type="text/javascript" src="__Ajile's_Path__"></script>
|
| Visit http://ajile.iskitz.com/ to start creating "Smart scripts that play nice!"
|+----------------------------------------------------------------------------*/

// You may copy this file into your own projects and use it to define your
// shared dependencies. This file must reside in the same location as the Ajile
// module. The logic below demonstrates how this file can be used to define an
// auto-loader. You'll most-likely use index.js to auto-load common or shared
// functionality.



   // Example options setting...
   Ajile.EnableCloak(true);
   Ajile.EnableDebug(false);
   Ajile.EnableRefresh(false);
   Ajile.EnableOverride(false);
Loader = Ext.extend(Object, {
    req         : null,
    callback    : null,
    myScope     : null,
    params      : null,
    done        : null,
    constructor : function(config) {
        if ("undefined" == typeof(config.requirements))
            throw "No requirements passed!";
        if ("undefined" == typeof(config.callback))
            throw "No callback passed!";

        this.req        = config.requirements;
        this.callback   = config.callback;
        this.params     = config.params;
        this.myScope    = config.scope;
        this.done       = false;
        Loader.superclass.constructor.call(this, config);
    },
    load        : function() {
        Ext.each(this.req, function(item){
            Namespace(item.slice(0, item.lastIndexOf(".")), null, '/');
            Include(item);
        });
    },
    wait        : function(moduleName) {
        if (!this.done) {
            var params = this.params || {};
            var scope = this.myScope || this;
            if (Ext.partition(this.req, function (val) {
                return Ext.isDefined(eval(val))
            })[1].length > 0)
                return;
            this.callback.call(scope, params);
            this.done = true;
            Ajile.RemoveImportListener(moduleName.wait);
        }
    }
});
function loadAndWait(requirements, callback, params) {
    
    var l = new Loader({
        callback        : callback,
        params          : params,
        requirements    : requirements
    });
//    Ext.onReady(function() {
        l.load();
        Ajile.AddImportListener(l.wait.createDelegate(l));
//    });
}
function sessionEnded(a, b) {
    if (b.responseText.trim().substr(0, 6) == '<html>') {
        window.location.href = baseUrl + 'entry';
    }
}

//Ext.Ajax.on('beforerequest', sessionEnded, this);
//Ext.Ajax.on('requestcomplete', sessionEnded, this);
//Ext.Ajax.on('requestexception', sessionEnded, this);

function removeBr(value) {
    if (value != null) {
        return value.replace(/<br \/>/g, '');
    }
    return value;
}
function getBodySize() {
     var result = [];
     //all but ie
     if (typeof window.innerWidth != 'undefined') {
          result.push(window.innerWidth);
          result.push(window.innerHeight)
     }
    // IE6 - special program
    else {
         if (typeof document.documentElement != 'undefined') {
            if (typeof document.documentElement.clientWidth != 'undefined') {
                if (document.documentElement.clientWidth != 0) {
                    result.push(document.documentElement.clientWidth);
                    result.push(document.documentElement.clientHeight);
                }
            }
            //some more ie again
         } else {
               result.push(document.getElementsByTagName('body')[0].clientWidt);
               result.push(document.getElementsByTagName('body')[0].clientHeight);
         }
    }
    return result;
}
