/**************************************************************************
*
*  @@@BUILDINFO@@@ 04defaults-2.jsx 2.0.0.54  08-Feb-2007
*  Copyright 2006-2007 Adobe Systems Incorporated
*  All Rights Reserved.
*
* NOTICE:  All information contained herein is, and remains the property of
* Adobe Systems Incorporated  and its suppliers,  if any.  The intellectual 
* and technical concepts contained herein are proprietary to  Adobe Systems 
* Incorporated  and its suppliers  and may be  covered by U.S.  and Foreign 
* Patents,patents in process,and are protected by trade secret or copyright 
* law.  Dissemination of this  information or reproduction of this material
* is strictly  forbidden  unless prior written permission is  obtained from 
* Adobe Systems Incorporated.
**************************************************************************/

//
// xml of defaults
//
var prefDefaults = 0;

//-----------------------------------------------------------------------------
// 
// PrefUtils(...)
// 
// Purpose: ctor (dummy)
// 
//-----------------------------------------------------------------------------

function PrefUtils()
{}

//-----------------------------------------------------------------------------
// 
// PrefUtils.getValue(...)
// 
// Purpose: [static] Get value for pref name and (optional) cast to given type
//                   If no preference value is available then try to fetch a
//                   default value.
// 
//-----------------------------------------------------------------------------

PrefUtils.getValue = function( name, type )
{
    var ret = null;
    
    var prefValue = eval( name );
    
    if( !prefValue || prefValue.toString().length <= 0 )
    {
        var defValue = PrefUtils.getDefault( name );
        
        if( defValue && defValue.toString().length > 0 )
            ret = defValue;
        else
            ret = prefValue;
    }
    else
        ret = prefValue;
    
    if( ret )
    {
        switch( type )
        {
            case 'String':
                ret = ret.toString();
                break;
                
            case 'Number':
                ret = Number( ret.toString() );
                break;
                
            case 'Boolean':
                ret = ( ret.toString() == 'true' );
                break;
        }
    }
    
    return ret;
}

//-----------------------------------------------------------------------------
// 
// PrefUtils.getDefaultVaue(...)
// 
// Purpose: [static] Return default value for pref name
// 
//-----------------------------------------------------------------------------

PrefUtils.getDefaultValue = function( name, type )
{
    var ret = '';

    var prefValue = PrefUtils.getDefault( name );
    
    if( prefValue && prefValue.toString().length > 0 )
        ret = prefValue;
    
    if( ret )
    {
        switch( type )
        {
            case 'String':
                ret = ret.toString();
                break;
                
            case 'Number':
                ret = Number( ret.toString() );
                break;
                
            case 'Boolean':
                ret = ( ret.toString() == 'true' );
                break;
        }
    }
    
    return ret;
}

//-----------------------------------------------------------------------------
// 
// PrefUtils.getDefault(...)
// 
// Purpose: [static] Return default string for pref name
// 
//-----------------------------------------------------------------------------

PrefUtils.getDefault = function( name )
{
    var ret = '';
    
    if( !prefDefaults )
    {
	    var f = new File( $.fileName );
	    f.changePath( '../defs.xml' );
	    
	    if( f.open() )
	    {
			f.encoding = "UTF-8";
		    var s = f.read();
		    f.close();
		  
		    try
		    {
			    prefDefaults = new XML(s);
		    }
		    catch(e)
		    {
			    prefDefaults = new XML;
		    }
	    }
    }
    
    if( prefDefaults )
    {
        var defName = name;
        
        if( defName.indexOf( 'prefs.' ) == 0 )
            defName = defName.substring( 6 );
            
        defName = 'prefDefaults.' + defName + '.text()';
                    
        try
        {
            ret = eval( defName );
        }
        catch(e)
        {
            ret = '';
        }
    }
    
    return ret;        
}

//-----------------------------------------------------------------------------
// 
// PrefUtils.hasValue(...)
// 
// Purpose: Return true if the given preference key exists and contains a valid
//          value (type is optional)
// 
//-----------------------------------------------------------------------------

PrefUtils.hasValue = function( name, type )
{
    var ret     = false;
    var value   = PrefUtils.getValue( name, type );
    
    if( value )
    {
        switch( type )
        {
            case 'String':
                ret = ( value.toString().length > 0 );
                break;
                
            case 'Number':
                ret = !isNaN( Number( value.toString() ) );
                break;
                
            case 'Boolean':
                ret = ( value.toString() == 'true' || value.toString() == 'false' );
                break;
                
            default:
                ret = ( value.toString().length > 0 );
        }
    }
    
    return ret;
}
