(function ($) {

	'use strict';

	/*
	Basic
	*/
	$('#treeBasic').jstree({
		'core': {
			'themes': {
				'responsive': false
			}
		},
		'types': {
			'default': {
				'icon': 'fa fa-folder'
			},
			'file': {
				'icon': 'fa fa-file'
			}
		},
		'plugins': ['types']
	});

	/*
	Checkbox
	*/
	$('#treeCheckbox').jstree({
		'core': {
			'themes': {
				'responsive': false
			}
		},
		'types': {
			'default': {
				'icon': 'fa fa-folder'
			},
			'file': {
				'icon': 'fa fa-file'
			}
		},
		'plugins': ['types', 'checkbox']
	});

	/*
	Ajax HTML
	*/
	$('#treeAjaxHTML').jstree({
		'core': {
			'themes': {
				'responsive': false
			},
			'check_callback': true,
			'data': {
				'url': './script/php/getTree.php?folder=' + folder,
			}
		},
		'types': {
			'default': {
				'icon': 'fa fa-folder'
			},
			'file': {
				'icon': 'fa fa-file'
			}
		},
		'plugins': ['types']
	}).on(
		'select_node.jstree',
		function (event, data) {

			data.instance.toggle_node(data.node);

			if (data.node.text.includes(".")) {
				console.log(data.node);
				var link = document.createElement("a");
				link.download = data.node.text;
				link.href = data.node.a_attr.href;
				document.body.appendChild(link);
				link.click();
				document.body.removeChild(link);

			}


		}).bind('loaded.jstree', function (e, data) {
		// invoked after jstree has loaded
		$('.jstree-anchor').each(function (index) {
			if ($(this).attr('href').includes(".dll") || $(this).attr('href').includes(".dyn") || $(this).attr('href').includes(".exe") || $(this).attr('href').includes(".msi")) {
				api.push($(this).attr('href'));
			}
		});
		if (api != null) {
			for(i = 0; i < api.length; i++){
				var fileNames = api[i].split("/");
				fileName = fileNames[fileNames.length - 1];
				$("#apiList").append("<a download href='" + api[i] + "' id='api" + i + "'>" + fileName + "</a><br>");
			}
		}
	});

	/*
	Ajax JSON
	*/
	$('#treeAjaxJSON').jstree({
		'core': {
			'themes': {
				'responsive': false
			},
			'check_callback': true,
			'data': {
				'url': function (node) {
					return node.id === '#' ? 'assets/ajax/ajax-treeview-roots.json' : 'assets/ajax/ajax-treeview-children.json';
				},
				'data': function (node) {
					return {
						'id': node.id
					};
				}
			}
		},
		'types': {
			'default': {
				'icon': 'fa fa-folder'
			},
			'file': {
				'icon': 'fa fa-file'
			}
		},
		'plugins': ['types']
	});

	/*
	Drag & Drop
	*/
	$('#treeDragDrop').jstree({
		'core': {
			'check_callback': true,
			'themes': {
				'responsive': false
			}
		},
		'types': {
			'default': {
				'icon': 'fa fa-folder'
			},
			'file': {
				'icon': 'fa fa-file'
			}
		},
		'plugins': ['types', 'dnd']
	});

}).apply(this, [jQuery]);