var vstartdate = [];
var datefor = null;
var testcal = "";

define(['angularAMD', 'angular-route', 'angular-animate', 'angular-bootstrap', 'moment', 'angularTreeview','angularjs-dropdown-multiselect'], function (angularAMD) {
	var app = angular.module("tti", ['ngRoute', 'ui.bootstrap', 'angularTreeview','angularjs-datetime-picker','angularjs-dropdown-multiselect']);

	app.config(function ($routeProvider) {
		$routeProvider
			.when("/login", angularAMD.route({
				templateUrl: 'module/login/login.html', controller: 'LoginCtrl', controllerUrl: 'module/login/js/login', languageUrl: "language/logdata"
			}))
			.when("/user", angularAMD.route({
				templateUrl: 'module/user/user.html', controller: 'UserCtrl', controllerUrl: 'module/user/js/user', languageUrl: "language/userdata"
			}))
			.when("/dealer", angularAMD.route({
				templateUrl: 'module/dealer/dealer.html', controller: 'DealerCtrl', controllerUrl: 'module/dealer/js/dealer', languageUrl: "language/dealerdata"
			}))
			.when("/question", angularAMD.route({
				templateUrl: 'module/question/question.html', controller: 'QuestionCtrl', controllerUrl: 'module/question/js/question', languageUrl: "language/questiondata"
			}))
			.when("/assessment", angularAMD.route({
				templateUrl: 'module/assessment/assessment.html', controller: 'AssessmentCtrl', controllerUrl: 'module/assessment/js/assessment', languageUrl: "language/assessmentdata"
			}))
			.when("/content", angularAMD.route({
				templateUrl: 'module/content/content.html', controller: 'ContentCtrl', controllerUrl: 'module/content/js/content', languageUrl: "language/contentdata"
			}))
			.when("/training", angularAMD.route({
				templateUrl: 'module/training/training.html', controller: 'TrainingCtrl', controllerUrl: 'module/training/js/training', languageUrl: "language/trainingdata"
			}))
			.when("/survey", angularAMD.route({
				templateUrl: 'module/survey/survey.html', controller: 'SurveyCtrl', controllerUrl: 'module/survey/js/survey', languageUrl: "language/surveydata"
			}))
			.when("/bulletin", angularAMD.route({
				templateUrl: 'module/bulletin/bulletin.html', controller: 'BulletinCtrl', controllerUrl: 'module/bulletin/js/bulletin', languageUrl: "language/bulletindata"
			}))
			.when("/redeem", angularAMD.route({
				templateUrl: 'module/redeem/redeem.html', controller: 'RedeemCtrl', controllerUrl: 'module/redeem/js/redeem', languageUrl: "language/redeemdata"
			}))
			.when("/user-registartion", angularAMD.route({
				templateUrl: 'module/report/user-registration/user-registration.html', controller: 'UserRegistrationCtrl', controllerUrl: 'module/report/user-registration/js/user-registration', languageUrl: "language/user-registration-data"
			}))
			.when("/user-report", angularAMD.route({
				templateUrl: 'module/report/user-report/user-report.html', controller: 'UserReportCtrl', controllerUrl: 'module/report/user-report/js/user-report', languageUrl: "language/user-report-data"
			}))
			.when("/content-report", angularAMD.route({
				templateUrl: 'module/report/content-report/content-report.html', controller: 'ContentReportCtrl', controllerUrl: 'module/report/content-report/js/content-report', languageUrl: "language/user-content-data"
			}))
			.when("/assessment-report", angularAMD.route({
				templateUrl: 'module/report/assessment-report/assessment-report.html', controller: 'AssessmentReportCtrl', controllerUrl: 'module/report/assessment-report/js/assessment-report', languageUrl: "language/user-assessment-data"
			}))
			.when("/sales-report", angularAMD.route({
				templateUrl: 'module/report/sales-report/sales-report.html', controller: 'SalesReportCtrl', controllerUrl: 'module/report/sales-report/js/sales-report', languageUrl: "language/user-sales-data"
			}))
			.when("/certification-report", angularAMD.route({
				templateUrl: 'module/report/certification-report/certification-report.html', controller: 'CertificationReportCtrl', controllerUrl: 'module/report/certification-report/js/certification-report', languageUrl: "language/user-certification-data"
			}))
			.when("/setsales-report", angularAMD.route({
				templateUrl: 'module/report/setsales-report/setsales-report.html', controller: 'SetsalesReportCtrl', controllerUrl: 'module/report/setsales-report/js/setsales-report', languageUrl: "language/user-setsales-data"
			}))
			.when("/redeem-report", angularAMD.route({
				templateUrl: 'module/report/redeem-report/redeem-report.html', controller: 'RedeemReportCtrl', controllerUrl: 'module/report/redeem-report/js/redeem-report', languageUrl: "language/redeem-report-data"
			}))
			.when("/attrition-report", angularAMD.route({
				templateUrl: 'module/report/attrition-report/attrition-report.html', controller: 'AttritionReportCtrl', controllerUrl: 'module/report/attrition-report/js/attrition-report', languageUrl: "language/attrition-data"
			}))
			.when("/customer-experience-report", angularAMD.route({
				templateUrl: 'module/report/customer-experience-report/customer-experience-report.html', controller: 'ExperienceReportCtrl', controllerUrl: 'module/report/customer-experience-report/js/customer-experience-report', languageUrl: "language/experience-data"
			}))
			.when("/current-login-report", angularAMD.route({
				templateUrl: 'module/report/current-login-report/current-login-report.html', controller: 'CurrentloginReportCtrl', controllerUrl: 'module/report/current-login-report/js/current-login-report', languageUrl: "language/user-current-login-report-data"
			}))
			.when("/bulletin-report", angularAMD.route({
				templateUrl: 'module/report/bulletin-report/bulletin-report.html', controller: 'BulletinReportCtrl', controllerUrl: 'module/report/bulletin-report/js/bulletin-report', languageUrl: "language/user-bulletin-report-data"
			}))
			.when("/enquiry-report", angularAMD.route({
				templateUrl: 'module/report/enquiry-report/enquiry-report.html', controller: 'EnquiryReportCtrl', controllerUrl: 'module/report/enquiry-report/js/enquiry-report', languageUrl: "language/user-enquiry-report-data"
			}))
			.when("/notification", angularAMD.route({
				templateUrl: 'module/report/notification/notification.html', controller: 'NotificationCtrl', controllerUrl: 'module/report/notification/js/notification', languageUrl: "language/notification"
			}))
			.when("/ilearning", angularAMD.route({
				templateUrl: 'module/ilearning/ilearning.html', controller: 'IlearningCtrl', controllerUrl: 'module/ilearning/js/ilearning', languageUrl: "language/ilearning"
			}))
			.when("/sales", angularAMD.route({
				templateUrl: 'module/import/sales/sales.html', controller: 'SalesCtrl', controllerUrl: 'module/import/sales/js/sales', languageUrl: "language/sales"
			}))
			.when("/dsesales", angularAMD.route({
				templateUrl: 'module/import/dsesales/dsesales.html', controller: 'DSESalesCtrl', controllerUrl: 'module/import/dsesales/js/dsesales', languageUrl: "language/dsesales"
			}))
			.otherwise({ redirectTo: "/login" });
	});
	app.run(function ($rootScope, $templateCache, $location, $route) {
		// alert($rootScope.is_logged)
		$rootScope.modalLogShown = false;
		$rootScope.cofirmWindow = false;
		$rootScope.selLanguage = "en_us";
		$rootScope.is_logged = true;
		$rootScope.sidemenuhigh = window.innerHeight - (180);
		$rootScope.logoutfn = function () {
			$rootScope.log = false;
		//	console.log($rootScope.log)
			$rootScope.cofirmWindow = true;
			$rootScope.modalLogShown = true;
			$rootScope.message = "Are you sure you want to logout ?";
		}
		$rootScope.$on("cofirm", function () {
			$rootScope.cofirmWindow = false;
			$rootScope.modalLogShown = false;
			$rootScope.is_logged = false;
		//	console.log($rootScope.log)
			$location.path("/login");

		})
	});

	app.directive('modalDialog', function ($rootScope) {
		return {
			templateUrl: "module/alert.html",
			restrict: 'E',
			transclude: true,
			replace: true,
			backdrop: 'static',
			scope: {
				show: '=',
				type: '=',
			},
			replace: true, // Replace with the template below
			transclude: true, // we want to insert custom content inside the directive
			link: function ($scope, element, attrs) {
				$('#' + attrs.id).css('display', 'block');

				$scope.dialogStyle = {};
				if (attrs.width)
					$scope.dialogStyle.width = attrs.width;
				if (attrs.height)
					$scope.dialogStyle.height = attrs.height;
				$scope.hideModal = function () {

					$scope.show = false;
					$scope.type = false;
				};
				$scope.okClickModal = function () {
				//	console.log(attrs.id)
					$rootScope.$broadcast('confirmDelete');
					$scope.show = false;
					$scope.type = false;
				};
			}
		};
	});

	app.directive('modalPublishForm', function ($rootScope) {
		return {
			templateUrl: "module/publish_form.html",
			restrict: 'E',
			transclude: true,
			replace: true,
			backdrop: 'static',
			scope: {
				show: '=',
				type: '=',
			},
			replace: true, // Replace with the template below
			transclude: true, // we want to insert custom content inside the directive
			link: function ($scope, element, attrs) {
				$('#' + attrs.id).css('display', 'block');
			//	console.log(attrs.id)
				$scope.dialogStyle = {};
				if (attrs.width)
					$scope.dialogStyle.width = attrs.width;
				if (attrs.height)
					$scope.dialogStyle.height = attrs.height;
				$scope.hideModal = function () {
					$scope.show = false;
					$scope.type = false;
					$rootScope.num = 1;
			//		console.log($rootScope.num)
				};
				$scope.okModal = function () {
			//		console.log("here")
					$rootScope.$broadcast('confirmPublish');
					$scope.show = false;
					$scope.type = false;
				};
			}
		};
	});
	app.directive('modalLogpopup', function ($rootScope) {
		return {
			templateUrl: "module/logout.html",
			restrict: 'E',
			transclude: true,
			replace: true,
			backdrop: 'static',
			scope: {
				show: '=',
				type: '=',
			},
			replace: true, // Replace with the template below
			transclude: true, // we want to insert custom content inside the directive
			link: function ($scope, element, attrs) {
				$('#' + attrs.id).css('display', 'block');
				$scope.dialogStyle = {};
				if (attrs.width) {
					$scope.dialogStyle.width = attrs.width;
				}
				if (attrs.height) {
					$scope.dialogStyle.height = attrs.height;
				}

				$scope.$watch(function (value) {
					$scope.title = $rootScope.popuptitle;
				});
				$scope.hideModal = function () {
					$scope.show = false;
					$scope.type = false;
				};
				$scope.okModal = function () {

					$rootScope.$broadcast('cofirm');
					$scope.show = false;
					$scope.type = false;
				};
			}
		};
	});
	app.directive('ngDatepickermin', function ($rootScope) {
		return {
			restrict: 'A',
			require: '?ngModel',
			scope: {

			},
			link: function ($scope, element, attrs, ngModel) {

				if (attrs.isblockdate == "true") {
					element.datepicker({
						dateFormat: 'yy-mm-dd',
						minDate: '01-01-1900',
						maxDate: '+3m',
						onSelect: function (date) {

							if (attrs.name == "dob") {
								$scope.$parent.formData.date_of_join = date;
							} else if (attrs.name == "start_date") {
								$scope.$parent.formData.publisheddate = date;
							} else if (attrs.name == "clsstartdate") {
								$scope.$parent.formData.startdate = date;
							}
							$scope.$parent.$apply();
						}
					});
				} else if (attrs.isblockdate == "false") {
					//console.log("Yes calender 2",attrs.isblockdate)
					element.datepicker({
						minDate: '01-01-16',
						dateFormat: 'dd-mm-yy',
						/*onSelect: function (date) {
							
							testcal=date;
								
							//vstartdate = String(date).split("-");
							datefor = date;
							//datefor = new Date("2016-7-20");
							//datefor = new Date(vstartdate[2],vstartdate[1]-1,vstartdate[0]);
							$scope.$parent.$apply();
						}*/
						onSelect: function (selected) {

							/*var date2 = $('#vechcal1').datepicker('getDate');
            				date2.setDate(date2.getDate()+4);
							
						$("#vechcal2").datepicker("option","maxDate", date2)
						$("#vechcal2").datepicker("option","minDate", selected)*/
							$rootScope.startdate = selected;

							//$scope.vstdate = selected;
							//$scope.$parent.formData.enddates = selected;
							//$scope.$parent.$apply();
							//console.log(vstdate)
						}
					});

				}
			}

		}
	})
	app.directive('ngDatepickermax', function ($rootScope) {
		return {
			restrict: 'A',
			require: '?ngModel',
			scope: {

			},
			link: function ($scope, element, attrs, ngModel) {
				if (attrs.isblockdate == "true") {
					element.datepicker({
						dateFormat: 'yy-mm-dd',
						minDate: 0,
						maxDate: '+3m',
						onSelect: function (date) {
							if (attrs.name == "clspublishdate") {
								$scope.$parent.formData.publishdate = date;
							} else if (attrs.name == "clsenddate") {
								$scope.$parent.formData.enddate = date;
							} else if (attrs.name == "clsstartdate") {
								$scope.$parent.formData.start_date = date;
							} else if (attrs.name == "clsend_date") {
								$scope.$parent.formData.end_date = date;
							}
							$scope.$parent.$apply();
						}
					});
				}
				else if (attrs.isblockdate == "false") {
					//console.log("Yes calender 2",attrs.isblockdate)
					element.datepicker({

						dateFormat: 'dd-mm-yy',
						/*onSelect: function (date) {
							console.log(datefor)	
							$scope.$parent.$apply();
						}
						*/
						onSelect: function (selected) {
							/*var date1 = $('#vechcal2').datepicker('getDate');
								date1.setDate(date1.getDate()-5);	
							$("#vechcal1").datepicker({"minDate": date1});*/
							$scope.$parent.vendtdate = selected;
							//$scope.$parent.formData.publishdates = selected;
							//console.log($scope.$parent.vendtdate);
							$rootScope.enddate = selected;
							//$scope.$parent.$apply();
							//$rootScope.vendtdate = selected;
						}
					});

				}
			}
		}
	})
	/////////////// Report datepicker /////////////
	app.directive('ngReportpickermin', function ($rootScope) {
		return {
			restrict: 'A',
			require: '?ngModel',
			scope: {

			},
			link: function ($scope, element, attrs, ngModel) {

				if (attrs.isblockdate == "true") {
					element.datepicker({
						dateFormat: 'yy-mm-dd',
						// minDate: '0',
						maxDate: '0',
						onSelect: function (date) {
						//	console.log(date)
							
							if (attrs.name == "fromdate") {
								$scope.$parent.formData.from_date = date;
								if((new Date($scope.$parent.formData.from_date).getTime() > new Date($scope.$parent.formData.to_date).getTime())){
									$scope.$parent.formData.to_date =  '';
								}
								var datearr = date.split('-');
						//		console.log(datearr, (Number(datearr[0]) + 1) + "-" + datearr[1] + "-" + datearr[2])
								$("#todate").datepicker("option", "maxDate", 0);
								$('#todate').datepicker("option", 'minDate', datearr[0] + "-" + datearr[1] + "-" + (Number(datearr[2]) + 1));
								// $scope.$parent.formData.to_date = ''
							} else if (attrs.name == "todate") {
								
								// $("#todate").datepicker("option", "maxDate", (Number(datearr[0]) + 1) + "-" + datearr[1] + "-" + datearr[2]);
								$scope.$parent.formData.to_date = date;
								if((new Date($scope.$parent.formData.from_date).getTime() > new Date($scope.$parent.formData.to_date).getTime())){
									$scope.$parent.formData.from_date =  '';
								}
							}
							$scope.$parent.$apply();
						}
					});
				} else if (attrs.isblockdate == "false") {
					//console.log("Yes calender 2",attrs.isblockdate)
					element.datepicker({
						minDate: '01-01-16',
						dateFormat: 'dd-mm-yy',
						/*onSelect: function (date) {
							
							testcal=date;
								
							//vstartdate = String(date).split("-");
							datefor = date;
							//datefor = new Date("2016-7-20");
							//datefor = new Date(vstartdate[2],vstartdate[1]-1,vstartdate[0]);
							$scope.$parent.$apply();
						}*/
						onSelect: function (selected) {

							/*var date2 = $('#vechcal1').datepicker('getDate');
            				date2.setDate(date2.getDate()+4);
							
						$("#vechcal2").datepicker("option","maxDate", date2)
						$("#vechcal2").datepicker("option","minDate", selected)*/
							$rootScope.startdate = selected;

							//$scope.vstdate = selected;
							//$scope.$parent.formData.enddates = selected;
							//$scope.$parent.$apply();
							//console.log(vstdate)
						}
					});

				}
			}

		}
	})
	app.directive('ngReportpickermax', function ($rootScope) {
		return {
			restrict: 'A',
			require: '?ngModel',
			scope: {

			},
			link: function ($scope, element, attrs, ngModel) {
				
				if (attrs.isblockdate == "true") {
					element.datepicker({
						dateFormat: 'yy-mm-dd',
						minDate: 0,
						maxDate: '+3m',
						onSelect: function (date) {
							if (attrs.name == "clspublishdate") {
								$scope.$parent.formData.publishdate = date;
							} else if (attrs.name == "clsenddate") {
								$scope.$parent.formData.enddate = date;
							} else if (attrs.name == "clsstartdate") {
								$scope.$parent.formData.start_date = date;
							} else if (attrs.name == "clsend_date") {
								$scope.$parent.formData.end_date = date;
							}
							$scope.$parent.$apply();
						}
					});
				}
				else if (attrs.isblockdate == "false") {
					//console.log("Yes calender 2",attrs.isblockdate)
					element.datepicker({

						dateFormat: 'dd-mm-yy',
						/*onSelect: function (date) {
							console.log(datefor)	
							$scope.$parent.$apply();
						}
						*/
						onSelect: function (selected) {
							/*var date1 = $('#vechcal2').datepicker('getDate');
								date1.setDate(date1.getDate()-5);	
							$("#vechcal1").datepicker({"minDate": date1});*/
							$scope.$parent.vendtdate = selected;
							//$scope.$parent.formData.publishdates = selected;
							//console.log($scope.$parent.vendtdate);
							$rootScope.enddate = selected;
							//$scope.$parent.$apply();
							//$rootScope.vendtdate = selected;
						}
					});

				}
			}
		}
	})
	app.directive('ngMonthPick', function ($rootScope) {
		return {
			restrict: 'A',
			require: '?ngModel',
			scope: {
				date: "=",
            	onSubmit: "&onSelect"
			},
			link: function (scope, element, attrs, ngModel) {
				 element.datepicker({
						changeMonth: true,
						changeYear: true,
						showButtonPanel: true,
						viewMode: "months",
						minViewMode: "months",
						pickTime: false,
						dateFormat: 'MM yy',
						maxDate:0,
						onChangeMonthYear:function(){
							$('.ui-datepicker-calendar').css({
								display: "none"
							})
						},
						onClose: function(dateText, inst) {
							
							$('.ui-datepicker-calendar').css({
								display: "none"
							})
 							function isDonePressed(){
                                return ($('#ui-datepicker-div').html().indexOf('ui-datepicker-close ui-state-default ui-priority-primary ui-corner-all ui-state-hover') > -1);
                            }
 							if (isDonePressed()){
                                var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
                                var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
                                $(this).datepicker('setDate', new Date(year, month, 1)).trigger('change');
                                
								 
                            }
                        },
						  beforeShow: function(input, ele) {
							var rect = input.getBoundingClientRect();
							setTimeout(function () {
								ele.dpDiv.css({ top: rect.top + 40, left: rect.left + 0 });
							}, 0);
						 	 $('.ui-datepicker-calendar').css({
								display: "none"
							})
							 
							 if ((datestr = $(this).val()).length > 0) {
							  year = datestr.substring(datestr.length - 4, datestr.length);
							  month = jQuery.inArray(datestr.substring(0, datestr.length - 5), $(this).datepicker('option', 'monthNames'));
							  $(this).datepicker('option', 'defaultDate', new Date(year, month, 1));
							  $(this).datepicker('setDate', new Date(year, month, 1));
							}
							
							var other = this.id == "from" ? "#to" : "#from";
							var option = this.id == "from" ? "maxDate" : "minDate";
							if ((selectedDate = $(other).val()).length > 0) {
							  year = selectedDate.substring(selectedDate.length - 4, selectedDate.length);
							  month = jQuery.inArray(selectedDate.substring(0, selectedDate.length - 5), $(this).datepicker('option', 'monthNames'));
							  $(this).datepicker("option", option, new Date(year, month, 1));
							}
							
						  }
						 
					}) 
					.on("click",function(e){
						
						 $('.ui-datepicker-calendar').css({
							display: "none"
						})
					})
					$('.custom_month').focusin(function(){
						
						 $('.ui-datepicker-calendar').css("display","none");
					  });
			}
		}
	})
	app.directive('monthPick', function ($rootScope) {
		return {
			restrict: 'A',
			require: '?ngModel',
			scope: {

			},
			link: function (scope, element, attrs, ngModel) {
			//	console.log(element)
					element.datepicker({
						changeMonth: true,
						changeYear: true,
						showButtonPanel: true,
						dateFormat: 'MM yy',
						maxDate:0,
						onClose: function(dateText, inst) {
							// console.log("inst-->",inst)
							$(this).datepicker('setDate', new Date(inst.selectedYear, inst.selectedMonth, 1));
						}
					})
			}
		}
	});
	// app.directive('datetimez', function ($rootScope) {
	// 	return {
	// 		restrict: 'A',
	// 		require: 'ngModel',
	// 		link: function (scope, element, attrs, ngModelCtrl) {
	// 			element.datetimepicker({
	// 				format: "MM-yyyy",
	// 				viewMode: "months",
	// 				minViewMode: "months",
	// 				pickTime: false,
	// 			}).on('changeDate', function (e) {
	// 				$('.bootstrap-datetimepicker-widget').css({
	// 					display: "none"
	// 				})
	// 				console.log(e, e.date.getMonth(), attrs)
	// 				ngModelCtrl.$setViewValue(e.date);
	// 				scope.$apply();
	// 			});
	// 		}
	// 	};
	// });
	app.service("AppData", function ($rootScope, $http, $httpParamSerializerJQLike) {
		$http.defaults.transformRequest.unshift($httpParamSerializerJQLike);
		$rootScope.loadingCont = false;
        //EDIT 20 Sept 
        var url_str =window.location.href;
        if(url_str.indexOf('.cygnusa.in') !== -1)
        {
            var url_arr=url_str.split("cygnusa.in/");
            $rootScope.static_url = url_arr[0]+"cygnusa.in/";
        }else{
            // $rootScope.static_url = "https://hyper-m-dev.cygnusa.in/";
			$rootScope.static_url = "https://hyper-m.cygnusa.in/";
        }
		var data = {};
		data.sendData = sendData;
		data.checkArrayValue = checkArrayValue;
		function rand() {
			return (new Date()).getTime();
		}
		function checkArrayValue(arr, type, value) {
			var obj = {};
			obj.cont = false;
			for (var id = 0; id < arr.length; id++) {
				if (arr[id][type] == value) {
					obj.cont = true;
					obj.mid = id;
					break;
				}
			}
			return obj;
		}
		function sendData(urllink, data, returnFun) {
		//	console.log(urllink)
			$rootScope.loadingCont = true;
			var req = {
				method: 'POST',
			 url: $rootScope.static_url + urllink + rand(),
				// url:  urllink + rand(),
				headers: { "Connection": "Keep-Alive", 'Content-Type': 'application/x-www-form-urlencoded' },
				data: data
			}
			$http(req).then(function (res) {
				$rootScope.loadingCont = false
				if (returnFun != undefined) {
					returnFun(res.data);
				}
			}, function () {


			});

		}

		return data;

	})
	app.directive('setHeight', function ($window) {
		return {
			link: function (scope, element, attrs) {
				element.css('height', $window.innerHeight - 105 + 'px');
				//element.height($window.innerHeight/3);
			}
		}
	})


	app.directive('uibPagination', function ($rootScope) {
		return {
			templateUrl: "module/pagination.html",
			restrict: 'E',
			transclude: true,
			replace: true,
			backdrop: 'static',
			scope: {
				show: '=',
				type: '=',
			},
			replace: true, // Replace with the template below
			transclude: true, // we want to insert custom content inside the directive
			link: function ($scope, element, attrs) {
			//	console.log(attrs)
				$('#' + attrs.id).css('display', 'block');
				$scope.dialogStyle = {};
				if (attrs.width) {
					$scope.dialogStyle.width = attrs.width;
				}
				if (attrs.height) {
					$scope.dialogStyle.height = attrs.height;
				}

				$scope.$watch(function (value) {
				});
				$scope.hideModal = function () {
					$scope.show = false;
					$scope.type = false;
				};
				$scope.okModal = function () {

					$rootScope.$broadcast('cofirm');
					$scope.show = false;
					$scope.type = false;
				};
			}
		};
	});
	app.directive('multipleAutocomplete', [
		'$filter',
		'$http',
		function ($filter, $http) {
			return {
				restrict: 'EA',
				scope: {
					suggestionsArr: '=?',
					modelArr: '=ngModel',
					apiUrl: '@',
					beforeSelectItem: '=?',
					afterSelectItem: '=?',
					beforeRemoveItem: '=?',
					afterRemoveItem: '=?'
				},
				templateUrl: 'multiple-autocomplete-tpl.html',
				link: function (scope, element, attr) {
					scope.objectProperty = attr.objectProperty;
					scope.selectedItemIndex = 0;
					scope.name = attr.name;
					scope.isRequired = attr.required;
					scope.errMsgRequired = attr.errMsgRequired;
					scope.isHover = false;
					scope.isFocused = false;
					var getSuggestionsList = function () {
						var url = scope.apiUrl;
						$http({
							method: 'GET',
							url: url
						}).then(function (response) {
							scope.suggestionsArr = response.data;
						}, function (response) {
						});
					};

					if (scope.suggestionsArr == null || scope.suggestionsArr == "") {
						if (scope.apiUrl != null && scope.apiUrl != "")
							getSuggestionsList();
						else {
						}
					}
				//	console.log("scope.modelArr ", scope.modelArr)
					if (scope.modelArr == null || scope.modelArr == "" || scope.modelArr == undefined) {
						scope.modelArr = [];

					}
				//	console.log(scope.modelArr)
					scope.onFocus = function () {
						scope.isFocused = true
					};

					scope.onMouseEnter = function () {
						scope.isHover = true
					};

					scope.onMouseLeave = function () {
						scope.isHover = false;
					};

					scope.onBlur = function () {
						scope.isFocused = false;
					};

					scope.onChange = function () {
						scope.selectedItemIndex = 0;
					};

					scope.keyParser = function ($event) {
						var keys = {
							38: 'up',
							40: 'down',
							8: 'backspace',
							13: 'enter',
							9: 'tab',
							27: 'esc'
						};
						var key = keys[$event.keyCode];
						if (key == 'backspace' && scope.inputValue == "") {
							if (scope.modelArr.length != 0) {
								scope.removeAddedValues(scope.modelArr[scope.modelArr.length - 1]);
								//scope.modelArr.pop();
							}
						}
						else if (key == 'down') {
							var filteredSuggestionArr = $filter('filter')(scope.suggestionsArr, scope.inputValue);
							filteredSuggestionArr = $filter('filter')(filteredSuggestionArr, scope.alreadyAddedValues);
							if (scope.selectedItemIndex < filteredSuggestionArr.length - 1)
								scope.selectedItemIndex++;
						}
						else if (key == 'up' && scope.selectedItemIndex > 0) {
							scope.selectedItemIndex--;
						}
						else if (key == 'esc') {
							scope.isHover = false;
							scope.isFocused = false;
						}
						else if (key == 'enter') {
							var filteredSuggestionArr = $filter('filter')(scope.suggestionsArr, scope.inputValue);
							filteredSuggestionArr = $filter('filter')(filteredSuggestionArr, scope.alreadyAddedValues);
							if (scope.selectedItemIndex < filteredSuggestionArr.length)
								scope.onSuggestedItemsClick(filteredSuggestionArr[scope.selectedItemIndex]);
						}
					};


					scope.onSuggestedItemsClick = function (selectedValue) {
						if (scope.modelArr == null || scope.modelArr == "" || scope.modelArr == undefined) {
							scope.modelArr = [];

						}
						if (scope.beforeSelectItem && typeof (scope.beforeSelectItem) == 'function')
							scope.beforeSelectItem(selectedValue);

						scope.modelArr.push(selectedValue);
					//	console.log(scope.modelArr)

						if (scope.afterSelectItem && typeof (scope.afterSelectItem) == 'function')
							scope.afterSelectItem(selectedValue);
						scope.inputValue = "";
					};

					var isDuplicate = function (arr, item) {
						var duplicate = false;
						if (arr == null || arr == "")
							return duplicate;

						for (var i = 0; i < arr.length; i++) {
							duplicate = angular.equals(arr[i], item);
							if (duplicate)
								break;
						}
						return duplicate;
					};

					scope.alreadyAddedValues = function (item) {
						var isAdded = true;
						isAdded = !isDuplicate(scope.modelArr, item);
						//if(scope.modelArr != null && scope.modelArr != ""){
						//    isAdded = scope.modelArr.indexOf(item) == -1;
						//    console.log("****************************");
						//    console.log(item);
						//    console.log(scope.modelArr);
						//    console.log(isAdded);
						//}
						return isAdded;
					};

					scope.removeAddedValues = function (item) {
						if (scope.modelArr != null && scope.modelArr != "") {
							var itemIndex = scope.modelArr.indexOf(item);
							if (itemIndex != -1) {
								if (scope.beforeRemoveItem && typeof (scope.beforeRemoveItem) == 'function')
									scope.beforeRemoveItem(item);

								scope.modelArr.splice(itemIndex, 1);

								if (scope.afterRemoveItem && typeof (scope.afterRemoveItem) == 'function')
									scope.afterRemoveItem(item);
							}
						}
					};

					scope.mouseEnterOnItem = function (index) {
						scope.selectedItemIndex = index;
					};
				}
			};
		}
	]);

	return angularAMD.bootstrap(app);
});