/**
 * @version 0.0.0.2
 * @author Paweł Łytko
 * @class Tool_html
 * Klasa obsługuje operacje na strukturze dokumentu html.
 * @property object _element_siblings, zawiera definicje istniejącego elementu. Definicja obiektu jest taka sama jak dla właściwości: _new_object_html.
 * @property object _new_object_html, zawiera definicje object_html
 * Lista właściwości, metod klasy object_html
 * @property string _object_child, obiekt html dziecko 
 * @property string _name oznaczenie znacznika, naprzykład: 'p'
 * @property string _txt wartość znacznika
 * @property array _list_attributes, lista atrybutów. Tablica zawierające listę obiektów. Poszczególny obiekt jest zbudowany według szablonu:
 *					@property string _name, nazwa atrybutu. Nazwa atrybutu jest obowiązkowa, jeśli zostanie pominięty wówczas nie zostanie atrybut dodany
 *					@property string _value, wartość atrybutu.
 *					@property string _operator, właściwość może przyjąć wartość: '', '=', '!=', '^=', '$=', '*='
 *					Można pominąć wartość dla właściwości: _value i _operator. Nie można pominoć wartości dla właściwości: _operator jeśli zostanie podana wartości dla właściwości: _value
 *					Należy przypisać puste string wartość dla właściwości _value i _operator aby nie były brane pod uwagę.    
 */


