/**
 * class	D_MarketingtoolProductInputs
 * author	Marco Troost
 */
var D_MarketingtoolProductInputs = new Class({
	
	/**
	 * initialize
	 * @return	void
	 */
	initialize: function(t)
	{
		// nodes
		this.root_node		= $('product_pdf_table');
	},
	
	/**
	 * start
	 * @return	void
	 */
	start: function()
	{
		var _this				= this;
	},

	/**
	 * new row
	 * @return	void
	 */
	newRow: function()
	{
		var _this					= this;
		
		// count inputs
		var productname_input		= this.root_node.getElements('.hidden_field')
		var count					= productname_input.length;	

		var last_hidden_input		= this.root_node.getElements('.hidden_field').getLast();
		var last_hidden_input_id 	= last_hidden_input.get('id');

		var prefix					= 'product_img_src_';
		var last_input_id			= last_hidden_input_id.replace(prefix, '');		
		var last_input_id			= last_input_id.toInt();
		
		var last_hidden_field 		= $$('.hidden_field').getLast();
		var img_source				= last_hidden_field.get('value');
		var input_nr 				= last_input_id +1;
		
		if(img_source)
		{
			var new_tr 				= new Element ('tr');
			var new_td 				= new Element ('td');
			
			var new_td_first = new Element ('td' , 
													{ 	
														'text'	: 'Product',
														'class'	: 'name'
													}
												);

			var new_anchor = new Element ('a' , 
													{ 	'id' 	: 'remove_product_'+input_nr,
														'class'	: 'remove_row',
														'href' 	: '#',
														'text' 	: 'X'
													}
												);
												
			var new_product_input = new Element ('input' , 
													{ 	'type'  : 'text' , 
														'id' 	: 'search_product_'+input_nr, 
														'class' : 'productname',
														'value' : '', 
														'name' 	: 'product['+input_nr+'][name]' 
													}
												);
													
			var new_hiddenfield_input = new Element ('input' , 
														{ 	'type'  : 'hidden' , 
															'id' 	: 'product_img_src_'+input_nr, 
															'class' : 'hidden_field',
															'value' : '',  
															'name' 	: 'product['+input_nr+'][img]' 
														}
													);
			
			var new_hiddenfield_input_prod_id = new Element ('input' , 
															{ 	'type'  : 'hidden' , 
																'id' 	: 'product_id_'+input_nr, 
																'class' : 'hidden_field_prod_id',
																'value' : '',  
																'name' 	: 'product['+input_nr+'][product_id]' 
															}
			);
			
			new_tr.inject($('product_pdf_table').getElement('tbody'));
			new_td_first.inject(new_tr);
			new_td.inject(new_tr);
			new_product_input.inject(new_td);
			new_anchor.inject(new_td);
			new_hiddenfield_input.inject(new_td);
			new_hiddenfield_input_prod_id.inject(new_td);
			
			// add delete events
			var delete_anchor_nodes			= $('product_pdf_table').getElements('.remove_row');
			var total_delete_anchor_nodes	= delete_anchor_nodes.length;
			
			if (total_delete_anchor_nodes > 0)
			{
				delete_anchor_nodes.each(function(delete_anchor_node, index)
				{
					delete_anchor_nodes.removeEvents();
					delete_anchor_nodes.addEvents(
					{
						'click' : function()
						{
							_this.disposeRow(this);
							return false;
						}
					});
				});
			}
			
			// remove searchresults
			_this.hideSearchresults();
			
			// set new div height
			var height 		= $('inner_tabs_content_marketing').getStyle('height').toInt();
			var new_height	= height + 20;
			var height		= $('inner_tabs_content_marketing').setStyle('height', new_height+'px');
				
			var cs_ajax_search_extended_2 = new D_AjaxSearchExtended('search_product_'+input_nr, 'searchresults', '/widgets/tradingsystem/http/account_marketingtools_product_search.php');
			cs_ajax_search_extended_2.start();
		}
	},	
	
	/**
	 * dispose row
	 * @return	void
	 */
	disposeRow: function(anchor_node)
	{
		if (anchor_node)
		{
			var tr_node = anchor_node.getParent('tr');
			if (tr_node)
			{
				tr_node.dispose();
			}
		}
	},
	
	/**
	 * hide searchresults
	 * @return	void
	 */
	hideSearchresults: function()
	{
		$('searchresults').set('text', '');
	}	
	
});