/**
 * The FlipArt framework. 
 * Version: 1 Beta.
 * 
 * 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 is mostly based on ActionScript 3
 * interfaces. It implements some of it's and MX's functionality (@see Binder, 
 * EventDispatcher).
 * 
 * 
 * PRINCIPLES
 * The framework is totally 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 functions.
 * - 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 tag-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 Spry-framework features
 * - Import libraries dynamically via fa.include
 * 
 * 
 * 
 * @copyright 2008, flipart.ru
 * @requires Spry.Utils
 */

/**
 * Core package
 */
fa = 
{
	EMPTY_IMG: '/images/empty.gif',
	
	include: function(library)
	{
		if (!Spry.Utils.loadURL)
		{
			throw new Exception('"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( eval(packageName) == undefined )
	        {
	            eval(packageName + "={};");
	        }
	    }
	    
	    
	    if( eval(library) ) //  already exists
	    {
	        return;
	    }
	    
	    var path = '/scripts/' + parts.join('/') + '.js';
	    
	    
	    //  import the path
	    
	    try
	    {
	        var req = Spry.Utils.loadURL('GET', path, false);
	        eval(req.xhRequest.responseText);
	    }
	    catch(e)
	    {
	    	throw new Exception('unable to import library "'+library+'"'); 
	    }
	  
	},
	
	/**
	 * thanks to Prototype
	 */
	extend: function (destination, source) 
	{
	    for (var property in source) {
	        destination[property] = source[property];
	    }
	    return destination;
	},
	
	/**
     * Shortcut to fa.Document.getElement
     */
    $: function()
    {
        return fa.Document.getElement.apply(null, arguments);
    },
    
	initialize: function()
    {
        fa.include('fa.Array');
        fa.include('fa.Object');
        fa.include('fa.String');
        fa.include('fa.Document');
        fa.include('fa.Element');
        fa.include('fa.SysInfo');
        
        // Process stylesheets
        var styles = fa.Document.getAllCssStyles(); 
        for (var i in styles)
        {
            var style = styles[i];
            
            //  png-trick for styles
            if (fa.SysInfo.browserIs(fa.SysInfo.IE, 5.5, 7))
            {
                if( style.backgroundImage )
                {
                	var matches = style.backgroundImage.match(/^url\(["']?([^"'()]+)["']?\)/);
                	if (matches) 
                	{
	                    var url = matches[1];
	                    if ('png' == url.substr(url.length - 3))
	                    {
	                        var absUrl = url;
	                        /*
	                        var filteredUrl = url.match( /^[\.\/]*(\w.*)$/ )[1];
	                        if (filteredUrl)
	                        {
	                            absUrl = '/' + filteredUrl;
	                        }
	                        */
	                        
	                        style.backgroundImage = "";
	                        style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + absUrl + "',sizingMethod='scale');";
	                    }
                	}
                }
            }
        }
        
        
        fa.Element.initialize(document.body);
    }
	
};





