function Tools_html()
{
	this._element_siblings = null;
	this._new_object_html = null;
	
	this.conversion_text = function(p_object)
	{
		var v_return = "";
		var i = 0;
		var list_attributes = "";
		try
		{
			if (jQuery.trim(p_object._name) != '')
			{
				if (typeof p_object._list_attributes == "object")
				{
					while (p_object._list_attributes.length > i)
					{
						list_attributes += p_object._list_attributes[i]._name + '="' + p_object._list_attributes[i]._value + '" '; 
						i++;
					}
				}
				
				list_attributes = jQuery.trim(list_attributes) != '' ? " " + jQuery.trim(list_attributes) : "";
				
				if (typeof p_object._txt == 'string')
				{
					v_return = '<' + p_object._name + list_attributes + ">\n" + p_object._txt + "\n</" + p_object._name + ">\n";
				}
				else
				{
					v_return = '<' + p_object._name + list_attributes + "/>\n";
				}
			}
		}
		catch (object_exception)
		{
			v_return = "";
		}
		return v_return;
	}
	
	this.set_new_object_txt = function(p_value)
	{
		var v_return = false;
		try
		{
			if (typeof this._new_object_html == 'object')
			{
				this._new_object_html._txt = (typeof p_value == 'string' ? p_value : '');
			}
		}
		catch (object_exception)
		{
			v_return = false;
		}
		return v_return;
	}
	
	/**
	 * @method string to_string_jquery przekształca na stringa zgodnego z regółami jQuery
	 * @param object p_object, obiekt object_html
	 */

	this.get_attribute_jquery = function(p_object)
	{
		var v_return = "";
		var i = 0;
		try
		{
			while (p_object._list_attributes.length > i)
			{
				v_return += '[' + p_object._list_attributes[i]._name + (p_object._list_attributes[i]._value != '' ? p_object._list_attributes[i]._operator + p_object._list_attributes[i]._value : '') + '] ';
				i++;
			}
		}
		catch (object_exception)
		{
			v_return = "";
		}
		return jQuery.trim(v_return);
	}
	
	this.to_string_jquery = function(p_object)
	{
		var v_return = '';
		try
		{
			v_return = jQuery.trim(p_object._name) + this.get_attribute_jquery(p_object);
		}
		catch (object_exception)
		{
			v_return = '';
		}
		return v_return;
	}
	
	this.is_exists_element = function(p_value)
	{
		var v_return = false;
		try
		{
			v_return = $(jQuery.trim(p_value)).length >= 1;
		}
		catch (object_exception)
		{
			v_return = false;
		}
		return v_return;
	}
	
	/**
	 * @method bool set_element, metoda zwraca wartość true gdy powiodło się ustawienie właściwości: _element_dom
	 */
	
	this.set_siblings = function(p_object)
	{
		var v_return = false;
		try
		{
			this._element_siblings = p_object;
			v_return = typeof this._element_siblings == 'object';
		}
		catch (object_exception)
		{
			v_return = false;
		}
		return v_return;
	}
	
	/**
	 * @method bool set_new_html
	 * @param object p_object
	 */
	
	this.set_new_object = function(p_object)
	{
		var v_return = false;
		try
		{
			this._new_object_html = p_object;
			v_return = typeof this._new_object_html == 'object';
		}
		catch (object_exception)
		{
			v_return = false;
		}
		return v_return;
	}
	
	/**
	 * @method string get_attribute przeszukuje tablicę: _list_attributes i zwraca wartość danego atrybutu
	 * @param string p_name, nazwa szukanego atrybutu
	 */
	
	this.get_attribute_value = function(p_name)
	{
		var v_return = null;
		var i = 0;
		try
		{
			while (this._new_object_html._list_attributes.length > i)
			{
				if (this._new_object_html._list_attributes[i]._name == jQuery.trim(p_name))
				{
					i = this._new_object_html._list_attributes.length;
					v_return = this._new_object_html._list_attributes[i]._value; 
				}
				i++;
			}
		}
		catch (object_exception)
		{
			v_return = null;
		}
		return v_return;
	}
	
	
	this.get_string_siblings = function()
	{
		return this.to_string_jquery(this._element_siblings);
	}
	
	this.get_string_new_object = function()
	{
		return this.to_string_jquery(this._new_object_html);
	}
	
	this.delete_txt = function(p_id_object)
	{
		try
		{
			if (this.is_exists_element(p_id_object) == true)
			{
				if ($(p_id_object).text() != "" || $(p_id_object).text() != null)
				{
					$(p_id_object).text("");
				}
			}
		}
		catch (object_exception)
		{
		}
	}
	
	this.delete_object = function(p_element)
	{
		if (this.is_exists_element(p_element) == true)
		{
			$(p_element).remove();
		}
	}
	
	/**
	 * @method void delete_nothing usuwa ze struktury dokumentu element, jeśli nie posiada podelementów
	 * @param string p_id_object
	 */
	
	this.delete_nothing = function(p_id_object)
	{
		try
		{
			if (this.is_exists_element(p_id_object) == true)
			{
				if ($(p_id_object + " *").length == 0)
				{
					$(p_id_object).remove();
				}
			}
		}
		catch (object_exception)
		{
			
		}
	}
	
	/**
	 * @method void add_before_txt dodaje nowy element w drzewie DOM lub uzupełnia istniejący element wartością z parametru funkcji. Metoda dodaje element przed elementem określonym przez właściwość: _element_dom.
	 * @param string p_value
	 */
	
	this.add_before = function()
	{
		var v_return = false;
		var f_return = null;
		try
		{
			if (typeof this._element_siblings == 'object' && typeof this._new_object_html == 'object' && this.is_exists_element(this.to_string_jquery(this._element_siblings)) == true)
			{
				if (this.is_exists_element(this.to_string_jquery(this._new_object_html)) == true)
				{
					if (typeof this._new_object_html._txt == "string")
					{
						$(this.to_string_jquery(this._new_object_html)).text(this._new_object_html._txt);
					}
					if (typeof this._new_object_html._object_child == "object")
					{
						if (typeof this._new_object_html._object_child._txt == "string")
						{
							if (this.is_exists_element(this.to_string_jquery(this._new_object_html._object_child)) == false)
							{
								$(this.to_string_jquery(this._new_object_html)).append(this.conversion_text(this._new_object_html._object_child));
							}
							else
							{
								$(this.to_string_jquery(this._new_object_html._object_child)).text(this._new_object_html._object_child._txt);
							}
						}
						else
						{
							$(this.to_string_jquery(this._new_object_html._object_child)).remove();
						}
					}
				}
				else
				{
					f_return = $(this.to_string_jquery(this._element_siblings)).before(this.conversion_text(this._new_object_html));
					if (typeof f_return == "object")
					{
						if (typeof this._new_object_html._object_child == "object")
						{
							if (typeof this._new_object_html._object_child._txt == "string")
							{
								if (this.is_exists_element(this.to_string_jquery(this._new_object_html._object_child)) == false)
								{
									$(this.to_string_jquery(this._new_object_html)).append(this.conversion_text(this._new_object_html._object_child));
								}
								else
								{
									$(this.to_string_jquery(this._new_object_html._object_child)).text(this._new_object_html._object_child._txt);
								}
							}
							else
							{
								$(this.to_string_jquery(this._new_object_html._object_child)).remove();
							}
						}
					}
				}
				v_return = true;
			}
		}
		catch (object_exception)
		{
			v_return = false;
		}
		return v_return;
	}
	
	this.add_after = function()
	{
		var v_return = false;
		var f_return = null;
		try
		{
			if (typeof this._element_siblings == 'object' && typeof this._new_object_html == 'object' && this.is_exists_element(this.to_string_jquery(this._element_siblings)) == true)
			{
				if (this.is_exists_element(this.to_string_jquery(this._new_object_html)) == true)
				{
					if (typeof this._new_object_html._txt == "string")
					{
						$(this.to_string_jquery(this._new_object_html)).text(this._new_object_html._txt);
					}
					if (typeof this._new_object_html._object_child == "object")
					{
						if (typeof this._new_object_html._object_child._txt == "string")
						{
							if (this.is_exists_element(this.to_string_jquery(this._new_object_html._object_child)) == false)
							{
								$(this.to_string_jquery(this._new_object_html)).append(this.conversion_text(this._new_object_html._object_child));
							}
						}
						else
						{
							$(this.to_string_jquery(this._new_object_html._object_child)).remove();
						}
					}
				}
				else
				{
					f_return = $(this.to_string_jquery(this._element_siblings)).after(this.conversion_text(this._new_object_html));
					if (typeof f_return == "object")
					{
						if (typeof this._new_object_html._object_child == "object")
						{
							if (typeof this._new_object_html._object_child._txt == "string")
							{
								if (this.is_exists_element(this.to_string_jquery(this._new_object_html._object_child)) == false)
								{
									$(this.to_string_jquery(this._new_object_html)).append(this.conversion_text(this._new_object_html._object_child));
								}
								else
								{
									$(this.to_string_jquery(this._new_object_html._object_child)).text(this._new_object_html._object_child._txt);
								}
							}
							else
							{
								$(this.to_string_jquery(this._new_object_html._object_child)).remove();
							}
						}
					}
				}
				v_return = true;
			}
		}
		catch (object_exception)
		{
			v_return = false;
		}
		return v_return;
	}
}
