/** whitelist **/
$(document).ready(function () {
	$('.summary-bar-header .whitelist-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('.whitelist-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').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('.whitelist-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.addClass('active-car-plate');
			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');
		}
	});

	$('.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) {
			$(this).addClass('control-filter-selected').siblings().removeClass('control-filter-selected');
			e.preventDefault();
			e.stopPropagation();
		}
	});

	var dragItems = document.querySelectorAll('[draggable=true]');
	var dropZones = document.querySelectorAll('.dropzone');

	var draggedElement = null;
	var lastEntered = null;

	for (var i = 0; i < dragItems.length; i++) {
		dragItems[i].addEventListener('dragstart', function (e) {
			e.target.setAttribute('data-moving', 'true');
			e.dataTransfer.setData('text', e.target.getAttribute('data-id'));
			e.dataTransfer.effectAllowed = 'move';
			draggedElement = this;
		});
		// Event Listener for when the drag interaction finishes.
		dragItems[i].addEventListener('dragend', function (e) {
			e.target.setAttribute('data-moving', '');
			draggedElement = null;
		});
	}
	// Event Listener for when the dragged element is over the drop zone.
	for (var i = 0; i < dropZones.length; i++) {
		dropZones[i].addEventListener('dragover', function (e) {
			if (e.preventDefault) {
				e.preventDefault();
			}

			e.dataTransfer.dropEffect = 'move';

			return false;
		});
		// Event Listener for when the dragged element enters the drop zone.
		dropZones[i].addEventListener('dragenter', function (e) {
			lastEntered = this;
			this.setAttribute('data-over', "over");
		});

		// Event Listener for when the dragged element leaves the drop zone.
		dropZones[i].addEventListener('dragleave', function (e) {
			if (lastEntered !== e.target) {
				lastEntered.setAttribute('data-over', "");
			}
			this.setAttribute('data-over', "");
		});

		// Event Listener for when the dragged element dropped in the drop zone.
		dropZones[i].addEventListener('drop', function (e) {
			if (e.preventDefault) {
				e.preventDefault();
			}
			if (e.stopPropagation) {
				e.stopPropagation();
			}

			this.setAttribute('data-over', "");
			//this.innerHTML = "Dropped " + e.dataTransfer.getData('text');

			var offset = addOverlay();
			extendOverlay('done');

			lastEntered = null;

			// Remove the element from the list.
			//document.querySelector('#drag-elements').removeChild(elementDragged);

			return false;
		});
	}

	var fullScreenOverlay = $('.fullscreen-overlay');
	var innerOffsetBox = $('.inner-fullscreen-offset');

	var checkArrowStatus = function () {
		var $top = Math.max(0, (($(window).height() - $(".navigation-overlay").outerHeight()) / 2)) - 69 + "px";
		$(".navigation-overlay").css("top", $top);

		if (!$(".options.active").next(".options").length) {
			console.log("hide 7")
			$(".icon-arrow-down1").hide();
		} else {
			if ($(".options.active").next(".options").hasClass("c-option-done")) {
				console.log("hide 8")
				$(".icon-arrow-down1").hide();
			} else {
				$(".icon-arrow-down1").show();
			}
		}
		if (!$(".options.active").prev(".options").length) {
			$(".icon-arrow-up1").hide();
		} else {
			$(".icon-arrow-up1").show();
		}

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


		if ($(".options.active .c-CirclesOption").length) {
			console.log("hide 4")
			$(".icon-arrow-down1").hide();
		}




	};

	$('.close-overlay').bind({
		click: function () {
			removeOverlay();
			return false;
		}
	});

	$('.navigation-overlay a').not(".u-gotOSummary").bind({
		click: function () {
			var $this = $(this),
					$navigation = $this.attr("data-navigation"),
					$options = $(".options"),
					setOne = $(".options.active");

			$(".c-Footer a").removeClass("active");
			if ($navigation == 'up') {
				var setTwo = $(".options.active").prev(".options");
				setOne.removeClass("active").fadeOut(800, function () {
					var twoHeight = setTwo.height();
					var windowHeight = $(window).height();
					var offsetTwo = (windowHeight / 2) - (twoHeight / 2) - 69;// + 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) - 69;// + windowOffset;
						setTwo.css('top', offsetTwo).fadeIn(800, function () {
							setTwo.addClass("active");
							checkArrowStatus();
							lastSlideClose(setTwo);
							checkDateTimeFooterButton(setTwo);
							checkFooterButton(setTwo);
						});
					});
				}
			}
			return false;
		}
	});

	$('.overlay-trigger').bind({
		click: function () {
			removeOverlay();
			addOverlay();
			/*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) - 50;// + windowOffset;

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

			return false;
		}
	});

	$('.personvehiclepermit').on('click', '.option', function () {
		var $this = $(this);

		$this.parents('.options').find(".option").removeClass("active");
		$this.addClass("active");
		var setOne = $(".options.active");
		var setNext = $(".options.active").next(".options");
		setOne.removeClass("active").fadeOut(800, function () {
			var twoHeight = setNext.height();
			var windowHeight = $(window).height();
			var offsetTwo = (windowHeight / 2) - (twoHeight / 2) - 69;// + windowOffset;
			setNext.css('top', offsetTwo).fadeIn(800, function () {
				setNext.addClass("active");
				$('.navigation-overlay').addClass("active");
				checkArrowStatus();
			});
		});
		return false;
	}).on('click', '.c-Circle-Option', function () {
		var $this = $(this),
				$day = $this.attr("data-day");

		$(".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") {
				$(".c-Circle-Option").removeClass("active");
				$(".c-Circle-Option .c-OptionTimer").removeClass("active");
				removeDateCircleSummary();
				$(".c-Circle-Option").not($this).attr({"data-start": "00:00", "data-end": "00:00"});
				addCircleToSummary("all");
			} else {
				//$(".c-Circle-Option.c-hasActive").removeClass("active");

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

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

				addCircleToSummary($day);
			}

			$(".c-Circle-Option").removeClass("c-hasActive");
			$this.addClass("active c-hasActive");
			//setTimeout(function () {
			OpenInputBox($this.parents('.options').find(".c-TimerSelectBox"), $this);

			$('.c-TimerSelect-Save').unbind("click").bind("click", function () {
				$this.find(".c-OptionTimer").addClass("active");
				$this.addClass("active");
				setTimeValues($this);
				CloseInputBox($(".c-TimerSelectBox"));
				return false;
			});
			//}, 1000);


		}
		checkFooterButton();
		return false;
	}).on('click', '.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");
				$('.c-TimerSelect-Save').unbind("click").bind("click", function () {
					$this.parents('.c-Circle-Option.active').find(".c-OptionTimer").addClass("active");
					CloseInputBox($this.parents('.options').find(".c-TimerSelectBox"));
					setTimeValues($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-TimerSelect-Close', function () {
		var $this = $(this);
		CloseInputBox($(".c-TimerSelectBox"));
		if ($(".c-Circle-Option.c-hasActive").length) {
			setTimeValues($(".c-Circle-Option.c-hasActive"));
		}
		return false;
	}).on('click', '.c-TimerSelect-AllDay', function () {
		var $this = $(this),
				$CircleOption = $(".c-Circle-Option.c-hasActive"),
				$startTime = $CircleOption.attr("data-start"),
				$endTime = $CircleOption.attr("data-end"),
				$TimerSelectBox = $(".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 setOne = $(".options.active");
		var setNext = $(".options.active").next(".options");
		setOne.removeClass("active").fadeOut(800, function () {
			var twoHeight = setNext.height();
			var windowHeight = $(window).height();
			var offsetTwo = (windowHeight / 2) - (twoHeight / 2) - 69;// + windowOffset;
			setNext.css('top', offsetTwo).fadeIn(800, function () {
				setNext.addClass("active");
				$('.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");
		$(".c-PermitDuration-StartEndDateSelection").removeClass("active");
		if (!$this.hasClass("active")) {
			$this.addClass("active");

			$(".c-PermitDuration-StartEndDateSelection #permit-add-day").val($day);
			$(".c-PermitDuration-StartEndDateSelection #permit-add-month").val($month);
			$(".c-PermitDuration-StartEndDateSelection #permit-add-year").val($year);

			$(".c-PermitDuration-StartEndSelection").fadeOut(250, function () {
				$(".c-PermitDuration-StartEndDateSelection").fadeIn(250, function () {
					$(this).addClass("active");
				});
			});
		}
		return false;
	}).on("click", ".c-PermitDuration-StartEndDateSelection .c-PermitDuration-StartEndDateSelection-BtnSave", function () {
		var $this = $(this),
				$startDateButton = $("#permit-StartDate"),
				$endDateButton = $("#permit-EndDate"),
				$activeButton = $(".c-PermitDuration-StartEndSelection .c-add-input.active"),
				$day = $(".c-PermitDuration-StartEndDateSelection #permit-add-day").val(),
				$month = $(".c-PermitDuration-StartEndDateSelection #permit-add-month").val(),
				$year = $(".c-PermitDuration-StartEndDateSelection #permit-add-year").val();

		if (1 > $day || 31 < $day) {
			alert("Please enter valid 2 digit day number.");
		} else if (1 > $month || 12 < $month) {
			alert("Please enter valid 2 digit month number.");
		} else if ($year.length != 4) {
			alert("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);
			$(".c-PermitDuration-StartEndDateSelection").fadeOut(250, function () {
				$(this).removeClass("active");
				$(".c-PermitDuration-StartEndSelection").fadeIn(250, function () {
					$activeButton.removeClass("active");
					var $StartDate = $startDateButton.attr("data-date"),
							$EndDate = $endDateButton.attr("data-date");
					if ("" !== $StartDate || "" !== $EndDate) {
						$(".c-PermitDuration-StartEndSelection-BtnClear").show();
					}
				});
			});
		}

		return false;
	}).on("click", ".c-PermitDuration-StartEndDateSelection .c-PermitDuration-StartEndDateSelection-BtnClear", function () {
		var $this = $(this),
				$activeButton = $(".c-PermitDuration-StartEndSelection .c-add-input.active");
		$(".c-PermitDuration-StartEndDateSelection .c-add-input").val("");
		$activeButton.attr({"data-date": '', "data-day": '', "data-month": '', "data-year": ''}).text($activeButton.attr("data-text"));
		$(".c-PermitDuration-StartEndDateSelection").fadeOut(250, function () {
			$(this).removeClass("active");
			$(".c-PermitDuration-StartEndSelection").fadeIn(250, function () {
				$activeButton.removeClass("active");
			});
		});
		return false;
	}).on("click", ".c-PermitDuration-StartEndSelection .c-PermitDuration-StartEndSelection-BtnClear", function () {
		var $this = $(this),
				$startDateButton = $("#permit-StartDate"),
				$endDateButton = $("#permit-EndDate");

		$(".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");

		$(".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");
		$(".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 = $(".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 $StartMonth_str = month_name($StartMonth);
				var $EndMonth_str = month_name($EndMonth);

				$(".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>');

				$(".c-PermitDuration-StartEndSelection .c-PermitDuration-StartEndSelection-BtnClear").hide();
				$(".c-PermitDuration-StartEndSelection .c-PermitDuration-StartEndSelection-BtnAddNew").addClass("active");
				$(".c-PermitDuration-StartEndSelection .c-PermitDuration-StartEndSelection-BtnSave").addClass("active");


				existing_names_actions();
			}
		} else {
			DateError("Oops! We Can't add nothing, please try valid dates..");
		}
		checkDateTimeFooterButton();
		return false;
	}).on("keyup", ".c-TimerSelectBox .c-TimerSelection-Input", function () {
		var $this = $(this),
				$startTimeInput = $("#c-TimerSelect-StartTime"),
				$startTimeInput = $("#c-TimerSelect-EndTime"),
				$StartTime = $startTimeInput.val(),
				$EndTime = $startTimeInput.val();
		console.log($StartTime);
		if ("00:00" !== $StartTime || "00:00" !== $EndTime) {
			$(".c-TimerSelect-AllDay").removeClass("active");
		} else if ("" == $StartTime && "" == $EndTime) {

		}

		return false;
	});

	$(".c-Footer.c-DayTime-Footer").on("click", ".c-Footer-Button", function () {
		$('.navigation-overlay a.icon-arrow-down1').trigger("click");
		return false;
	});
	$(".c-Footer.c-DateTime-Footer").on("click", ".c-Footer-Button", function () {
		$('.navigation-overlay a.icon-arrow-down1').trigger("click");
		return false;
	});

	$(".c-CirclesOption").on('click', '.c-Circles', function () {
		var $this = $(this),
				$set = $this.attr("data-step");
		console.log($set);
		console.info($(".options.set-" + $set));
		if ($(".options.set-" + $set).length) {
			/*$("html, body").animate({ scrollTop: $(".personvehiclepermit .set-".$set).offset().top }, 600);*/
			var setOne = $this.parents(".options"),
					setTwo = $(".options.set-" + $set);

			setOne.removeClass("active").fadeOut(800, function () {
				var twoHeight = setTwo.height();
				var windowHeight = $(window).height();
				var offsetTwo = (windowHeight / 2) - (twoHeight / 2) - 69;// + windowOffset;
				setTwo.css('top', offsetTwo).fadeIn(800, function () {
					setTwo.addClass("active");
					checkArrowStatus();
					lastSlideClose(setTwo);
					$(".u-gotOSummary").addClass("active");
				});
			});
		}
		return false;
	});

	$(".u-gotOSummary").on('click', function () {
		var $this = $(this),
				setOne = $(".options.active"),
				setTwo = $(".options.set-summary");

		setOne.removeClass("active").fadeOut(800, function () {
			var twoHeight = setTwo.height();
			var windowHeight = $(window).height();
			var offsetTwo = (windowHeight / 2) - (twoHeight / 2) - 69;// + windowOffset;
			setTwo.css('top', offsetTwo).fadeIn(800, function () {
				setTwo.addClass("active");
				checkArrowStatus();
				lastSlideClose(setTwo);
				$(".u-gotOSummary").removeClass("active");
			});
		});
		return false;
	});

	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 $TimerSelectBox = $(".c-TimerSelectBox"),
				$startInput = $TimerSelectBox.find(".c-TimerSelect-StartTime"),
				$endInput = $TimerSelectBox.find(".c-TimerSelect-EndTime");
		var $start = "00:00";
		if ("" !== $startInput.val()) {
			$start = $startInput.val();
		}
		var $end = "00:00";
		if ("" !== $endInput.val()) {
			$end = $endInput.val();
		}

		$CircleOption.find(".c-OptionTimer").addClass("active");
		$CircleOption.attr({"data-start": $start, "data-end": $end});

		checkFooterButton();
	}
	function checkFooterButton() {
		if ($(".options.active").hasClass("set-three")) {
			if ($(".c-Circle-Option.active").length) {
				$(".c-DayTime-Footer .c-Footer-Button").addClass("active").fadeIn(200, function () {
				});
				$(".icon-arrow-down1").show();
			} else {
				$(".c-DayTime-Footer .c-Footer-Button").fadeOut(200, function () {
					$(this).removeClass("active");
				});
				console.log("hide 5")
				$(".icon-arrow-down1").hide();
			}
		}
	}
	function checkDateTimeFooterButton() {
		if($(".options.active").hasClass("set-two")){
			if ($(".u-DateTime.active").length) {
				$(".c-DateTime-Footer .c-Footer-Button").addClass("active").fadeIn(200, function () {
				});
				$(".icon-arrow-down1").show();
			} else {
				$(".c-DateTime-Footer .c-Footer-Button").fadeOut(200, function () {
					$(this).removeClass("active");
				});
				console.log("hide 6")
				$(".icon-arrow-down1").hide();
			}
		}
	}
	function OpenInputBox($TimerSelectBox, $CircleOption) {
		if ($(".c-TimerSelectBox").hasClass("active")) {
			CloseInputBox($(".c-TimerSelectBox"), $TimerSelectBox, $CircleOption);
		} else {
			var $float = $CircleOption.attr("data-float"),
					$set = $CircleOption.attr("data-set"),
					$top = (33 * ($set - 1)) + 2,
					$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 + "%").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");
				}

				setTimeout(function () {
					$this.find(".c-TimerSelection-Inputs-Box").fadeIn(100);
				}, 500);
			});
		}
	}
	function CloseInputBox($TimerSelectBox, $Open, $CircleOption) {
		var $TimerSelectBox = $(".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();
				$(".c-Circle-Option").not($CircleOption).removeClass("c-hasActive");
				if ($Open) {
					OpenInputBox($Open, $CircleOption);
				}
			}, 500);
		});
	}

	var lastSlideClose = function (setNext) {
		if (setNext.hasClass("c-option-done")) {
			$('.navigation-overlay').removeClass("active");
			setTimeout(function () {
				setNext.fadeOut(800, function () {
					removeOverlay();
				});
			}, 3000);
		} else {
			checkArrowStatus();
		}
	};
	var existing_names_actions = function () {
		$('.personvehiclepermit .existing-names a').on('click', function () {

			var $this = $(this),
					$val = $this.text(),
					$parents = $this.parents(".option-container"),
					$set = $this.parents('.options').attr("data-set");

			$this.parents(".existing-names").find("a").removeClass("active");
			$this.addClass("active");

			$(".c-Footer a").removeClass("active");

			if ($set == 'two') {
				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);

					$(".c-PermitDuration-StartEndSelection .c-PermitDuration-StartEndSelection-BtnAddNew").addClass("active");
					$(".c-PermitDuration-StartEndSelection .c-PermitDuration-StartEndSelection-BtnSave").addClass("active");

					$(".c-CirclesOption .c-Circles[data-step='" + $set + "'] .c-CirclesText .permit-startDate").html($StartMonth_str + " " + $StartDay + " " + $StartYear);
					$(".c-CirclesOption .c-Circles[data-step='" + $set + "'] .c-CirclesText .permit-endDate").html($EndMonth_str + " " + $EndDay + " " + $EndYear);
					checkDateTimeFooterButton();
				} else {
					$(".c-CirclesOption .c-Circles[data-step='" + $set + "'] .c-CirclesText .permit-startDate").html($val);
					$(".c-CirclesOption .c-Circles[data-step='" + $set + "'] .c-CirclesText .permit-endDate").html('');
				}
			} else {

				$parents.find(".personvehiclepermit-Box-overlay").removeClass("active");
				$this.parents('.options').find(".option").removeClass("active");
				$this.addClass("active");
				var setOne = $(".options.active");
				var setNext = $(".options.active").next(".options");
				setOne.removeClass("active").fadeOut(800, function () {
					var twoHeight = setNext.height();
					var windowHeight = $(window).height();
					var offsetTwo = (windowHeight / 2) - (twoHeight / 2) - 69;// + windowOffset;
					setNext.css('top', offsetTwo).fadeIn(800, function () {
						setNext.addClass("active");
						lastSlideClose(setNext);
					});
				});

				$parents.find(".c-add-input").val("").trigger("keyup").val($val);
				$parents.find(".c-BtnClear").show();

				$(".c-CirclesOption .c-Circles[data-step='" + $set + "'] .c-CirclesText span").html($val);
			}


			return false;

		});
	};
	existing_names_actions();

	$('.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){
				$(".u-DateTime").removeClass("active");
			}
		} else {
			$this.parents(".option-container").find(".c-BtnClear").hide();
			existing_name.removeClass("c-found").show();
		}

	});

	setInterval(function () {
		if ($(".existing-names a.c-found").length) {
			$(".existing-names a.c-found").removeClass("hvr-buzz-out-active").addClass("hvr-buzz-out-hover");
			setTimeout(function () {
				$(".existing-names a.c-found").removeClass("hvr-buzz-out-hover");
			}, 500);
		}
	}, 3000);

	$(".c-BtnClear").on("click", function () {
		var $this = $(this),
				$input = $this.parents(".option-container").find(".c-add-input");
		if ($input.length) {
			$input.val("").trigger("keyup").focus();
			$this.hide();
			$this.parents(".option-container").find(".existing-names .active").removeClass("active");
		}
		return false;
	});

	$('.personvehiclepermit .c-BtnSave').on('click', function () {
		var $this = $(this),
				existing_div = $this.parents(".option-container"),
				existing_name = existing_div.find(".existing-names"),
				$name = existing_div.find('.c-add-input').val();
		if ("" !== $name) {
			var element = existing_name.find("a").filter(function () {
				var string = $(this).text().trim().replace(/ +/g, "").toLowerCase();
				if (string != $name.trim().replace(/ +/g, "").toLowerCase()) {
				} else {
					return $(this);
				}
			});
			if (!element.length) {
				existing_name.append('<a href="#">' + $name + '</a>');
				$('.personvehiclepermit .c-add-input').val("").trigger("keyup");
				existing_names_actions();
			} 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 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');
		innerOffsetBox.css({position: 'absolute', top: $(window).scrollTop() + 50});
		$(".fullscreen-overlay").removeClass("active");
		$(".options").attr("style", "");
	}

	function removeOverlay() {
		$('body').removeClass('activate-full-screen');
		$(".personvehiclepermit").removeClass("active");
		$(".fullscreen-overlay").find(".active").removeClass("active");
		$(".fullscreen-overlay").find(".c-found").removeClass("c-found");
		$(".navigation-overlay a").not(".u-gotOSummary").show();
		$('.overlay-item').hide();
		$('.c-TimerSelectBox, .c-TimerSelection-Inputs-Box').hide();
		$(".c-DayTimes .c-hasActive").removeClass("c-hasActive");
	}

	function extendOverlay(action) {
		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});

			done.animate({opacity: 1}, 300, function () {
				setTimeout(function () {
					done.animate({opacity: 0}, 300, function () {
						removeOverlay();
					});
				}, 3000);
			});
		} 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");
		$(".c-Circles-DateTime .c-CirclesText .c-CirclesText-DateTime-Day[data-day=" + $day + "]").addClass("active");
	}
	function removeCircleToSummary($day) {
		$(".c-Circles-DateTime .c-CirclesText .c-CirclesText-DateTime-Day[data-day=" + $day + "]").removeClass("active");
	}
	function removeDateCircleSummary() {
		$(".c-Circles-DateTime .c-CirclesText .c-CirclesText-DateTime-Day").removeClass("active");
	}

});