/**
 * The FlipArt framework. 
 * Version: 1.01
 * 
 * A collective framework which gathers all the best practices from most popular 
 * competative frameworks. At it's core the framework pursues the aim to provide
 * the ability of Java-style-programming and implements some of essential 
 * ActionScript 3 and MX's interfaces (@see Binder, EventDispatcher).
 * 
 * 
 * PRINCIPLES
 * The framework is based on these principles:
 * - Bother no standards
 * - No modification to any JavaScript objects or its native functionality. All 
 * the extension-functionality is gained via namespaced static classes
 * - Complete namespacing. The whole framework is located in 'fa' namespace
 * - Unobtrusive JavaScript means no JavaScript in HTML: the whole functionality 
 * is gained via 'fa:'-namespaced tags and attributes 
 * 
 * Thanks to those principles the framework can't possibly bother any external 
 * code. 
 * Thanks to Prototype-framework messing with the core javascript functionality
 * and thus destroying the ability to use the 'for.. in..' loop in some cases it 
 * may conflict with it.
 * 
 * 
 * ARCHITECTURE
 * - Complete namespacing. The whole framework is located in 'fa' namespace.
 * - Java-style packaging. The package-names are lowercased, the class-names are
 * uc-first.
 * 
 * 
 * FEATURES
 * - Extension and overriding of some of Spry-framework's features
 * - Import libraries dynamically via fa.include
 * - Very powerful event handling functionality
 * 
 * 
 * 
 * @copyright 2008, flipart.ru
 * @requires Spry.Utils
 */

/**
 * Core package
 * @version 4
 */
fa = 
{
	include: function(library)
	{
		
		//	a hack for Spry to change namespace to global when importing Spry libraries 
		var Spry = window.Spry;
		
		
	    if (! Spry.Utils.loadURL)
		{
			throw '"Spry.Utils.loadURL" library is required'; 
		}
		
	    var parts = library.split('.');
	    
	    var packageName = '';
	    for (var i=0; i<parts.length-1; i++)
	    {
	        if(packageName)
	        {
	            packageName += '.';
	        }
	        packageName += parts[i];
	        if (undefined == eval(packageName))
	        {
	            eval(packageName + "={};");
	        }
	    }
	    
	    
	    if( eval(library) ) //  already exists
	    {
	        return;
	    }
	    
	    	
		var path = '/scripts/' + parts.join('/') + '.js';
	    
	    //	trick caching of scripts - for testing purposes:
	    //	    path += '?' + Math.random();
	    //	deprecated in favor of Apache's
	    //  	Header set "Cache-Control" "no-cache"
	    
	    
	    
	    //  import the path
	    try
	    {
	        var req = Spry.Utils.loadURL('GET', path, false);
	        
	        eval(req.xhRequest.responseText);
	    }
	    catch (e)
	    {
	    	throw 'unable to import library "'+library+'"';
	    }
	  
	},
	
	/**
	 * Is used for extension of prototype objects (classes)
	 * 
	 * Thanks to Prototype framework
	 */
	extend: function (destination, source) 
	{
		for (var property in source)
	    {
	        destination[property] = source[property];
	    }
	    return destination;
	},
	
	/**
     * Shortcut to fa.Document.getElement
     */
    $: function()
    {
    	fa.include('fa.Document');
        return fa.Document.getElement.apply(null, arguments);
    },
    
    trace: function (str)
    {
    	Spry.Debug.trace(str);
    }
};
