function tree_click(id, init){
	var get_id = $('.'+id);
	
	//console.log(get_id.parents('li').length);
	if(init == 1) {
		get_id.addClass("cur");
		if(get_id.parents('li').length > 0 ) {
			get_id.parents('li').each(function (k,ele) {
				//console.log(get_id.attr('id') + " - "+k + " ==> "+ $(ele).attr('id'));
				tree_click($(ele).attr('id'));
			});
		}
	}
	var openedClass = 'fa-minus-square';
	var closedClass = 'fa-plus-square';
	var icon = get_id.children('i:first');
	icon.toggleClass(openedClass + " " + closedClass);
	get_id.children('ul').toggle();
}

$.fn.extend({
    treed: function (o) {
		var openedClass = 'fa-minus-square';
		var closedClass = 'fa-plus-square';
		var noClass     = 'fa-square';
		
		
		//var openedClass = 'fa-chevron-up';
		//var closedClass = 'fa-chevron-down';
		
		var is_loop = true;
		if (typeof o != 'undefined'){
			if (typeof o.openedClass != 'undefined'){
				openedClass = o.openedClass;
			}
			if (typeof o.closedClass != 'undefined'){
				closedClass = o.closedClass;
			}
			if (typeof o.noClass != 'undefined'){
				noClass = o.noClass;
			}
		};
		
        //initialize each of the top levels
        var tree = $(this);
        tree.addClass("tree");
		var li_obj = (is_loop) ? tree.find('li') : tree.children('li');
		
		li_obj.each(function (k,ele) {
			var branch = $(this); //li with children ul
			
			//$(this).has("a").addClass('ellipsis');
			if(branch.has("ul").length)
			{	
				branch.prepend("<i class='indicator fa " + closedClass + "'></i>");
				branch.addClass('branch');
				
				
				//--- 全選 ---
				$(this).children('input[type=checkbox]:first').on('click', function (e) {
					
					var get_checkbox = $(this).prop("checked");
					branch.children('ul:first').children('li').children('[type=checkbox]').prop("checked",get_checkbox);
					console.log( " tuoch "  );
					//e.preventDefault();
				});
				
				branch.on('click', function (e) {
					if (this == e.target) {
						var icon = $(this).children('i:first');
						icon.toggleClass(openedClass + " " + closedClass);
						$(this).children('ul').children('li').toggle();
					}
					//e.preventDefault();
					
				})
				/*
				$(this).children('i:first').on('click', function (e) {
					if (this == e.target) {
						$(this).toggleClass(openedClass + " " + closedClass);
						branch.children('ul:first').children('li').toggle();
					}
				});
				*/
				branch.children('ul:first').children('li').toggle();
			}else{
				//branch.prepend("<i class='indicator " + noClass + "'></i>");
			}
		});
		
        //fire event from the dynamically added icon
		tree.find('.branch .indicator').each(function(){
			$(this).on('click', function (e) {
				$(this).closest('li').click();
				e.preventDefault();
			});
		});
		
		$('li.cur').parents('li').click();
		$('li.cur').click();
		
		/*
        //fire event to open branch if the li contains an anchor instead of text
        tree.find('.branch>a').each(function () {
            $(this).on('click', function (e) {
                $(this).closest('li').click();
                e.preventDefault();
            });
        });
        //fire event to open branch if the li contains a button instead of text
        tree.find('.branch>button').each(function () {
            $(this).on('click', function (e) {
                $(this).closest('li').click();
                e.preventDefault();
            });
        });
		*/
    }
});

/*
$('#tree5').treed({openedClass:'fa fa-minus-square', closedClass:'fa fa-plus-square', noClass: 'fa fa-square'});
*/
