/**
 * Various general-purpose JavaScript utility methods.
 *
 * Presume YUI core (yahoo, dom, event) has already been included,
 * and a window.logger has been defined; but degrade gracefully whenever possible.
 */
 
/*
 * Perform the following TSG JavaScript init functions:
 * - Establish a namespace in this window (lazily) for TSG
 * - Establish a namespace in this window (lazily) for TSG.util.
 */ 
(function() {
    try {
        if ( !window.TSG ) {
            window.TSG = {};
        }
        if ( !window.TSG.util ) {
            
            window.TSG.util = {
            
                // Return an array of strings representing an object's "own" properties.
                propertyNames: function ( ob ) {
                    var arr = [];
                    if ( !ob ) {
                        return arr;
                    }
                    for ( prop in ob ) {
                        if ( YAHOO.lang.hasOwnProperty( ob, prop ) && !YAHOO.lang.isFunction( ob[prop] ) ) {
                            arr.push( prop );
                        }
                    }
                    return arr;
                },

                // Better alternative to toString, using YAHOO.lang.dump if available.
                dump: function( ob ) {
                    if ( YAHOO && YAHOO.lang && YAHOO.lang.dump ) {
                        return YAHOO.lang.dump( ob );
                    }
                    else {
                        return ob;
                    }
                },

                // Adaptive error logging (YUI, console) and alerting                
                error: function( e, msg ) {
                    msg = msg || "ERROR";
                    msg += ": " + dump( e );
                    if ( window.logger ) {
                        logger.log( msg, "error" );
                    }
                    if ( window.console && window.console.error ) {
                        console.error( msg );
                    }
                    alert( msg );
                },
                
                // Adaptive info logging (YUI, console); alert if neither defined
                info: function( msg ) {
                    var logged = false;
                    if ( window.logger ) {
                        logger.log( msg, "info" );
                        logged = true;
                    }
                    if ( window.console && window.console.info ) {
                        console.info( msg );
                        logged = true;
                    }
                    if ( !logged ) {
                        alert( msg );
                    }
                },
                
                // Adaptive debug logging (YUI, console); nothing if neither defined
                debug: function( msg ) {
                    if ( window.logger ) {
                        logger.log( msg, "debug" );
                    }
                    if ( window.console && window.console.debug ) {
                        console.debug( msg );
                    }
                },
                
                // Decodes URL encoded spaces 
			    decode: function( str ) {
			    	var result = "";
			    	for ( var i = 0; i < str.length; i++ ) {
			    		if ( str.charAt( i ) == "+" ) {
			    			result += " ";
			    		} else {
			    			result += str.charAt( i );
			    		}
			    	}
			    	
			    	return unescape( result );
			    },
                
                // Generic front for window.onload or onDOMReady functionality: call the given function fn
                // when DOM has fully loaded.
                // IE6+ has a problem with YUI 2.5.1's onDOMReady, so if browser is IE, use window.onload.
                onPageReady: function( fn ) {
                    if ( YAHOO.env.ua.ie ) {
                        YAHOO.util.Event.addListener( window, "load", fn );
                    }
                    else { 
                        YAHOO.util.Event.onDOMReady( fn );
                    }
                }
                
            }  // TSG.util
        }
        
        
    }
    catch( e ) {
        var msg = (YAHOO && YAHOO.lang && YAHOO.lang.dump ) ? YAHOO.lang.dump( e ) : e.message;
        window.alert( "ERROR in tsg-init: " + msg );
    }
})();