/* ##################################################################
 * Este Plug-in foi desenvolvido pela Agencia Milagro
 * 
 * Responsável por manupular tabelas adicionando ou removento
 * linhas dinamicamente.
 * 
 * @author: Weverson Cachinsky
 * @creation: 28/01/2011
 * @version: 0.1
 * ##################################################################
 */
(function( $ )
{
	$.fn.tableRows = function(option)
	{
		var table = $(this).parents('table').get(0);
		var tbody = $(table).find('tbody').get(0);
		var tr = $(tbody).children('tr:first');
		
		switch(option)
		{
			case 'add':
				var classe = (($(tbody).children('tr').length) % 2 == 0) ? 'color0' : 'color1';
				
				var html  = '<tr class="' + classe + '">';
				html += $(tr).html();
				html += '</tr>';
				
				$(tbody).append(html);
				$(tbody).children('tr:last').find('input').attr('value', '');
				$(tbody).children('tr:last').find('textarea').text('');
				$(tbody).children('tr:last').find('span').text('');
				
				fixInputNames(tbody);
				
				while($(tbody).find('tbody:last').children('tr').length > 1)
				{
					$(tbody).find('tbody:last').children('tr:last').remove();
				}
				
				
			break;
			case 'remove':
				if($(tbody).children('tr').length > 1)
				{
					
					$(this).parents('tr:first').remove();
				}
				else
				{
					$(tbody).children('tr:last').find('input').attr('value', '');
					$(tbody).children('tr:last').find('textarea').text('');
					
					while($(tbody).find('tbody:last').children('tr').length > 1)
					{
						$(tbody).find('tbody:last').children('tr:last').remove();
					}
				}
			break;
			case 'order':
				tableRowFixNames($(this));
			break;
		}
		
		tableRowFixButtons(tbody);
		tableRowFixNames($(this).parents('table').parents('table'));
	};
	
	function tableRowFixNames(obj){
		obj.find('tbody:first > tr').each(function($_parent_index){
			$(this).find('tbody > tr').each(function($_children_index){						
				$(this).find('input:checkbox, input:hidden').each(function(){
					$_name = $(this).attr('name');
					if($_name.indexOf('[') && $_name.indexOf(']')){
						$_indexOf = -1, $_index = 0;
						$_cleanName = $_name.substr(0, $_name.indexOf('['));
						while($_indexOf = $_name.indexOf('[', ($_indexOf + 1))){									
							if($_indexOf < 0)
								break;
								
							$_lastIndexOf = $_name.indexOf(']', ($_indexOf + 1));
							if(($_lastIndexOf - $_indexOf) == 1){
								$_cleanName += '[]';
							} else {
								$_cleanName += ($_index == 0) ? '[' + $_parent_index + ']' : '[' + $_children_index + ']';
							}
							$_index++;
						}								
					}
					$(this).attr('name', $_cleanName);
				});
			});
		});
	}
	
	function tableRowFixButtons(tbody)
	{
		/************************************************************************
		 * Faz com que o botao de ADICIONAR fique visivel somente na ultima linha
		 *************************************************************************/
		$(tbody).children("tr").each(function(i, element)
		{
			if((i+1) < $(tbody).children("tr").length)
			{
				$(element).children("td").children('.btn_add').hide();
			}
			else
			{
				$(element).children("td").children('.btn_add').show();
			}
		});
		$(tbody).find('tbody:last').children('tr:last').children('td').children('.btn_add').show();
		/*********************************************************************************/
		/*********************************************************************************/

	}
	
	function fixInputNames(tbody)
	{
		if($(tbody).find('tbody').length > 0)
		{
			$(tbody).find('tbody').each(function(i, body)
			{
				$(body).find('input').each(function(j, input)
				{
					var oldName = $(input).attr('name');
					var newName = oldName.replace(/[0-9]/, i);
					$(input).attr('name', newName);
				});
			});
		}
	}
})( jQuery );
