/*
Title: JAME - Javascript Animations Made Easy 

Script: JAME.js
	JAME - Core functions

	Define all the core functions used in all packages, modules.

About:
	Version 0.01

License:
	MIT-style license.

Credits:
       - inspired by Mootools, Jquery,Protoype and the community.
*/

JAME = function () {};
JAME = JAME.prototype = function () {};
JAME.VERSION=0.01;

function $(elm) {

		if(document.getElementById) {
			this.$ = function(elm) {
				if(document.getElementById(elm)) return document.getElementById(elm);
			}
		}
		if(document.getElementById(elm)) return document.getElementById(elm);
}

/*

Section: Namespaces related functions

Function: JAME.Package
	Create a namespace.

	The On-Demand Downloader will decompose namespaces by changing dots into slashes
	and add the js extension to the final namespace:
	JAME.FX.CSS.Colors
	will be understood as
	JAME/FX/CSS/Colors.js

Arguments:
	sName - the namespace delimited by dots.

Example:
	>JAME.Package("JAME.FX.CSS.Colors");
	>JAME.FX.CSS.Colors = { ... };

Returns:
	void
*/

JAME.Package = function (sname) {

    //split the name by dots

    var namespaces=sname.split('.') || [sname];
    var nlen=namespaces.length;
	
    var root = window;
    var F    = function() {};

    for(var i=0;i<nlen;i++) {
        var ns = namespaces[i];
        if(typeof(root[ns])==='undefined') {
            root = root[ns] = F;
            root = root.prototype = F;
        }
        else
           root = root[ns];
    }
}



/*
Function: JAME.Import
	Import into the global namespace package functions or variables.

Arguments:
	oNames - the namespaces to import.

Example:
	>JAME.Import(JAME.DOM , JAME.FX.Transition , JAME.Util.String);

Returns:
	void.

See Also:

      <JAME.Exporter> , <JAME.Export>
*/

JAME.Import = function () {
	var nlen=arguments.length;
	for(var i=0;i<nlen;i++) {
		var pk=arguments[i];
		if(pk.hasOwnProperty('Export')) {
			pk.Export();
        }
		else if (pk.prototype.Export!==undefined) {
			pk.prototype.Export();
		}
	}
}

/*
Function: JAME.Exporter
	Define methods, variables importable into the global namespace.
	All methods, variables starting with '_'  are considered private and won't be exported.
	This is just a convention, you can always use anonymous functions to create real private methods.

Arguments:
	oName - key : value pair where the key is the original namespace and value the exported name.

Example:
	>JAME.Exporter({ "JAME.Package" : "Package", "JAME.Import" : "Import"});
	>//or in order to export everything:
	>JAME.Exporter(this);


Returns:
	void.

See also:
	<JAME.Export>
*/

JAME.Exporter = function (oName,sPref) {

	if(!sPref) sPref='';

	for(var method in oName) {
	    if(method==="Export" || /^_/.test(method)) continue;
	    window[sPref+method]=oName[method];
    }


}

/*
Function: JAME.Export
	Call by <JAME.Import>

Arguments:
	none.

Example:
	>JAME.Export = function () {
	>JAME.Exporter({ "JAME.Package" : 'Package', "JAME.Import" : 'Import'});
	>}


Returns:
	void.

See also:
	<JAME.Exporter>
*/

JAME.Export = function () {
	JAME.Exporter({ "Package" : JAME.Package, "Import" : JAME.Import});
}

/*
Section: Exported functions
	JAME.Import(JAME);

Function: Package
	<JAME.Package>

Function: Import
	<JAME.Import>

Section: Creating a basic package
	
       Here is a little example based on the JAME.Util.String.
	It combines all the above functions.

Example:

	(start code)
       //package example


	//create your namespace
	JAME.Package("JAME.Toolbox.Strings");


	//define its content
	JAME.Toolbox.Strings = {
		firstToUpperCase : function (str) {
			return str.replace(/^[a-z]/,function(m,l) { 
                                    return l.toUpperCase() } 
                            );
		},
		_privateMethod : function (str) { //private method not exported
                   //calculation here
		},
              myPublicMethod : function (str) {
			str = split('',str);
			return _privateMethod(str);
		},
              Export : function() {
			JAME.Exporter(this); //export firstToUppercase, myPublicMethod
			JAME.Exporter({"JTSfirstToUppercase":this.firstToUpperCase}); //export just JTSfirstToUpperCase
		}
	};
       
       // user code


        //nothing imported
        JAME.Toolbox.Strings.firstToUpperCase(str);


        //import everything from the package to the global space
	 JAME.Import(JAME.Toolbox.Strings);
	 firstToUpperCase(str);
       

        //alias the package 
        var JS=JAME.Toolbox.Strings;
	 JS.firstToUpperCase(str);

	(end)

*/

function extend(destination, source) {  
    function initClassIfNecessary(obj) {  
        if( typeof obj["_super"] == "undefined" ) {  
            obj["_super"] = function() {  
                var methodName = arguments[0];  
                var parameters = arguments[1];  
                this["__parent_methods"][methodName].apply(this, parameters);  
            }  
        }  
      
        if( typeof obj["__parent_methods"] == "undefined" ) {  
            obj["__parent_methods"] = {}  
        }  
    }  
  
    for (var element in source) {  
	
        if( typeof destination[element] != "undefined" ) {  
            initClassIfNecessary(destination);  
            destination["__parent_methods"][element] = source[element];  
        } else {  

            destination[element] = source[element];  
        }  
    }  
}

