/** whitelist **/
/*
* Remove element classes with wildcard matching. Optionally add classes:
*   $( '#foo' ).alterClass( 'foo-* bar-*', 'foobar' )
*/
(function ( $ ) {
	$.fn.alterClass = function ( removals, additions ) {
		var self = this;
		if ( removals.indexOf( '*' ) === -1 ) {
			// Use native jQuery methods if there is no wildcard matching
			self.removeClass( removals );
			return !additions ? self : self.addClass( additions );
		}
		var patt = new RegExp( '\\s' + 
				removals.
					replace( /\*/g, '[A-Za-z0-9-_]+' ).
					split( ' ' ).
					join( '\\s|\\s' ) + 
				'\\s', 'g' );

		self.each( function ( i, it ) {
			var cn = ' ' + it.className + ' ';
			while ( patt.test( cn ) ) {
				cn = cn.replace( patt, ' ' );
			}
			it.className = $.trim( cn );
		});
		return !additions ? self : self.addClass( additions );
	};
})(jQuery);

$(document).ready(function () {
	soothScroll = function($scrollTo, topOffset){
		if($scrollTo.length){
			if(undefined == topOffset){
				topOffset = 0;
			}
			$("html, body").animate({ scrollTop: $scrollTo.offset().top + topOffset }, 800);
		}
	};

	$(".u-WaitroseButton").on('click', function (e) {
		var $this = $(this);

		if($(".activate-overlay-screen").length){
			$(".activate-overlay-screen").find(".close-overlay").trigger("click");
		}
		if(!$(".tab-account").hasClass("tab-opened")){
			$(".tab-account").trigger("click");
		} else{
			$("html, body").animate({ scrollTop: $(".tab-account").offset().top }, 800);
		}
		return false;
	});

	$(".c-Expiring-Button").on("click", function(){
		setTimeout(function(){
			$(".c-Car-Plate-Box .c-Car-Plate-Box-Message").show();
			setTimeout(function(){
				$(".c-Car-Plate-Box .c-Car-Plate-Box-Message").slideUp();
				$(".tab-whitelist .tab-container").height("auto");
			}, 5000);

			$('.summary-bar-tabs-Search .c-Search-Small-Block-Inner-Block-Status').addClass("c-Search-Small-Block-Inner-Block-Active");
			$("#whitelist-live-controls .control-filters .control-filter-expiring").trigger("toggleFilter");
			setTimeout(function(){
				$('.summary-bar-tabs-Search .c-Search-Small-Block-Inner-Block-Status').removeClass("c-Search-Small-Block-Inner-Block-Active");
				$("#whitelist-live-controls").removeClass("tab-active	").hide();

				var playlists = $(".c-Car-Plate-Box .licence-plate-list-Parents"),
					ele_status_selected = playlists.find(".car-plate-status .car-plate-status-text:contains('Expiring')");

				playlists.hide();
				if (ele_status_selected.length) {
					platlists = ele_status_selected.parents(".licence-plate-list-Parents");
					platlists.show();
				}
				$(".tab-whitelist .tab-container").height("auto");
			}, 200);

			setTimeout(function(){
				$(".tab-whitelist .tab-container").height("auto");
			}, 1000);
		}, 200);

		if($(".tab-whitelist").hasClass("tab-opened")){
			return false;
		}
	});

	$('.summary-bar-header .accordian-dropdown-toggle').bind({
		click: function (e) {
			$(this).trigger('toggleDisplay');
			e.preventDefault();
			e.stopPropagation();
			return false;
		},
		tap: function (e) {
			$(this).trigger('toggleDisplay');
			e.preventDefault();
			e.stopPropagation();
			return false;
		},
		toggleDisplay: function (e) {
			var bar = $(this).closest('.summary-bar'),
					container = bar.closest('.tab-container'),
					target = bar.find('.accordian-dropdown'),
					animateTarget = target.find('.animate-target');
			container.css('height', 'auto');
			if (!container.hasClass('animating')) {
				bar.addClass('animating-contents');
				if (bar.hasClass('show-summary-dropdown')) {
					animateTarget.animate({opacity: 0}, 150, function () {
						target.animate({height: 0}, 200, function () {
							bar.removeClass('show-summary-dropdown');
							bar.removeClass('animating-contents');
							container.removeClass('animating');
						});
					});
				} else {
					bar.addClass('show-summary-dropdown');
					animateTarget.css({opacity: 0});
					var h = parseInt(target.height()) + parseInt(target.css("padding-top")) + parseInt(target.css("padding-bottom"));
					target.css({height: 0}).animate({height: h}, 200, function () {
						target.css({height: "auto"});
						animateTarget.animate({opacity: 1}, 150, function () {
							container.removeClass('animating');
						});
					});
				}
			}
			e.preventDefault();
			e.stopPropagation();
		}
	});

	$('.summary-bar-tabs-Search .accordian-dropdown-toggle').bind({
		click: function (e) {
			$(this).trigger('toggleDisplay');
			e.preventDefault();
			e.stopPropagation();
			return false;
		},
		tap: function (e) {
			$(this).trigger('toggleDisplay');
			e.preventDefault();
			e.stopPropagation();
			return false;
		},
		toggleDisplay: function (e) {
			var $this = $(this),
					container = $this.closest('.tab-container'),
					bar = container.find('.summary-bar-tabs-content'),
					toggle_link = $this.find(".tab-dropdown-toggle"),
					target_id = toggle_link.attr("href"),
					target = container.find(target_id),
					summary_bar_tabs = target.parents(".summary-bar-tabs-Search"),
					animateTarget = target.find('.animate-target');
			container.css('height', 'auto');
			if (!container.hasClass('animating')) {
				if (target.hasClass("tab-active")) {
					console.log("1");
					toggle_link.removeClass("icon-dropdown2").addClass("icon-dropdown");
					$this.removeClass("c-Search-Small-Block-Inner-Block-Active");
					animateTarget.animate({opacity: 0}, 150, function () {
						target.animate({height: 0}, 200, function () {
							bar.removeClass('show-summary-dropdown');
							container.removeClass('animating');
							bar.find(".tab-dropdown").hide();
							target.hide();
							target.removeClass("tab-active");
						});
					});
				} else {

					if (bar.find(".tab-active").length) {
						summary_bar_tabs.find(".tab-dropdown-toggle").removeClass("icon-dropdown2").addClass("icon-dropdown");
						var _animateTarget = bar.find(".tab-active").find('.animate-target');
						summary_bar_tabs.find(".c-Search-Small-Block-Inner-Block-Active").removeClass("c-Search-Small-Block-Inner-Block-Active");
						console.log("3");
						_animateTarget.animate({opacity: 0}, 150, function () {
							bar.find(".tab-active").animate({height: 0}, 200, function () {
								bar.removeClass('show-summary-dropdown');
								container.removeClass('animating');
								bar.find(".tab-dropdown").hide();
								bar.find(".tab-active").hide();
								bar.find(".tab-active").removeClass("tab-active");
								$this.trigger("click");
							});
						});
					} else {
						toggle_link.removeClass("icon-dropdown").addClass("icon-dropdown2");
						summary_bar_tabs.find(".c-Search-Small-Block-Inner-Block-Active").removeClass("c-Search-Small-Block-Inner-Block-Active");
						$this.addClass("c-Search-Small-Block-Inner-Block-Active");
						target.show();
						target.addClass("tab-active");
						bar.addClass('show-summary-dropdown');
						console.log("4");
						animateTarget.css({opacity: 0});
						var h = parseInt(target.height()) + parseInt(target.css("padding-top")) + parseInt(target.css("padding-bottom"));
						target.css({height: 0}).animate({height: h}, 200, function () {
							target.css({height: "auto"});
							animateTarget.animate({opacity: 1}, 150, function () {
								container.removeClass('animating');
							});
						});
					}
				}

			}
			e.preventDefault();
			e.stopPropagation();
		}
	});

	$('.summary-bar').filter('.dropdown-toggle').find('.summary-bar-header').bind({
		click: function (e) {
			$(this).trigger('toggleDisplay');
			e.preventDefault();
			e.stopPropagation();
			return false;
		},
		tap: function (e) {
			$(this).trigger('toggleDisplay');
			e.preventDefault();
			e.stopPropagation();
			return false;
		},
		toggleDisplay: function (e) {
			$(this).find('.accordian-dropdown-toggle').trigger('click');
			e.preventDefault();
			e.stopPropagation();
		}
	});

	$('._car-plate-status').bind({
		click: function (e) {
			$(this).trigger('toggleSelectedPlate');
			e.preventDefault();
			e.stopPropagation();
			return false;
		},
		tap: function (e) {
			$(this).trigger('toggleSelectedPlate');
			e.preventDefault();
			e.stopPropagation();
			return false;
		},
		toggleSelectedPlate: function (e) {
			var $this = $(this).closest('.car-plate');
			//$this.siblings().removeClass('active-car-plate');
			//$(this).parents(".licence-plate-list").find(".car-plate-status").text("Make Active");

			$this.toggleClass('active-car-plate');
			if ($this.hasClass('active-car-plate')) {
				//$this.removeClass('active-car-plate');
				$(this).html('<span class="car-plate-status-text">Active</span><span class="car-plate-status-button">Suspend</span>');
			} else {
				//$this.addClass('active-car-plate');
				$(this).html('<span class="car-plate-status-text">Dormant</span><span class="car-plate-status-button">Activate</span>');
			}
			e.preventDefault();
			e.stopPropagation();
		}
	});
	$('.car-plate-status').bind({
		click: function (e) {
			$(this).trigger('toggleSelectedPlate');
			e.preventDefault();
			e.stopPropagation();
			return false;
		},
		tap: function (e) {
			$(this).trigger('toggleSelectedPlate');
			e.preventDefault();
			e.stopPropagation();
			return false;
		},
		toggleSelectedPlate: function (e) {
			var $this = $(this),
					$parents = $this.closest('.car-plate');
			if ($parents.hasClass('active-car-plate')) {
				$parents.removeClass('active-car-plate expired-car-plate').addClass('suspended-car-plate');
				$this.find(".car-plate-status-text").text("SUSPENDED");
				$this.find(".car-plate-status-button").text("MAKE LIVE");
			} else if ($parents.hasClass('suspended-car-plate')) {
				$parents.removeClass('suspended-car-plate expired-car-plate').addClass('active-car-plate');
				$this.find(".car-plate-status-text").text("LIVE");
				$this.find(".car-plate-status-button").text("SUSPEND");
			} else if ($parents.hasClass('expired-car-plate')) {
				$parents.removeClass('suspended-car-plate expired-car-plate').addClass('active-car-plate');
				$this.find(".car-plate-status-text").text("LIVE");
				$this.find(".car-plate-status-button").text("SUSPEND");
			}  else if ($parents.hasClass('expiring-car-plate')) {
				$parents.removeClass('suspended-car-plate expiring-car-plate').addClass('active-car-plate');
				$this.find(".car-plate-status-text").text("LIVE");
				$this.find(".car-plate-status-button").text("SUSPEND");
			}
			e.preventDefault();
			e.stopPropagation();
		}
	});

	$('.car-plate-status').bind({
		mouseenter: function () {
			$(this).closest('.car-plate').addClass('car-status-hover');
		},
		mouseleave: function () {
			$(this).closest('.car-plate').removeClass('car-status-hover');
		}
	});
	$('.car-plate-value').bind({
		mouseenter: function () {
			$(this).closest('.car-plate').addClass('car-plate-hover');
		},
		mouseleave: function () {
			$(this).closest('.car-plate').removeClass('car-plate-hover');
		}
	});

	var filterTimeout = [];
	var animatedSearchBlockFilter = function () {
		if ($(".tab-whitelist .c-Search-Small-Block").length) {
			//console.info($(".c-Search-Small-Block").length);
			$(".tab-whitelist .c-Search-Small-Block .accordian-dropdown-toggle").each(function (i, $th) {
				if (filterTimeout[i]) {
					clearInterval(filterTimeout[i]);
				}
				filterTimeout[i] = setInterval(function () {
					var $this = $($th),
							$span = $this.find("span"),
							$spanLength = $this.find("span").length;

					var nextone = $($th).find('span').not("span:visible");
					nextone.hide();
					if ($($th).find("span:visible").index() == ($($th).find("span").length - 1)) {
						nextone = $($th).find("span:first");
					}
					if ($($th).find("span").length > 1) //dont- loop singles
					{
						$($th).find("span:visible").fadeOut(400, function () {
							$(this).hide();
							nextone.hide().fadeIn(400);
						});
					}
					if ($($th).find("span:visible").length == 0) {
						$($th).find("span:first").hide().fadeIn(400);
					}
				}, 5000);
			});
		}
	};
	animatedSearchBlockFilter();

	$('.control-filter').bind({
		click: function (e) {
			$(this).trigger('toggleFilter');
			e.preventDefault();
			e.stopPropagation();
			return false;
		},
		tap: function (e) {
			$(this).trigger('toggleFilter');
			e.preventDefault();
			e.stopPropagation();
			return false;
		},
		toggleFilter: function (e, close) {

			var $this = $(this),
					TimeSelectContainer = $(".c-controlFilters-CustomTime");
			if ($this.hasClass("c-controlFilters-btnTimeCustom")) {
				if ($this.hasClass("control-filter-selected")) {
					TimeSelectContainer.slideUp();
				} else {
					TimeSelectContainer.slideDown();
				}
				$(this).toggleClass('control-filter-selected').siblings().removeClass('control-filter-selected');
			} else {
				TimeSelectContainer.slideUp();
				$(this).addClass('control-filter-selected').siblings().removeClass('control-filter-selected');
			}

			if ($this.parents(".tab-whitelist").length) {
				var $tab = $this.parents(".tab-whitelist"),
						$searchBlock = $tab.find(".u-AnimatedSearchBlocks"),
						$activeOption = $searchBlock.find(".c-Search-Small-Block-Inner-Block.c-Search-Small-Block-Inner-Block-Active");

				if ($activeOption.length && $this.hasClass("control-filter-selected")) {
					$activeOption.find("span.u-answerSpan").remove();
					$activeOption.find("span").first().hide();
					$activeOption.find("span").first().after("<span class='u-answerSpan' style='display:block;'>" + $this.text() + "</span>");
					if (!$this.hasClass("c-controlFilters-btnTimeCustom")) {
						$activeOption.trigger("click");
					}
					// trigger search if live tab
					$tab.find(".c-Search-Input").trigger("key");
				} else {
					$activeOption.find("span.u-answerSpan").remove();
					$activeOption.find("span").first().show();
				}
				animatedSearchBlockFilter();
			}

			return false;
			e.preventDefault();
			e.stopPropagation();
		}
	});
	$('.c-controlFilters-CustomTime-Save').unbind("click").bind("click", function () {
		$('.summary-bar-tabs-Search .accordian-dropdown-toggle.c-Search-Small-Block-Inner-Block-Active').trigger("click");
		return false;
	});

	var fullScreenOverlay = $('.personvehiclepermit-overlay'),
			innerOffsetBox = fullScreenOverlay.find('.inner-fullscreen-offset'),
			PinLockBox = fullScreenOverlay.find(".personvehiclepermit-PinLock-Box-overlay"),
			$pinLogout = $(".pin-logout");


	var getTopOffset = 30;

	var detachDragHandle = function () {
		if ($(".summary-bar [draggable=true]").draggable()) {
			$(".summary-bar [draggable=true]").draggable('destroy');
		}
	};
	dragHandle = function () {
		var dragItems = $('.c-contact-selected[draggable=true]'),
				//singleDragItems = $('[draggable=true]').not(".c-contact-selected"),
				dropZones = $('.dropzone');

		var selectedObjs;

		var offset_start = {};
		detachDragHandle();

		dragItems.draggable({
			opacity: 0.7, /*helper: "clone",*/
			tolerance: "",
			cursorAt: {
				top: 34,
				left: 125
			},
			refreshPositions: true,
			helper: function (event, ui) {
				dragItems.removeClass("u-Dragged");

				var selected = dragItems;
				if (selected.length === 0) {
					var selected = $(this);
				}

				var reversed = selected.get().reverse();
				var container = $('<div/>').attr('id', 'draggingContainer');
				container.html($(reversed).clone());
				//container.css({position:"absolute", left:ui.offset.left-this.offsetLeft, top:ui.offset.top-this.offsetTop});;

				selected.addClass("u-Dragged");

				//setTimeout(function () {
				var $width = selected.width(),
						$height = selected.height() * selected.length;

				//console.log("helper => width : ", $width, " height : ", $height);
				var $arr = {
					left: Math.floor($width / 2),
					top: Math.floor($height / 2)
				};
				//console.info("helper => $arr ", $arr);
				$(this).draggable("option", "cursorAt", $arr);
				//}, 0);
				return container;
			},
			start: function (event, ui) {
				$(this).attr('data-moving', 'true');
			},
			stop: function (event, ui) {
				$(this).attr('data-moving', '');
			},
			drag: function (event, ui) {
			}
		});
		dropZones.droppable({
			over: function (event, ui) {
				$(this).not(".c-AssignContact-Contacts").css('z-index', 0);
				ui.helper.css('z-index', 1);
				$(this).attr("data-over", "over");
			},
			out: function (event, ui) {
				$(this).attr("data-over", "");

			},
			drop: function (event, ui) {
				//console.info($(this));
				$(this).attr("data-over", "");
				var draggedElement = ui.helper.children();
				//console.info(draggedElement);

				dragItems.removeClass("c-contact-selected");
				detachDragHandle();

				var offset = addOverlay();
				extendOverlay('error', 'This action will be activated once connected via API to Rangers database. For now, pls use your imagination that this action has been performed with software grace & beauty');
			}
		});


	};
	dragHandle();


	var checkArrowStatus = function () {
		return false;
		var $top = Math.max(0, (($(window).height() - $(".personvehiclepermit-overlay .navigation-overlay").outerHeight()) / 2)) - 65 + "px";
		$(".personvehiclepermit-overlay .navigation-overlay").css("top", $top);
		if (!$(".personvehiclepermit-overlay .options.active").next(".options").length) {
			console.log("hide 7")
			$(".personvehiclepermit-overlay .icon-arrow-down1").hide();
		} else {
			if ($(".personvehiclepermit-overlay .options.active").next(".options").hasClass("c-option-done")) {
				console.log("hide 8")
				$(".personvehiclepermit-overlay .icon-arrow-down1").hide();
			} else {
				$(".personvehiclepermit-overlay .icon-arrow-down1").show();
			}
		}
		if (!$(".personvehiclepermit-overlay .options.active").prev(".options").length) {
			$(".personvehiclepermit-overlay .icon-arrow-up1").hide();
		} else {
			$(".personvehiclepermit-overlay .icon-arrow-up1").show();
		}

		if ($(".personvehiclepermit-overlay .options.active").find(".option").length) {
			if (!$(".personvehiclepermit-overlay .options.active").find(".option.active").length) {
				console.log("hide 1")
				$(".personvehiclepermit-overlay .icon-arrow-down1").hide();
			} else {
				$(".personvehiclepermit-overlay .icon-arrow-down1").show();
			}
		} else if ($(".personvehiclepermit-overlay .options.active").find(".existing-names").length) {
			if (!$(".personvehiclepermit-overlay .options.active").find(".existing-names a.active").length) {
				console.log("hide 2")
				$(".personvehiclepermit-overlay .icon-arrow-down1").hide();
			} else {
				$(".personvehiclepermit-overlay .icon-arrow-down1").show();
			}
		} else if ($(".personvehiclepermit-overlay .c-Circle-Option").length) {
			if (!$(".personvehiclepermit-overlay .c-Circle-Option.active").length) {
				console.log("hide 3")
				$(".personvehiclepermit-overlay .icon-arrow-down1").hide();
			} else {
				$(".personvehiclepermit-overlay .icon-arrow-down1").show();
			}
		} else {
			$(".personvehiclepermit-overlay .icon-arrow-down1").show();
		}


		if ($(".personvehiclepermit-overlay .options.active .c-CirclesOption").length) {
			console.log("hide 4")
			$(".personvehiclepermit-overlay .icon-arrow-down1").hide();
		}
	};
	var assignValuestoNavigation = function ($set) {
		//console.log("assignValuestoNavigation ", assignValuestoNavigation, $set);
		var navigation = $(".personvehiclepermit-overlay .u-navigationCircle[data-set='" + $set + "']"),
				options = $(".personvehiclepermit-overlay .options[data-set='" + $set + "']");

		if (options.length) {
			if (options.hasClass("c-Vehicle-Options") || options.hasClass("c-Person-Options") || options.hasClass("c-GroupBox-Options")) {
				if (options.find(".existing-names a.active").length) {
					var $val = options.find(".existing-names a.active").text();
					if (options.hasClass("c-GroupBox-Options")) {
						$val = options.find(".existing-names a.active .c-Cycle-GroupDetails-Criteria-FirstStep").text();
					}
					navigation.find(".u-navigationCircle-Animated").remove();
					if ("" !== $val) {
						navigation.append('<span class="u-navigationCircle-Span1 u-navigationCircle-Animated"><span class="u-navigationCircle-Span2">' + $val + '</span></span>');
						navigationInterval();
					}
				} else {
					navigation.find(".u-navigationCircle-Animated").remove();
				}
			} else if (options.hasClass("c-Permit-Options")) {
				var $activeCircle = options.find(".c-Circle-Option.active");
				navigation.find(".u-navigationCircle-Animated").remove();
				if ($activeCircle.length) {
					var start = $activeCircle.attr("data-start"),
							end = $activeCircle.attr("data-end");
					navigation.append('<span class="u-navigationCircle-Span1 u-navigationCircle-Animated"><span class="u-navigationCircle-Span2">' + (start.replace(',', '')) + ' - ' + (end.replace(',', '')) + '</span></span>');
					navigationInterval();
				}
			} else if (options.hasClass("c-DayTime-Options")) {
				var $activeCircle = options.find(".c-Circle-Option.active");
				navigation.find(".u-navigationCircle-Animated").remove();
				if ($activeCircle.length) {
					var $str = '';
					$.each($activeCircle, function (i, $th) {
						var $this = $($th),
								$day = $this.attr("data-day");
						if ($day = "all") {
							$str = "all days";
						} else {
							$str += $day.substr(0, 2) + ",";
						}
					});

					navigation.append('<span class="u-navigationCircle-Span1 u-navigationCircle-Animated"><span class="u-navigationCircle-Span2">' + $str.slice(0, -1) + '</span></span>');
					navigationInterval();
				}
			} else if (options.hasClass("c-Location-Options")) {
				var $activeCircle = options.find(".existing-names a.active");
				navigation.find(".u-navigationCircle-Animated").remove();
				if ($activeCircle.length) {
					var $text = '';
					$.each($activeCircle, function (i, $th) {
						if (i < 3) {
							$text += '<span>' + $($th).text() + '' + ((i < ($activeCircle.length - 1) && i < 2) ? "," : "") + '</span>';
						}
					});
					navigation.append('<span class="u-navigationCircle-Span1 u-navigationCircle-Animated"><span class="u-navigationCircle-Span2">' + $text + '</span></span>');
					navigationInterval();
				}
			}
		}
	};
	var checkNavigationStatus = function ($option) {

		if (undefined == $option) {
			$option = $(".personvehiclepermit-overlay .options.active");
		}

		var $set = $option.attr("data-set");
		var $optionCricle = $(".personvehiclepermit-overlay .options .option.active"),
				$opt = $optionCricle.attr("data-option");
		if ($opt != "permit") {
			$(".navigation-overlay .u-setDone:first").nextAll().removeClass("u-setDone");
		}
		$(".personvehiclepermit-overlay .navigation-overlay .active").removeClass("active");
		$(".personvehiclepermit-overlay .u-navigationCircle[data-set='" + $set + "']").addClass("active");
		assignValuestoNavigation($set);
		$(".personvehiclepermit-overlay .u-navigationCircle.active").prevAll().addClass("u-setDone");

		showDeleteLocationButton();
	};
	$('.personvehiclepermit-overlay .close-overlay').bind({
		click: function () {
			if (fullScreenOverlay.find(".c-Pin-Option-Selection").hasClass("active") && fullScreenOverlay.find(".options").hasClass("u-PinLock-Active")) {
				var setOne = fullScreenOverlay.find(".c-Pin-Option-Selection");
				var setTwo = fullScreenOverlay.find(".options.u-PinLock-Active");

				setOne.removeClass("active").fadeOut(800, function () {
					var getTopOffset = 60;
					var twoHeight = setTwo.height();
					var windowHeight = $(window).height();
					var offsetTwo = (windowHeight / 2) - (twoHeight / 2) - getTopOffset;// + windowOffset;
					setTwo.css('top', offsetTwo).fadeIn(800, function () {
						fullScreenOverlay.find(".u-PinLock-Active").removeClass("u-PinLock-Active").addClass("active");
						fullScreenOverlay.find(".navigation-overlay").show();
					});
				});
			} else {
				removeOverlay();
			}
			return false;
		}
	});
	$('.personvehiclepermit-overlay .navigation-overlay a').not(".u-gotOSummary").bind({
		_click: function () {
			var $this = $(this),
					$navigation = $this.attr("data-navigation"),
					$options = $(".options"),
					setOne = $(".options.active");
			$(".personvehiclepermit-overlay .c-Footer a").removeClass("active");
			if ($navigation == 'up') {
				var setTwo = $(".personvehiclepermit-overlay .options.active").prev(".options");
				setOne.removeClass("active").fadeOut(800, function () {
					var twoHeight = setTwo.height();
					var windowHeight = $(window).height();
					var offsetTwo = (windowHeight / 2) - (twoHeight / 2) - getTopOffset; // + windowOffset;
					setTwo.css('top', offsetTwo).fadeIn(800, function () {
						setTwo.addClass("active");
						checkArrowStatus();
						lastSlideClose(setTwo);
						checkDateTimeFooterButton();
						checkFooterButton(setTwo);
					});
				});
			} else {
				var setTwo = $(".options.active").next(".options");
				var slide = true;
				if (slide) {
					setOne.removeClass("active").fadeOut(800, function () {
						var twoHeight = setTwo.height();
						var windowHeight = $(window).height();
						var offsetTwo = (windowHeight / 2) - (twoHeight / 2) - getTopOffset; // + windowOffset;
						setTwo.css('top', offsetTwo).fadeIn(800, function () {
							setTwo.addClass("active");
							checkArrowStatus();
							lastSlideClose(setTwo);
							checkDateTimeFooterButton(setTwo);
							checkFooterButton(setTwo);
						});
					});
				}
			}
			return false;
		},
		click: function () {
			var $this = $(this),
					$set = $this.attr("data-set"),
					$navigation = $this.attr("data-navigation"),
					$options = $(".personvehiclepermit-overlay .options"),
					setActive = $(".personvehiclepermit-overlay .options.active"),
					$activeSet = setActive.attr("data-set");
			console.log($set, " == ", $activeSet);
			if ($set == $activeSet) {
				return false;
			} else {
				showAllSteps();
				var validate = validateBlock($this, setActive);
				//var validate = true;
				console.log("validate ", validate);
				if (validate) {
					//$(".c-Footer a").removeClass("active");
					var setTwo = $(".personvehiclepermit-overlay .options[data-set='" + $set + "']");
					closePinLock(setTwo);
					setActive.removeClass("active").fadeOut(800, function () {
						var twoHeight = setTwo.height();
						var windowHeight = $(window).height();
						var offsetTwo = (windowHeight / 2) - (twoHeight / 2) - getTopOffset; // + windowOffset;

						openPinLock(setTwo, offsetTwo);
						setTwo.css('top', offsetTwo).fadeIn(800, function () {
							setTwo.addClass("active");
							/*checkArrowStatus();*/
							lastSlideClose(setTwo);
							checkDateTimeFooterButton();
							checkFooterButton(setTwo);
							checkNavigationStatus();
						});
					});
				}
			}
			return false;
		}
	});

	var openPinStep = function ($action) {
		var setTwo = fullScreenOverlay.find('.c-Pin-Option-Selection'),
				setOne = fullScreenOverlay.find('.options.active');
		fullScreenOverlay.find(".navigation-overlay").hide();
		setOne.removeClass("active").fadeOut(800, function () {
			var getTopOffset = 60;
			var twoHeight = setTwo.height();
			var windowHeight = $(window).height();
			var offsetTwo = (windowHeight / 2) - (twoHeight / 2) - getTopOffset;// + windowOffset;
			setTwo.css('top', offsetTwo).fadeIn(800, function () {
				console.log(setTwo);
				setTwo.addClass("active");
				if (undefined !== $action) {
					setTwo.attr("action", $action);
				} else {
					setTwo.attr("action", '');
				}
			});
		});
	};
	var validateCircleOptionLinks = function () {
		var $body = $("body"),
				$addPin = $body.attr('data-add-pin'),
				$masterPin = $body.attr('data-GroupMasterPinAccepted'),
				$valid = false;
		if ("1" === $masterPin) {
			$valid = true;
		} else {
			openPinStep('master');
		}
		return $valid;
	};
	fullScreenOverlay.find('.c-Pin-Option-Selection .c-Pin-Footer .c-Footer-Cancel-Button').on('click', function () {
		fullScreenOverlay.find(".close-overlay").trigger("click");
		return false;
	});

	var openCustomPinLock = function (setActive, offsetActive) {
		var twoHeight = setActive.height();
		var windowHeight = $(window).height();
		var offsetActive = (windowHeight / 2) - (twoHeight / 2) - getTopOffset; // + windowOffset;

		var $result = true;
		var $isPinNeed = $("body").attr("data-GroupReviewPinAccepted");
		PinLockBox.removeClass("active");
		if ("" == $isPinNeed) {
			$isPinNeed = $("body").attr("data-GroupMasterPinAccepted");
		}
		if ("" == $isPinNeed) {
			$result = false;
			offsetActive = offsetActive + 25;
			/* Open Lockbox ui */
			fullScreenOverlay.find(".c-Options-Locked").removeClass("c-Options-Locked");
			setActive.addClass("c-Options-Locked")
					.find(".option-container").addClass("u-TempHide");
			setActive.find(".c-Footer").addClass("u-TempHide");
			PinLockBox.css('top', offsetActive).addClass("active");
		}
		return $result;
	};
	var openPinLock = function (setActive, offsetActive) {
		var $isPinNeed = $("body").attr("data-GroupReviewPinAccepted");
		PinLockBox.removeClass("active");
		if ("" == $isPinNeed) {
			$isPinNeed = $("body").attr("data-GroupMasterPinAccepted");
		}
		if ("" == $isPinNeed) {
			offsetActive = offsetActive + 25;
			/* Open Lockbox ui */
			if (setActive.hasClass("c-Permit-Options") || setActive.hasClass("c-DayTime-Options") || setActive.hasClass("c-Location-Options")) {
				var GroupSelected = fullScreenOverlay.find(".c-GroupBox-Options .existing-names a.active"),
						lock_duration = GroupSelected.attr("data-lock_duration"),
						lock_daytime = GroupSelected.attr("data-lock_daytime"),
						lock_person = GroupSelected.attr("data-lock_person"),
						lock_vehicle = GroupSelected.attr("data-lock_vehicle"),
						lock_location = GroupSelected.attr("data-lock_location");
				var openLockBox = false;
				/*setActive.find(".c-PinLock-Options-Box").addClass("active");*/
				if (setActive.hasClass("c-Permit-Options") && undefined !== lock_duration && "1" === lock_duration) {
					openLockBox = true;
				} else if (setActive.hasClass("c-DayTime-Options") && undefined !== lock_daytime && "1" === lock_daytime) {
					openLockBox = true;
				} else if (setActive.hasClass("c-Location-Options") && undefined !== lock_location && "1" === lock_location) {
					openLockBox = true;
				}
				fullScreenOverlay.find(".c-Options-Locked").removeClass("c-Options-Locked");
				if (openLockBox) {
					setActive.addClass("c-Options-Locked")
							.find(".option-container").addClass("u-TempHide");
					setActive.find(".c-Footer").addClass("u-TempHide");
					PinLockBox.css('top', offsetActive).addClass("active");
				}
			}

		}
	};
	var closePinLock = function (setActive) {
		PinLockBox.removeClass("active");
		setActive.find(".c-PinLock-Options-Box").removeClass("active");
		setActive.removeClass("c-Options-Locked").find(".option-container").removeClass("u-TempHide");
		setActive.find(".c-Footer").removeClass("u-TempHide");
	};
	function validateBlock($this, $activeOption) {
		var $set = $this.attr("data-set"),
				$navigation = $this.attr("data-navigation"),
				$options = $(".personvehiclepermit-overlay .options"),
				setActive = $(".personvehiclepermit-overlay .options.active"),
				$activeSet = setActive.attr("data-set");
		var $result = true;
		if ($this.prev(".u-navigationCircle").length) {
			if (!$this.prevAll('.u-navigationCircle:not(.u-TempHide)').filter(':first').hasClass("u-setDone")) {
				console.log(1);
				$result = false;
			}
		} else if (!$this.hasClass("u-setDone")) {

			if ($(".personvehiclepermit-overlay .options.active").find(".option").length) {
				if (!$(".personvehiclepermit-overlay .options.active").find(".option.active").length) {
					console.log(2);
					$result = false;
				}
			} else if ($(".personvehiclepermit-overlay .options.active").find(".existing-names").length) {
				if (!$(".personvehiclepermit-overlay .options.active").find(".existing-names a.active").length) {
					console.log(3);
					$result = false;
				}
			} else if ($(".personvehiclepermit-overlay .c-Circle-Option").length) {
				if (!$(".personvehiclepermit-overlay .c-Circle-Option.active").length) {
					console.log(4);
					$result = false;
				}
			} else {

			}

			if ($(".personvehiclepermit-overlay .options.active .c-CirclesOption").length) {
				$result = false;
			}
		}
		return $result;
	}

	$('.overlay-trigger').bind({
		click: function () {
			removeOverlay();
			addOverlay();
			showAllSteps();
			/*extendOverlay('personvehiclepermit');*/
			var outerTarget = fullScreenOverlay.find('.personvehiclepermit').css({display: 'block'});
			$('.personvehiclepermit .set-two').css({display: 'none', top: 0});
			var innerTarget = outerTarget.find('.set-one');
			var targetHeight = innerTarget.height();
			var windowHeight = $(window).height();
			var windowOffset = $(window).scrollTop();
			var offsetTop = (windowHeight / 2) - (targetHeight / 2) - getTopOffset; // + windowOffset;

			innerTarget.css({top: offsetTop, display: 'block'});
			outerTarget.fadeIn(800, function () {
				innerTarget.addClass("active");
				/*checkArrowStatus();*/
				checkNavigationStatus();
			});
			return false;
		}
	});
	var showAllSteps = function () {
		$('.personvehiclepermit-overlay .navigation-overlay a.u-navigationCircle').removeClass("u-hideUnwanted u-showWanted");
	};
	var hideUnwantedSteps = function ($step) {
		$('.personvehiclepermit-overlay .navigation-overlay a.u-navigationCircle').addClass("u-hideUnwanted");
		$('.personvehiclepermit-overlay .navigation-overlay a.u-navigationCircle:first, .personvehiclepermit-overlay .navigation-overlay a.u-navigationCircle[data-set="' + $step + '"]').removeClass("u-hideUnwanted");
		$('.personvehiclepermit-overlay .navigation-overlay a.u-navigationCircle[data-set="' + $step + '"]').addClass("u-showWanted");
	};

	var PermitTimeDurationError = function ($error) {
		var $TimerSelectBox = $(".personvehiclepermit-overlay .c-DayTimes .c-TimerSelectBox");
		$TimerSelectBox.find(".widgetpopup-Box-overlay").addClass("active").find(".c-Contentbox span").html($error);
		setTimeout(function () {
			$TimerSelectBox.find(".widgetpopup-Box-overlay").removeClass("active");
		}, 4000);
	};
	$('.personvehiclepermit').on('click', '.option', function () {
		var $this = $(this),
				$step = $this.attr("data-set"),
				$optins = $this.attr("data-option");
		$this.parents('.options').find(".option").removeClass("active");
		$this.addClass("active");
		var setOne = $(".personvehiclepermit-overlay .options.active");
		showAllSteps($step);
		if ($optins == "permit") {
			var setNext = $(".personvehiclepermit-overlay .options.active").next(".options");
			$('.personvehiclepermit-overlay .navigation-overlay a.u-navigationCircle[data-set="one"]').addClass("u-hideUnwanted");
		} else if ($optins == "group") {
			var $isPinNeed = $("body").attr("data-GroupReviewPinAccepted");
			if ("" == $isPinNeed) {
				$isPinNeed = $("body").attr("data-GroupMasterPinAccepted");
			}
			if ("" == $isPinNeed) {
				$('.personvehiclepermit-overlay').fadeOut();
				$(".groupswidget-overlay").attr({"from": "personvehiclepermit", "backTofirst": true}).fadeIn();
				$('.c-Group-Widget-Button:first').trigger("click");
			}
			var setNext = $(".personvehiclepermit-overlay .options[data-set='" + $step + "']");
			hideUnwantedSteps($step);
		} else {
			var setNext = $(".personvehiclepermit-overlay .options[data-set='" + $step + "']");
			hideUnwantedSteps($step);
		}

		setOne.removeClass("active").fadeOut(800, function () {
			var twoHeight = setNext.height();
			var windowHeight = $(window).height();
			var offsetTwo = (windowHeight / 2) - (twoHeight / 2) - getTopOffset; // + windowOffset;
			setNext.css('top', offsetTwo).fadeIn(800, function () {
				setNext.addClass("active");
				$('.personvehiclepermit-overlay .navigation-overlay').addClass("active");
				checkNavigationStatus();
			});
		});
		return false;
	}).on('click', '.c-DayTimes .c-Circle-Option', function () {
		var $this = $(this),
				$day = $this.attr("data-day");
		/* if locked than return false without any action */
		if ($(".personvehiclepermit-overlay").find(".c-DayTime-Options").hasClass("c-Options-Locked")) {
			return false;
		}

		$(".personvehiclepermit-overlay .c-DayTimes .c-Footer a").removeClass("active");
		if ($this.hasClass("active")) {
			$this.removeClass("active hasActive");
			$this.find(".c-OptionTimer").removeClass("active c-hasActive");
			removeCircleToSummary($day);
		} else {
			if ($this.find(".c-Circle").text() == "ALL DAYS") {
				$(".personvehiclepermit-overlay .c-DayTimes .c-Circle-Option").removeClass("active");
				$(".personvehiclepermit-overlay .c-DayTimes .c-Circle-Option .c-OptionTimer").removeClass("active");
				removeDateCircleSummary();
				$(".personvehiclepermit-overlay .c-DayTimes .c-Circle-Option").not($this).attr({"data-start": "", "data-end": ""});
				addCircleToSummary("all");
			} else {
//$(".c-Circle-Option.c-hasActive").removeClass("active");

				$(".personvehiclepermit-overlay .c-DayTimes .c-Circle-Option[data-day='all']").removeClass("active").attr({"data-start": "", "data-end": ""});
				if ($(".personvehiclepermit-overlay .c-DayTimes .c-Circle-Option.c-hasActive").length) {
					console.log("setTimeValues 3");
					setTimeValues($(".personvehiclepermit .c-DayTimes .c-Circle-Option.c-hasActive"));
				}

				$(".personvehiclepermit-overlay .c-DayTimes .c-Circle-Option[data-day='all'] .c-OptionTimer").removeClass("active");
				addCircleToSummary($day);
			}

			$(".personvehiclepermit-overlay .c-DayTimes .c-Circle-Option").removeClass("c-hasActive");
			$this.addClass("active c-hasActive");
			//setTimeout(function () {
			OpenInputBox($this.parents('.options').find(".c-TimerSelectBox"), $this);
			$('.personvehiclepermit-overlay .c-DayTimes .c-TimerSelect-Save').unbind("click").bind("click", function () {
				console.log("setTimeValues 1");
				if (setTimeValues($this)) {
					$this.find(".c-OptionTimer").addClass("active");
					$this.addClass("active");
					CloseInputBox($(".personvehiclepermit .c-DayTimes .c-TimerSelectBox"));
				}
				return false;
			});
			//}, 1000);


		}
		checkFooterButton();
		return false;
	}).on('click', '.c-DayTimes .c-OptionTimer', function () {
		var $this = $(this);
		if ($this.hasClass("active")) {
			$this.addClass("c-hasActive");
			$this.parents('.c-Circle-Option').addClass("c-hasActive");
			setTimeout(function () {
				OpenInputBox($this.parents('.options').find(".c-TimerSelectBox"), $this.parents('.c-Circle-Option'));
				//$this.parents('.options').find(".c-TimerSelectBox").addClass("active");
				$('.personvehiclepermit-overlay .c-DayTimes .c-TimerSelect-Save').unbind("click").bind("click", function () {
					if (setTimeValues($this.parents('.c-Circle-Option'))) {
						$this.parents('.c-Circle-Option.active').find(".c-OptionTimer").addClass("active");
						console.log("setTimeValues 2");
						CloseInputBox($this.parents('.options').find(".c-TimerSelectBox"));
						$this.removeClass("c-hasActive");
						$this.parents('.c-Circle-Option').removeClass("c-hasActive");
					}
					return false;
				});
			}, 1000);
		} else {
//$this.addClass("active");
		}
		return false;
	}).on('click', '.c-DayTimes .c-TimerSelect-Close', function () {
		var $this = $(this);
		CloseInputBox($(".personvehiclepermit .c-DayTimes .c-TimerSelectBox"));
		if ($(".personvehiclepermit-overlay .c-DayTimes .c-Circle-Option.c-hasActive").length) {
			//console.log("setTimeValues 5");
			//setTimeValues($(".personvehiclepermit-overlay .c-DayTimes .c-Circle-Option.c-hasActive"));
		}
		return false;
	}).on('click', '.c-DayTimes .c-TimerSelect-AllDay', function () {
		var $this = $(this),
				$CircleOption = $(".personvehiclepermit-overlay .c-DayTimes .c-Circle-Option.c-hasActive"),
				$startTime = $CircleOption.attr("data-start"),
				$endTime = $CircleOption.attr("data-end"),
				$TimerSelectBox = $(".personvehiclepermit-overlay .c-DayTimes .c-TimerSelectBox"),
				$startInput = $TimerSelectBox.find(".c-TimerSelect-StartTime"),
				$endInput = $TimerSelectBox.find(".c-TimerSelect-EndTime");
		/*if ($this.hasClass("active")) {
		 $startInput.val($startTime);
		 $endInput.val($endTime);
		 $this.removeClass("active");
		 } else {*/
		$startInput.val("00:00");
		$endInput.val("00:00");
		$this.addClass("active");
		//}
		return false;
	}).on("click", ".c-Done-Footer .c-Footer-Button, .c-Done-Footer .c-Footer-Cancel-Button", function () {
		var $this = $(this);
		var setOne = $(".personvehiclepermit-overlay .options.active");
		var setNext = $(".personvehiclepermit-overlay .options.active").next(".options");
		setOne.removeClass("active").fadeOut(800, function () {
			var twoHeight = setNext.height();
			var windowHeight = $(window).height();
			var offsetTwo = (windowHeight / 2) - (twoHeight / 2) - getTopOffset; // + windowOffset;
			if (!$this.hasClass("c-Footer-Cancel-Button")) {
				setNext.find(".Done-text").text("SAVED");
			} else {
				setNext.find(".Done-text").text("CANCELLED");
			}
			setNext.css('top', offsetTwo).fadeIn(800, function () {
				setNext.addClass("active");
				$('.personvehiclepermit-overlay .navigation-overlay').removeClass("active");
				lastSlideClose(setNext);
			});
		});
		return false;
	}).on("click", ".c-PermitDuration-StartEndSelection .c-add-input", function () {
		var $this = $(this),
				$day = $this.attr("data-day"),
				$month = $this.attr("data-month"),
				$year = $this.attr("data-year");
		$(".personvehiclepermit .c-PermitDuration-StartEndDateSelection").removeClass("active");
		if (!$this.hasClass("active")) {
			$this.addClass("active");
			$(".personvehiclepermit .c-PermitDuration-StartEndDateSelection #permit-add-day").val($day);
			$(".personvehiclepermit .c-PermitDuration-StartEndDateSelection #permit-add-month").val($month);
			$(".personvehiclepermit .c-PermitDuration-StartEndDateSelection #permit-add-year").val($year);
			$(".personvehiclepermit .c-PermitDuration-StartEndSelection").fadeOut(250, function () {
				$(".personvehiclepermit .c-PermitDuration-StartEndDateSelection").fadeIn(250, function () {
					$(this).addClass("active");
				});
			});
		}
		return false;
	}).on("click", ".c-PermitDuration-StartEndDateSelection .c-PermitDuration-StartEndDateSelection-BtnSave", function () {
		var $this = $(this),
				optioncontainer = $this.parents(".option-container"),
				$startDateButton = $("#permit-StartDate"),
				$endDateButton = $("#permit-EndDate"),
				$activeButton = $(".personvehiclepermit .c-PermitDuration-StartEndSelection .c-add-input.active"),
				$day = $(".personvehiclepermit .c-PermitDuration-StartEndDateSelection #permit-add-day").val(),
				$month = $(".personvehiclepermit .c-PermitDuration-StartEndDateSelection #permit-add-month").val(),
				$year = $(".personvehiclepermit .c-PermitDuration-StartEndDateSelection #permit-add-year").val();
		var DateError = function ($error) {
			optioncontainer.find(".personvehiclepermit-Box-overlay").addClass("active").find(".c-Contentbox span").html($error);
			setTimeout(function () {
				optioncontainer.find(".personvehiclepermit-Box-overlay").removeClass("active");
			}, 4000);
		};
		if (1 > $day || 31 < $day) {
			DateError("Please enter valid 2 digit day number.");
		} else if (1 > $month || 12 < $month) {
			DateError("Please enter valid 2 digit month number.");
		} else if ($year.length != 4) {
			DateError("Please enter valid 4 digit year number.");
		} else {
			var $date = $day + "/" + $month + "/" + $year;
			var day = ordinal_suffix_of($day);
			var month = month_name($month);
			$activeButton.attr({"data-date": $date, "data-day": $day, "data-month": $month, "data-year": $year}).text(day + " " + month + " " + $year);
			$(".personvehiclepermit .c-PermitDuration-StartEndDateSelection").fadeOut(250, function () {
				$(this).removeClass("active");
				$(".personvehiclepermit .c-PermitDuration-StartEndSelection").fadeIn(250, function () {
					$activeButton.removeClass("active");
					var $StartDate = $startDateButton.attr("data-date"),
							$EndDate = $endDateButton.attr("data-date");
					if ("" !== $StartDate || "" !== $EndDate) {
						$(".personvehiclepermit .c-PermitDuration-StartEndSelection-BtnClear").show();
					}
				});
			});
		}

		return false;
	}).on("click", ".personvehiclepermit .c-PermitDuration-StartEndDateSelection .c-PermitDuration-StartEndDateSelection-BtnClear", function () {
		var $this = $(this),
				existing_div = $this.parents(".option-container").find(".existing-div"),
				$activeButton = $(".personvehiclepermit .c-PermitDuration-StartEndSelection .c-add-input.active");
		$(".personvehiclepermit .c-PermitDuration-StartEndDateSelection .c-add-input").val("");
		$activeButton.attr({"data-date": '', "data-day": '', "data-month": '', "data-year": ''}).text($activeButton.attr("data-text"));
		$(".personvehiclepermit .c-PermitDuration-StartEndDateSelection").fadeOut(250, function () {
			$(this).removeClass("active");
			$(".personvehiclepermit .c-PermitDuration-StartEndSelection").fadeIn(250, function () {
				$activeButton.removeClass("active");
			});
		});
		if (existing_div.find(".u-DateTime").length) {
			$(".personvehiclepermit .u-DateTime").removeClass("active");
		}
		return false;
	}).on("click", ".c-PermitDuration-StartEndSelection .c-PermitDuration-StartEndSelection-BtnClear", function () {
		var $this = $(this),
				existing_div = $this.parents(".option-container").find(".existing-div"),
				$startDateButton = $("#permit-StartDate"),
				$endDateButton = $("#permit-EndDate");
		$(".personvehiclepermit .c-PermitDuration-StartEndDateSelection .c-add-input").val("");
		$startDateButton.attr({"data-date": '', "data-day": '', "data-month": '', "data-year": ''}).text($startDateButton.attr("data-text"));
		$endDateButton.attr({"data-date": '', "data-day": '', "data-month": '', "data-year": ''}).text($endDateButton.attr("data-text"));
		$this.hide();
		return false;
	}).on("click", ".c-PermitDuration-StartEndSelection .c-PermitDuration-StartEndSelection-BtnAddNew", function () {
		var $this = $(this),
				$startDateButton = $("#permit-StartDate"),
				$endDateButton = $("#permit-EndDate");
		$(".personvehiclepermit .c-PermitDuration-StartEndDateSelection .c-add-input").val("");
		$startDateButton.attr({"data-date": '', "data-day": '', "data-month": '', "data-year": ''}).text($startDateButton.attr("data-text"));
		$endDateButton.attr({"data-date": '', "data-day": '', "data-month": '', "data-year": ''}).text($endDateButton.attr("data-text"));
		$this.removeClass("active");
		$(".personvehiclepermit .c-PermitDuration-StartEndSelection .c-PermitDuration-StartEndSelection-BtnSave").removeClass("active");
		return false;
	}).on("click", ".c-PermitDuration-StartEndSelection .c-PermitDuration-StartEndSelection-BtnSave", function () {
		var $this = $(this),
				optioncontainer = $this.parents(".option-container"),
				$DateButtons = $(".personvehiclepermit .c-PermitDuration-StartEndSelection .c-add-input"),
				existing_div = $this.parents(".option-container"),
				existing_name = existing_div.find(".existing-names"),
				$startDateButton = $("#permit-StartDate"),
				$endDateButton = $("#permit-EndDate"),
				$StartDate = $startDateButton.attr("data-date"),
				$StartDay = $startDateButton.attr("data-day"),
				$StartMonth = $startDateButton.attr("data-month"),
				$StartYear = $startDateButton.attr("data-year"),
				$EndDate = $endDateButton.attr("data-date"),
				$EndDay = $endDateButton.attr("data-day"),
				$EndMonth = $endDateButton.attr("data-month"),
				$EndYear = $endDateButton.attr("data-year");
		var DateError = function ($error) {
			optioncontainer.find(".personvehiclepermit-Box-overlay").addClass("active").find(".c-Contentbox span").html($error);
			setTimeout(function () {
				optioncontainer.find(".personvehiclepermit-Box-overlay").removeClass("active");
			}, 4000);
		};
		if ("" !== $StartDate && "" !== $EndDate) {
			if ($EndYear < $StartYear) {
				DateError("Oops! The start date has to be before the end date, pls try again!..");
			} else if (($EndMonth < $StartMonth) && ($StartYear == $EndYear)) {
				DateError("Oops! End date needs to be after the start date..");
			} else if (($EndDay < $StartDay) && ($StartMonth == $EndMonth) && ($StartYear == $EndYear)) {
				DateError("Oops! The start date has to be before the end date, pls try again!..");
			} else {


				var element = existing_name.find("a").filter(function () {
					var str_Date = $(this).attr("data-startdate");
					var end_date = $(this).attr("data-enddate");
					if (str_Date == $StartDate && end_date == $EndDate) {
						return $(this);
					}
				});
				if (element.length) {
					element.removeClass("hvr-buzz-out-active").addClass("hvr-buzz-out-hover");
					DateError("Oops! Date already exists, pls select from list, it's shaking for your attention!");
				} else {

					var $StartMonth_str = month_name($StartMonth);
					var $EndMonth_str = month_name($EndMonth);
					$(".personvehiclepermit .u-DateTime").parents(".existing-names").find(".active").removeClass("active");
					existing_name.append('<a href="#" class="u-DateTime active" data-startDate="' + $StartDate + '" data-startDay="' + $StartDay + '" data-startMonth="' + $StartMonth + '" data-startYear="' + $StartYear + '" data-endDate="' + $EndDate + '" data-endDay="' + $EndDay + '" data-endMonth="' + $EndMonth + '" data-endYear="' + $EndYear + '">' + $StartMonth_str + ' ' + $StartYear + ' to ' + $EndMonth_str + ' ' + $EndYear + '</a>');
					$(".personvehiclepermit .c-PermitDuration-StartEndSelection .c-PermitDuration-StartEndSelection-BtnClear").hide();
					$(".personvehiclepermit .c-PermitDuration-StartEndSelection .c-PermitDuration-StartEndSelection-BtnAddNew").addClass("active");
					$(".personvehiclepermit .c-PermitDuration-StartEndSelection .c-PermitDuration-StartEndSelection-BtnSave").addClass("active");
					existing_personvehiclepermit_names_actions();
				}
			}
		} else {
			DateError("Oops! We Can't add nothing, please try valid dates..");
		}
		checkDateTimeFooterButton();
		return false;
	}).on('click', '.c-PermitDuration-Container .c-Circle-Option', function (e) {

		/* if locked than return false without any action */
		if ($(".personvehiclepermit-overlay").find(".c-Permit-Options").hasClass("c-Options-Locked")) {
			return false;
		}

		var $this = $(this),
				$date = $this.attr("data-date_numbers"),
				$filter = $this.attr("data-date_filter"),
				$startDate = $this.attr("data-start"),
				$endDate = $this.attr("data-end");
		$(".personvehiclepermit-overlay .c-PermitDuration-Container .c-Footer a").removeClass("active");
		//$(".personvehiclepermit-overlay .c-PermitDuration-Container .c-Circle-Option .c-OptionTimer").removeClass("active c-hasActive");
		if ($this.hasClass("active")) {
			$this.removeClass("active hasActive");
			$this.find(".c-OptionTimer").removeClass("active c-hasActive");
			checkPermitWidgetDateFooterButton();
		} else {
			//$(".personvehiclepermit-overlay .c-PermitDuration-Container .c-Circle-Option").removeClass("active hasActive");
			//$(".personvehiclepermit-overlay .c-Circle-Option").not($this).attr({"data-startdate": "", "data-enddate": ""});
			var is_add = false;
			if ($this.find(".c-Circle").hasClass("c-Circle-NewAdd")) {
				is_add = true;
			} else {
				if ($(".personvehiclepermit-overlay .c-PermitDuration-Container .c-Circle-Option.c-hasActive").length) {
					console.log("setTimeValues 4");
					setTimeValues($(".personvehiclepermit .c-PermitDuration-Container .c-Circle-Option.c-hasActive"));
				}
				if ("" != $startDate && "" != $endDate) {
					addPermitWidgetDateCircleToGroup($startDate, $endDate);
				}
			}

			$(".personvehiclepermit-overlay .c-PermitDuration-Container .c-Circle-Option").removeClass("c-hasActive");
			$this.addClass("c-hasActive");
			//setTimeout(function () {
			OpenPermitWidgetDateInputBox($this.parents('.options').find(".c-TimerSelectBox"), $this, e);
			$('.personvehiclepermit-overlay .c-PermitDuration-Container .c-TimerSelect-Save').unbind("click").bind("click", function () {
				var valid = validatePermitWidgetDurationDates($this);
				$(".personvehiclepermit-overlay .c-PermitDuration-Container .c-Circle-Option").removeClass("active c-hasActive");
				$(".personvehiclepermit-overlay .c-PermitDuration-Container .c-Circle-Option .c-OptionTimer").removeClass("active c-hasActive");
				if (valid) {
					if (is_add) {
						$this.removeClass("active");
						addPermitWidgetDateCricle($this);
					} else {
						$this.find(".c-OptionTimer").addClass("active");
						$this.addClass("active");
						setPermitWidgetDateValues($this);
					}
					ClosePermitWidgetDateInputBox($(".personvehiclepermit .c-PermitDuration-Container .c-TimerSelectBox"));
				} else {
				}
				return false;
			});
			//}, 1000);


		}
		return false;
	}).on('click', '.c-PermitDuration-Container .c-OptionTimer', function (e) {
		var $this = $(this);
		if ($this.hasClass("active")) {
			$this.addClass("c-hasActive");
			$this.parents('.c-Circle-Option').addClass("c-hasActive");
			setTimeout(function () {
				OpenPermitWidgetDateInputBox($this.parents('.options').find(".c-TimerSelectBox"), $this.parents('.c-Circle-Option'), e);
				//$this.parents('.options').find(".c-TimerSelectBox").addClass("active");
				$('.personvehiclepermit-overlay .c-PermitDuration-Container .c-TimerSelect-Save').unbind("click").bind("click", function () {
					var valid = validatePermitWidgetDurationDates($this);
					console.log('.c-PermitDuration-Container .c-OptionTimer', valid);
					$(".personvehiclepermit-overlay .c-PermitDuration-Container .c-Circle-Option").removeClass("active");
					if (valid) {
						$(".personvehiclepermit-overlay .c-PermitDuration-Container .c-Circle-Option").removeClass("active");
						$this.parents('.c-Circle-Option').addClass("active");
						ClosePermitWidgetDateInputBox($this.parents('.options').find(".c-TimerSelectBox"));
						setPermitWidgetDateValues($this.parents('.c-Circle-Option'));
						$this.removeClass("c-hasActive");
						$this.parents('.c-Circle-Option').removeClass("c-hasActive");
						return false;
					}
				});
			}, 1000);
		} else {
//$this.addClass("active");
		}
		return false;
	}).on('click', '.c-PermitDuration-Container .c-TimerSelect-Close', function () {
		var $this = $(this);
		ClosePermitWidgetDateInputBox($(".personvehiclepermit .c-PermitDuration-Container .c-TimerSelectBox"));
		return false;
	}).on("keyup", ".c-DayTimes .c-TimerSelectBox .c-TimerSelection-Input", function () {
		var $this = $(this),
				$startTimeInput = $(".personvehiclepermit .c-DayTimes .c-TimerSelect-StartTime"),
				$startTimeInput = $(".personvehiclepermit .c-DayTimes .c-TimerSelect-EndTime"),
				$StartTime = $startTimeInput.val(),
				$EndTime = $startTimeInput.val();
		if ("00:00" !== $StartTime || "00:00" !== $EndTime) {
			$(".personvehiclepermit .c-DayTimes .c-TimerSelect-AllDay").removeClass("active");
		} else if ("" == $StartTime && "" == $EndTime) {

		}

		return false;
	}).on("click", ".option-container-firstbox .option-container-firstbox-BtnGo", function () {
		var $this = $(this),
				$parents = $this.parents(".option-container");
		if ($this.hasClass("c-AddGroupWidget")) {
			$('.personvehiclepermit-overlay').fadeOut();
			$(".groupswidget-overlay").attr({"from": "personvehiclepermit"}).fadeIn();
			$('.c-Group-Widget-Button:first').trigger("click");
		} else if (!$parents.find(".option-container-firstbox").hasClass("isHidden")) {
			$parents.find(".option-container-firstbox").fadeOut(200, function () {
				$parents.find(".option-container-secondbox").fadeIn(200).find(".c-add-input").focus();
			});
		}
		return false;
	});
	$(".personvehiclepermit-overlay .c-Common-Footer").on("click", ".c-Footer-Button", function () {
		var $optionActive = $(".personvehiclepermit-overlay .c-Setone-Option-Selection .option.active"),
				$option = $optionActive.attr("data-option");
		console.log($optionActive, " $option ", $option);
		if ($option != "permit") {
			openLastSlide();
		} else {
//$('.personvehiclepermit-overlay .navigation-overlay a.u-navigationCircle.active').next().trigger("click");
			$('.personvehiclepermit-overlay .navigation-overlay a.u-navigationCircle.active').nextAll('a:not(.u-TempHide)').filter(':first').trigger("click");
		}
		return false;
	});
	var openLastSlide = function () {
		$('.personvehiclepermit-overlay .navigation-overlay').removeClass("active");
		var setOne = $(".personvehiclepermit-overlay .options.active");
		var setTwo = $(".personvehiclepermit-overlay .options.c-option-done");
		setOne.removeClass("active").fadeOut(800, function () {
			var twoHeight = setTwo.height();
			var windowHeight = $(window).height();
			var offsetTwo = (windowHeight / 2) - (twoHeight / 2) - getTopOffset; // + windowOffset;
			setTwo.css('top', offsetTwo).fadeIn(800, function () {
				setTwo.addClass("active");
				checkArrowStatus();
				lastSlideClose(setTwo);
				$(".personvehiclepermit-overlay .u-gotOSummary").removeClass("active");
			});
		});
	};
	$(".personvehiclepermit-overlay .c-Footer.c-DayTime-Footer").on("click", ".c-Footer-Button", function () {
		$('.personvehiclepermit-overlay .navigation-overlay a.u-navigationCircle.active').nextAll('a:not(.u-TempHide)').filter(':first').trigger("click");
		checkNavigationStatus();
		return false;
	});
	$(".personvehiclepermit-overlay .c-Footer.c-DateTime-Footer").on("click", ".c-Footer-Button", function () {
		$('.personvehiclepermit-overlay .navigation-overlay a.u-navigationCircle.active').nextAll('a:not(.u-TempHide)').filter(':first').trigger("click");
		return false;
	});
	$(".personvehiclepermit-overlay .c-Footer.c-PermitDate-Footer").on("click", ".c-Footer-Button", function () {
		$('.personvehiclepermit-overlay .navigation-overlay a.u-navigationCircle.active').nextAll('a:not(.u-TempHide)').filter(':first').trigger("click");
		return false;
	});
	$(".personvehiclepermit-overlay .c-CirclesOption")
		.on('click', '.c-Circles', function () {
			var $this = $(this),
					$set = $this.attr("data-step");
			if ($(".personvehiclepermit-overlay .options.set-" + $set).length) {
				/*$("html, body").animate({ scrollTop: $(".personvehiclepermit .set-".$set).offset().top }, 600);*/
				var setOne = $this.parents(".options"),
						setTwo = $(".personvehiclepermit-overlay .options.set-" + $set);
				closePinLock(setTwo);
				setOne.removeClass("active").fadeOut(800, function () {
					var twoHeight = setTwo.height();
					var windowHeight = $(window).height();
					var offsetTwo = (windowHeight / 2) - (twoHeight / 2) - getTopOffset; // + windowOffset;
					openPinLock(setTwo, offsetTwo);
					setTwo.css('top', offsetTwo).fadeIn(800, function () {
						setTwo.addClass("active");
						/*checkArrowStatus();*/
						checkNavigationStatus();
						lastSlideClose(setTwo);
						fullScreenOverlay.find(".navigation-overlay").find(".u-navigationCircle-Summary").addClass("u-SummaryActive");
						/*$(".u-gotOSummary").addClass("active");*/
					});
				});
			}
			return false;
		})
		.on('click', '.c-CircleOption-Padlock', function () {
			var $this = $(this),
					$parents = $this.parents(".option-icon");
			$parents.find(".c-Circles").trigger("click");
			return false;
		});
	$(".personvehiclepermit-overlay .u-gotOSummary").on('click', function () {
		var $this = $(this),
				setOne = $(".personvehiclepermit-overlay .options.active"),
				setTwo = $(".personvehiclepermit-overlay .options.set-summary");
		setOne.removeClass("active").fadeOut(800, function () {
			var twoHeight = setTwo.height();
			var windowHeight = $(window).height();
			var offsetTwo = (windowHeight / 2) - (twoHeight / 2) - getTopOffset; // + windowOffset;
			setTwo.css('top', offsetTwo).fadeIn(800, function () {
				setTwo.addClass("active");
				checkArrowStatus();
				lastSlideClose(setTwo);
				$(".personvehiclepermit-overlay .u-gotOSummary").removeClass("active");
			});
		});
		return false;
	});
	function addPermitWidgetDateCricle($addCircleOption) {
		var $TimerSelectBox = $(".personvehiclepermit-overlay .c-PermitDuration-Container .c-TimerSelectBox"),
				$startInput = $TimerSelectBox.find(".c-TimerSelect-StartTime"),
				$endInput = $TimerSelectBox.find(".c-TimerSelect-EndTime");
		var $start = "";
		if ("" !== $startInput.val()) {
			$start = $startInput.attr("data-date");
		}
		var $end = "";
		if ("" !== $endInput.val()) {
			$end = $endInput.attr("data-date");
		}

		var $lastOption = $(".personvehiclepermit .c-PermitDuration-Container .c-Circle-Option").last(),
				$lastSet = $lastOption.attr("data-set"),
				$lastFloat = $lastOption.attr("data-float");
		var $newSet = $lastSet;
		if ($(".personvehiclepermit .c-PermitDuration-Container .c-Circle-Option[data-set='" + $lastSet + "']").length == 3) {
			$newSet = parseInt($lastSet) + 1;
		}

		var $newFloat = "left";
		if ($lastFloat == "left") {
			$newFloat = "center";
		} else if ($lastFloat == "center") {
			$newFloat = "right";
		} else if ($lastFloat == "right") {
			$newFloat = "left";
		}

		var $CircleOption = '<div data-float="' + $newFloat + '" data-set="' + $newSet + '" data-enddate="' + moment($end).format('YYYY/MM/DD') + '" data-startdate="' + moment($start).format('YYYY/MM/DD') + '" data-end="' + $endInput.val() + '" data-start="' + $startInput.val() + '" class="c-Circle-Option active"><div class="option-icon"><span class="c-Circle"><span>' + $startInput.val() + '</span><span>' + $endInput.val() + '</span></span></div><span class="c-OptionTimer active icon-calendar"></span></div>';
		$(".personvehiclepermit .c-PermitDuration-Container .c-Circle-Option-Box").append($CircleOption);
		addPermitWidgetDateCircleToGroup($startInput.val(), $endInput.val());
	}
	function setPermitWidgetDateValues($CircleOption) {
		var $TimerSelectBox = $(".personvehiclepermit-overlay .c-PermitDuration-Container .c-TimerSelectBox"),
				$startInput = $TimerSelectBox.find(".c-TimerSelect-StartTime"),
				$endInput = $TimerSelectBox.find(".c-TimerSelect-EndTime");
		var $start = "";
		if ("" !== $startInput.val()) {
			$start = $startInput.attr("data-date");
		}
		var $end = "";
		if ("" !== $endInput.val()) {
			$end = $endInput.attr("data-date");
		}

		$CircleOption.find(".c-OptionTimer").addClass("active");
		$CircleOption.attr({"data-startdate": moment($start).format('YYYY/MM/DD'), "data-enddate": moment($end).format('YYYY/MM/DD'), "data-start": $startInput.val(), "data-end": $endInput.val()});
		addPermitWidgetDateCircleToGroup($startInput.val(), $endInput.val());
	}

	function addPermitWidgetDateCircleToGroup($startDate, $endDate) {
		$(".personvehiclepermit-overlay .c-PermitDuration-CirclesOption").removeClass("c-CircleOption-Empty");
		$(".personvehiclepermit-overlay .c-PermitDuration-CirclesOption.c-CirclesOption .permit-startDate").text($startDate);
		$(".personvehiclepermit-overlay .c-PermitDuration-CirclesOption.c-CirclesOption .permit-endDate").text($endDate);
		checkPermitWidgetDateFooterButton();
	}

	function checkPermitWidgetDateFooterButton() {
//if ($(".personvehiclepermit-overlay .options.active").hasClass("c-Permit-Options")) {
//var $set = $(".personvehiclepermit-overlay .options.active").attr("data-set");
		if ($(".personvehiclepermit-overlay .c-PermitDuration-Container .c-Circle-Option.active").length) {
			$(".personvehiclepermit-overlay .c-PermitDate-Footer .c-Footer-Button").addClass("active").fadeIn(200, function () {

			});
			/*$(".icon-arrow-down1").show();*/
			$(".personvehiclepermit-overlay .u-navigationCircle.u-navigationCircle-PermitDuration").addClass("u-setDone");
			assignValuestoNavigation("four");
		} else {
			$(".personvehiclepermit-overlay .c-PermitDate-Footer .c-Footer-Button").fadeOut(200, function () {
				$(this).removeClass("active");
			});
			/*$(".icon-arrow-down1").hide();*/
			$(".personvehiclepermit-overlay .u-navigationCircle.u-navigationCircle-PermitDuration").removeClass("u-setDone");
			$(".personvehiclepermit-overlay .u-navigationCircle.u-navigationCircle-PermitDuration .u-navigationCircle-Animated").remove();
			$(".personvehiclepermit-overlay .u-navigationCircle.u-navigationCircle-PermitDuration .u-navigationCircle-Span1").addClass("animate").show();
		}
//}
	}

	function validatePermitWidgetDurationDates($CircleOption) {
		var $TimerSelectBox = $(".personvehiclepermit-overlay .c-PermitDuration-Container .c-TimerSelectBox"),
				$startInput = $TimerSelectBox.find(".c-TimerSelect-StartTime"),
				$start = $startInput.attr("data-date"),
				$endInput = $TimerSelectBox.find(".c-TimerSelect-EndTime"),
				$end = $endInput.attr("data-date");
		var $result = true;
		var PermitDateError = function ($error) {
			$TimerSelectBox.find(".widgetpopup-Box-overlay").addClass("active").find(".c-Contentbox span").html($error);
			setTimeout(function () {
				$TimerSelectBox.find(".widgetpopup-Box-overlay").removeClass("active");
			}, 4000);
		};
		if ("" == $startInput.val() || "" == $start) {
			$result = false;
			PermitDateError("Please enter start date!");
		} else if ("" == $endInput.val() || "" == $end) {
			$result = false;
			PermitDateError("Please enter end date!");
		} else if (($end != $start) && !moment($end).isAfter($start)) {
			$result = false;
			PermitDateError("Oops! End date needs to be after the start date..");
		}

		return $result;
	}
	function addPermitWidgetDateCircleToGroup($startDate, $endDate) {
		$(".personvehiclepermit-overlay .c-PermitDuration-CirclesOption.c-CirclesOption .permit-startDate").text($startDate);
		$(".personvehiclepermit-overlay .c-PermitDuration-CirclesOption.c-CirclesOption .permit-endDate").text($endDate);
		checkPermitWidgetDateFooterButton();
	}
	function removePermitWidgetDateCircleToGroup() {
		$(".personvehiclepermit-overlay .c-PermitDuration-CirclesOption.c-CirclesOption").addClass("c-CircleOption-Empty");
		checkPermitWidgetDateFooterButton();
	}
	function OpenPermitWidgetDateInputBox($TimerSelectBox, $CircleOption, event) {
		if ($(".personvehiclepermit-overlay .c-PermitDuration-Container .c-TimerSelectBox").hasClass("active")) {
			ClosePermitWidgetDateInputBox($(".personvehiclepermit-overlay .c-PermitDuration-Container .c-TimerSelectBox"), $TimerSelectBox, $CircleOption, event);
		} else {
			var parentOffset = $CircleOption.parent().offset();
			var $float = $CircleOption.attr("data-float"),
					$set = $CircleOption.attr("data-set"),
					$top = ($CircleOption.position().top),
					$startInput = $TimerSelectBox.find(".c-TimerSelect-StartTime"),
					$endInput = $TimerSelectBox.find(".c-TimerSelect-EndTime"),
					$date_filter = $CircleOption.attr("data-date_filter"),
					$date_number = $CircleOption.attr("data-date_number"),
					$startTime = $CircleOption.attr("data-startdate"),
					$endTime = $CircleOption.attr("data-enddate");
			//$(".c-Circle-Option.c-hasActive").removeClass("active");
			$TimerSelectBox.find(".c-TimerSelection-Inputs").addClass("c-float-" + $float);
			$TimerSelectBox.css("top", $top + "px").fadeIn(5, function () {
				var $this = $(this);
				$this.addClass("active");
				$TimerSelectBox.find(".c-TimerSelection-Input").attr("disabled", false);
				// HIde date selection from redefined dates lime 1 day, 1 week etc
				if ((undefined !== $date_filter)) {
					$endInput.attr("disabled", true);
				} else {
					$endInput.attr("disabled", false);
				}
				if ($(".personvehiclepermit-overlay").find(".c-Permit-Options").hasClass("c-Options-Locked")) {
					$TimerSelectBox.find(".c-TimerSelection-Input").attr("disabled", true);
				}

// set time if already selected
				if ("" != $startTime) {
					$startInput.val(moment($startTime).format("Do MMM YYYY"));
				}
				if ("" != $endTime) {
					$endInput.val(moment($endTime).format("Do MMM YYYY"));
				}

				if ($CircleOption.find(".c-Circle-NewAdd").length) {
					$TimerSelectBox.find(".c-TimerSelect-Save span").text("ADD");
				} else {
					$TimerSelectBox.find(".c-TimerSelect-Save span").text("SAVE");
				}

				setTimeout(function () {
					$this.find(".c-TimerSelection-Inputs-Box").fadeIn(100);
				}, 500);
			});
		}
	}
	function ClosePermitWidgetDateInputBox($TimerSelectBox, $Open, $CircleOption, event) {
		var $TimerSelectBox = $(".personvehiclepermit-overlay .c-PermitDuration-Container .c-TimerSelectBox"),
				$startInput = $TimerSelectBox.find(".c-TimerSelect-StartTime"),
				$endInput = $TimerSelectBox.find(".c-TimerSelect-EndTime");
		$(".personvehiclepermit-overlay .c-Circle-Option.active .c-Circle-NewAdd").parents(".c-Circle-Option").removeClass("active");
		$TimerSelectBox.find(".c-TimerSelection-Inputs-Box").fadeOut(500, function () {
			$TimerSelectBox.removeClass("active");
			$TimerSelectBox.find(".c-TimerSelection-Input").val('');
			$TimerSelectBox.find(".c-TimerSelect-AllDay").removeClass("active");
			setTimeout(function () {
				$TimerSelectBox.find(".c-TimerSelection-Inputs").removeClass("c-float-left c-float-center c-float-right");
				$TimerSelectBox.hide();
				$TimerSelectBox.find(".c-TimerSelection-Inputs-Box").hide();
				$(".personvehiclepermit-overlay .c-PermitDuration-Container .c-Circle-Option").not($CircleOption).removeClass("c-hasActive");
				if ($Open) {
					OpenPermitWidgetDateInputBox($Open, $CircleOption, event);
				}
			}, 500);
		});
	}

	function ordinal_suffix_of(i) {
		var j = i % 10,
				k = i % 100;
		if (j == 1 && k != 11) {
			return i + "st";
		}
		if (j == 2 && k != 12) {
			return i + "nd";
		}
		if (j == 3 && k != 13) {
			return i + "rd";
		}
		return i + "th";
	}
	function month_name(m) {
		var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "June", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
		return monthNames[(m - 1)];
	}

	/*$(".options").not(".option-container").on("click", function () {
	 $('.close-overlay').trigger("click");
	 return false;
	 });*/

	function setTimeValues($CircleOption) {
		var $parents = $CircleOption.parents(".c-DayTimes");
		var $TimerSelectBox = $parents.find(".c-TimerSelectBox"),
				$startInput = $TimerSelectBox.find(".c-TimerSelect-StartTime"),
				$endInput = $TimerSelectBox.find(".c-TimerSelect-EndTime");
		//var $start = "00:00";
		var $start = "";
		if ("" !== $startInput.val()) {
			$start = $startInput.val();
		}
		//var $end = "00:00";
		var $end = "";
		if ("" !== $endInput.val()) {
			$end = $endInput.val();
		}
		if ('' != $start && '' != $end) {
			$CircleOption.find(".c-OptionTimer").addClass("active");
			$CircleOption.attr({"data-start": $start, "data-end": $end});
			$startInput.val('');
			$endInput.val('');
			checkFooterButton();
			return true;
		} else {
			PermitTimeDurationError('Please pick a start and end time during the day or click ALL DAY');
		}
		return false;
	}
	function checkFooterButton() {
//if ($(".personvehiclepermit-overlay .options.active").hasClass("c-DayTime-Options")) {
//var $set = $(".personvehiclepermit-overlay .options.active").attr("data-set");
		if ($(".personvehiclepermit-overlay .c-DayTime-Options .c-Circle-Option.active").length) {
			$(".personvehiclepermit-overlay .c-DayTime-Footer .c-Footer-Button").addClass("active").fadeIn(200, function () {

			});
			/*$(".icon-arrow-down1").show();*/
			$(".personvehiclepermit-overlay .u-navigationCircle.u-navigationCircle-DayTime").addClass("u-setDone");
			assignValuestoNavigation("five");
		} else {
			$(".personvehiclepermit-overlay .c-DayTime-Footer .c-Footer-Button").fadeOut(200, function () {
				$(this).removeClass("active");
			});
			/*$(".icon-arrow-down1").hide();*/
			$(".personvehiclepermit-overlay .u-navigationCircle.u-navigationCircle-DayTime").removeClass("u-setDone");
			$(".personvehiclepermit-overlay .u-navigationCircle.u-navigationCircle-DayTime .u-navigationCircle-Animated").remove();
			$(".personvehiclepermit-overlay .u-navigationCircle.u-navigationCircle-DayTime .u-navigationCircle-Span1").addClass("animate").show();
		}
//}
	}
	function checkDateTimeFooterButton() {
		if ($(".personvehiclepermit-overlay .options.active").hasClass("c-Permit-Options")) {
			if ($(".personvehiclepermit-overlay .u-DateTime.active").length) {
				$(".personvehiclepermit-overlay .c-DateTime-Footer .c-Footer-Button").addClass("active").fadeIn(200, function () {
				});
				/*$(".icon-arrow-down1").show();*/
				$(".personvehiclepermit-overlay .u-navigationCircle.active").addClass("u-setDone");
			} else {
				$(".personvehiclepermit-overlay .c-DateTime-Footer .c-Footer-Button").fadeOut(200, function () {
					$(this).removeClass("active");
				});
				/*$(".icon-arrow-down1").hide();*/
				$(".personvehiclepermit-overlay .u-navigationCircle.active").removeClass("u-setDone");
				$(".personvehiclepermit-overlay .u-navigationCircle.active .u-navigationCircle-Span1").addClass("animate").show();
			}
		}
	}
	function OpenInputBox($TimerSelectBox, $CircleOption) {
		if ($(".personvehiclepermit .c-DayTimes .c-TimerSelectBox").hasClass("active")) {
			CloseInputBox($(".personvehiclepermit .c-DayTimes .c-TimerSelectBox"), $TimerSelectBox, $CircleOption);
		} else {
			var $float = $CircleOption.attr("data-float"),
					$set = $CircleOption.attr("data-set"),
					//$top = (33 * ($set - 1)) + 2,
					$top = parseInt($CircleOption.position().top) + 8,
					$startInput = $TimerSelectBox.find(".c-TimerSelect-StartTime"),
					$endInput = $TimerSelectBox.find(".c-TimerSelect-EndTime"),
					$startTime = $CircleOption.attr("data-start"),
					$endTime = $CircleOption.attr("data-end");
			//$(".c-Circle-Option.c-hasActive").removeClass("active");
			$TimerSelectBox.find(".c-TimerSelection-Inputs").addClass("c-float-" + $float);
			$TimerSelectBox.css("top", $top + "px").fadeIn(5, function () {
				var $this = $(this);
				$this.addClass("active");
				// set time if already selected
				if ("" != $startTime) {
					$startInput.val($startTime);
				}
				if ("" != $endTime) {
					$endInput.val($endTime);
				}
				$TimerSelectBox.find(".c-TimerSelect-AllDay").removeClass("active");
				if ("00:00" == $startTime && "00:00" == $endTime) {
					$TimerSelectBox.find(".c-TimerSelect-AllDay").addClass("active");
				}

				/* Disable if pinlocked */
				if ($(".personvehiclepermit-overlay").find(".c-DayTime-Options").hasClass("c-Options-Locked")) {
					$TimerSelectBox.find(".c-TimerSelection-Input").attr("disabled", true);
				} else {
					$TimerSelectBox.find(".c-TimerSelection-Input").attr("disabled", false);
				}

				setTimeout(function () {
					$this.find(".c-TimerSelection-Inputs-Box").fadeIn(100);
				}, 500);
			});
		}
	}
	function CloseInputBox($TimerSelectBox, $Open, $CircleOption) {
		var $TimerSelectBox = $(".personvehiclepermit .c-DayTimes .c-TimerSelectBox"),
				$startInput = $TimerSelectBox.find(".c-TimerSelect-StartTime"),
				$endInput = $TimerSelectBox.find(".c-TimerSelect-EndTime");
		$TimerSelectBox.find(".c-TimerSelection-Inputs-Box").fadeOut(500, function () {
			$TimerSelectBox.removeClass("active");
			$TimerSelectBox.find(".c-TimerSelection-Input").val('');
			$TimerSelectBox.find(".c-TimerSelect-AllDay").removeClass("active");
			setTimeout(function () {
				$TimerSelectBox.find(".c-TimerSelection-Inputs").removeClass("c-float-left c-float-center c-float-right");
				$TimerSelectBox.hide();
				$TimerSelectBox.find(".c-TimerSelection-Inputs-Box").hide();
				$(".personvehiclepermit .c-DayTimes .c-Circle-Option").not($CircleOption).removeClass("c-hasActive");
				if ($Open) {
					OpenInputBox($Open, $CircleOption);
				}
			}, 500);
		});
	}

	var lastSlideClose = function (setNext) {
		if (setNext.hasClass("c-option-done")) {
			$('.personvehiclepermit-overlay .navigation-overlay').removeClass("active");
			setTimeout(function () {
				setNext.fadeOut(800, function () {
					removeOverlay();
				});
			}, 1000);
		} else {
			checkArrowStatus();
		}
	};
	var nextProcess = function ($options, $set, $opt) {
		if ($opt != "permit") {
			$options.find(".c-Common-Footer").find("a").text("DONE").addClass("active");
		} else if ($options.hasClass("c-GroupBox-Options") && $opt == "permit") {
			$options.find(".c-Common-Footer").find("a").text("NEXT");/*.addClass("active");*/
		} else {
			$options.find(".c-Common-Footer").find("a").text("DONE").trigger("click");
		}
	};
	existing_personvehiclepermit_names_actions = function () {
		$('.personvehiclepermit .existing-names a').not(".u-Permit-GroupEdit").not(".c-list-delete").unbind("click").bind('click', function () {

			var $this = $(this),
					$val = $this.text(),
					$options = $this.parents(".options"),
					$parents = $this.parents(".option-container"),
					$set = $this.parents('.options').attr("data-set"),
					$opt = $(".personvehiclepermit-overlay .c-Setone-Option-Selection .option.active").attr("data-option");
			// if group button then take val from button data-group
			if ($options.hasClass("c-GroupBox-Options")) {
				$val = $this.attr("data-group_name");
			}

			$(".personvehiclepermit-overlay .c-Footer a").removeClass("active");
			$(".groupswidget-overlay .u-navigationCircle.active").removeClass("u-setDone");
			if (!$parents.find(".option-container-firstbox").hasClass("isHidden")) {
				$parents.find(".option-container-firstbox").fadeOut(200, function () {
					$parents.find(".option-container-secondbox").fadeIn(200);
				});
			}

			if ($this.parents('.options').hasClass("c-DayTime-Options")) {
				if ($this.hasClass("u-DateTime")) {
					var $startDateButton = $parents.find(".c-PermitDuration-StartEndSelection #permit-StartDate"),
							$endDateButton = $parents.find(".c-PermitDuration-StartEndSelection #permit-EndDate"),
							$StartDate = $this.attr("data-startdate"),
							$StartDay = $this.attr("data-startday"),
							$StartMonth = $this.attr("data-startmonth"),
							$StartYear = $this.attr("data-startyear"),
							$EndDate = $this.attr("data-enddate"),
							$EndDay = $this.attr("data-endday"),
							$EndMonth = $this.attr("data-endmonth"),
							$EndYear = $this.attr("data-endyear"),
							$StartMonth_str = month_name($StartMonth),
							$EndMonth_str = month_name($EndMonth);
					$startDateButton.attr({"data-date": $StartDate, "data-day": $StartDay, "data-month": $StartMonth, "data-year": $StartYear}).text(ordinal_suffix_of($StartDay) + " " + month_name($StartMonth) + " " + $StartYear);
					$endDateButton.attr({"data-date": $EndDate, "data-day": $EndDay, "data-month": $EndMonth, "data-year": $EndYear}).text(ordinal_suffix_of($EndDay) + " " + month_name($EndMonth) + " " + $EndYear);
					$(".personvehiclepermit .c-PermitDuration-StartEndSelection .c-PermitDuration-StartEndSelection-BtnAddNew").addClass("active");
					$(".personvehiclepermit .c-PermitDuration-StartEndSelection .c-PermitDuration-StartEndSelection-BtnSave").addClass("active");
					$(".personvehiclepermit .c-CirclesOption .c-Circles[data-step='" + $set + "'] .c-CirclesText .permit-startDate").html($StartMonth_str + " " + $StartDay + " " + $StartYear);
					$(".personvehiclepermit .c-CirclesOption .c-Circles[data-step='" + $set + "'] .c-CirclesText .permit-endDate").html($EndMonth_str + " " + $EndDay + " " + $EndYear);
					checkDateTimeFooterButton();
				} else {
					$(".personvehiclepermit .c-CirclesOption .c-Circles[data-step='" + $set + "'] .c-CirclesText .permit-startDate").html($val);
					$(".personvehiclepermit .c-CirclesOption .c-Circles[data-step='" + $set + "'] .c-CirclesText .permit-endDate").html('');
				}
				checkNavigationStatus();
			} else if ($this.parents('.set-box').hasClass("c-option-location")) {

				$parents.find(".personvehiclepermit-Box-overlay").removeClass("active");
				$this.parents('.options').find(".option").removeClass("active");

				if ($this.hasClass("active")) {
					$this.removeClass("active");
					//$this.parents('.c-option-location').find(".c-BtnClear").trigger("click");
				} else {
					$this.addClass("active");
					$(".personvehiclepermit-overlay .c-Footer a").addClass("active");
				}

				$parents.find(".c-add-input").val("").trigger("keyup").val($val);
				$parents.find(".c-BtnClear").show();
				$(".personvehiclepermit-overlay .u-navigationCircle.active").addClass("u-setDone");
				checkNavigationStatus();
				if (!$this.parents(".existing-names").find("a.active").length) {
					$(".personvehiclepermit-overlay .c-Footer a").removeClass("active");
				} else {
					$(".personvehiclepermit-overlay .c-Footer a").addClass("active");
				}

				var $activeNames = $(".personvehiclepermit .c-CirclesOption .c-Circles[data-step='" + $set + "']");
				$activeNames.html("");
				$this.parents(".existing-names").find("a.active").each(function (i, $ths) {
					$activeNames.append("<span class='c-CirclesText'><span>" + $($ths).text() + "</span></span>");
				});
			} else {

				$this.parents(".existing-names").find("a").removeClass("active");
				$this.addClass("active");
				$parents.find(".personvehiclepermit-Box-overlay").removeClass("active");
				$this.parents('.options').find(".option").removeClass("active");
				$parents.find(".c-add-input").val("").trigger("keyup").val($val);
				$parents.find(".c-BtnClear").show();
				$(".personvehiclepermit-overlay .u-navigationCircle.active").addClass("u-setDone");
				checkNavigationStatus();
				$(".personvehiclepermit .c-CirclesOption .c-Circles[data-step='" + $set + "'] .c-CirclesText span").html($val);
				// if permit or person step then open exting popup
				var $stepBox = $this.parents('.set-box');

				if (($stepBox.hasClass("c-option-person") || $stepBox.hasClass("c-option-vehicle")) && $this.parents('.options').find(".c-ExistContact-Box-overlay").length && !$this.hasClass("c-newAdded")) {
					$this.parents('.options').find(".c-ExistContact-Box-overlay").addClass("active");
				} else {
					if ($this.parents('.options').hasClass("c-GroupBox-Options")) {
						if ($this.find(".c-Cycle-GroupDetails-Criteria-Step-Animated").length) {
							openGroupConfirmation($this);
						} else {
							assignGroupDatatoPermit($this);
							fullScreenOverlay.find(".c-GroupBox-Options .c-Footer-Button").addClass("active");
						}
					}
					nextProcess($options, $set, $opt);
				}

			}

			return false;
		});
		$('.personvehiclepermit .existing-names a .c-list-delete').unbind("click").bind('click', function () {
			var $this = $(this),
					$parents = $this.parents("a"),
					$isPinNeed = $("body").attr("data-GroupMasterPinAccepted");
			if (undefined == $isPinNeed || "" == $isPinNeed) {
				$this.parents(".c-option-location").find(".option-container-secondbox .c-BtnSave").trigger("click");
			} else {
				$parents.fadeOut(function () {
					$parents.remove();
				});
			}
			return false;
		});
		var intervalExistingPermit;
		$(".c-ExistContact-Box-overlay").on("click", ".u-ExistingIgnore", function () {
			var $this = $(this),
					$options = $this.parents(".options"),
					$parents = $this.parents(".c-ExistContact-Box-overlay"),
					$set = $this.parents('.options').attr("data-set"),
					$opt = $(".personvehiclepermit-overlay .c-Setone-Option-Selection .option.active").attr("data-option");
			$parents.removeClass("active");
			setTimeout(function () {
				nextProcess($options, $set, $opt);
				$options.find(".c-Common-Footer").find("a").trigger("click");
			}, 500);
			return false;
		}).on("click", ".c-ExistContact-Item", function () {
			var $this = $(this),
					setOne = $this.parents(".options"),
					$parents = $this.parents(".c-ExistContact-Box-overlay"),
					setTwo = fullScreenOverlay.find(".c-ExistingPermit-Options");
			setOne.fadeOut(800, function () {
				$parents.removeClass("active");
				fullScreenOverlay.find('.navigation-overlay').hide();
				var twoHeight = setTwo.height();
				var windowHeight = $(window).height();
				var offsetTwo = (windowHeight / 2) - (twoHeight / 2) - getTopOffset; // + windowOffset;
				setTwo.css('top', offsetTwo).fadeIn(800, function () {
					//setTwo.addClass("active");
					var counter = 10;
					if (intervalExistingPermit) {
						clearInterval(intervalExistingPermit);
					}
					intervalExistingPermit = setInterval(function () {
						counter--;
						var supix = "secs";
						if (counter == 1 || counter == 0) {
							supix = "sec";
						}
						setTwo.find(".c-CountDown-Timer").text(counter + supix);
						if (counter == 0) {
							// Display a login box
							clearInterval(intervalExistingPermit);
							fullScreenOverlay.find(".c-ExistingPermit-Options").find(".c-ExistingPermit-Footer .c-Footer-Button").trigger("click");
							setTimeout(function () {
								setTwo.find(".c-CountDown-Timer").text('10secs');
							}, 1000);
						}
					}, 1000);
				});
			});
			return false;
		});
		$(".c-ExistingPermit-Options").on("click", ".c-ExistingPermit-Footer .c-Footer-Button", function () {
			var $this = $(this),
					setOne = $this.parents(".options"),
					setTwo = fullScreenOverlay.find(".c-Summary-Options");
			fullScreenOverlay.find(".options").removeClass("active");
			fullScreenOverlay.find(".c-ExistContact-Box-overlay").removeClass("active");
			fullScreenOverlay.find(".options").find(".c-option-vehicle").find(".existing-names a").eq(0).trigger("click");
			assignValuestoNavigation("one");
			fullScreenOverlay.find(".options").find(".c-option-person").find(".existing-names a").eq(2).trigger("click");
			assignValuestoNavigation("two");
			fullScreenOverlay.find(".options").find(".c-option-category").find(".existing-names a").eq(1).trigger("click");
			assignValuestoNavigation("three");
			fullScreenOverlay.find(".navigation-overlay").find(".u-navigationCircle").addClass("u-setDone");
			fullScreenOverlay.find(".navigation-overlay").find(".u-navigationCircle-PermitDuration").addClass("u-setDone");
			fullScreenOverlay.find(".navigation-overlay").find(".u-navigationCircle-DayTime").addClass("u-setDone");
			fullScreenOverlay.find(".navigation-overlay").find(".u-navigationCircle.active").removeClass("active");
			fullScreenOverlay.find(".navigation-overlay").find(".u-navigationCircle-Summary").addClass("active");

			if (intervalExistingPermit) {
				clearInterval(intervalExistingPermit);
				fullScreenOverlay.find(".c-ExistingPermit-Options").find(".c-CountDown-Timer").text('10secs');
			}
			setOne.removeClass("active").fadeOut(800, function () {
				fullScreenOverlay.find(".c-ExistContact-Box-overlay").removeClass("active");
				fullScreenOverlay.find('.navigation-overlay').attr("style", "");
				var twoHeight = setTwo.height();
				var windowHeight = $(window).height();
				var offsetTwo = (windowHeight / 2) - (twoHeight / 2) - getTopOffset; // + windowOffset;
				setTwo.css('top', offsetTwo).fadeIn(800, function () {
					setTwo.addClass("active");
				});
			});
			return false;
		}).on("click", ".c-ExistingPermit-Footer .c-Footer-Cancel-Button", function () {
			var $this = $(this),
					setOne = $this.parents(".options"),
					setTwo = fullScreenOverlay.find(".options.active");
			if (intervalExistingPermit) {
				clearInterval(intervalExistingPermit);
				fullScreenOverlay.find(".c-ExistingPermit-Options").find(".c-CountDown-Timer").text('10secs');
			}
			setOne.removeClass("active").removeClass("active").fadeOut(800, function () {
				fullScreenOverlay.find('.navigation-overlay').show();
				var twoHeight = setTwo.height();
				var windowHeight = $(window).height();
				var offsetTwo = (windowHeight / 2) - (twoHeight / 2) - getTopOffset; // + windowOffset;
				setTwo.css('top', offsetTwo).fadeIn(800, function () {
					setTwo.addClass("active");
				});
			});
			return false;
		});
		$('.personvehiclepermit .existing-names a .u-Permit-GroupEdit').on("click", function () {
			var $this = $(this),
					$group = $this.parents("a"),
					$group_uid = $group.attr("data-group_uid"),
					$parents = $this.parents(".option-container");
			$('.personvehiclepermit-overlay').fadeOut();
			$(".groupswidget-overlay").attr({"from": "personvehiclepermit", "update": $group_uid}).fadeIn();
			$('.c-Group-Widget-Button:first').trigger("click");
			$this.parents(".existing-names").find("a").removeClass("active");
			$group.addClass("active");
			assignPermitDatatoGroup();

			return false;
		});
	};
	existing_personvehiclepermit_names_actions();

	var getRealyDayName = function ($day) {
		if ($day == "mon") {
			$day = "Monday";
		} else if ($day == "tue") {
			$day = "Tuesday";
		} else if ($day == "wed") {
			$day = "Wednesday";
		} else if ($day == "thur") {
			$day = "Thursday";
		} else if ($day == "fri") {
			$day = "Friday";
		} else if ($day == "sat") {
			$day = "Saturday";
		} else if ($day == "sun") {
			$day = "Sunday";
		}
		return $day;
	}
	var openGroupConfirmation = function ($this) {
		if ($this) {
			var durationstart = $this.attr("data-durationstart"),
					durationend = $this.attr("data-durationend"),
					hide_duration = $this.attr("data-hide_duration"),
					lock_duration = $this.attr("data-lock_duration"),
					daytime = $this.attr("data-daytime"),
					hide_daytime = $this.attr("data-hide_daytime"),
					lock_daytime = $this.attr("data-lock_daytime"),
					person = $this.attr("data-person"),
					hide_person = $this.attr("data-hide_person"),
					lock_person = $this.attr("data-lock_person"),
					vehicle = $this.attr("data-vehicle"),
					hide_vehicle = $this.attr("data-hide_vehicle"),
					lock_vehicle = $this.attr("data-lock_vehicle"),
					location = $this.attr("data-location"),
					hide_location = $this.attr("data-hide_location"),
					lock_location = $this.attr("data-lock_location"),
					$groupName = $this.attr("data-group_name"),
					$groupsWidget = $(".groupswidget-overlay"),
					$existingOverlay = $this.parents('.options').find(".c-ExistGroupContact-Box-overlay");

			// set group name first
			$existingOverlay.find(".c-GroupContact-Name").text($groupName);

			var $itemBox = $existingOverlay.find(".c-ExistGroupContact-Items-Box .c-ExistGroupContact-Items");
			$itemBox.html("");
			var $stepHtml = '';
			if (("" !== durationstart && "" !== durationend) && undefined !== durationstart) {
				var date_start = $this.attr("data-durationstart"),
						date_end = $this.attr("data-durationend");
				$stepHtml = '<div class="c-ExistGroupContact-Item c-ExistGroupContact-PermitDuration"><div class="c-ExistGroupContact-Item-Left">PERMIT DURATION</div><div class="c-ExistGroupContact-Item-Right">' + date_start + ' - ' + date_end + '</div></div>';
				$itemBox.append($stepHtml);

			}
			if ("1" === hide_duration && undefined !== hide_duration) {
				$itemBox.find(".c-ExistGroupContact-PermitDuration").hide();
			}

			if ("" !== daytime && undefined !== daytime) {
				var $day = daytime,
						$filterdays = $day.split(",");
				$stepHtml = '';
				$.each($filterdays, function (i, $t) {
					var $start = $this.attr("data-daytime-" + $t + "-start"),
							$end = $this.attr("data-daytime-" + $t + "-end");
					$stepHtml += '<span class="c-ExistGroupContact-Item-Right-DayTime-Spans">' + getRealyDayName($t) + ' - ' + $start + ' to ' + $end + '</span>';
				});
				$itemBox.append('<div class="c-ExistGroupContact-Item c-ExistGroupContact-DayTime"><div class="c-ExistGroupContact-Item-Left">DAY & TIME</div><div class="c-ExistGroupContact-Item-Right">' + $stepHtml + '</div></div>');
			}
			if ("1" === hide_daytime && undefined !== hide_daytime) {
				$itemBox.find(".c-ExistGroupContact-DayTime").hide();
			}

			if ("" !== location && undefined !== location) {
				$stepHtml = location;
				$itemBox.append('<div class="c-ExistGroupContact-Item c-ExistGroupContact-Location"><div class="c-ExistGroupContact-Item-Left">LOCATION</div><div class="c-ExistGroupContact-Item-Right">' + $stepHtml + '</div></div>');
			}
			if ("1" === hide_location && undefined !== hide_location) {
				$itemBox.find(".c-ExistGroupContact-Location").hide();
			}

			fullScreenOverlay.find(".c-GroupBox-Options .c-Footer-Button").removeClass("active");
			$existingOverlay.addClass("active");
		}
	};
	var deAssignGroupDatatoPermit = function ($this) {
		var $steps = "",
				$permitDuration = fullScreenOverlay.find(".c-PermitDuration-Container"),
				date_filter = $this.attr("data-durationdatefilter"),
				date_number = $this.attr("data-durationdatenumber"),
				date_start = $this.attr("data-durationstartdate"),
				date_end = $this.attr("data-durationenddate"),
				$activeOption = $permitDuration.find(".c-Circle-Option.active"),
				$durationOption = $permitDuration.find(".c-Circle-Option"),
				$activeOptionLength = $activeOption.length;
		$.each($durationOption, function (i, $t) {
			if ($($t).attr("data-enddate")) {
				$($t).attr({"data-start": '', "data-end": $($t).attr("data-end"), "data-startdate": '', "data-enddate": $($t).attr("data-enddate")});
			}
		});
		$activeOption.trigger("click");
		$durationOption.removeClass("active");
		$durationOption.find(".c-OptionTimer").removeClass("active");
		fullScreenOverlay.find(".c-PermitDate-Footer a").removeClass("active");
		fullScreenOverlay.find(".u-navigationCircle-PermitDuration").removeClass("u-setDone");
		var $daytimeBlock = fullScreenOverlay.find(".c-DayTimes"),
				dattimeActiveCricleOption = $daytimeBlock.find(".c-Circle-Option.active"),
				dattimeCricleOption = $daytimeBlock.find(".c-Circle-Option");
		dattimeCricleOption.attr({"data-start": '', "data-end": ''});
		dattimeActiveCricleOption.trigger("click");
		dattimeCricleOption.removeClass("active");
		dattimeCricleOption.find(".c-OptionTimer").removeClass("active");
		fullScreenOverlay.find(".c-DayTime-Footer a").removeClass("active");
	};
	assignGroupDatatoPermit = function ($this, forcegotoSummary) {
		deAssignGroupDatatoPermit($this);
		//var $this = $(".personvehiclepermit .c-GroupBox-Options .existing-div a.active");

		var durationstart = $this.attr("data-durationstart"),
				durationend = $this.attr("data-durationend"),
				hide_duration = $this.attr("data-hide_duration"),
				lock_duration = $this.attr("data-lock_duration"),
				daytime = $this.attr("data-daytime"),
				hide_daytime = $this.attr("data-hide_daytime"),
				lock_daytime = $this.attr("data-lock_daytime"),
				person = $this.attr("data-person"),
				hide_person = $this.attr("data-hide_person"),
				lock_person = $this.attr("data-lock_person"),
				vehicle = $this.attr("data-vehicle"),
				hide_vehicle = $this.attr("data-hide_vehicle"),
				lock_vehicle = $this.attr("data-lock_vehicle"),
				location = $this.attr("data-location"),
				hide_location = $this.attr("data-hide_location"),
				lock_location = $this.attr("data-lock_location"),
				gotoSummary = true;

		var $isPinNeed = $("body").attr("data-GroupReviewPinAccepted");
		if ("" == $isPinNeed) {
			$isPinNeed = $("body").attr("data-GroupMasterPinAccepted");
		}


		fullScreenOverlay.find(".u-TempHide").removeClass("u-TempHide");
		if (("" !== durationstart && "" !== durationend) && undefined !== durationstart) {
			var $steps = "",
					$permitDuration = fullScreenOverlay.find(".c-PermitDuration-Container"),
					date_filter = $this.attr("data-durationdatefilter"),
					date_number = $this.attr("data-durationdatenumber"),
					date_start = $this.attr("data-durationstartdate"),
					date_end = $this.attr("data-durationenddate"),
					$activeOption = $permitDuration.find(".c-Circle-Option.active"),
					$activeOptionLength = $activeOption.length;

			if ((undefined !== date_filter && "" !== date_filter && "undefined" !== date_filter) && "" != date_number) {
				var $permitDurationCricle = $permitDuration.find(".c-Circle-Option[data-date_filter='" + date_filter + "'][data-date_number='" + date_number + "']");
			} else {
				var $permitDurationCricle = $permitDuration.find(".c-Circle-Option[data-start='" + durationstart + "'][data-end='" + durationend + "']");
			}

			if ($permitDurationCricle.length) {
				$permitDurationCricle.attr({"data-start": durationstart, "data-end": durationend, "data-startdate": date_start, "data-enddate": date_end}).trigger("click");
				setTimeout(function () {
					$permitDuration.find(".c-TimerSelectBox .c-TimerSelect-Save").trigger("click");
					/*
					 * need for once active and again click on cricle its diactivate circle 
					 * this patch needs for active them again
					 */
					setTimeout(function () {
						if ($activeOptionLength) {
							$activeOption.addClass("active");
							$activeOption.find(".c-OptionTimer").addClass("active");
							fullScreenOverlay.find(".c-PermitDate-Footer a").addClass("active");
						}
					}, 200);
					//fullScreenOverlay.find(".c-PermitDate-Footer a").addClass("active");
				}, 100);
			}
		} else {
			gotoSummary = false;
		}

		if ("1" === hide_duration && undefined !== hide_duration) {
			fullScreenOverlay.find(".u-navigationCircle-PermitDuration").addClass("u-TempHide");
			fullScreenOverlay.find(".options.c-Permit-Options").addClass("u-TempHide");
			fullScreenOverlay.find(".options.c-Summary-Options .c-PermitDuration-CirclesOption").addClass("u-TempHide");
		}
		
		if ("1" === lock_duration && undefined !== lock_duration) {
			if("" == $isPinNeed){
				fullScreenOverlay.find(".options.c-Summary-Options .c-PermitDuration-CirclesOption .c-CircleOption-Padlock").addClass("show");
				fullScreenOverlay.find(".u-navigationCircle-PermitDuration .c-navigationCircle-Icon-Padlock").addClass("show");
			}
		}

		if ("" !== daytime && undefined !== daytime) {
			var $daytimeBlock = fullScreenOverlay.find(".c-DayTimes"),
					$day = daytime,
					$filterdays = $day.split(",");
			$.each($filterdays, function (i, $t) {
				var dattimeCricleOption = $daytimeBlock.find(".c-Circle-Option[data-day='" + $t + "']");
				if (dattimeCricleOption.length) {
					var $start = $this.attr("data-daytime-" + $t + "-start"),
							$end = $this.attr("data-daytime-" + $t + "-end");
					dattimeCricleOption.attr({"data-start": $start, "data-end": $end}).trigger("click");
					setTimeout(function () {
						$daytimeBlock.find(".c-TimerSelectBox .c-TimerSelect-Save").trigger("click");
						//fullScreenOverlay.find(".c-DayTime-Footer a").addClass("active");

						setTimeout(function () {
							if ($activeOptionLength) {
								dattimeCricleOption.addClass("active");
								dattimeCricleOption.find(".c-OptionTimer").addClass("active");
								fullScreenOverlay.find(".c-DayTime-Footer a").addClass("active");
							}
						}, 200);
					}, 100);
				}
			});
			checkNavigationStatus();
		} else {
			gotoSummary = false;
		}
		if ("1" === hide_daytime && undefined !== hide_daytime) {
			fullScreenOverlay.find(".u-navigationCircle-DayTime").addClass("u-TempHide");
			fullScreenOverlay.find(".options.c-DayTime-Options").addClass("u-TempHide");
			fullScreenOverlay.find(".options.c-Summary-Options .c-DayTime-CirclesOption").addClass("u-TempHide");
		}
		if ("1" === lock_daytime && undefined !== lock_daytime) {
			if("" == $isPinNeed){
				fullScreenOverlay.find(".options.c-Summary-Options .c-DayTime-CirclesOption .c-CircleOption-Padlock").addClass("show");
				fullScreenOverlay.find(".u-navigationCircle-DayTime .c-navigationCircle-Icon-Padlock").addClass("show");
			}
		}

		/*if ("" !== person && undefined !== person) {
		 fullScreenOverlay.find(".options.c-Person-Options .option-container-firstbox-BtnGo").trigger("click");
		 fullScreenOverlay.find(".options.c-Person-Options .c-add-input").val(person).trigger("keyup");
		 fullScreenOverlay.find(".options.c-Person-Options .existing-names a.c-found").trigger("click");
		 }
		 if ("1" === hide_person && undefined !== hide_person) {
		 fullScreenOverlay.find(".u-navigationCircle-Person").addClass("u-TempHide");
		 fullScreenOverlay.find(".options.c-Person-Options").addClass("u-TempHide");
		 }
		 if ("" !== vehicle && undefined !== vehicle) {
		 fullScreenOverlay.find(".options.c-Vehicle-Options .option-container-firstbox-BtnGo").trigger("click");
		 fullScreenOverlay.find(".options.c-Vehicle-Options .c-add-input").val(vehicle).trigger("keyup");
		 fullScreenOverlay.find(".options.c-Vehicle-Options .existing-names a.c-found").trigger("click");
		 fullScreenOverlay.find(".options.c-Summary-Options .c-Vehicle-CirclesOption").addClass("u-TempHide");
		 }
		 if ("1" === hide_vehicle && undefined !== hide_vehicle) {
		 fullScreenOverlay.find(".u-navigationCircle-Vehicle").addClass("u-TempHide");
		 fullScreenOverlay.find(".options.c-Vehicle-Options").addClass("u-TempHide");
		 fullScreenOverlay.find(".options.c-Summary-Options .c-Vehicle-CirclesOption").addClass("u-TempHide");
		 }*/
		if ("" !== location && undefined !== location) {
			fullScreenOverlay.find(".options.c-Location-Options .option-container-firstbox-BtnGo").trigger("click");
			//fullScreenOverlay.find(".options.c-Location-Options.c-add-input").val(location).trigger("keyup");
			var arrayLocation = location.split(',');
			$.each(arrayLocation, function (i, $ths) {
				fullScreenOverlay.find(".c-option-location .existing-names a").filter(function () {
					var string = $.trim($(this).text());
					if (string == $.trim($ths)) {
						$(this).trigger("click");
					}
				});
			});
			fullScreenOverlay.find(".options.c-Location-Options .existing-names a.c-found").trigger("click");
		} else {
			gotoSummary = false;
		}
		if ("1" === hide_location && undefined !== hide_location) {
			fullScreenOverlay.find(".u-navigationCircle-Location").addClass("u-TempHide");
			fullScreenOverlay.find(".options.c-Location-Options").addClass("u-TempHide");
			fullScreenOverlay.find(".options.c-Summary-Options .c-Location-CirclesOption").addClass("u-TempHide");
		}
		if ("1" === lock_location && undefined !== lock_location) {
			if("" == $isPinNeed){
				fullScreenOverlay.find(".options.c-Summary-Options .c-Location-CirclesOption .c-CircleOption-Padlock").addClass("show");
				fullScreenOverlay.find(".u-navigationCircle-Location .c-navigationCircle-Icon-Padlock").addClass("show");
			}
		}

		var triggerSummary = function () {
			fullScreenOverlay.find(".u-navigationCircle-Summary").trigger("click");
			fullScreenOverlay.find(".c-GroupBox-Options .c-Footer-Button").addClass("active");
		};
		// now check if all next step done then jump to summary page
		if (undefined !== forcegotoSummary) {
			if (true === forcegotoSummary) {
				triggerSummary();
			}
		} else if (gotoSummary) {
			triggerSummary();
		}
	};
	assignPermitDatatoGroup = function () {
		var $this = $(".personvehiclepermit .c-GroupBox-Options .existing-div a.active");
		var durationstart = $this.attr("data-durationstart"),
				durationend = $this.attr("data-durationend"),
				hide_duration = $this.attr("data-hide_duration"),
				lock_duration = $this.attr("data-lock_duration"),
				daytime = $this.attr("data-daytime"),
				hide_daytime = $this.attr("data-hide_daytime"),
				lock_daytime = $this.attr("data-lock_daytime"),
				person = $this.attr("data-person"),
				hide_person = $this.attr("data-hide_person"),
				lock_person = $this.attr("data-lock_person"),
				vehicle = $this.attr("data-vehicle"),
				hide_vehicle = $this.attr("data-hide_vehicle"),
				lock_vehicle = $this.attr("data-lock_vehicle"),
				location = $this.attr("data-location"),
				hide_location = $this.attr("data-hide_location"),
				lock_location = $this.attr("data-lock_location"),
				$groupName = $this.attr("data-group_name"),
				$groupsWidget = $(".groupswidget-overlay"),
				$optionSelection = $groupsWidget.find(".options.c-Setone-Option-Selection");
		// set group name first
		$groupsWidget.find("#group-add").val($groupName).trigger("keyup");
		$groupsWidget.find(".groupswidget-Box .c-BtnSave").attr('saved', true);
		var $steps = "";
		if (("" !== durationstart && "" !== durationend) && undefined !== durationstart) {
			var $permitDuration = $groupsWidget.find(".c-PermitDuration-Container"),
					date_filter = $this.attr("data-durationdatefilter"),
					date_number = $this.attr("data-durationdatenumber"),
					date_start = $this.attr("data-durationstartdate"),
					date_end = $this.attr("data-durationenddate");
			if ((undefined !== date_filter && "" !== date_filter && "undefined" !== date_filter) && "" != date_number) {
				var $permitDurationCricle = $permitDuration.find(".c-Circle-Option[data-date_filter='" + date_filter + "'][data-date_number='" + date_number + "']");
			} else {
				var $permitDurationCricle = $permitDuration.find(".c-Circle-Option[data-start='" + durationstart + "'][data-end='" + durationend + "']");
			}
			if ($permitDurationCricle.length) {
				$permitDurationCricle.attr({"data-start": durationstart, "data-end": durationend, "data-startdate": date_start, "data-enddate": date_end}).trigger("click");
				setTimeout(function () {
					$permitDuration.find(".c-TimerSelectBox .c-TimerSelect-Save").trigger("click");
				}, 100);
			}

		}
		if ("1" === hide_duration && undefined !== hide_duration) {
			$optionSelection.find(".c-PermitDuration-CirclesOption .c-CircleOption-Hide").addClass("icon-hide2").removeClass("icon-hide1");
		} else {
			$optionSelection.find(".c-PermitDuration-CirclesOption .c-CircleOption-Hide").addClass("icon-hide1").removeClass("icon-hide2");
		}
		if ("1" === lock_duration && undefined !== lock_duration) {
			$optionSelection.find(".c-PermitDuration-CirclesOption .c-CircleOption-Padlock").addClass("c-Padlock-Active");
		} else {
			$optionSelection.find(".c-PermitDuration-CirclesOption .c-CircleOption-Padlock").removeClass("c-Padlock-Active");
		}

		if ("" !== daytime && undefined !== daytime) {
			var $daytimeBlock = $groupsWidget.find(".c-DayTimes"),
					$day = daytime,
					$filterdays = $day.split(",");
			$.each($filterdays, function (i, $t) {
				var dattimeCricleOption = $daytimeBlock.find(".c-Circle-Option[data-day='" + $t + "']");
				if (dattimeCricleOption.length) {
					var $start = $this.attr("data-daytime-" + $t + "-start"),
							$end = $this.attr("data-daytime-" + $t + "-end");
					dattimeCricleOption.attr({"data-start": $start, "data-end": $end}).trigger("click");
					setTimeout(function () {
						$daytimeBlock.find(".c-TimerSelectBox .c-TimerSelect-Save").trigger("click");
					}, 100);
				}
			});
		}
		if ("1" === hide_daytime && undefined !== hide_daytime) {
			$optionSelection.find(".c-DayTime-CirclesOption .c-CircleOption-Hide").addClass("icon-hide2").removeClass("icon-hide1");
		} else {
			$optionSelection.find(".c-DayTime-CirclesOption .c-CircleOption-Hide").addClass("icon-hide1").removeClass("icon-hide2");
		}
		if ("1" === lock_daytime && undefined !== lock_daytime) {
			$optionSelection.find(".c-DayTime-CirclesOption .c-CircleOption-Padlock").addClass("c-Padlock-Active");
		} else {
			$optionSelection.find(".c-DayTime-CirclesOption .c-CircleOption-Padlock").removeClass("c-Padlock-Active");
		}
		/*if ("" !== person && undefined !== person) {
		 $groupsWidget.find(".options.set-five .option-container-firstbox-BtnGo").trigger("click");
		 $groupsWidget.find(".options.set-four .c-add-input").val(person).trigger("keyup");
		 $groupsWidget.find(".options.set-four .existing-names a.c-found").trigger("click");
		 }
		 if ("" !== vehicle && undefined !== vehicle) {
		 $groupsWidget.find(".options.set-five .option-container-firstbox-BtnGo").trigger("click");
		 $groupsWidget.find(".options.set-five .c-add-input").val(vehicle).trigger("keyup");
		 $groupsWidget.find(".options.set-five .existing-names a.c-found").trigger("click");
		 }*/

		if ("" !== location && undefined !== location) {
			$groupsWidget.find(".options.c-Options-Location .option-container-firstbox-BtnGo").trigger("click");
			var arrayLocation = location.split(',');
			$.each(arrayLocation, function (i, $ths) {
				$groupsWidget.find(".c-option-location .existing-names a").filter(function () {
					var string = $.trim($(this).text());
					if (string == $.trim($ths)) {
						$(this).trigger("click");
					}
				});
			});
			$groupsWidget.find(".options.c-Options-Location .existing-names a.c-found").trigger("click");
		}
		if ("1" === hide_location && undefined !== hide_location) {
			$optionSelection.find(".c-Location-CirclesOption .c-CircleOption-Hide").addClass("icon-hide2").removeClass("icon-hide1");
		} else {
			$optionSelection.find(".c-Location-CirclesOption .c-CircleOption-Hide").addClass("icon-hide1").removeClass("icon-hide2");
		}
		if ("1" === lock_location && undefined !== lock_location) {
			$optionSelection.find(".c-Location-CirclesOption .c-CircleOption-Padlock").addClass("c-Padlock-Active");
		} else {
			$optionSelection.find(".c-Location-CirclesOption .c-CircleOption-Padlock").removeClass("c-Padlock-Active");
		}

		var $isPinNeed = $("body").attr("data-GroupMasterPinAccepted");
		if (undefined == $isPinNeed || "" == $isPinNeed) {
			hideGroupInactiveElements();
		} else {
			showGroupActiveElements();
		}

	};
	$('.personvehiclepermit .c-add-input').on('keyup', function () {
		var $this = $(this),
				existing_div = $this.parents(".option-container").find(".existing-div"),
				existing_name = existing_div.find(".existing-names a"),
				$val = $this.val();
		existing_div.find(".personvehiclepermit-Box-overlay").removeClass("active");
		if ("" !== $val) {
			$this.parents(".option-container").find(".c-BtnClear").show();
			existing_name.removeClass("c-found");
			var element = existing_name.filter(function () {
				var string = $(this).text().trim().replace(/ +/g, "").toLowerCase();
				if (string.indexOf($val.trim().replace(/ +/g, "").toLowerCase()) > -1) {
					if (string == $val.trim().replace(/ +/g, "").toLowerCase()) {
						$(this).addClass("c-found");
					} else {
						$(this).removeClass("c-found");
					}
					return $(this);
				}
			});
			if (element.length) {
				existing_name.hide();
				element.show();
			} else {
				existing_name.show();
			}

			if (existing_div.find(".u-DateTime").length) {
				$(".personvehiclepermit .u-DateTime").removeClass("active");
			}
		} else {
			$this.parents(".option-container").find(".c-BtnClear").hide();
			existing_name.removeClass("c-found").show();
		}
	});
	$('.personvehiclepermit .existing-names-search .existing-names-search-input').on('keyup', function () {
		var $this = $(this),
				existing_div = $this.parents(".option-container").find(".existing-div"),
				existing_name = existing_div.find(".existing-names a"),
				$val = $this.val();
		existing_div.find(".personvehiclepermit-Box-overlay").removeClass("active");
		if ("" !== $val) {
			$this.parents(".option-container").find(".existing-names-search-clear").show();
			existing_name.removeClass("c-found");
			var element = existing_name.filter(function () {
				var string = $(this).text().trim().replace(/ +/g, "").toLowerCase();
				if (string.indexOf($val.trim().replace(/ +/g, "").toLowerCase()) > -1) {
					if (string == $val.trim().replace(/ +/g, "").toLowerCase()) {
						$(this).addClass("c-found");
					} else {
						$(this).removeClass("c-found");
					}
					return $(this);
				}
			});
			console.log(element);
			if (element.length) {
				existing_name.hide();
				element.show();
			} else {
				existing_name.show();
			}

			if (existing_div.find(".u-DateTime").length) {
				$(".personvehiclepermit .u-DateTime").removeClass("active");
			}
		} else {
			$this.parents(".option-container").find(".existing-names-search-clear").hide();
			existing_name.removeClass("c-found").show();
		}
	});
	setInterval(function () {
		if ($(".personvehiclepermit .existing-names a.c-found").length) {
			$(".personvehiclepermit .existing-names a.c-found").removeClass("hvr-buzz-out-active").addClass("hvr-buzz-out-hover");
			setTimeout(function () {
				$(".personvehiclepermit .existing-names a.c-found").removeClass("hvr-buzz-out-hover");
			}, 500);
		}
	}, 3000);

	var RemoveSelected = function ($this, $set) {
		$this.parents(".option-container").find(".existing-names .active").removeClass("active");
		$(".personvehiclepermit-overlay .u-navigationCircle[data-set='" + $set + "']").removeClass("u-setDone hovered");
		$(".u-setDone.active").removeClass("u-setDone");
		$this.parents(".options").find(".c-Common-Footer").find("a.c-Footer-Button").removeClass("active");
	};
	$(".personvehiclepermit-overlay .c-BtnClear").on("click", function () {
		var $this = $(this),
				$input = $this.parents(".option-container").find(".c-add-input"),
				$set = $this.parents(".options").attr("data-set");
		if ($input.length) {
			$input.val("").trigger("keyup").focus();
			$this.hide();

			if ($this.parents(".option-container").find(".c-add-input")) {
				if (!$this.parents(".option-container").find(".existing-names .active").length) {
					RemoveSelected($this, $set);
				}
			} else {
				RemoveSelected($this, $set);
			}
		}
		return false;
	});
	$(".personvehiclepermit-overlay .existing-names-search-clear").on("click", function () {
		var $this = $(this),
				$input = $this.parents(".option-container").find(".existing-names-search-input"),
				$set = $this.parents(".options").attr("data-set");
		if ($input.length) {
			$input.val("").trigger("keyup").focus();
			$this.hide();
			if (!$this.parents(".option-container").find(".existing-names .active").length) {
				RemoveSelected($this, $set);
			}
		}
		return false;
	});


	var showDeleteLocationButton = function () {
		var $isPinNeed = $("body").attr("data-GroupReviewPinAccepted");
		if (1 == $isPinNeed) {
			var option = $(".c-option-location");
			option.find(".existing-names a .c-list-delete").addClass("u-show");
		} else {
			hideDeleteLocationButton();
		}
	};
	var hideDeleteLocationButton = function () {
		var option = $(".c-option-location");
		option.find(".existing-names a .c-list-delete").removeClass("u-show");
	};
	var resetExistingNames = function (existing_names) {
		existing_names.find("a").removeClass("u-show").hide();
		existing_names.find("a:lt(5)").addClass("u-show").show();
	};
	resetExistingNames($(".existing-search-div .existing-names"));

	//console.log($(".personvehiclepermit-overlay .existing-search-div .existing-names a:gt(4):lt(10)"));
	$(".personvehiclepermit-overlay .existing-search-div .existing-names-navigation-btn").on("click", function () {
		var $this = $(this),
				$container = $this.parents(".option-container"),
				$existingnamesdiv = $container.find(".existing-names"),
				$existingnames = $existingnamesdiv.find("a"),
				$existingnamesshow = $existingnamesdiv.find("a.c-show"),
				$next = $container.find(".existing-names-navigation-next"),
				$previous = $container.find(".existing-names-navigation-previous");
		var items = $existingnames.length;
		if (items) {
			if ($this.hasClass("existing-names-navigation-next")) {
				var showEle = $($container.find(".existing-search-div .existing-names a.u-show").last()).nextAll("a").slice(0, 5);
				if (showEle.length) {
					$existingnames.removeClass("u-show").hide();
					showEle.addClass("u-show").show();
				}
			} else {
				var showEle = $($container.find(".existing-search-div .existing-names a.u-show").first()).prevAll("a:lt(5)");
				if (showEle.length) {
					$existingnames.removeClass("u-show").hide();
					showEle.addClass("u-show").show();
				}
			}
		}
		return false;
	});
	$('.personvehiclepermit .c-BtnSave').on('click', function () {
		var $this = $(this),
				options = $this.parents(".options"),
				existing_div = $this.parents(".option-container"),
				existing_name = existing_div.find(".existing-names"),
				$name = existing_div.find('.c-add-input').val();

		if (options.hasClass("c-Location-Options")) {
			if (!validateCircleOptionLinks(options)) {
				options.addClass("u-PinLock-Active")
				return false;
			}
		}

		if (options.hasClass("c-Pin-Option-Selection")) {
			var $pin = existing_div.find('.c-pin-input').val(),
					$body = $("body"),
					$addPin = $body.attr('data-add-pin'),
					$masterPin = $body.attr('data-master-pin'),
					$isUpdate = fullScreenOverlay.attr('update'),
					$attrAction = options.attr('action'),
					$isbackTofirst = fullScreenOverlay.attr('backTofirst'),
					$tempPin = $addPin;
			if ("" !== $pin) {
				var $action = "master";
				if (undefined !== $attrAction && "" !== $attrAction) {
					$action = $attrAction;
				} else if (undefined != $isUpdate && "" != $isUpdate) {
					$action = "review";
				}
				$.post("", {'group-pin': 1, "action": $action, "password": $pin}, function (json) {
					if (json.status) {
						inputSuccess(existing_div, "Correct, though may pass!");
						existing_div.find('.c-pin-input').val('');
						if ($pin == $masterPin) {
							$body.attr({"data-GroupMasterPinAccepted": 1, "data-GroupReviewPinAccepted": 1});
							showGroupActiveElements();
						} else {
							$body.attr("data-GroupReviewPinAccepted", 1);
							hideGroupInactiveElements();
						}
						$(".pin-logout").addClass("u-loggedIn");

						var innerTarget = fullScreenOverlay.find('.u-PinLock-Active');
						var getTopOffset = 60;
						var setOne = fullScreenOverlay.find(".options.active");
						var setNext = innerTarget;

						innerTarget.css({display: 'none', top: 0});
						$this.parents('.options').find(".option").removeClass("active");
						$this.addClass("active");

						setOne.removeClass("active").fadeOut(800, function () {
							var twoHeight = setNext.height();
							var windowHeight = $(window).height();
							var offsetTwo = (windowHeight / 2) - (twoHeight / 2) - getTopOffset;// + windowOffset;
							setNext.css('top', offsetTwo).fadeIn(800, function () {
								fullScreenOverlay.find(".navigation-overlay").show();
								setNext.removeClass("u-PinLock-Active").addClass("active");
								// active delete location buttons
								showDeleteLocationButton();
							});
						});
					} else {
						inputErrors(existing_div, "Ooops! Your pin is not correct, pls try again.");
						existing_div.find('.c-pin-input').val('');
					}
				}, 'json');

			} else {
				inputErrors(existing_div, "Ooops! Pls add in your pin…");
			}
		} else if ("" !== $name) {
			var element = existing_name.find("a").filter(function () {
				var string = $(this).text().trim().replace(/ +/g, "").toLowerCase();
				if (options.hasClass("c-GroupBox-Options")) {
					string = $(this).attr("data-group_name").replace(/ +/g, "").toLowerCase();
				}
				if (string != $name.trim().replace(/ +/g, "").toLowerCase()) {
				} else {
					return $(this);
				}
			});
			if (!element.length) {
				if (options.hasClass("c-GroupBox-Options")) {
					var $uid = Math.floor((Math.random() * 1000) + 1);
					var $html = '<a data-group_name="' + $name + '" data-group_uid="' + $uid + '" href="#" style="display: block;">' + $name + '<span class="u-Permit-GroupEdit">EDIT</span></a>';
					if (existing_name.find(".existing-names-search").length) {
						existing_name.find(".existing-names-search").after($html);
					} else {
						existing_name.prepend($html);
					}
					resetExistingNames(existing_name);
					existing_personvehiclepermit_names_actions();
					existing_div.find('.c-add-input').val("").trigger("keyup");
					existing_name.find("a:last").trigger("click");
					// trigger for edit if group step afer add
					if (options.hasClass("c-GroupBox-Options")) {
						existing_name.find("a:last").find(".u-Permit-GroupEdit").trigger("click");
					}
				} else {
					// adding delete button is location step list added
					var $btnDelete = '';
					if (options.hasClass("c-Location-Options")) {
						$btnDelete = '<span class="c-list-delete icon-cross c-circle" href="#"></span>';
					}
					var $html = '<a href="#" class="c-newAdded">' + $name + $btnDelete + '</a>';
					if (existing_name.find(".existing-names-search").length) {
						existing_name.find(".existing-names-search").after($html);
					} else {
						existing_name.prepend($html);
					}
					resetExistingNames(existing_name);
					existing_personvehiclepermit_names_actions();
					existing_div.find('.c-add-input').val("").trigger("keyup");
					showDeleteLocationButton();
				}
//$('.personvehiclepermit .c-add-input').val("").trigger("keyup");

				existing_name.find("a.c-newAdded").trigger("click");
				existing_name.find("a.c-newAdded").removeClass("c-newAdded");
			} else {
				if (options.hasClass("c-Location-Options")) {
					inputErrors(existing_div, $name + " already exists, Please enter another location!");
				} else {
					inputErrors(existing_div, $name + " already exists, pls select from list, it's shaking for your attention!");
				}
			}
		} else {
			inputErrors(existing_div, "Oops! We Can't add nothing, please try again..");
		}
		return false;
	});
	$(".personvehiclepermit-Box-overlay").unbind("click").bind("click", function () {
		$(this).removeClass("active");
		$(this).parents(".option-container").find('.c-add-input').trigger("focus").trigger("keyup");
	});
	function inputSuccess(existing_div, $error) {
		existing_div.find(".personvehiclepermit-Success-Box-overlay").addClass("active").find(".c-Contentbox span").html($error);
		setTimeout(function () {
			existing_div.find(".personvehiclepermit-Success-Box-overlay").removeClass("active");
		}, 4000);
	}
	function inputErrors(existing_div, $error) {
		existing_div.find(".personvehiclepermit-Box-overlay").addClass("active").find(".c-Contentbox span").html($error);
		setTimeout(function () {
			existing_div.find(".personvehiclepermit-Box-overlay").removeClass("active");
		}, 4000);
	}

	function addOverlay() {
		$('body').addClass('activate-full-screen');
		$('.personvehiclepermit-overlay').addClass('activate-overlay-screen');
		innerOffsetBox.css({position: 'absolute', top: $(window).scrollTop() + 50});
		$(".personvehiclepermit-overlay").removeClass("active");
		$(".personvehiclepermit-overlay .options").attr("style", "");
		$(".c-TipHelp-Container").fadeOut().removeClass("active");
	}

	function removeOverlay() {
		$('body').removeClass('activate-full-screen');
		$('.personvehiclepermit-overlay').removeClass('activate-overlay-screen');
		$(".personvehiclepermit-overlay .personvehiclepermit").removeClass("active");
		$(".personvehiclepermit-overlay").removeClass("show");
		$(".personvehiclepermit-overlay").find(".active").removeClass("active");
		$(".personvehiclepermit-overlay").find(".c-found").removeClass("c-found");
		$(".personvehiclepermit-overlay .navigation-overlay a").not(".u-gotOSummary").show();
		$('.personvehiclepermit-overlay .personvehiclepermit').hide();
		$('.personvehiclepermit-overlay .c-TimerSelectBox, .personvehiclepermit-overlay .c-TimerSelection-Inputs-Box').hide();
		$(".personvehiclepermit-overlay .c-DayTimes .c-hasActive").removeClass("c-hasActive");
		$(".personvehiclepermit-overlay .personvehiclepermit .c-PermitDuration-StartEndSelection-BtnAddNew").trigger("click");
		$(".personvehiclepermit-overlay .personvehiclepermit input").val("");
		$(".personvehiclepermit-overlay .personvehiclepermit .option-container-firstbox").show();
		$(".personvehiclepermit-overlay .personvehiclepermit .option-container-secondbox").hide();
		$(".personvehiclepermit-overlay .navigation-overlay .u-setDone").removeClass("u-setDone");
		$(".personvehiclepermit-overlay .navigation-overlay .u-navigationCircle-Animated").remove();
		$(".personvehiclepermit-overlay .navigation-overlay .u-navigationCircle-Span1").addClass("animate");
		$(".personvehiclepermit-overlay .navigation-overlay .active").removeClass("active");
		$(".personvehiclepermit-overlay .navigation-overlay").removeClass("u-hideUnwanted u-showWanted");
		$(".personvehiclepermit-overlay .hovered").removeClass("hovered");
		$(".personvehiclepermit-overlay .u-TempHide").removeClass("u-TempHide");
		$(".personvehiclepermit-overlay, .personvehiclepermit-overlay .u-navigationCircle").css({display: ""});
		$(".personvehiclepermit-overlay .options").removeClass("c-Options-Locked");
		$(".personvehiclepermit-overlay .u-PinLock-Active").removeClass("u-PinLock-Active");
	}

	function extendOverlay(action, $html) {
		if ('done' == action) {
			var done = fullScreenOverlay.find('.done').css({display: 'block', opacity: 0});
			var targetHeight = done.height();
			var windowHeight = $(window).height();
			var offsetTop = (windowHeight / 2) - (targetHeight / 2) - 50; // + windowOffset;
			done.css({top: offsetTop});
			$('.personvehiclepermit-overlay .navigation-overlay').hide();
			done.animate({opacity: 1}, 300, function () {
				setTimeout(function () {
					done.animate({opacity: 0}, 300, function () {
						removeOverlay();
						$('.personvehiclepermit-overlay .navigation-overlay').show();
					});
				}, 1000);
			});
		} else if ('error' == action) {
			var outerTarget = fullScreenOverlay.find('.personvehiclepermit').css({display: 'block', opacity: 1});
			var done = fullScreenOverlay.find('.c-ErrorPopups-Options').addClass("active").css({display: 'block', opacity: 0});
			var targetHeight = done.height();
			var windowHeight = $(window).height();
			var offsetTop = (windowHeight / 2) - (targetHeight / 2) - 50; // + windowOffset;
			done.css({top: offsetTop});
			done.find(".c-ErrorPopups-Options-Error-overlay").addClass("active");
			done.find(".c-ErrorPopups-Options-Error-overlay .c-Contentbox").html($html);
			done.find(".c-Footer-Button").addClass("active");
			$('.personvehiclepermit-overlay .navigation-overlay').hide();
			done.animate({opacity: 1}, 300, function () {
			});
			done.find(".c-AssignToGroup-Footer a").unbind("click").bind("click", function () {
				setTimeout(function () {
					done.animate({opacity: 0}, 300, function () {
						removeOverlay();
						$('.personvehiclepermit-overlay .navigation-overlay').show();
					});
				}, 800);
				return false;
			});
		} else if ('personvehiclepermit' == action) {
			var outerTarget = fullScreenOverlay.find('.personvehiclepermit').css({display: 'block', opacity: 0});
			$('.personvehiclepermit .set-two').css({display: 'none', opacity: 0, top: 0});
			var innerTarget = outerTarget.find('.set-one');
			var targetHeight = innerTarget.height();
			var windowHeight = $(window).height();
			var windowOffset = $(window).scrollTop();
			var offsetTop = (windowHeight / 2) - (targetHeight / 2) - 50; // + windowOffset;

			innerTarget.css({top: offsetTop, display: 'block', opacity: 1});
			outerTarget.animate({opacity: 1}, 300, function () {

			});
		}
	}

	function addCircleToSummary($day) {
		removeCircleToSummary("all");
		$(".personvehiclepermit-overlay .c-Circles-DateTime .c-CirclesText .c-CirclesText-DateTime-Day[data-day=" + $day + "]").addClass("active");
	}
	function removeCircleToSummary($day) {
		$(".personvehiclepermit-overlay .c-Circles-DateTime .c-CirclesText .c-CirclesText-DateTime-Day[data-day=" + $day + "]").removeClass("active");
	}
	function removeDateCircleSummary() {
		$(".personvehiclepermit-overlay .c-Circles-DateTime .c-CirclesText .c-CirclesText-DateTime-Day").removeClass("active");
	}


	$(".car-plate-more-button").on("click", function () {
		var $this = $(this),
				$list = $this.parents(".whitelist-dropdown").find(".licence-plate-list"),
				$plate = $list.find(".car-plate:last").not(".active-car-plate");
		$list.append($plate.clone());
	});
	$(".licence-plate-list .car-plate-delete").on("click", function () {
		var $this = $(this),
				$list = $this.parents(".car-plate");

		openDeletePoup();
		deleteOverlay.find(".c-DeleteConfirm-Button-Yes").unbind("click")
			.on("click", function(){
			$list.removeClass("car-plate-selected").fadeOut(function () {
				removeDeleteOverlay();
				toggelWhitelistDragBlock();
			});
		});		
		return false;
	});
	$(".c-contact-selection").on("click", function () {
		var $this = $(this),
				$parents = $this.parents(".summary-bar-header");
		$parents.toggleClass("c-contact-selected");
		dragHandle();
		return false;
	});
	$(".control-search input[type=text]").focusin(function () {
		$(this).addClass("search-focus");
		$(this).attr("placeholder", "SEARCH FOR PERMITS, PEOPLE, VEHICLES");
	}).focusout(function () {
		$(this).removeClass("search-focus");
		$(this).attr("placeholder", "SEARCH");
	}).on("keyup", function () {
		var $this = $(this),
				$search = $this.parents(".control-search"),
				feed = $this.parents(".feed"),
				summarybar = feed.find(".summary-bar.dropdown-toggle"),
				conact_name = summarybar.find("h3"),
				conact_vehicle = summarybar.find(".licence-plate"),
				$val = $this.val();
		if ("" !== $val) {
			$search.find(".c-search-Clear-Handle").show();
			$this.parents(".option-container").find(".c-BtnClear").show();
			summarybar.removeClass("c-contact-found");
			var element = conact_name.filter(function () {
				var string = $(this).text().trim().replace(/ +/g, "").toLowerCase();
				if (string.indexOf($val.trim().replace(/ +/g, "").toLowerCase()) > -1) {
					if (string == $val.trim().replace(/ +/g, "").toLowerCase()) {
						//$(this).parents(".summary-bar").addClass("c-contact-found");
					} else {
						//$(this).parents(".summary-bar").removeClass("c-contact-found");
					}
					return $(this);
				}
			});
			var element_vehicle = conact_vehicle.filter(function () {
				var string = $(this).text().trim().replace(/ +/g, "").toLowerCase();
				if (string.indexOf($val.trim().replace(/ +/g, "").toLowerCase()) > -1) {
					if (string == $val.trim().replace(/ +/g, "").toLowerCase()) {
						//$(this).parents(".summary-bar").addClass("c-contact-found");
					} else {
						//$(this).parents(".summary-bar").removeClass("c-contact-found");
					}
					return $(this);
				}
			});
			summarybar.hide();
			if (element.length) {
				element.parents(".summary-bar").show();
			}
			if (element_vehicle.length) {
				element_vehicle.parents(".summary-bar").show();
			}

		} else {
			$search.find(".c-search-Clear-Handle").hide();
			summarybar.show();
		}
	});
	$(".feed-search input[type=text]").focusin(function () {
		$(this).addClass("search-focus");
		$(this).attr("placeholder", "SEARCH FOR FEEDS");
	}).focusout(function () {
		$(this).removeClass("search-focus");
		$(this).attr("placeholder", "SEARCH");
	}).on("keyup", function () {
		var $this = $(this),
				$search = $this.parents(".feed-search"),
				feed = $this.parents(".feed"),
				summarybar = feed.find(".feed-item"),
				feed_title = summarybar.find(".feed-title"),
				feed_summary = summarybar.find(".feed-summary"),
				$val = $this.val();
		if ("" !== $val) {
			$search.find(".c-search-Clear-Handle").show();
			$this.parents(".option-container").find(".c-BtnClear").show();
			summarybar.removeClass("c-contact-found");
			var element_title = feed_title.filter(function () {
				var string = $(this).text().trim().replace(/ +/g, "").toLowerCase();
				if (string.indexOf($val.trim().replace(/ +/g, "").toLowerCase()) > -1) {
					if (string == $val.trim().replace(/ +/g, "").toLowerCase()) {
						//$(this).parents(".summary-bar").addClass("c-contact-found");
					} else {
						//$(this).parents(".summary-bar").removeClass("c-contact-found");
					}
					return $(this);
				}
			});
			var element_summary = feed_summary.filter(function () {
				var string = $(this).text().trim().replace(/ +/g, "").toLowerCase();
				if (string.indexOf($val.trim().replace(/ +/g, "").toLowerCase()) > -1) {
					if (string == $val.trim().replace(/ +/g, "").toLowerCase()) {
						//$(this).parents(".summary-bar").addClass("c-contact-found");
					} else {
						//$(this).parents(".summary-bar").removeClass("c-contact-found");
					}
					return $(this);
				}
			});
			summarybar.hide();
			if (element_title.length) {
				element_title.parents(".feed-item").show();
			}
			if (element_summary.length) {
				element_summary.parents(".feed-item").show();
			}

		} else {
			$search.find(".c-search-Clear-Handle").hide();
			summarybar.show();
		}
	});
	$(".c-Search-Block .c-search-Clear-Handle").on("click", function () {
		var $this = $(this),
				$search = $this.parents(".c-Search-Block"),
				$input = $search.find("input[type=text]");
		$input.trigger("focusout").blur().val("").trigger("keyup");
		return false;
	});
	$(".c-Search-Block .c-Search-Block-Input").on("focus", function () {
		var $this = $(this),
			tab = $this.parents(".tab");
			console.log(tab.offset().top);
		$('html, body').animate({scrollTop: $this.offset().top-10}, 800);
	});

	$('.u-SearchWhiteList')
		.on('keyup', function () {
			var $this = $(this),
					$search = $this.parents(".control-search"),
					tab_container = $this.parents(".tab-container"),
					platlists = tab_container.find(".licence-plate-list-Parents"),
					status_selected_element = $("#whitelist-live-controls").find(".control-filter.control-filter-selected"),
					$val = $this.val();

			$(".c-Car-Plate-Box .c-Car-Plate-Box-Message").hide();
			tab_container.find(".licence-plate-list-Parents").alterClass('found_*');
					
			var status_selected = '';
			if (status_selected_element.length) {
				if (status_selected_element.text() != "ALL") {
					status_selected = status_selected_element.text();
				}
			}
			//console.log("status_selected ", status_selected);
			var ele_status_selected = platlists.find(".car-plate-status .car-plate-status-text:contains('" + status_selected + "')");
			//console.log("ele_status_selected ", ele_status_selected);

			if (ele_status_selected.length) {
				platlists = ele_status_selected.parents(".licence-plate-list-Parents");
			}
			//console.log("platlists ", platlists);

			var ele_numberplate = platlists.find(".car-plate-numberplate"),
					ele_details = platlists.find(".car-plate-value-spans .car-plate-small"),
					ele_status = platlists.find(".car-plate-small");

			if ("" !== $val) {
				//console.log("val", $val);
				var explodedval = $val.split(",");
				console.log("explodedval", explodedval)
				$this.parents(".option-container").find(".c-BtnClear").show();
				platlists.removeClass("c-contact-found");

				tab_container.find(".licence-plate-list-Parents").hide();
				
				if(explodedval.length > 1){
					var numberplats = new Array();
					var details = new Array();
					var status = new Array();
					var $classFounded = "";
					$.each(explodedval, function(i, val){
						var $val = val.trim().replace(/ +/g, "").toLowerCase();
						if("" !== $val){
							$class = "found_"+i;
							$classFounded += "."+$class;
							//console.info("val",val.trim().replace(/ +/g, "").toLowerCase());
							numberplats[i] = ele_numberplate.filter(function () {
								var string = $(this).text().trim().replace(/ +/g, "").toLowerCase();
								if (string.indexOf($val) > -1) {
									$(this).parents(".licence-plate-list-Parents").addClass($class);
									return $(this).addClass("found");
								}
							});
							details[i] = ele_details.filter(function () {
								var string = $(this).text().trim().replace(/ +/g, "").toLowerCase();
								if (string.indexOf($val) > -1) {
									$(this).parents(".licence-plate-list-Parents").addClass($class);
									return $(this).addClass("found");
								}
							});

							status[i] = ele_status.filter(function () {
								var string = $(this).text().trim().replace(/ +/g, "").toLowerCase();
								if (string.indexOf($val) > -1) {
									$(this).parents(".licence-plate-list-Parents").addClass($class);
									return $(this).addClass("found");
								}
							});
							//console.log("status[i] "+i, status[i]);
						}


						
					});

					console.log("$classFounded",$classFounded);
					console.log(tab_container.find(".licence-plate-list-Parents"+$classFounded));
					tab_container.find(".licence-plate-list-Parents"+$classFounded).show();

					/*console.log(numberplats);	
					console.log(details);
					console.log(status);*/
					//if (numberplats.length && details.length && status.length) {
						//numberplats.parents(".licence-plate-list-Parents").show();
						//details.parents(".licence-plate-list-Parents").show();
						//status.parents(".licence-plate-list-Parents").show();
					//}
					
				} else{
					var numberplats = ele_numberplate.filter(function () {
						var string = $(this).text().trim().replace(/ +/g, "").toLowerCase();
						if (string.indexOf($val.trim().replace(/ +/g, "").toLowerCase()) > -1) {
							return $(this);
						}
					});
					var details = ele_details.filter(function () {
						var string = $(this).text().trim().replace(/ +/g, "").toLowerCase();
						if (string.indexOf($val.trim().replace(/ +/g, "").toLowerCase()) > -1) {
							return $(this);
						}
					});
					var status = ele_status.filter(function () {
						var string = $(this).text().trim().replace(/ +/g, "").toLowerCase();
						if (string.indexOf($val.trim().replace(/ +/g, "").toLowerCase()) > -1) {
							return $(this);
						}
					});
					
					
					if (numberplats.length) {
						numberplats.parents(".licence-plate-list-Parents").show();
					}
					if (details.length) {
						details.parents(".licence-plate-list-Parents").show();
					}
					if (status.length) {
						status.parents(".licence-plate-list-Parents").show();
					}
				}

			} else {
				tab_container.find(".licence-plate-list-Parents").show();
			}
		});
	
	$('.u-SearchFeedsearch ')
		.on('keyup', function () {
			var $this = $(this),
					$search = $this.parents(".control-search"),
					tab_container = $this.parents(".tab-container"),
					feeditems = tab_container.find(".feed-items"),
					$val = $this.val();
			
			feeditem = feeditems.find(".feed-item");		
			feeditem.alterClass('found_*');
					
			/*var status_selected = '';
			if (status_selected_element.length) {
				if (status_selected_element.text() != "ALL") {
					status_selected = status_selected_element.text();
				}
			}
			//console.log("status_selected ", status_selected);
			var ele_status_selected = platlists.find(".car-plate-status .car-plate-status-text:contains('" + status_selected + "')");
			//console.log("ele_status_selected ", ele_status_selected);

			if (ele_status_selected.length) {
				platlists = ele_status_selected.parents(".licence-plate-list-Parents");
			}
			*/
			console.log("feeditem ", feeditem);

			var ele_feedsummary = feeditem.find(".feed-summary"),
					ele_feedtitle = feeditem.find(".feed-title");

			if ("" !== $val) {
				//console.log("val", $val);
				var explodedval = $val.split(",");
				console.log("explodedval", explodedval)
				$this.parents(".option-container").find(".c-BtnClear").show();
				feeditem.removeClass("c-contact-found");

				feeditem.hide();
				
				if(explodedval.length > 1){
					var feedsummary = new Array();
					var feedtitle = new Array();
					var $classFounded = "";
					$.each(explodedval, function(i, val){
						var $val = val.trim().replace(/ +/g, "").toLowerCase();
						if("" !== $val){
							$class = "found_"+i;
							$classFounded += "."+$class;
							//console.info("val",val.trim().replace(/ +/g, "").toLowerCase());
							feedsummary[i] = ele_feedsummary.filter(function () {
								var string = $(this).text().trim().replace(/ +/g, "").toLowerCase();
								if (string.indexOf($val) > -1) {
									$(this).parents(".feed-item").addClass($class);
									return $(this).addClass("found");
								}
							});
							feedtitle[i] = ele_feedtitle.filter(function () {
								var string = $(this).text().trim().replace(/ +/g, "").toLowerCase();
								if (string.indexOf($val) > -1) {
									$(this).parents(".feed-item").addClass($class);
									return $(this).addClass("found");
								}
							});
						}


						
					});

					console.log("$classFounded",$classFounded);
					console.log(tab_container.find(".feed-item"+$classFounded));
					tab_container.find(".feed-item"+$classFounded).show();

					
				} else{
					var feedsummary = ele_feedsummary.filter(function () {
						var string = $(this).text().trim().replace(/ +/g, "").toLowerCase();
						if (string.indexOf($val.trim().replace(/ +/g, "").toLowerCase()) > -1) {
							return $(this);
						}
					});
					var feedtitle = ele_feedtitle.filter(function () {
						var string = $(this).text().trim().replace(/ +/g, "").toLowerCase();
						if (string.indexOf($val.trim().replace(/ +/g, "").toLowerCase()) > -1) {
							return $(this);
						}
					});
					
					
					if (feedsummary.length) {
						feedsummary.parents(".feed-item").show();
					}
					if (feedtitle.length) {
						feedtitle.parents(".feed-item").show();
					}
				}

			} else {
				tab_container.find(".feed-item").show();
			}
		});

	var PinLockInputSuccess = function ($parents, $msg) {
		$parents.find(".c-PinLock-Form-Box-Success-overlay").addClass("active").find(".c-Contentbox span").html($msg);
		setTimeout(function () {
			$parents.find(".c-PinLock-Form-Box-Success-overlay").removeClass("active");
		}, 4000);
	};
	var PinLockInputError = function ($parents, $msg) {
		$parents.find(".c-PinLock-Form-Box-Error-overlay").addClass("active").find(".c-Contentbox span").html($msg);
		setTimeout(function () {
			$parents.find(".c-PinLock-Form-Box-Error-overlay").removeClass("active");
		}, 4000);
	};
	$(".personvehiclepermit-PinLock-Box-overlay").on("keyup", ".c-PinLock-Form-Box-Input .c-PinLock-Form-Input", function () {
		var $this = $(this),
				$val = $.trim($this.val());
		if ("" !== $val) {
			$this.parents(".c-PinLock-Form-Box-Input").find(".c-Pinlock-Handle").addClass("active");
		} else {
			$this.parents(".c-PinLock-Form-Box-Input").find(".c-Pinlock-Handle").removeClass("active");
		}
	}).on("click", ".c-PinLock-Form-Box-Inputs .c-BtnPinlock-Clear", function () {
		var $this = $(this);
		$this.parents(".c-PinLock-Form-Box-Input").find(".c-PinLock-Form-Input").val("").trigger("keyup").focus();
		return false;
	}).on("click", ".c-PinLock-Form-Box-Inputs .c-BtnPinLock-GO", function () {
		var $this = $(this),
				$body = $('body'),
				$addPin = $body.attr('data-add-pin'),
				$masterPin = $body.attr('data-master-pin'),
				$parents = $this.parents(".c-PinLock-Box-overlay"),
				$pinInput = $parents.find(".c-PinLock-Form-Input"),
				$activeOption = fullScreenOverlay.find(".options.c-Options-Locked"),
				$pin = $pinInput.val(),
				//$action = "master";
				$action = "review";
		$parents.find(".c-PinLock-Form-Box-overlay").removeClass("active");
		if ("" == $pin) {
			PinLockInputError($parents, "Ooops! Pls add in your pinâ€¦");
		} else {
			$.post("", {'group-pin': 1, "action": $action, "password": $pin}, function (json) {
				if (json.status) {
					PinLockInputSuccess($parents, "Correct, though may pass!");
					if (json.master) {
						$body.attr({"data-GroupMasterPinAccepted": 1, "data-GroupReviewPinAccepted": 1});
					} else {
						$body.attr("data-GroupReviewPinAccepted", 1);
					}
					$(".pin-logout").addClass("u-loggedIn");
					$parents.removeClass("active");
					closePinLock($activeOption);
				} else {
					PinLockInputError($parents, "Ooops! Your pin is not correct, pls try again.");
					$pinInput.val('');
				}
			}, 'json');
		}
		return false;
	}).on("click", ".c-PinLock-Form-Buttons .c-PinLock-Form-Button-Review", function () {
		var $this = $(this),
				$body = $('body'),
				$parents = $this.parents(".c-PinLock-Box-overlay"),
				setActive = fullScreenOverlay.find(".options.active"),
				$pinInput = $parents.find(".c-PinLock-Form-Input");
		$parents.removeClass("active");
		setActive.find(".option-container").removeClass("u-TempHide");
		setActive.find(".c-Footer").removeClass("u-TempHide");
		setActive.find(".c-PinLock-Options-Box").addClass("active");
		return false;
	}).on("click", ".c-PinLock-Form-Buttons .c-PinLock-Form-Button-Skip", function () {
		var $this = $(this),
				//$parents = fullScreenOverlay.find(".options.c-Options-Locked");
				$parents = fullScreenOverlay.find(".options.active");
		$parents.find(".c-Footer a").trigger("click");
		return false;
	}).on("click", ".c-PinLock-Form-Box-overlay", function () {
		var $this = $(this),
				$parents = $this.parents(".c-PinLock-Box-overlay"),
				$pinInput = $parents.find(".c-PinLock-Form-Input");
		$this.removeClass("active");
		if ($parents) {
			$pinInput.trigger("focus").trigger("keyup");
		}
	});
	var PinLockOptionInput = function ($parents, $type, $msg) {
		$parents.find(".c-PinLock-Options-overlay").removeClass("active");
		var $overlay = $parents.find(".c-PinLock-Options-Error-overlay");
		if ($type == 'success') {
			$overlay = $parents.find(".c-PinLock-Options-Success-overlay");
		}
		$overlay.addClass("active").find(".c-Contentbox span").html($msg);
		setTimeout(function () {
			$overlay.removeClass("active");
		}, 4000);
	};
	$(".c-PinLock-Options-Box").on("keyup", ".c-PinLock-Options-Box-Input", function () {
		var $this = $(this),
				$val = $this.val(),
				$parents = $this.parents(".c-PinLock-Options-Box");
		if ("" !== $val) {
			$parents.find(".c-PinLock-Options-Box-Button-Review").addClass("active");
		} else {
			$parents.find(".c-PinLock-Options-Box-Button-Review").removeClass("active");
		}
	}).on("click", ".c-PinLock-Options-Box-Button-Skip", function () {
		var $this = $(this),
				$parents = $this.parents(".c-Options-Locked");
		$parents.find(".c-Footer a").trigger("click");
		return false;
	}).on("click", ".c-PinLock-Options-Box-Button-Review", function () {
		var $this = $(this),
				$body = $('body'),
				$addPin = $body.attr('data-add-pin'),
				$masterPin = $body.attr('data-master-pin'),
				$activeOption = $this.parents(".c-Options-Locked"),
				$parents = $this.parents(".c-PinLock-Options-Box"),
				$pinInput = $parents.find(".c-PinLock-Options-Box-Input"),
				$pin = $pinInput.val(),
				//$action = "master";
				$action = "review";
		if ("" == $pin) {
			PinLockOptionInput($parents, 'error', "Ooops! Pls add in your pinâ€¦");
		} else {
			$.post("", {'group-pin': 1, "action": $action, "password": $pin}, function (json) {
				if (json.status) {
					PinLockOptionInput($parents, 'success', "Correct, though may pass!");
					if (json.master) {
						$body.attr({"data-GroupMasterPinAccepted": 1, "data-GroupReviewPinAccepted": 1});
					} else {
						$body.attr("data-GroupReviewPinAccepted", 1);
					}
					$(".pin-logout").addClass("u-loggedIn");
					closePinLock($activeOption);
				} else {
					console.log("PinLockOptionInput");
					PinLockOptionInput($parents, 'error', "Ooops! Your pin is not correct, pls try again.");
					$pinInput.val('');
				}
			}, 'json');
		}
		return false;
	}).on("click", ".c-PinLock-Options-overlay", function () {
		var $this = $(this),
				$parents = $this.parents(".c-PinLock-Options-Box"),
				$pinInput = $parents.find(".c-PinLock-Options-Box-Input");
		$this.removeClass("active");
		if ($parents) {
			$pinInput.trigger("focus").trigger("keyup");
		}
	});
	var summaryLocationTimeInterval = setInterval(function () {
		var $summaryOptions = fullScreenOverlay.find(".c-Summary-Options"),
				$locationCirclesOption = $summaryOptions.find(".c-Location-CirclesOption"),
				$locationCirclesText = $locationCirclesOption.find(".c-CirclesText");
		if ($locationCirclesText.length) {
			var $locationCircleStep = $locationCirclesText.first(),
					$locationCircleNextStep = $locationCircleStep.next(),
					$locationCircleStepActive = $locationCirclesOption.find(".c-CirclesText.active");
			$locationCirclesText.fadeOut(1);
			if ($locationCircleStepActive.length) {
				$locationCircleStep = $locationCircleStepActive;
				if ($locationCircleStep.next(".c-CirclesText").length) {
					$locationCircleNextStep = $locationCircleStepActive.next(".c-CirclesText");
				} else {
					$locationCircleNextStep = $locationCirclesText.first();
				}
			}

			if (!$locationCircleNextStep.length) {
				$locationCircleNextStep = $locationCirclesText.first();
			}

			$locationCircleStepActive.removeClass("active");
			$locationCircleStep.fadeOut(300, function () {
				$locationCircleNextStep.fadeIn(300, function () {
					$(this).addClass("active");
				});
			});
		}

	}, 3000);


	var navigationTimeInterval = [];
	var navigationInterval = function () {
		if (navigationTimeInterval.length) {
			$.each(navigationTimeInterval, function (n) {
				clearInterval(navigationTimeInterval[n]);
			});
		}
		var $navigationOptions = fullScreenOverlay.find(".navigation-overlay .u-navigationCircle");
		var $first = [];
		$navigationOptions.find(".animate").removeClass("animate").hide();
		$navigationOptions.find(".u-navigationCircle-Span1:first-child").addClass("animate").show();
		$.each($navigationOptions, function (nth, $th) {
			var $this = $($th);
			$first[nth] = true;
			navigationTimeInterval[nth] = setInterval(function () {
				if ($this.hasClass("u-setDone")) {
					//$this.addClass("hovered");
					var $navigationCircles = $this.find(".u-navigationCircle-Span1");
					var $navigationCircleStep = $navigationCircles.first(),
							$navigationCircleNextStep = $navigationCircles.next(),
							$navigationCircleStepActive = $this.find(".u-navigationCircle-Span1.animate");
					$navigationCircles.fadeOut(1);
					if ($navigationCircleStepActive.length) {
						$navigationCircleStep = $navigationCircleStepActive;
						if ($navigationCircleStep.next(".u-navigationCircle-Span1").length) {
							$navigationCircleNextStep = $navigationCircleStepActive.next(".u-navigationCircle-Span1");
						} else {
							$navigationCircleNextStep = $navigationCircles.first();
						}
					}

					if (!$navigationCircleNextStep.length) {
						$navigationCircleNextStep = $navigationCircles.first();
					}

					if (!$first[nth] && $navigationCircleStep.index() == (parseInt($navigationCircles.length) - 1) && $this.hasClass("hovered")) {
						$this.removeClass("hovered");
					} else {
						$this.addClass("hovered");
						$navigationCircleStepActive.removeClass("animate");
						$navigationCircleStep.addClass("animating").fadeOut(300, function () {
							$navigationCircleStep.removeClass("animating");
							if (!$navigationCircleNextStep.hasClass("animating")) {
								$navigationCircleNextStep.addClass("animating").fadeIn(300, function () {
									$(this).addClass("animate").removeClass("animating");
								});
							} else {
								$(this).addClass("animate").removeClass("animating");
							}
						});
					}
					$first[nth] = false;
				}
			}, 3000);
		});
	};
	navigationInterval();

	fullScreenOverlay.find(".c-ExistGroupContact-Box-overlay").on("click", ".c-ExistGroupContact-Footer-Button", function () {
		var $this = $(this),
				$parentOverlay = $this.parents(".c-ExistGroupContact-Box-overlay"),
				$parentOptions = $parentOverlay.parents(".c-GroupBox-Options"),
				$activeGroup = $parentOptions.find(".existing-names a.active");

		if ($activeGroup.length) {
			$parentOverlay.removeClass("active");
			assignGroupDatatoPermit($activeGroup);
		}
		return false;
	}).on("click", ".c-ExistGroupContact-Footer-Button-Edit", function () {
		var $this = $(this),
				$parentOverlay = $this.parents(".c-ExistGroupContact-Box-overlay"),
				$parentOptions = $parentOverlay.parents(".c-GroupBox-Options"),
				$activeGroup = $parentOptions.find(".existing-names a.active");
		if ($activeGroup.length) {
			$parentOverlay.removeClass("active");
			$activeGroup.find(".u-Permit-GroupEdit").trigger("click");
		}
		return false;
	}).on("click", ".c-ExistGroupContact-Footer-Button-Cancel", function () {
		var $this = $(this),
				$parentOverlay = $this.parents(".c-ExistGroupContact-Box-overlay"),
				$parentOptions = $parentOverlay.parents(".c-GroupBox-Options"),
				$activeGroup = $parentOptions.find(".existing-names a.active");
		$parentOverlay.removeClass("active");
		deAssignGroupDatatoPermit($activeGroup);
		fullScreenOverlay.find(".u-navigationCircle-Group").removeClass("u-setDone");
		$activeGroup.removeClass("active");
		return false;
	});


	assignPermitData = function ($this, $parents) {
		var $vehicle = $parents.attr("data-vehicle"),
				$person = $parents.attr("data-person"),
				$group_uid = $parents.attr("data-group_uid"),
				vehicleOptions = fullScreenOverlay.find(".c-Vehicle-Options"),
				personOptions = fullScreenOverlay.find(".c-Person-Options"),
				groupBoxOptions = fullScreenOverlay.find(".c-GroupBox-Options");

		fullScreenOverlay.find(".u-TempHide").removeClass("u-TempHide");

		if (undefined !== $vehicle && "" != $vehicle) {
			var vehicleButton = vehicleOptions.find(".existing-names a").filter(function () {
				var string = $(this).text().trim().replace(/ +/g, "").toLowerCase();
				if (string == $vehicle.trim().replace(/ +/g, "").toLowerCase()) {
					return $(this);
				} else {
				}
			});
			if (vehicleButton.length) {
				vehicleButton.trigger("click");
			}
			vehicleOptions.find(".c-ExistContact-Box-overlay").removeClass("active");
		}
		if (undefined !== $person && "" != $person) {
			fullScreenOverlay.find(".options").removeClass("active");
			personOptions.addClass("active");
			var personButton = personOptions.find(".existing-names a").filter(function () {
				var string = $(this).text().trim().replace(/ +/g, "").toLowerCase();
				if (string == $person.trim().replace(/ +/g, "").toLowerCase()) {
					return $(this);
				} else {
				}
			});
			if (personButton.length) {
				setTimeout(function () {
					personButton.trigger("click");
					fullScreenOverlay.find(".u-navigationCircle-Person").addClass("u-setDone");
					checkNavigationStatus(fullScreenOverlay.find(".u-navigationCircle-Person"));
					personOptions.find(".c-ExistContact-Box-overlay").removeClass("active");
				}, 100);
			}
		}

		if (undefined !== $group_uid && "" != $group_uid) {
			fullScreenOverlay.find(".options").removeClass("active").hide();
			groupBoxOptions.addClass("active");
			var groupButton = groupBoxOptions.find(".existing-names a[data-group_uid='" + $group_uid + "']");
			if (personButton.length) {
				groupButton.trigger("click");
				setTimeout(function () {
					if (groupBoxOptions.find(".c-ExistGroupContact-Box-overlay").hasClass("active")) {
						groupBoxOptions.find(".c-ExistGroupContact-Box-overlay.active").removeClass("active");
						var GotoSummary = false;
						console.log("car-plate-edit ", $this.hasClass("car-plate-edit"));
						if ($this.hasClass("car-plate-edit")) {
							GotoSummary = true;
						} else if ($this.hasClass("car-plate-group")) {
							var windowHeight = $(window).height();
							var windowOffset = $(window).scrollTop();
							var offsetTop = (windowHeight / 2) - (groupBoxOptions.height() / 2) - getTopOffset; // + windowOffset;
							groupBoxOptions.css({top: offsetTop, display: 'block'}).addClass("active");
						}
						assignGroupDatatoPermit(groupButton, GotoSummary);
					} else {
						fullScreenOverlay.find(".u-navigationCircle").removeClass("active");
						fullScreenOverlay.find(".u-navigationCircle-Group").addClass("active");
						var windowHeight = $(window).height();
						var windowOffset = $(window).scrollTop();
						var offsetTop = (windowHeight / 2) - (groupBoxOptions.height() / 2) - getTopOffset; // + windowOffset;
						groupBoxOptions.css({top: offsetTop, display: 'block'}).addClass("active");
					}
					groupBoxOptions.find(".c-Footer-Button").addClass("active");
				}, 100);
			}
			personOptions.find(".c-ExistContact-Box-overlay").removeClass("active");
		}

		setTimeout(function () {
			if ($this.hasClass("car-plate-group")) {
				fullScreenOverlay.find(".u-navigationCircle-Group").trigger("click");
			} else if ($this.hasClass("car-plate-duration")) {
				fullScreenOverlay.find(".u-navigationCircle-PermitDuration").trigger("click");
			} else if ($this.hasClass("car-plate-daytime")) {
				fullScreenOverlay.find(".u-navigationCircle-DayTime").trigger("click");
			}  else if ($this.hasClass("car-plate-location")) {
				fullScreenOverlay.find(".u-navigationCircle-Location").trigger("click");
			} else if ($this.hasClass("car-plate-location")) {
				fullScreenOverlay.find(".u-navigationCircle-Location").trigger("click");
			}
		}, 1000);
	};


	var detachWhitelistDragHandle = function () {
		if ($(".c-Car-Plate-Box .licence-plate-list .car-plate").draggable()) {
			$(".c-Car-Plate-Box .licence-plate-list .car-plate").draggable('destroy');
		}
	};
	whitelistDragHandle = function () {
		var dragItems = $('.c-Car-Plate-Box .licence-plate-list .car-plate.car-plate-selected'),
				dropZones = $('#whitelist-bulkaction .dropzone');

		var selectedObjs;

		var offset_start = {};
		detachWhitelistDragHandle();

		dragItems.draggable({
			opacity: 0.7, /*helper: "clone",*/
			tolerance: "",
			cursorAt: {
				top: 34,
				left: 600
			},
			refreshPositions: true,
			helper: function (event, ui) {
				dragItems.removeClass("u-Dragged");

				var selected = dragItems;
				if (selected.length === 0) {
					var selected = $(this);
				}

				var reversed = selected.get().reverse();
				var container = $('<div/>').attr('id', 'draggingContainer');
				container.html($(reversed).clone());
				//container.css({position:"absolute", left:ui.offset.left-this.offsetLeft, top:ui.offset.top-this.offsetTop});;

				selected.addClass("u-Dragged");

				//setTimeout(function () {
				var $width = selected.width(),
						$height = selected.height() * selected.length;

				//console.log("helper => width : ", $width, " height : ", $height);
				var $arr = {
					left: Math.floor($width + ($width/4)),
					top: Math.floor($height / 2)
				};
				//console.info("helper => $arr ", $arr);
				$(this).draggable("option", "cursorAt", $arr);
				//}, 0);
				return container;
			},
			start: function (event, ui) {
				$(this).attr('data-moving', 'true');
				$(".licence-plate-list").css('z-index', 0);
				ui.helper.css('z-index', 101);
			},
			stop: function (event, ui) {
				$(this).attr('data-moving', '');
			},
			drag: function (event, ui) {
			}
		});
		dropZones.droppable({
			over: function (event, ui) {
				$(".licence-plate-list").css('z-index', 0);
				ui.helper.css('z-index', 101);
				$(this).attr("data-over", "over");
			},
			out: function (event, ui) {
				$(this).attr("data-over", "");

			},
			drop: function (event, ui) {
				//console.info($(this));
				$(this).attr("data-over", "");
				var draggedElement = ui.helper.children();
				//console.info(draggedElement);

				dragItems.removeClass("car-plate-selected");
				$("#whitelist-bulkaction").removeClass("whitelist-bulkaction-active");
				detachWhitelistDragHandle();

				var offset = addOverlay();
				extendOverlay('error', 'This action will be activated once connected via API to Rangers database. For now, pls use your imagination that this action has been performed with software grace & beauty');
			}
		});


	};
	//whitelistDragHandle();

	var toggelWhitelistDragBlock = function () {
		if ($(".licence-plate-list .car-plate-selected").length) {
			$("#whitelist-bulkaction").addClass("whitelist-bulkaction-active");
		} else {
			$("#whitelist-bulkaction").removeClass("whitelist-bulkaction-active")
		}
	};
	$(".licence-plate-list").on("click", ".u-car-plate-action", function () {
		var $this = $(this),
				$parents = $this.parents(".licence-plate-list-Parents"),
				$car_plate = $this.parents(".car-plate"),
				$section = $this.parents("section.tab-container"),
				$carPlateBox = $this.parents(".c-Car-Plate-Box"),
				$main = $parents.find(".c-Car-Plate-Main"),
				$inner = $parents.find(".c-Car-Plate-Inner");
		if ($this.hasClass("car-plate-person") || $this.hasClass("car-plate-history")) {
			var $inner_block = $inner.find(".c-Car-Plate-Inner-Block");
			var $active_block = $inner.find(".c-Car-Plate-Inner-Person");
			if ($this.hasClass("car-plate-history")) {
				$active_block = $inner.find(".c-Car-Plate-Inner-History");
			}
			if($inner.hasClass("u-show")){
				$inner.find(".car-plate-close").trigger("click");
			} else{
				$inner.addClass("u-show").show();
				$inner_block.slideUp(200, function () {
					$active_block.removeClass("c-Car-Plate-Inner-Active");
					$section.height('auto');
				});

				$active_block.slideDown(200, function () {
					$active_block.addClass("c-Car-Plate-Inner-Active");
					$section.height('auto');
				});
			}
		} else {
			var $personvehiclepermit = $('.personvehiclepermit-overlay'),
					$options = $personvehiclepermit.find('.options'),
					$permitButton = $options.find(".option[data-option='permit']");
			$('.overlay-trigger:first').trigger("click");
			assignPermitData($this, $car_plate);
			$permitButton.addClass("active");
		}
		return false;
	}).on("click", ".car-plate-close", function () {
		var $this = $(this),
				$parents = $this.parents(".licence-plate-list-Parents"),
				$main = $parents.find(".c-Car-Plate-Main"),
				$inner = $parents.find(".c-Car-Plate-Inner"),
				$inner_block = $inner.find(".c-Car-Plate-Inner-Block");

		$inner_block.slideUp(200, function () {
			$inner_block.removeClass("c-Car-Plate-Inner-Active");
			$inner.removeClass("u-show").slideUp();
		});
		return false;
	}).on("click", ".car-plate-delete", function () {
		var $this = $(this),
				$parents = $this.parents(".licence-plate-list-Parents"),
				$section = $this.parents("section.tab-container"),
				$main = $parents.find(".c-Car-Plate-Main"),
				$inner = $parents.find(".c-Car-Plate-Inner");
		$parents.fadeOut(200, function () {
			$parents.remove();
			$section.height('auto');
		});
		return false;
	}).on("click", ".car-plate-tick", function () {
		var $this = $(this),
				$parents = $this.parents(".car-plate");
			if($parents.hasClass("car-plate-selected")){
				$this.removeClass("u-licence-plate-selected").html("BULK ACTION");
			} else{
				$this.html("<i class='icon-tick'></i>");
			}
			$parents.toggleClass("car-plate-selected");
			toggelWhitelistDragBlock();
			//whitelistDragHandle();
		return false;
	});

	$(".c-Car-Plate-Box").on("click", ".u-More-CarPlates", function () {
		var $this = $(this),
				$tab_container = $this.parents(".tab-container"),
				$parents = $this.parents(".c-Car-Plate-Box");

		$parents.find(".licence-plate-list-Parents.u-Hide").removeClass("u-Hide");
		$this.hide();
		$tab_container.css({height: 'auto'});
		return false;
	}).on("click",".c-Car-Plate-Box-DrillDown-Button-Link", function(){
		var $this = $(this),
			$parents = $this.parents(".c-Car-Plate-Box"),
			$parentsDrillDownBox = $parents.find(".c-Car-Plate-Box-DrillDown");
		
		$parentsDrillDownBox.find(".c-Car-Plate-Box-DrillDown-Button-Link").removeClass("active");
		$this.addClass("active");
		return false;
	});

	$.expr[":"].contains = $.expr.createPseudo(function (arg) {
		return function (elem) {
			return $(elem).text().toUpperCase().indexOf(arg.toUpperCase()) >= 0;
		};
	});
	$('.c-Search-Small-Block .c-Search-Small-Block-Input').on('keyup', '.c-Search-Input.u-SearchWhiteList', function () {
		var $this = $(this),
				$search = $this.parents(".control-search"),
				tab_container = $this.parents(".tab-container"),
				platlists = tab_container.find(".licence-plate-list-Parents"),
				status_selected_element = $("#whitelist-live-controls").find(".control-filter.control-filter-selected"),
				$val = $this.val();

		var status_selected = '';
		if (status_selected_element.length) {
			if (status_selected_element.text() != "ALL") {
				status_selected = status_selected_element.text();
			}
		}
		console.log("status_selected ", status_selected);
		var ele_status_selected = platlists.find(".car-plate-status .car-plate-status-text:contains('" + status_selected + "')");
		console.log("ele_status_selected ", ele_status_selected);

		if (ele_status_selected.length) {
			platlists = ele_status_selected.parents(".licence-plate-list-Parents");
		}
		console.log("platlists ", platlists);

		var ele_numberplate = platlists.find(".car-plate-numberplate"),
				ele_details = platlists.find(".car-plate-value-spans .car-plate-small"),
				ele_status = platlists.find(".car-plate-status .car-plate-status-text");

		if ("" !== $val) {
			$search.find(".c-search-Clear-Handle").show();
			$this.parents(".option-container").find(".c-BtnClear").show();
			platlists.removeClass("c-contact-found");
			var numberplats = ele_numberplate.filter(function () {
				var string = $(this).text().trim().replace(/ +/g, "").toLowerCase();
				if (string.indexOf($val.trim().replace(/ +/g, "").toLowerCase()) > -1) {
					if (string == $val.trim().replace(/ +/g, "").toLowerCase()) {
						//$(this).parents(".summary-bar").addClass("c-contact-found");
					} else {
						//$(this).parents(".summary-bar").removeClass("c-contact-found");
					}
					return $(this);
				}
			});
			var details = ele_details.filter(function () {
				var string = $(this).text().trim().replace(/ +/g, "").toLowerCase();
				if (string.indexOf($val.trim().replace(/ +/g, "").toLowerCase()) > -1) {
					if (string == $val.trim().replace(/ +/g, "").toLowerCase()) {
						//$(this).parents(".summary-bar").addClass("c-contact-found");
					} else {
						//$(this).parents(".summary-bar").removeClass("c-contact-found");
					}
					return $(this);
				}
			});
			var status = ele_status.filter(function () {
				var string = $(this).text().trim().replace(/ +/g, "").toLowerCase();
				if (string.indexOf($val.trim().replace(/ +/g, "").toLowerCase()) > -1) {
					if (string == $val.trim().replace(/ +/g, "").toLowerCase()) {
						//$(this).parents(".summary-bar").addClass("c-contact-found");
					} else {
						//$(this).parents(".summary-bar").removeClass("c-contact-found");
					}
					return $(this);
				}
			});
			tab_container.find(".licence-plate-list-Parents").hide();
			if (numberplats.length) {
				numberplats.parents(".licence-plate-list-Parents").show();
			}
			if (details.length) {
				details.parents(".licence-plate-list-Parents").show();
			}
			if (status.length) {
				status.parents(".licence-plate-list-Parents").show();
			}

		} else {
			$search.find(".c-search-Clear-Handle").hide();
			tab_container.find(".licence-plate-list-Parents").show();
		}
	}).on('click', '.c-search-Clear-Handle', function () {
		var $this = $(this),
				$search = $this.parents(".c-Search-Small-Block-Input"),
				$input = $search.find("input[type=text]");
		$input.trigger("focusout").blur().val("").trigger("keyup");
		return false;
	});

	$("#whitelist-bulkaction")
		.on("click", ".whitelist-bulkaction-block-button", function(){
			var $this = $(this),
				items = $('.c-Car-Plate-Box .licence-plate-list .car-plate.car-plate-selected');
				
				items.find(".car-plate-tick").addClass("u-licence-plate-selected").html("<i class='icon-tick'></i>");
				items.removeClass("c-contact-selected");


				$(".whitelist-bulkaction-close").trigger("click");

				var offset = addOverlay();
				extendOverlay('error', 'This action will be activated once connected via API to Rangers database. For now, pls use your imagination that this action has been performed with software grace & beauty');
			return false;
		})
		.on("click", ".whitelist-bulkaction-close", function () {
			var $this = $(this),
					$parents = $this.parents("#whitelist-bulkaction");
			$parents.removeClass("whitelist-bulkaction-active");
			$(".car-plate-selected").find(".car-plate-tick").removeClass("u-licence-plate-selected").html("BULK ACTION");
			$(".car-plate-selected").removeClass("car-plate-selected")
			return false;
		});


	var toggelFeedDragBlock = function () {
		if ($(".tab-feed .feed-item.feed-item-selected").length) {
			$("#feed-bulkaction").addClass("bulkaction-active");
		} else {
			$("#feed-bulkaction").removeClass("bulkaction-active")
		}
	};
	$(".tab-feed")
		.on("click", ".u-Feed-BulkAction", function () {
			var $this = $(this),
					$parents = $this.parents(".feed-item");
				if($parents.hasClass("feed-item-selected")){
					$this.removeClass("u-feed-item-selected").html("BULK ACTION");
				} else{
					$this.html("<i class='icon-tick'></i>");
				}
				$parents.toggleClass("feed-item-selected");
				toggelFeedDragBlock();
			return false;
		});
		
	$("#feed-bulkaction")
		.on("click", ".feed-bulkaction-block-button", function(){
			var $this = $(this),
				items = $('.tab-feed .feed-item.feed-item-selected');
				
				items.find(".u-Feed-BulkAction").addClass("u-feed-item-selected").html("<i class='icon-tick'></i>");

				$(".feed-bulkaction-close").trigger("click");

				var offset = addOverlay();
				extendOverlay('error', 'This action will be activated once connected via API to Rangers database. For now, pls use your imagination that this action has been performed with software grace & beauty');
			return false;
		})
		.on("click", ".feed-bulkaction-close", function () {
			var $this = $(this),
					$parents = $this.parents("#feed-bulkaction");
			$parents.removeClass("bulkaction-active");
			$(".feed-item-selected").find(".u-Feed-BulkAction").removeClass("u-feed-item-selected").html("BULK ACTION");
			$(".feed-item-selected").removeClass("feed-item-selected");
			return false;
		});
			
	touchcarouselInt = function(){
		$('.tab-opened .touchcarousel').addClass("notbind").each(function(){
			var $this = $(this);
			if($this.parents(".licence-plate-list-Parents").length){
				var $parents = $this.parents(".licence-plate-list-Parents"),
					$parent = $this.parents(".car-plate-value-spans"),
					$touchcarousel_container = $parent.find(".touchcarousel-container");

				var $width = $touchcarousel_container.outerWidth()+10;
			} else{
				var $parent = $this,
					$touchcarousel_container = $parent.find(".touchcarousel-container");

					var margin = 28;
					if($(window).width() <= 430){
						margin = 5;
					}

				var $width = ($touchcarousel_container.find("li").length*margin)+($touchcarousel_container.find("li").length*$touchcarousel_container.find("li").width());
			}

			//console.log($touchcarousel_container.find("li").length, $touchcarousel_container.find("li").width())	
			//console.log($parent,  $width+" >= "+ $parent.width());
			
			if($width >= $parent.width()){
				//console.log($(this));
				$(this).touchCarousel({
					/* carousel options go here see Javascript Options section for more info */
					scrollbar: false,              // Scrollbar disabled.
					pagingNavControls: false,      // Paging controls (bullets).
					directionNav:false         // Direction (arrow) navigation (true or false).
				});
				$(this).removeClass("notbind");
			} else{
				$(this).addClass("notbind");
			}
		});
	};
	//touchcarouselInt();
	

	$('.tab-touchcarousel-block').each(function(){
		/*var $this = $(this),
			$touchcarousel_container = $this.find(".touchcarousel-container");

		var $width = ($touchcarousel_container.find("li").length*20)+($touchcarousel_container.find("li").length*$touchcarousel_container.find("li").width());
		
		if($width >= $this.width()){*/
			//console.log($(this));
			$(this).touchCarousel({
				/* carousel options go here see Javascript Options section for more info */
				scrollbar: false,              // Scrollbar disabled.
				pagingNavControls: false,      // Paging controls (bullets).
				directionNav:false         // Direction (arrow) navigation (true or false).
			});	
		/*} else{
			$(this).addClass("notbind");
		}*/
	});

	deleteOverlay = $(".deleteconfirm-overlay");
	addDeleteOverlay = function(){
		$('body').addClass('activate-full-screen');
		deleteOverlay.addClass('activate-overlay-screen');
		deleteOverlay.find('.inner-fullscreen-offset').css({position: 'absolute', top: $(window).scrollTop() + 50});
		deleteOverlay.removeClass("active");
		deleteOverlay.find(".options").attr("style", "");
	};
	function removeDeleteOverlay() {
		$('body').removeClass('activate-full-screen');
		deleteOverlay.removeClass('activate-overlay-screen');
		deleteOverlay.find(".personvehiclepermit").removeClass("active");
		deleteOverlay.find(".active").removeClass("active");
		deleteOverlay.find(".c-DeleteConfirm-Button-Yes").unbind("click");
	}

	openDeletePoup = function (){
		removeDeleteOverlay();
		addDeleteOverlay();
		var outerTarget = deleteOverlay.find('.widgetpopup').css({display: 'block'});
		var innerTarget = outerTarget.find('.c-DeleteConfirm-Options');
		var targetHeight = innerTarget.height();
		var windowHeight = $(window).height();
		var windowOffset = $(window).scrollTop();
		var offsetTop = (windowHeight / 2) - (targetHeight / 2) - 30; // + windowOffset;

		innerTarget.css({top: offsetTop, display: 'block'});
		outerTarget.fadeIn(800, function () {
			innerTarget.addClass("active");
		});
	};

	deleteOverlay
		.on("click", ".close-overlay", function(){
			removeDeleteOverlay();
			return false;
		})
		.on("click", ".c-DeleteConfirm-Button", function(){
			removeDeleteOverlay();
			return false;
		});
});