// ===============================================
// Global interfaces for exchanging data within
// MyAlaska applications
// ===============================================

// Initialise global "ALASKA" namespace
if( typeof ALASKA=="undefined"){var ALASKA={};}
ALASKA.namespace=function(){}

// Initialise application and applet namespaces
ALASKA.namespace("app");
ALASKA.namespace("applet");

ALASKA.app = function() {
    var errorForm   = "centercontent";
    var mainForm    = "centercontent";
    var contentArea = "formcontent";
    var loginForm   = "/user-account-mgmt/myalaska/loginForm";

    return {
        setErrorForm:    function(cName)          { errorForm = cName; },
        getErrorForm:    function()               { return errorForm; },
        setMainForm:     function(cName)          { mainForm  = cName; },
        getMainForm:     function()               { return mainForm; },
        setContentArea:  function(cName)          { contentArea  = cName; },
        getContentArea:  function()               { return contentArea; },
        setLoginForm:    function(cName)          { loginForm = cName; },
        getLoginForm:    function()               { return loginForm; },

        AJAXUpdatePage:  function(cURI, x)        { return ALASKA.app.AJAXUpdate(cURI, ALASKA.app.getMainForm(), x); },
        AJAXUpdateContent:function(cURI, x)       { return ALASKA.app.AJAXUpdate(cURI, ALASKA.app.getContentArea(), x); },

        AJAXUpdate:      function(cURI, cElem, x, l) { 
                                                       if( l == null ) {
                                                          _method = 'get';
                                                       }
                                                       else {
                                                          _method = l.toString();
                                                       }

                                                       if( x == null ) {
                                                          new Ajax.Updater({failure: '',
                                                                            success: cElem},
                                                                            cURI,{method: _method,
                                                                            evalScripts: true,
                                                                            on401: function(){ ALASKA.app.AJAXUpdate(ALASKA.app.getLoginForm(),ALASKA.app.getErrorForm());} });
                                                       }
                                                       else {
                                                          new Ajax.Updater( {failure: '',
                                                                             success: cElem},
                                                                             cURI,{method: _method,
                                                                             evalScripts: true,
                                                                             parameters: x,
                                                                             on401: function(){ ALASKA.app.AJAXUpdate(ALASKA.app.getLoginForm(),ALASKA.app.getErrorForm());} });
                                                       }
                                                     }
    }
}();

ALASKA.applet = function() {
    var appletContainer;
    var applets         = new Array( 0 );
    var apps            = new Array( 0 );
    var numApplets      = 0;
    var numApps         = 0;

    return {
        setContainer:    function(o)              { appletContainer=o; },
        getContainer:    function()               { return appletContainer; },

        addApplet:       function(oTab,cName,cForm) {
                                                    var a=new Array(3);
                                                    a[0] = oTab;
                                                    a[1] = cName;
                                                    a[2] = cForm;
                                                    applets.push( a );
                                                    numApplets = numApplets+1;
                                                    },
        addApp:          function(cApplet,oApplet,oApp) {
                                                    var a=new Array(3);
                                                    a[0] = oApp;
                                                    a[1] = oApplet;
                                                    a[2] = cApplet;
                                                    apps.push(a);
                                                    numApps = numApps+1; },
        getAppletCount:  function()               { return numApplets; },
        getAppCount:     function()               { return numApps; },

        getAppletForm:   function(cApplet)        { for( var i=0; i<numApplets; i++ ) {
                                                      if( applets[i][1] == cApplet ) {
                                                        return applets[i][2];
                                                      }
                                                    }
                                                  },
        getAppletNameIdx:function(n)              { return applets[n][1]; },
        getAppletFormIdx:function(n)              { return applets[n-1][2]; },

        activateApplet:  function(cApplet,bFocus) { for( var i=0; i<numApplets; i++ ) {
                                                      if( applets[i][1] == cApplet ) {
                                                        activateAppletIdx( i, bFocus );
                                                        return;
                                                      }
                                                    }
                                                  },
        activateAppletIdx:function(n,bFocus)      { if( bFocus == true ) {
                                                      appletContainer.set( "activeIndex", n );
                                                    }

                                                    for( var i=0; i<numApps; i++ ) {
                                                      if( apps[i][1] == applets[n][1] &&
                                                          !document.getElementById(applets[n][2]).hasChildNodes() ) {
                                                          apps[i][0].click();
                                                          apps[i][0].set( 'checked', true );
                                                        break;
                                                      }
                                                    }
                                                  },
        activateApp:     function(cApp)           { var nTmp  = cApp.indexOf( "." );
                                                    var cCont = cApp.substr( 0, nTmp );
                                                    var cApp  = cApp.substr( nTmp+1 );
                                                    for( var i=0; i<numApps; i++ ) {
                                                        if( apps[i][2] == cApp ) {
                                                           apps[i][0].click();
                                                           apps[i][0].set( 'checked', true );
                                                           break;
                                                        }
                                                     }
                                                     for( var i=0; i<numApplets; i++ ) {
                                                        if( applets[i][1] == cCont ) {
                                                           appletContainer.set( "activeIndex", i );
                                                           break;
                                                        }
                                                     }
                                                    }
    }
}();
