/*
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
code language:		javascript 1.8
folder name:            files/dclib_5.05/dcjs_5.0
class name:             dcjs_datastorage
class type:             dynamic
version:                1.0
date:                   20120108
copyright:              massimo cardascia
url:                    www.plustic.de / www.dot-control.com
code style:		whitesmiths style variant - 8 spaces tab - http://en.wikipedia.org/wiki/indent_style
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
*/

///*	class definition /////////////////////////////////////////////////////////////////////////////////////
function DCJS_dataStorage($o_parameter){this.f_construct($o_parameter);}

///*    class versioning /////////////////////////////////////////////////////////////////////////////////////
	DCJS_dataStorage.__s_classVersion = '5.00';		///h : dclib intern class version
	DCJS_dataStorage.__s_className = 'DCJS_dataStorage';	///h : dclib intern class name
	DCJS_dataStorage.__s_classType = 'class';		///h : dclib intern class type

///*    class properties /////////////////////////////////////////////////////////////////////////////////////
	DCJS_dataStorage.prototype.__s_uniqueSearchKey = '';	///h : stores unique search key
	DCJS_dataStorage.prototype.__o_data = null;		///h : stores data holding all storage objects
	DCJS_dataStorage.prototype.__i_elements = -1;		///h : stores storage elements ammount

///*    class system init ////////////////////////////////////////////////////////////////////////////////////
	DCJS_dataStorage.f_initSystem = function()
		{
		}

///*    class constructor ////////////////////////////////////////////////////////////////////////////////////
	DCJS_dataStorage.prototype.f_construct = function($o_parameter)
		{
		this.f_initData($o_parameter);
		}

///*    public static functions //////////////////////////////////////////////////////////////////////////////


///*    public functions /////////////////////////////////////////////////////////////////////////////////////

///*	------------------------------------------------------------------------------------------------------
	/**
	 * get array
	 * gets all storage elements as an indexed array.
	 *
	 * @access public
	 * @return array
	 *
	*/
	DCJS_dataStorage.prototype.f_getArray = function()
		{
		///h : properties
		var $a_storage = [];		///h : stores storage elements
		var $s_index = '';		///h : stores loop index string

		///h : fill array with storage elements
		for($s_index in this.__o_data)
			{
			$a_storage.push(this.__o_data[$s_index]);
			}

		///h : return data as array
		return($a_storage);
		}

///*	------------------------------------------------------------------------------------------------------
	/**
	 * get data by index
	 * return storage element by indexed array element.
	 *
	 * @access public
	 * @param integer $i_index
	 * @return object
	 *
	*/
	DCJS_dataStorage.prototype.f_getData_byIndex = function($i_index)
		{
		return(this.f_getArray()[$i_index]);
		}

///*	------------------------------------------------------------------------------------------------------
	/**
	 * get data by unique search key
	 * return storage element by unique search key. the function is more performant because the data
	 * has not to be parsed and compared.
	 *
	 * @access public
	 * @param object $o_searchKey
	 * @return object
	 *
	*/
	DCJS_dataStorage.prototype.f_getData_byUniqueSearchKey = function($o_searchKey)
		{
		return(this.__o_data[$o_searchKey]);
		}

///*	------------------------------------------------------------------------------------------------------
	/**
	 * get data
	 * parse all storage elements and compared the transmitted data object which each. if every properties
	 * are equal the relevant storage element is returned.
	 *
	 * @access public
	 * @param object $o_data
	 * @return object
	 *
	*/
	DCJS_dataStorage.prototype.f_getData = function($o_data)
		{
///*		properties
		var $storage_data = null;	///h : stores storage
		var $s_storage_key = null;	///h : stores key
		var $s_var_name = '';		///h : stores var name
		var $i_var_values = -1;		///h : stores number of values
		var $b_found = false;		///h : stores found flag

		///h : find object
		if($o_data[this.__s_uniqueSearchKey] != undefined)
			{
			///h : find storage element by unique search key
			$storage_data = (this.__o_data[$o_data[this.__s_uniqueSearchKey]]);
			}
		else
			{
			///h : parse data
			$i_var_values = 0;
			for($s_var_name in $o_data)
				{
				$i_var_values++;
				}

			///h : parse data
			for($s_storage_key in this.__o_data)
				{
				///h : get relevant storage element
				$storage_data = this.__o_data[$s_storage_key];

				///h : case a : one propery transmitted
				if($i_var_values == 1)
					{
					///h : compare transmitted data with pared data
					if($storage_data[$s_var_name] == $o_data[$s_var_name])
						{
						$b_found = true;
						break;
						}
					}
				///h : case b : more properties transmitted
				else
					{
					///h : compare transmitted data with pared data
					for($s_var_name in $o_data)
						{
						if($storage_data[$s_var_name] == $o_data[$s_var_name])
							{
							$b_found = true;
							break;
							}
						}
					}
				}

			///h : check if storage found or not
			if($b_found != true)
				{
				$storage_data = null;
				}
			}

		///h : return storage
		return($storage_data);
		}


///*	------------------------------------------------------------------------------------------------------
	/**
	 * set data
	 * parse all storage elements and compared the transmitted data object which each. if every properties
	 * are equal the relevant storage element is updated. if no element with relevant unique search key
	 * exists, a new storage element is created.
	 *
	 * @access public
	 * @param object $o_searchKey
	 * @return object
	 *
	*/
	DCJS_dataStorage.prototype.f_setData = function($o_data)
		{
///*		properties
		var $s_storageID = '';		///h : stores unque search key value
		var $storage_data = null;	///h : stores storage
		var $s_var_name;		///h : stores var name

		///h : get unqique search key value and use it as id
		$s_storageID = $o_data[this.__s_uniqueSearchKey];

		///h : search if storage object with same storage id is existing
		if(($storage_data = this.__o_data[$s_storageID]) == null)
			{
			///h : create new storage element
			$storage_data = this.__o_data[$s_storageID] = {};
			this.__i_elements ++;
			}

		///h : update all data with transmitted one old data is overwritten if existing
		for($s_var_name in $o_data)
			{
			$storage_data[$s_var_name] = $o_data[$s_var_name];
			}

		///h : return storage element
		return($storage_data);
		}

///*    pivate static functions //////////////////////////////////////////////////////////////////////////////

///*    private functions ////////////////////////////////////////////////////////////////////////////////////


///*	------------------------------------------------------------------------------------------------------
	/**
	 * init data
	 * init all important data when a new data storage object is created.
	 *
	 * @access private
	 * @param object $o_parameter
	 * @return void
	 *
	*/
	DCJS_dataStorage.prototype.f_initData = function($o_parameter)
		{
		///h : init dall data
		this.__o_data = {};
		this.__i_elements = 0;
		this.__s_uniqueSearchKey = $o_parameter.s_uniqueSearchKey;
		}

///*    class init ///////////////////////////////////////////////////////////////////////////////////////////

/*
--------------------------------------------------------------------------------------------------------------
description:
--------------------------------------------------------------------------------------------------------------
dcjs_datastorage is the part of the dclib js framework.
the class provides database like storage if data storage elements. each storage element can have unlimited
properties, consiting of a var name and its var value.
--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------
events:
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------
getter properties:
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------
usage:
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
*/
