/**
 * VectorGrid jQuery Plugin
 * @author Marcus Alexandre Gomes Maia (marcusagmaia@gmail.com)
 * @requires jQuery v1.2.4 or later
 * @version 0.1
 *
 * Copyright (c) 2008 Vector Internet Bussiness - Belo Hozironte - Brazil
 * www.vectornet.com.br
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 */
 
 $.fn.vectorTree = function(options) {
	//Opções de configuração do sistema, veja mais a cima a definição do array cols
	options = $.extend({
		cssChecked:		"tree_check",
		cssUnchecked:	"tree_uncheck",
		cssSemichecked:	"tree_semicheck",
		cssCollapsed:	"tree_collapsed",
		cssExpanded:	"tree_expanded",
		cssLastNo:		"tree_sheet",
		cssPlus:		"tree_plus",
		cssMinus:		"tree_minus",
		oneInMin:		true
	}, options);
	
	// Esconde todas as listas filhas
	$(this)	.find("ul")
			.hide()
			.addClass(options.cssCollapsed);
	
	// Percorre todos os itens adicionando os botões de omitir e mostrar as listas
	// filhas
	$(this)	.find("li:has( > ul )")
			.prepend('<button type="button" class="' + options.cssPlus + '">+</button>')
			.find('button').click(
				function () {
					// Omite ou mostra a lista filha de itens, adicionando a classe
					// correta
					$(this)	.siblings("ul:first")
								.toggle()
								.toggleClass(options.cssExpanded)
								.toggleClass(options.cssCollapsed);
					
					// Altera a classe e o texto do botão de mostrar e omitir
					if( $(this).hasClass(options.cssPlus) )
					{
						$(this).text("-")
							.removeClass(options.cssPlus)
							.addClass(options.cssMinus);
				  	}
				  	else
				  	{
				  		$(this).text("+")
			  				.removeClass(options.cssMinus)
			  				.addClass(options.cssPlus);
				  	}
				}
			);
	
	// Adiciona a ação de marcar todos os checkbox filhos do que recebeu o click
	$(this)	.find("li:has( > ul ) input:checkbox").click(
				function () {
					var UlFilha = $(this).siblings("ul:first");

					if($(this).attr("checked"))
						UlFilha.find("input:checkbox").attr("checked", "checked");
					else
						UlFilha.find("input:checkbox").removeAttr("checked");
					// Send a change event to our parent checkbox:
					$(this).parents("ul:first").siblings(":checkbox").change();
				}
			)
			
			// Garante que a hierarquia ficará corretamente marcada, adicionando um
			// método para verificar a marcação e classes dos checkbox pais.
			.change(function() {
				if ( $(this).siblings('ul:first').length != 0 )
				{
					var UlAbaixo = $(this).siblings('ul:first');
					// Váriáveis que verificam se há checkbox filhos marcados.
					// o tamanho deve ser igual a 1, para eliminar o checkbox atual.
					var any_checked = UlAbaixo.find(':checkbox:checked:first').length == 1;
					var any_unchecked = UlAbaixo.find(':checkbox:not(:checked):first').length == 1;
					
					if (any_checked) {
						$(this).attr('checked', 'checked');
						if (any_unchecked){
							$(this).addClass(options.cssSemichecked)
								.removeClass(options.cssChecked)
								.removeClass(options.cssUnchecked)
							;
						}
						else {
							$(this)
								.addClass(options.cssChecked)
								.removeClass(options.cssSemichecked)
								.removeClass(options.cssUnchecked)
							;
						}
					}
					else {
						$(this).removeClass(options.cssChecked + ' ' + options.cssSemichecked).addClass(options.cssUnchecked);
						$(this).attr('checked', '');
					}
					$(this).parents("ul:first").siblings(":checkbox").change();
				}
			});
};