// File: /include/js/Shared/Views/WidgetViewController.js
// Desc: view controller
// $Revision: 57$
// $Date: 05/14/2007 12:10:36 PM$
// $Author: Donnie Tognazzini$
// $NoKeywords$

var controller;

function VM_WidgetViewController( source, version )
{
    try
    {
        // set global pointer
        controller = this;

        UTILS.inheritFromBase( this, 'VM_WidgetViewController', document );

        // Initialize members

        this.source    = source;
        this.version   = version;
        this.osVersion = navigator.platform;

        this.addProperty( "viewState"       , true , this.VIEW_STATE_UNDEFINED );
        this.addProperty( "inBackground"    , true , false );
        this.addProperty( "custName"        , false, null );
        this.addProperty( "emailAddress"    , false, null );
        this.addProperty( "queryString"     , false, null );

        this.m_callListView     = new VM_CallListView( source, version, this.osVersion );
        this.m_activationView   = new VM_ActivationView( this );
        this.m_loginView        = new VM_LoginView( this );
        this.m_registerView     = new VM_RegisterView( this );
        this.m_autoLoginView    = new VM_AutoLoginView( this );
        this.m_accountHandler   = new ACC_AccountHandler( this.source, this.version, this.osVersion );

        // Event handlers

        this.bindToEvent( "Start" );
        this.bindToEvent( "GetAccountInfoSuccess" );
        this.bindToEvent( "GetAccountInfoFailure" );
        this.bindToEvent( "LoginRequired" );
        this.bindToEvent( "AccountActivated" );

        var self = this;
        this.bindToEvent( "#lnkStopAutoLogin",     "click", function( ) { self.viewState.set( self.VIEW_STATE_SHOW_LOGIN ); } );
        this.bindToEvent( "#lnkNewAccount",        "click", function( ) { self.viewState.set( self.VIEW_STATE_SHOW_REGISTER ); } );
        this.bindToEvent( "#lnkAlreadySubscribed", "click", function( ) { self.viewState.set( self.VIEW_STATE_SHOW_LOGIN ); } );
        this.bindToEvent( "#lnkLogout",            "click", this.onLogoutLinkClick );

        VM_URL._updateUrls( );

        $( "a" ).mouseover( function( ) { if ( this.title ) window.status = this.title; } ).mouseout( function( ) { window.status = ""; } );
        for ( var id in this._links ) this._links[ id ].elem = document.getElementById( id );
    }
    catch ( e )
    {
        EX_ASSERT_NO_EXCEPTIONS( e, "VM_WidgetViewController::VM_WidgetViewController( )" );
    }
}

