/**
 * Opel eMagazine javascript library: core
 * 
 * @projectDescription	Provides core functionalities
 * @namespace			opel
 * 
 * @author 				Heimann
 * @version				2011.02.23
 * @copyright			ACHTGRAU GmbH, 2010-2011
 */

/* global opel object and namespace */
var opel = {

	/**
	 * eMagazine namespace
	 */
	emag:  {
	
		/**
		 * all components
		 */
		comp: {},
	
		/**
		 * all components configurations needed on rendertime
		 */
		conf: {},
	
		/**
		 * library of common/useful functions
		 */
		lib: {
	
			/**
			 * get the object to deal with (opel.emag.comp or opel.emag.conf)
			 * @param {Boolean} conf - true, if to use the config object
			 * @return {Object} object
			 */
			getObject: function(conf) {
				return conf ? opel.emag.conf : opel.emag.comp;
			},
	
			/**
			 * add a component with the given name to the (config) object
			 * @param {String} componentId - id of component
			 * @param {Boolean} conf - true, if to use the opel.emag.conf object
			 * @param {Object} compObj - component object; if it is null, then set an empty object
			 * @return {Object} returns the added object
			 */
			addComponent: function(componentId, conf, compObj) {
				var obj = opel.emag.lib.getObject(conf);
				obj[componentId] = compObj || {};
				return obj[componentId];
			},
	
			/**
			 * returns component(config) - if component not present then add an empty one
			 * @param {String} componentId - id of component
			 * @param {Boolean} conf - true, if to use the opel.emag.conf object
			 * @return {Object} component
			 */
			getComponent: function(componentId, conf) {
				var comp = opel.emag.lib.getObject(conf)[componentId];
				return (!comp) ? opel.emag.lib.addComponent(componentId, conf, {}) : comp;
			},
	
			/**
			 * extend one object with another
			 * @param {Object} obj1 - object to be extended
			 * @param {Object} obj2 - object with new properties
			 * @return {Object} object
			 */
			extend: function(obj1, obj2) {
				if (obj1 && obj2) {
					for (var i in obj2) {
						obj1[i] = obj2[i];
					}
				} 

				return obj1 || null;
			}
		}
	}
};
