// File: /widgets/shared/views/preferences.js
// Desc: Voicemail prefs definitions
// $Revision: 12$
// $Date: 4/24/2007 11:14:07 AM$
// $Author: Donnie Tognazzini$
// $NoKeywords$

var PREF_KEY_CELL_NUMBER            = "CellNumber";
var PREF_KEY_ACCOUNT                = "Account";
var PREF_KEY_U_STRING               = "UString";
var PREF_KEY_SHOW_ACTIVATION        = "ShowActivation";
var PREF_KEY_MAX_CALLLIST_CALLS     = "maxCalllistCalls";
var PREF_KEY_ENABLE_MESSAGE_ALERT   = "EnableMessageAlert";
var PREF_KEY_AUTH_INFO              = "AuthInfo";

function PREF_PreferenceInfo( key, defaultValue, hasChangeEvent, equivalenceFunctor )
{
    this.key                = key;
    this.hasChangeEvent     = hasChangeEvent;
    this.defaultValue       = defaultValue;
    this.equivalenceFunctor = equivalenceFunctor;

    this.toString = function( ) { return this.key; };
    this.reset = function( ) { this.set( this.defaultValue ); };

    var self = this;
    function IsEqual( lhs, rhs )
    {
        if ( self.equivalenceFunctor )
        {
            return self.equivalenceFunctor( lhs, rhs );
        }

        return lhs == rhs;
    }

    this.isReset = function( )
    {
        return IsEqual( this.defaultValue, this.get( ) );
    };

    this.get = function( )
    {
        var value = UTILS.getNativePreference( this.key );

        if ( !value )
        {
            return this.defaultValue;
        }

        return eval( "(" + value + ")" );
    };

    this.set = function( value )
    {
        if ( !value && value != "0" )
        {
            value = this.defaultValue;
        }
        else if ( value.constructor != this.defaultValue.constructor )
        {
            value = new this.defaultValue.constructor( value );
        }

        var currentValue = this.get( );

        if ( this.hasChangeEvent && ! IsEqual( currentValue, value ) )
        {
            UTILS.jDocument.trigger( this.key + 'Change', [ currentValue, value ] );
        }

        UTILS.setNativePreference( this.key, UTILS.toJSONString( value ) );
    };
};

function PREF_Preferences( )
{
    var self = this;
    function AddPreference( name, key, defaultValue, hasChangeEvent, isEqualsFunctor )
    {
        self[ name ] = new PREF_PreferenceInfo( key, defaultValue, hasChangeEvent, isEqualsFunctor );
    }

    AddPreference( "CellNumber",            PREF_KEY_CELL_NUMBER         , "",                                false );
    AddPreference( "AuthInfo",              PREF_KEY_AUTH_INFO           , { accountNumber: 0, uString: "" }, true, function( lhs, rhs ) { return lhs.accountNumber == rhs.accountNumber && lhs.uString == rhs.uString; } );
    AddPreference( "ShowActivation",        PREF_KEY_SHOW_ACTIVATION     , false,                             false );
    AddPreference( "CallListLength",        PREF_KEY_MAX_CALLLIST_CALLS  , 4,                                 false );
    AddPreference( "MessageAlertEnabled",   PREF_KEY_ENABLE_MESSAGE_ALERT, true,                              false );
}

function PREF_GetPreferences( )
{
    if ( typeof __PREF_GetPreferences == "undefined" )
    {
        __PREF_GetPreferences = new PREF_Preferences( );
    }

    return __PREF_GetPreferences;
}