var VM_WidgetViewController_prototype =
{
    VIEW_STATE_UNDEFINED       : 0,
    VIEW_STATE_SHOW_AUTOLOGIN  : 1,
    VIEW_STATE_SHOW_REGISTER   : 2,
    VIEW_STATE_SHOW_LOGIN      : 3,
    VIEW_STATE_SHOW_CALL_LIST  : 4,
    VIEW_STATES                : [ "UNDEFINED", "SHOW_AUTOLOGIN", "SHOW_REGISTER", "SHOW_LOGIN", "SHOW_CALL_LIST" ],

    _links                     : { lnkTOS          : { url: "/v2w/termsOfService.html" },
                                   lnkAvfLearnMore : { url: "/v2w/learnmore.html" },
                                   lnkRvfLearnMore : { url: "/v2w/learnmore.html" },
                                   lnkAccount      : { url: "/v2w/account.html" },
                                   lnkFeedback     : { url: "/v2w/feedback.html" },
                                   lnkSeeAllCalls  : { url: "/v2w/seeAllCalls.html" } },

    /***************************************************************************
     ***************************************************************************
      Event handlers
     ***************************************************************************
     ***************************************************************************/
     onAutoConnectCanceled : function( )
    {
        try
        {
            // Cancel the GETA request
            this.m_accountHandler.StopGetAccountInfoRequest();

            // Clear state
            PREF_GetPreferences( ).AuthInfo.reset( );

            // Display Login View
            this.viewState.set( this.VIEW_STATE_SHOW_LOGIN );
        }
        catch ( e )
        {
            EX_ASSERT_NO_EXCEPTIONS( e, "VM_WidgetViewController::onAutoConnectCanceled( )" );
        }
    },

    onGetAccountInfo : function( )
    {
        try
        {
            this.m_accountHandler.getAccountInfo( PREF_GetPreferences( ).AuthInfo.get( ).accountNumber,
                                                  PREF_GetPreferences( ).AuthInfo.get( ).uString );
        }
        catch ( e )
        {
            EX_ASSERT_NO_EXCEPTIONS( e, "VM_WidgetViewController::onGetAccountInfoSuccess( )" );
        }
    },

    onGetAccountInfoSuccess : function( e, responseMessage )
    {
        try
        {
            switch ( responseMessage.m_status )
            {
            case MSG_ResponseStatus.SUCCESS:
                {
                    PREF_GetPreferences( ).CellNumber.set( responseMessage.m_phone );

                    this.m_activationView.setAccountInfo(
                        responseMessage.m_fwdCodes.m_forwardCodeList,
                        responseMessage.m_carriers.m_carriers,
                        responseMessage.m_carrierId,
                        responseMessage.m_actStatus,
                        responseMessage.m_hideRegCarrier );

                    // TODO: set calllistview timer

                    this.viewState.set( this.VIEW_STATE_SHOW_CALL_LIST ) ;

                    break;
                }

            case MSG_ResponseStatus.GENERAL_ERROR:
            case MSG_ResponseStatus.GETA_LOGIN_FAILURE:
            case MSG_ResponseStatus.GETA_UPGRADE_REQUIRED:
            default:
                {
                    PREF_GetPreferences( ).AuthInfo.reset( );
                    this.viewState.set( this.VIEW_STATE_SHOW_LOGIN );
                    CW_GetMessageArea( ).display( responseMessage.m_statusMsg, CW_GetMessageArea( ).MESSAGE_TYPE_ALERT );

                    break;
                }
            }
        }
        catch ( e )
        {
            EX_ASSERT_NO_EXCEPTIONS( e, "VM_WidgetViewController::onGetAccountInfoSuccess( )" );
        }
    },

    onGetAccountInfoFailure : function( e, statusMessage )
    {
        try
        {
            PREF_GetPreferences( ).AuthInfo.reset( );
            this.viewState.set( this.VIEW_STATE_SHOW_LOGIN );
            CW_GetMessageArea( ).display( this.m_loginView.LOGIN_FAILED_MESSAGE, CW_GetMessageArea( ).MESSAGE_TYPE_ALERT );

            EX_Log( "VM_WidgetViewController::onGetAccountInfoFailure( ) : " + statusMessage );
        }
        catch ( e )
        {
            EX_ASSERT_NO_EXCEPTIONS( e, "VM_WidgetViewController::onGetAccountInfoFailure( )" );
        }
    },

    onviewStateChange : function( e, oldState, newState )
    {
        document.getElementsByTagName( "body" ).item( 0 ).className = this.VIEW_STATES[ newState ];
    },

    onStart : function( e )
    {
        try
        {
            UTILS.raise( "ready" );

            this.updateQueryString( PREF_GetPreferences( ).AuthInfo.get( ).uString );

            if ( ! this.viewState.isReset( ) )
            {
                // TODO: can this happen?
                return;
            }

            if ( ! PREF_GetPreferences( ).AuthInfo.isReset( ) )
            {
                // We have saved preferences, attempt to login.
                this.viewState.set( this.VIEW_STATE_SHOW_AUTOLOGIN );
                this.onGetAccountInfo( );
                return;
            }

            if ( ! PREF_GetPreferences( ).CellNumber.isReset( ) )
            {
                this.viewState.set( this.VIEW_STATE_SHOW_LOGIN );
                return;
            }

            // No preferences. Go to the registration screen.
            this.viewState.set( this.VIEW_STATE_SHOW_REGISTER );
        }
        catch ( e )
        {
            EX_ASSERT_NO_EXCEPTIONS( e, "VM_WidgetViewController::onStart( )" );
        }
    },

    onLogoutLinkClick : function( )
    {
        try
        {
            // Clear state
            PREF_GetPreferences( ).ShowActivation.reset( );
            PREF_GetPreferences( ).AuthInfo.reset( );
            this.m_activationView.handleLogout( );
            this.m_callListView.handleLogout( );

            // Show login screen
            this.viewState.set( this.VIEW_STATE_SHOW_LOGIN );
        }
        catch ( e )
        {
            EX_ASSERT_NO_EXCEPTIONS( e, "VM_WidgetViewController::onLogoutLinkClick( )" );
        }
    },

    onLoginRequired : function( evt, responseMsg )
    {
        try
        {
            this.onLogoutLinkClick( );
            CW_GetMessageArea( ).display( responseMsg.m_statusMsg, CW_GetMessageArea( ).MESSAGE_TYPE_ALERT );
        }
        catch ( e )
        {
            EX_ASSERT_NO_EXCEPTIONS( e, "VM_WidgetViewController::onLoginRequired( )" );
        }
    },

    onAccountActivated: function( e, statusMessage )
    {
        try
        {
            this.m_activationView.activationStatus.set( TYPE_ACTIVATION_STATE_ACTIVATED );
            this.m_activationView.setVisibility( );
        }
        catch ( e )
        {
            EX_ASSERT_NO_EXCEPTIONS( e, "VM_WidgetViewController::ononAccountActivated( )" );
        }
    },

    /***************************************************************************
     ***************************************************************************
      Set methods
     ***************************************************************************
     ***************************************************************************/

    updateQueryString : function( uparam )
    {
        var qs = "?src=" + this.source
               + "&ver=" + this.version
               + ( uparam ? "&u=" + uparam : "" )
               + "&os=" + UTILS.escapeUri( this.osVersion );

        this.queryString.set( qs );

        for ( var id in this._links )
        {
            this._links[ id ].elem.href = VM_URL.WWW + this._links[ id ].url + qs;
        }
    }
};
