<script type="text/javascript">
    app.controller('myController<?=$prop_id?>', ['$scope', function($scope) {

    	$scope.positions = <?=CUtil::PhpToJSObject($arPositions)?>;
		$scope.departments = <?=CUtil::PhpToJSObject($arDepartments)?>;

    	$scope.isChecked = function(value) {
    		var unchecked =  (typeof value == 'undefined') || (typeof value == 'boolean' && value === false);
    		return !unchecked;
    	};

    	$scope.hasChecked = function(item) {
    		var checked = item.checked;
    		if ($scope.isChecked(checked)) return true;

    		var cats = item.categories;
    		
    		for (var c in cats) {
    			var g = cats[c];
    			var checked = $scope.isChecked(g.checked);

    			if (checked) return true;

    			if (typeof g.categories == 'object') {
    				return $scope.hasChecked(g);
    			}
    		}

    		return false;
    	}

    	$scope.getCategoryByID = function(id, categories) {
    		var cat = false;

    		for (var index in categories) {
    			var category = categories[index];
    			
    			if (category.id == id) {
    				cat = category;
    			} else if (typeof category.categories != 'undefined') {
    				cat = $scope.getCategoryByID(id, category.categories);
    			}

    			if (cat) {
    				break;
    			}
			}

			return cat;
    	};

    	$scope.getCategory = function(id, name) {
    		var category = $scope.getCategoryByID(id, $scope[name]);
    		return category;
    	};

    	$scope.checkCategory = function(id, name, params) {
			var category = $scope.getCategory(id, name);

			if (category) {
				for (var p in params) {
					category[p] = params[p];
				}

				var categories = category.categories;

				if (typeof categories == 'object') {
					for (var index in categories) {
						var cat = categories[index];
						$scope.checkCategory(cat.id, name, params);
					}
				}
			}
    	};

    	$scope.checkItem = function(id, value) {
    		if (value === null) value = true;
    		if (typeof value != 'boolean') return;

    		$scope.checkCategory(id, 'positions', { 'checked' : value });
    		$scope.checkCategory(id, 'departments', { 'checked' : value });
    	};

    	$scope.showModal = function() {
    		$('#modal<?=$prop_id?>').modal().modal('show');
            return false;
    	}

    	$scope.showAll = function(jqx, jqxItems) {
    		for (var index in jqxItems) {
    			var item = jqxItems[index];
    			var elem = item.element;
    			$(elem).show();
    		}
    	}

    	$scope.hideAll = function(jqx, jqxItems) {
    		for (var index in jqxItems) {
    			var item = jqxItems[index];
    			var elem = item.element;
    			$(elem).hide();
    		}
    	}

    	$scope.showItem = function(jqx, item) {
			if (typeof item == 'undefined' || item == null) return;

			var elem = item.element;
			$(elem).show();

			if (item.level > 0) {
				var parentElement = item.parentElement;
				var parent = jqx.jqxTree('getItem', parentElement);
				$scope.showItem(jqx, parent);
			}
    	}

    	$scope.hideItem = function(jqx, item) {
			var elem = item.element;
			$(elem).hide();
    	}

    	$scope.filterItems = function(jqx, jqxItems, pattern) {
    		$scope.hideAll(jqx, jqxItems);

    		for (var index in jqxItems) {
    			var item = jqxItems[index];
    			
    			var text = item.label;
    			var result = text.search(pattern);

    			if (result >= 0) {
    				$scope.showItem(jqx, item);
    			}
    		}        		
    	}

    	$scope.search = function(selector, search) {
    		var jqx = $(selector);
    		var items = jqx.jqxTree('getItems');
    		var pattern = new RegExp(search, "i");

    		if (typeof search == 'undefined' || search == '') {
    			$scope.showAll(jqx, items);
    			return;
    		}

    		$scope.filterItems(jqx, items, pattern);
    	}

    	$scope.initPopup = function() {
    		$('.ui .label').each(function() {
        		var elem = $(this).find('.popup');
        		
        		if (elem.length > 0) {
        			elem = elem[0];
            		$(this).popup({
            			popup: elem
            		});
        		}
        	});
    	}

    	$scope.$watch('searchPosition', function(newVal) {
    		var selector = '#jqxTree-position<?=$prop_id?>';
    		$scope.search(selector, newVal);
    	});

    	$scope.$watch('searchDepartment', function(newVal) {
    		var selector = '#jqxTree-department<?=$prop_id?>';
    		$scope.search(selector, newVal);
    	});

    	angular.element(document).ready(function () {
	        $scope.initPopup();
            $('.edit-button').click(function() {
                return false;
            });
	    });
    }]);

	app.directive('semanticTab<?=$prop_id?>', function() {
		return function($scope, elem, attrs) {
			$(elem).tab();
		};
	});

	app.directive('jqxTree<?=$prop_id?>', [function() {
		return {
			restrict: 'A',
			scope: {
		      'model': '='
		    },
		    link: function($scope, elem, attrs) {
		    	$(document).ready(function() {
			    	$(elem).jqxTree({
			        	hasThreeStates: true,
			        	checkboxes: true,
			        	height: '300px'
			        })
			        .on('checkChange', function (event) {
					    var args = event.args;
					    var element = args.element;
					    var checked = args.checked;
					    var id = $(element).attr('item-id');
					    
					    $scope.$apply(function() {
				            $scope.$parent.checkItem(id, checked);
				            
				            // delay for init popup
				            setTimeout(function() {
				            	$scope.$parent.initPopup();
				            }, 500);
				        });
					})
					.css('visibility', 'visible');
		    	});
		    }
		}
	}]);
</script>