$(document).ready(function () {

	var getTopOffset = 30;
	var fullScreenOverlay = $('.groupswidget-overlay');
	var innerOffsetBox = fullScreenOverlay.find('.inner-fullscreen-offset');

	var checkNavigationStatus = function () {
		var $option = $(".groupswidget-overlay .options.active"),
				$set = $option.attr("data-set");
		$(".groupswidget-overlay .navigation-overlay .active").removeClass("active");
		$(".groupswidget-overlay .u-navigationCircle[data-set='" + $set + "']").addClass("active");
		//$(".groupswidget-overlay .u-navigationCircle.active").prevAll().addClass("u-setDone");
	};

	$('.groupswidget-overlay .close-overlay').bind({
		click: function () {
			var $from = $(".groupswidget-overlay").attr("from");
			if (undefined != $from && "" != $from) {
				openFromWidget($from, false);
			} else {
				removeOverlay();
			}

			return false;
		}
	});

	$('.groupswidget-overlay .navigation-overlay a').not(".u-gotOSummary").bind({
		click: function () {
			var $this = $(this),
					$set = $this.attr("data-set"),
					$navigation = $this.attr("data-navigation"),
					$options = $(".groupswidget-overlay .options"),
					setActive = $(".groupswidget-overlay .options.active"),
					$activeSet = setActive.attr("data-set");
			if ($set == $activeSet) {
				return false;
			} else {
				var validate = true;
				//var validate = validateBlock($this, setActive);
				if (validate) {
					//$(".c-Footer a").removeClass("active");
					var setTwo = $(".groupswidget-overlay .options[data-set='" + $set + "']");
					setActive.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");
							checkNavigationStatus();
						});
					});
				}
			}
			return false;
		}
	});

	function validateBlock($this, $activeOption) {
		var $set = $this.attr("data-set"),
				$navigation = $this.attr("data-navigation"),
				$options = $(".groupswidget-overlay .options"),
				setActive = $(".groupswidget-overlay .options.active"),
				$activeSet = setActive.attr("data-set");

		var $result = true;
		console.info($this, $this.prev(".u-navigationCircle"), $this.prev(".u-navigationCircle").hasClass("u-setDone"));
		if ($this.prev(".u-navigationCircle").length) {
			if (!$this.prev(".u-navigationCircle").hasClass("u-setDone")) {
				$result = false;
			}
		} else if (!$this.hasClass("u-setDone")) {

			if ($(".groupswidget-overlay .options.active").find(".option").length) {
				if (!$(".groupswidget-overlay .options.active").find(".option.active").length) {
					$result = false;
				}
			} else if ($(".groupswidget-overlay .options.active").find(".existing-names").length) {
				if (!$(".groupswidget-overlay .options.active").find(".existing-names a.active").length) {
					$result = false;
				}
			} else if ($(".groupswidget-overlay .c-Circle-Option").length) {
				if (!$(".groupswidget-overlay .c-Circle-Option.active").length) {
					$result = false;
				}
			} else {

			}


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

	$('.c-Group-Widget-Button').bind({
		click: function () {
			removeOverlay();
			addOverlay();
			var outerTarget = fullScreenOverlay.find('.groupswidget').css({display: 'block'});

			var PinBlock = outerTarget.find('.c-Pin-Option-Selection'),
					ConfirmeBlock = outerTarget.find('.c-Confirmation-Option'),
					$isUpdate = fullScreenOverlay.attr('update'),
					$from = fullScreenOverlay.attr("from");

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

			fullScreenOverlay.find(".option-container-singlebox").find("#group-add").attr("disabled", false);
			fullScreenOverlay.find(".option-container-singlebox").find(".c-Clear-Handle, .c-BtnSave").removeClass("unseen");

			var tempOffset = getTopOffset;
			if ("" == $isPinNeed) {
				fullScreenOverlay.find(".navigation-overlay").hide();
				$('.groupswidget .set-one').css({display: 'none', top: 0});
				var innerTarget = PinBlock;
				tempOffset = 60;
			} else {
				if (undefined != $isUpdate && "" != $isUpdate && $from !== "assignwidget") {
					fullScreenOverlay.find(".navigation-overlay").hide();
					$('.groupswidget .set-one').css({display: 'none', top: 0});
					var innerTarget = ConfirmeBlock;
					tempOffset = 60;
				} else {
					fullScreenOverlay.find(".navigation-overlay").show();
					$('.groupswidget .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) - parseInt(tempOffset);// + windowOffset;

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

			return false;
		}
	});

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

		$this.parents('.options').find(".option").removeClass("active");
		$this.addClass("active");
		var setOne = $(".groupswidget-overlay .options.active");
		var setNext = $(".groupswidget-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;
			setNext.css('top', offsetTwo).fadeIn(800, function () {
				setNext.addClass("active");
				$('.groupswidget-overlay .navigation-overlay').addClass("active");
				checkNavigationStatus();
			});
		});
		return false;
	}).on('click', '.c-PermitDuration-Container .c-Circle-Option', function (e) {
		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");

		$(".groupswidget-overlay .c-PermitDuration-Container .c-Footer a").removeClass("active");
		//$(".groupswidget-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");
			removePermitDateCircleToGroup();
		} else {
			//$(".groupswidget-overlay .c-PermitDuration-Container .c-Circle-Option").removeClass("active hasActive");
			//$(".groupswidget-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 ($(".groupswidget-overlay .c-PermitDuration-Container .c-Circle-Option.c-hasActive").length) {
					setTimeValues($(".groupswidget .c-PermitDuration-Container .c-Circle-Option.c-hasActive"));
				}
				if ("" != $startDate && "" != $endDate) {
					addPermitDateCircleToGroup($startDate, $endDate);
				}
			}

			$(".groupswidget-overlay .c-PermitDuration-Container .c-Circle-Option").removeClass("c-hasActive");
			$this.addClass("c-hasActive");
			//setTimeout(function () {
			OpenPermitDateInputBox($this.parents('.options').find(".c-TimerSelectBox"), $this, e);

			$('.groupswidget-overlay .c-PermitDuration-Container .c-TimerSelect-Save').unbind("click").bind("click", function () {
				var valid = validatePermitDurationDates($this);
				$(".groupswidget-overlay .c-PermitDuration-Container .c-Circle-Option").removeClass("active c-hasActive");
				$(".groupswidget-overlay .c-PermitDuration-Container .c-Circle-Option .c-OptionTimer").removeClass("active c-hasActive");
				if (valid) {
					if (is_add) {
						$this.removeClass("active");
						addPermitDateCricle($this);
					} else {
						$this.find(".c-OptionTimer").addClass("active");
						$this.addClass("active");
						setPermitDateValues($this);
					}
					ClosePermitDateInputBox($(".groupswidget .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 () {
				OpenPermitDateInputBox($this.parents('.options').find(".c-TimerSelectBox"), $this.parents('.c-Circle-Option'), e);
				//$this.parents('.options').find(".c-TimerSelectBox").addClass("active");
				$('.groupswidget-overlay .c-PermitDuration-Container .c-TimerSelect-Save').unbind("click").bind("click", function () {
					var valid = validatePermitDurationDates($this);
					$(".groupswidget-overlay .c-PermitDuration-Container .c-Circle-Option").removeClass("active");
					if (valid) {
						$(".groupswidget-overlay .c-PermitDuration-Container .c-Circle-Option").removeClass("active");

						$this.parents('.c-Circle-Option').addClass("active");
						ClosePermitDateInputBox($this.parents('.options').find(".c-TimerSelectBox"));
						setPermitDateValues($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);
		ClosePermitDateInputBox($(".groupswidget .c-PermitDuration-Container .c-TimerSelectBox"));
		return false;
	})/*.on('click', '.c-PermitDuration-Container .c-TimerSelect-AllDay', function () {
	 var $this = $(this),
	 $CircleOption = $(".groupswidget-overlay .c-PermitDuration-Container .c-Circle-Option.c-hasActive"),
	 $startTime = $CircleOption.attr("data-start"),
	 $endTime = $CircleOption.attr("data-end"),
	 $TimerSelectBox = $(".groupswidget-overlay .c-PermitDuration-Container .c-TimerSelectBox"),
	 $startInput = $TimerSelectBox.find(".c-TimerSelect-StartTime"),
	 $endInput = $TimerSelectBox.find(".c-TimerSelect-EndTime");
	 
	 $startInput.val("");
	 $endInput.val("");
	 $this.addClass("active");
	 return false;
	 })*/.on('click', '.c-DayTimes .c-Circle-Option', function () {
		var $this = $(this),
				$day = $this.attr("data-day");

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

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

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

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



		}

		if ($(".groupswidget-overlay .c-Circles-DateTime .c-CirclesText .c-CirclesText-DateTime-Day.active").length) {
			$(".groupswidget-overlay .c-Circles-DateTime").parents(".c-DayTime-CirclesOption").removeClass("c-CircleOption-Empty");
		} else {
			$(".groupswidget-overlay .c-Circles-DateTime").parents(".c-DayTime-CirclesOption").addClass("c-CircleOption-Empty");
		}
		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 () {
				OpenGroupDayTimeInputBox($this.parents('.options').find(".c-TimerSelectBox"), $this.parents('.c-Circle-Option'));
				//$this.parents('.options').find(".c-TimerSelectBox").addClass("active");
				$('.groupswidget-overlay .c-DayTimes .c-TimerSelect-Save').unbind("click").bind("click", function () {
					$this.parents('.c-Circle-Option.active').find(".c-OptionTimer").addClass("active");
					CloseGroupDayTimeInputBox($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-DayTimes .c-TimerSelect-Close', function () {
		var $this = $(this);
		CloseGroupDayTimeInputBox($(".groupswidget .c-DayTimes .c-TimerSelectBox"));
		if ($(".groupswidget-overlay .c-DayTimes .c-Circle-Option.c-hasActive").length) {
			setTimeValues($(".groupswidget-overlay .c-DayTimes .c-Circle-Option.c-hasActive"));
		}
		return false;
	}).on('click', '.c-DayTimes .c-TimerSelect-AllDay', function () {
		var $this = $(this),
				$CircleOption = $(".groupswidget-overlay .c-DayTimes .c-Circle-Option.c-hasActive"),
				$startTime = $CircleOption.attr("data-start"),
				$endTime = $CircleOption.attr("data-end"),
				$TimerSelectBox = $(".groupswidget-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, .c-Done-Footer .c-Footer-Delete-Button", function () {
		var $this = $(this);
		var setOne = $(".groupswidget-overlay .options.active");
		var setNext = $(".groupswidget-overlay .options.c-option-done");
		var $from = $(".groupswidget-overlay").attr("from");
		var $error = false;
		if ($this.hasClass("c-Footer-Button")) {
			var $val = $(".groupswidget-overlay .c-Setone-Option-Selection #group-add").val();
			if ("" == $val) {
				$error = true;
				$(".groupswidget-overlay .c-Setone-Option-Selection .c-BtnSave").trigger("click");
			} else if ($(".groupswidget .c-Setone-Option-Selection .c-BtnSave").attr("saved") !== "true") {
				$error = true;
				//$(".groupswidget-overlay .c-Setone-Option-Selection .c-BtnSave").trigger("click");
				var existing_div = $(".groupswidget-overlay .c-Setone-Option-Selection .c-BtnSave").parents(".option-container");
				inputErrors(existing_div, "Please save group name first!");
			} else {
				/*if (!$(".groupswidget-overlay .c-CirclesOption:not(.c-CircleOption-Empty)").length) {
				 $error = true;
				 $this.parents(".c-Done-Footer").addClass("c-footerGroupError");
				 $this.text("PLEASE ENTER A CRITERIA");
				 setTimeout(function () {
				 $this.parents(".c-Done-Footer").removeClass("c-footerGroupError");
				 $this.text("SAVE");
				 }, 3000);
				 }*/
			}
		} else if ($this.hasClass("c-Footer-Delete-Button")) {
			$error = false;
			$(".personvehiclepermit-overlay .c-GroupBox-Options .existing-names a.active").remove();
		}

		if (!$error) {
			/*if (undefined == $from && "" == $from) {
			 openFromWidget($from, false);
			 } else {*/
			$(".groupswidget-overlay .u-navigationCircle.active").addClass("u-setDone");
			setOne.removeClass("active").fadeOut(800, function () {
				var twoHeight = setNext.height();
				var windowHeight = $(window).height();
				var offsetTwo = (windowHeight / 2) - (twoHeight / 2) - getTopOffset;// + windowOffset;
				var success = false;
				if ($this.hasClass("c-Footer-Button")) {
					setNext.find(".Done-text").text("SAVED");
					success = true;
				} else if ($this.hasClass("c-Footer-Cancel-Button")) {
					setNext.find(".Done-text").text("CANCELLED");
				}else if ($this.hasClass("c-Footer-Delete-Button")) {
					setNext.find(".Done-text").text("DELETED");
				}
				setNext.css('top', offsetTwo).fadeIn(800, function () {
					setNext.addClass("active");
					if (undefined != $from && "" != $from) {
						openFromWidget($from, success);
					} else {
						$('.groupswidget-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");
		$(".groupswidget .c-PermitDuration-StartEndDateSelection").removeClass("active");
		if (!$this.hasClass("active")) {
			$this.addClass("active");

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

			$(".groupswidget .c-PermitDuration-StartEndSelection").fadeOut(250, function () {
				$(".groupswidget .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 = $("#groups-StartDate"),
				$endDateButton = $("#groups-EndDate"),
				$activeButton = $(".groupswidget .c-PermitDuration-StartEndSelection .c-add-input.active"),
				$day = $(".groupswidget .c-PermitDuration-StartEndDateSelection #groups-add-day").val(),
				$month = $(".groupswidget .c-PermitDuration-StartEndDateSelection #groups-add-month").val(),
				$year = $(".groupswidget .c-PermitDuration-StartEndDateSelection #groups-add-year").val();
		var DateError = function ($error) {
			optioncontainer.find(".groupswidget-Box-overlay").addClass("active").find(".c-Contentbox span").html($error);
			setTimeout(function () {
				optioncontainer.find(".groupswidget-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);
			$(".groupswidget .c-PermitDuration-StartEndDateSelection").fadeOut(250, function () {
				$(this).removeClass("active");
				$(".groupswidget .c-PermitDuration-StartEndSelection").fadeIn(250, function () {
					$activeButton.removeClass("active");
					var $StartDate = $startDateButton.attr("data-date"),
							$EndDate = $endDateButton.attr("data-date");
					if ("" !== $StartDate || "" !== $EndDate) {
						$(".groupswidget .c-PermitDuration-StartEndSelection-BtnClear").show();
					}
				});
			});
		}

		return false;
	}).on("click", ".c-PermitDuration-StartEndDateSelection .c-PermitDuration-StartEndDateSelection-BtnClear", function () {
		var $this = $(this),
				existing_div = $this.parents(".option-container").find(".existing-div"),
				$activeButton = $(".groupswidget .c-PermitDuration-StartEndSelection .c-add-input.active");
		$(".groupswidget .c-PermitDuration-StartEndDateSelection .c-add-input").val("");
		$activeButton.attr({"data-date": '', "data-day": '', "data-month": '', "data-year": ''}).text($activeButton.attr("data-text"));
		$(".groupswidget .c-PermitDuration-StartEndDateSelection").fadeOut(250, function () {
			$(this).removeClass("active");
			$(".groupswidget .c-PermitDuration-StartEndSelection").fadeIn(250, function () {
				$activeButton.removeClass("active");
			});
		});

		if (existing_div.find(".u-DateTime").length) {
			$(".groupswidget .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 = $("#groups-StartDate"),
				$endDateButton = $("#groups-EndDate");

		$(".groupswidget .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 = $("#groups-StartDate"),
				$endDateButton = $("#groups-EndDate");

		$(".groupswidget .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");
		$(".groupswidget .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 = $(".groupswidget .c-PermitDuration-StartEndSelection .c-add-input"),
				existing_div = $this.parents(".option-container"),
				existing_name = existing_div.find(".existing-names"),
				$startDateButton = $("#groups-StartDate"),
				$endDateButton = $("#groups-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(".groupswidget-Box-overlay").addClass("active").find(".c-Contentbox span").html($error);
			setTimeout(function () {
				optioncontainer.find(".groupswidget-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);

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

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

					existing_groupswidget_names_actions();
				}
			}
		} else {
			DateError("Oops! We Can't add nothing, please try valid dates..");
		}
		checkDateTimeFooterButton();
		return false;
	}).on("keyup", ".c-DayTimes .c-TimerSelectBox .c-TimerSelection-Input", function () {
		var $this = $(this),
				$startTimeInput = $(".groupswidget .c-DayTimes .c-TimerSelect-StartTime"),
				$startTimeInput = $(".groupswidget .c-DayTimes .c-TimerSelect-EndTime"),
				$StartTime = $startTimeInput.val(),
				$EndTime = $startTimeInput.val();
		if ("00:00" !== $StartTime || "00:00" !== $EndTime) {
			$(".groupswidget .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 (!$parents.find(".option-container-firstbox").hasClass("isHidden")) {
			$parents.find(".option-container-firstbox").fadeOut(200, function () {
				$parents.find(".option-container-secondbox").fadeIn(200);
			});
		}
		return false;
	});

	$(".groupswidget-overlay .c-Footer.c-DayTime-Footer").on("click", ".c-Footer-Button", function () {
		$('.groupswidget-overlay .navigation-overlay a.u-navigationCircle.c-GroupsSummaryCircle').trigger("click");
		checkNavigationStatus();
		return false;
	});
	$(".groupswidget-overlay .c-Footer.c-DateTime-Footer").on("click", ".c-Footer-Button", function () {
		$('.groupswidget-overlay .navigation-overlay a.u-navigationCircle.c-GroupsSummaryCircle').trigger("click");
		return false;
	});
	$(".groupswidget-overlay .c-Footer.c-PermitDate-Footer").on("click", ".c-Footer-Button", function () {
		$('.groupswidget-overlay .navigation-overlay a.u-navigationCircle.c-GroupsSummaryCircle').trigger("click");
		return false;
	});

	$(".groupswidget-overlay .c-CirclesOption").on('click', '.c-Circles', function () {
		var $this = $(this),
				$set = $this.attr("data-step");

		if ($(".groupswidget-overlay .options.set-" + $set).length) {
			/*$("html, body").animate({ scrollTop: $(".groupswidget .set-".$set).offset().top }, 600);*/
			var setOne = $this.parents(".options"),
					setTwo = $(".groupswidget-overlay .options.set-" + $set);

			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");
					checkNavigationStatus();
					lastSlideClose(setTwo);
				});
			});
		}
		return false;
	});

	$(".groupswidget-overlay .u-gotOSummary").on('click', function () {
		var $this = $(this),
				setOne = $(".groupswidget-overlay .options.active"),
				setTwo = $(".groupswidget-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");
				lastSlideClose(setTwo);
				$(".groupswidget-overlay .u-gotOSummary").removeClass("active");
			});
		});
		return false;
	});



	function openFromWidget($from, $saved, $cancelled) {
		var $update = $(".groupswidget-overlay").attr("update"),
				overlay = $("." + $from + "-overlay"),
				inner_overlay = overlay.find(".inner-fullscreen-offset"),
				$isbackTofirst = fullScreenOverlay.attr('backTofirst');
		if ($saved) {
			var $assignwidget_overlay = $(".assignwidget-overlay"),
					$AssignGroupList = $assignwidget_overlay.find(".c-AssignContact-Groups .c-AssignContact-Box-Contacts-List"),
					$GroupName = $.trim(fullScreenOverlay.find("#group-add").val()),
					$GroupSummary = fullScreenOverlay.find(".c-Setone-Option-Selection .c-CirclesOption:not(.c-CircleOption-Empty)"),
					$savedData = groupSavedAttriblutes($GroupSummary),
					$uid = Math.floor((Math.random() * 1000) + 1);
			if (undefined != $update && "" != $update) {
				var $assignGroup = $(".assignwidget-overlay .c-AssignContact-Groups .c-AssignContact-Box-Contacts-Item[data-uid=" + $update + "]");
				if ($assignGroup) {
					$assignGroup.html('<a ' + $savedData.attributes + ' href="#">' + $GroupName + '<span class="icon-arrow-right2"></span></a><span class="c-AssignContact-Clear icon-cross" href="#"></span>');
					$assignGroup.find("a").trigger("click");

					$(".personvehiclepermit-overlay .c-GroupBox-Options .existing-names a[data-group_uid='" + $update + "']").replaceWith('<a ' + $savedData.attributes + ' data-group_uid="' + $update + '" data-group_name="' + $GroupName + '" href="#" ><span class="c-Cycle-GroupDetails-Criteria"><span class="c-Cycle-GroupDetails-Criteria-Item c-Cycle-GroupDetails-Criteria-Step c-Cycle-GroupDetails-Criteria-FirstStep CriteriaActive" data-step="1">' + $GroupName + '</span></span>   <span class="u-Permit-GroupEdit">EDIT</span></a>');

					if ("" !== $savedData.html) {
						$(".personvehiclepermit-overlay .c-GroupBox-Options .existing-names a[data-group_uid='" + $update + "'] .c-Cycle-GroupDetails-Criteria").append($savedData.html);
					}

					animateGroupCriteria();
					existing_personvehiclepermit_names_actions();
					$(".personvehiclepermit-overlay .c-GroupBox-Options .existing-names a[data-group_uid='" + $update + "']").trigger("click");
				}
			} else {
				var groupHTML = '<div data-uid=' + $uid + ' id="group-' + $uid + '" class="c-AssignContact-Box-Contacts-Item"><a  ' + $savedData.attributes + ' href="#">' + $GroupName + '<span class="icon-arrow-right2"></span></a><span class="c-AssignContact-Clear icon-cross" href="#"></span></div>';
				$AssignGroupList.append(groupHTML);

				dragAssignHandle();
				moveContact($(".c-AssignContact-Container .u-Dragged"), $AssignGroupList.find(".c-AssignContact-Box-Contacts-Item:last"));

				// append same group to permit widget as well
				$(".personvehiclepermit-overlay .c-GroupBox-Options .existing-names").append('<a ' + $savedData.attributes + ' href="#" data-group_uid="' + $uid + '" data-group_name="' + $GroupName + '"><span class="c-Cycle-GroupDetails-Criteria"><span class="c-Cycle-GroupDetails-Criteria-Item c-Cycle-GroupDetails-Criteria-Step c-Cycle-GroupDetails-Criteria-FirstStep CriteriaActive" data-step="1">' + $GroupName + '</span></span>  <span class="u-Permit-GroupEdit">EDIT</span></a>');

				if ("" !== $savedData.html) {
					$(".personvehiclepermit-overlay .c-GroupBox-Options .existing-names a[data-group_uid='" + $uid + "'] .c-Cycle-GroupDetails-Criteria").append($savedData.html);
				}

				animateGroupCriteria();
				existing_personvehiclepermit_names_actions();
				$(".personvehiclepermit-overlay .c-GroupBox-Options .existing-names a[data-group_uid='" + $uid + "']").trigger("click");
			}
		} else {
			if (undefined != $update && "" != $update) {
				//$(".personvehiclepermit-overlay .c-GroupBox-Options .existing-names a[data-group_uid='" + $update + "']").remove();
			} else {
			}

			console.info("$isbackTofirst ", $isbackTofirst);
			if ("" !== $isbackTofirst && undefined !== $isbackTofirst) {
				if ($from == "personvehiclepermit") {
					overlay.find(".navigation-overlay .u-navigationCircle:first").trigger("click");
				}
			}
		}
		fullScreenOverlay.attr('backTofirst', "");


		$(".groupswidget-overlay").attr({"from": "", "update": ""}).fadeOut(function () {
			$('.groupswidget-overlay .close-overlay').trigger("click");
			overlay.attr("hidden", "").fadeIn();
			inner_overlay.fadeIn();
			$("body").addClass("activate-full-screen");

			var twoHeight = overlay.find(".options.active").height();
			var windowHeight = $(window).height();
			var offsetTwo = (windowHeight / 2) - (twoHeight / 2) - getTopOffset;// + windowOffset;
			overlay.find(".options.active").css('top', offsetTwo);
		});

	}

	function getPermitGroupStepHtml($step, $CriteriaName, $CriteriaValue) {
		var $active = ($step == 1) ? "CriteriaActive" : "";
		return '<span class="c-Cycle-GroupDetails-Criteria-Item c-Cycle-GroupDetails-Criteria-Step c-Cycle-GroupDetails-Criteria-Step-Animated ' + $active + '" data-step=' + $step + '><span class="c-Cycle-GroupDetails-Criteria-Item-Name">' + $CriteriaName + '</span><span class="c-Cycle-GroupDetails-Criteria-Item-Value">' + $CriteriaValue + '</span></span>';
	}

	var groupSavedAttriblutes = function ($GroupSummary) {
		var $response = {"attributes": "", "html": ""};
		$GroupSummary.each(function (e, ele) {
			var $this = $(ele),
					$Circles = $this.find(".c-Circles");

			var $hideStatus = $this.find(".c-CircleOption-Hide.icon-hide2");
			var $padlockStatus = $this.find(".c-CircleOption-Padlock.c-Padlock-Active");

			var $step = 2;
			if ($Circles.hasClass("c-Circles-PermitDuration")) {
				var $PermitDurationContainer = $(".groupswidget-overlay .c-PermitDuration-Container"),
						$activeCircle = $PermitDurationContainer.find(".c-Circle-Option.active"),
						startDate = $GroupSummary.find(".groups-startDate").text(),
						endDate = $GroupSummary.find(".groups-endDate").text();
				if ("" !== startDate) {
					var date_filter = "", date_number = "";
					if ($activeCircle.length) {
						date_filter = $activeCircle.attr("data-date_filter");
						date_number = $activeCircle.attr("data-date_number");
					}
					var start = $activeCircle.attr("data-startdate"),
							end = $activeCircle.attr("data-enddate");
					$response.attributes += 'data-durationstart="' + startDate + '" data-durationend="' + endDate + '" data-durationdatefilter="' + date_filter + '" data-durationdatenumber="' + date_number + '" data-durationstartdate="' + start + '" data-durationenddate="' + end + '" ';
					var $value = "" + startDate + " - " + endDate + "";
					if (!$hideStatus.length) {
						$response.html += getPermitGroupStepHtml($step, "DURATION", $value);
					}
				}

				if ($hideStatus.length) {
					$response.attributes += 'data-hide_duration="1" ';
				}
				if ($padlockStatus.length) {
					$response.attributes += 'data-lock_duration="1" ';
				}
			} else if ($Circles.hasClass("c-Circles-DateTime")) {

				var days = $Circles.find(".c-CirclesText-DateTime-Day.active"), steDay = "", steDayTimes = "";
				days.each(function (e, $days) {

					var $day = $($days).attr("data-day"),
							$start = $($days).attr("data-start"),
							$end = $($days).attr("data-end");
					steDay += $day + ",";
					steDayTimes += 'data-daytime-day="' + $day + '" data-daytime-' + $day + '-start="' + $start + '" data-daytime-' + $day + '-end="' + $end + '" ';
				});
				steDay = steDay.slice(0, -1);
				$response.attributes += 'data-daytime="' + steDay + '" ';

				if ($hideStatus.length) {
					$response.attributes += 'data-hide_daytime="1" ';
				} else {
					$step = parseInt($step) + 1;
					$response.html += getPermitGroupStepHtml($step, "DAY & TIME", steDay);
				}
				if ($padlockStatus.length) {
					$response.attributes += 'data-lock_daytime="1" ';
				}
				$response.attributes += steDayTimes;
			} else if ($Circles.hasClass("c-Circles-Person")) {
				var value = $.trim($Circles.find(".c-CircleOption-Filled").text());

				$response.attributes += 'data-person="' + value + '" ';


				if ($hideStatus.length) {
					$response.attributes += 'data-hide_person="1" ';
				} else {
					$step = parseInt($step) + 1;
					$response.html += getPermitGroupStepHtml($step, "DAY & TIME", value);
				}
				if ($padlockStatus.length) {
					$response.attributes += 'data-lock_person="1" ';
				}
			} else if ($Circles.hasClass("c-Circles-Vehicle")) {
				var value = $.trim($Circles.find(".c-CircleOption-Filled").text());

				$response.attributes += 'data-vehicle="' + value + '" ';
				if ($hideStatus.length) {
					$response.attributes += 'data-hide_vehicle="1" ';
				} else {
					$step = parseInt($step) + 1;
					$response.html += getPermitGroupStepHtml($step, "VEHICLE", value);
				}
				if ($padlockStatus.length) {
					$response.attributes += 'data-lock_vehicle="1" ';
				}
			} else if ($Circles.hasClass("c-Circles-Location")) {
				var Filled = ($Circles.find(".c-CircleOption-Filled .c-CirclesText")),
						value = '';
				Filled.each(function (i, $ths) {
					value += $.trim($($ths).find("span").text()) + ',';
				});
				var val = (value.substring(0, value.length - 1));
				$response.attributes += 'data-location="' + val + '" ';

				if ($hideStatus.length) {
					$response.attributes += 'data-hide_location="1" ';
				} else {
					$step = parseInt($step) + 1;
					$response.html += getPermitGroupStepHtml($step, "VEHICLE", val);
				}
				if ($padlockStatus.length) {
					$response.attributes += 'data-lock_location="1" ';
				}
			}
			$hideStatus.addClass("icon-hide1").removeClass("icon-hide2");
			$padlockStatus.removeClass("c-Padlock-Active");
		});

		$response.attributes += 'data-group_name="' + fullScreenOverlay.find("#group-add").val() + '" ';
		return $response;
	};

	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)];
	}

	function addPermitDateCricle($addCircleOption) {
		var $TimerSelectBox = $(".groupswidget-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 = $(".groupswidget .c-PermitDuration-Container .c-Circle-Option").last(),
				$lastSet = $lastOption.attr("data-set"),
				$lastFloat = $lastOption.attr("data-float");

		var $newSet = $lastSet;
		if ($(".groupswidget .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>';
		$(".groupswidget .c-PermitDuration-Container .c-Circle-Option-Box").append($CircleOption);

		addPermitDateCircleToGroup($startInput.val(), $endInput.val());
	}
	function validatePermitDurationDates($CircleOption) {
		var $TimerSelectBox = $(".groupswidget-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(".groupswidget-Box-overlay").addClass("active").find(".c-Contentbox span").html($error);
			setTimeout(function () {
				$TimerSelectBox.find(".groupswidget-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 setPermitDateValues($CircleOption) {
		var $TimerSelectBox = $(".groupswidget-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()});

		addPermitDateCircleToGroup($startInput.val(), $endInput.val());
	}
	function setTimeValues($CircleOption) {
		var $TimerSelectBox = $(".groupswidget-overlay .c-DayTimes .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});

		$(".groupswidget-overlay .c-Circles-DateTime .c-CirclesText .c-CirclesText-DateTime-Day[data-day=" + $CircleOption.attr("data-day") + "]").attr({"data-start": $start, "data-end": $end});

		checkFooterButton();
	}
	function checkFooterButton() {
		if ($(".groupswidget-overlay .options.active").hasClass("c-DayTime-Options")) {
			var $set = $(".groupswidget-overlay .options.active").attr("data-set");
			if ($(".groupswidget-overlay .c-DayTime-Options .c-Circle-Option.active").length) {
				$(".groupswidget-overlay .c-DayTime-Footer .c-Footer-Done-Button").addClass("active");
				$(".groupswidget-overlay .c-DayTime-Footer .c-Footer-Button").addClass("active").fadeIn(200, function () {
				});
				/*$(".icon-arrow-down1").show();*/
				$(".groupswidget-overlay .u-navigationCircle.active").addClass("u-setDone");
			} else {
				$(".groupswidget-overlay .c-DayTime-Footer .c-Footer-Button").fadeOut(200, function () {
					$(this).removeClass("active");
					$(".groupswidget-overlay .c-DayTime-Footer .c-Footer-Done-Button").removeClass("active");
				});
				/*$(".icon-arrow-down1").hide();*/
				$(".groupswidget-overlay .u-navigationCircle.active").removeClass("u-setDone");
			}
		}
	}
	function checkPermitDateFooterButton() {
		if ($(".groupswidget-overlay .options.active").hasClass("c-Permit-Options")) {
			var $set = $(".groupswidget-overlay .options.active").attr("data-set");
			if ($(".groupswidget-overlay .c-PermitDuration-Container .c-Circle-Option.active").length) {
				$(".groupswidget-overlay .c-PermitDate-Footer .c-Footer-Done-Button").addClass("active");
				$(".groupswidget-overlay .c-PermitDate-Footer .c-Footer-Button").addClass("active").fadeIn(200, function () {

				});
				/*$(".icon-arrow-down1").show();*/
				$(".groupswidget-overlay .u-navigationCircle.active").addClass("u-setDone");
			} else {
				$(".groupswidget-overlay .c-PermitDate-Footer .c-Footer-Button").fadeOut(200, function () {
					$(this).removeClass("active");
					$(".groupswidget-overlay .c-PermitDate-Footer .c-Footer-Done-Button").removeClass("active");
				});
				/*$(".icon-arrow-down1").hide();*/
				$(".groupswidget-overlay .u-navigationCircle.active").removeClass("u-setDone");
			}
		}
	}
	function checkDateTimeFooterButton() {
		if ($(".groupswidget-overlay .options.active").hasClass("c-Permit-Options")) {
			if ($(".groupswidget-overlay .u-DateTime.active").length) {
				$(".groupswidget-overlay .c-DateTime-Footer .c-Footer-Done-Button").addClass("active");
				$(".groupswidget-overlay .c-DateTime-Footer .c-Footer-Button").addClass("active").fadeIn(200, function () {
				});
				/*$(".icon-arrow-down1").show();*/
				$(".groupswidget-overlay .u-navigationCircle.active").addClass("u-setDone");
			} else {
				$(".groupswidget-overlay .c-DateTime-Footer .c-Footer-Button").fadeOut(200, function () {
					$(this).removeClass("active");
					$(".groupswidget-overlay .c-DateTime-Footer .c-Footer-Done-Button").removeClass("active");
				});
				/*$(".icon-arrow-down1").hide();*/
				$(".groupswidget-overlay .u-navigationCircle.active").removeClass("u-setDone");
			}
		}
	}
	function OpenGroupDayTimeInputBox($TimerSelectBox, $CircleOption) {
		if ($(".groupswidget-overlay .c-DayTimes .c-TimerSelectBox").hasClass("active")) {
			CloseGroupDayTimeInputBox($(".groupswidget-overlay .c-DayTimes .c-TimerSelectBox"), $TimerSelectBox, $CircleOption);
		} else {
			var $float = $CircleOption.attr("data-float"),
					$set = $CircleOption.attr("data-set"),
					//$top = (33 * ($set - 1)),
					$top = ($CircleOption.position().top),
					$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");
				}

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


	function OpenPermitDateInputBox($TimerSelectBox, $CircleOption, event) {
		if ($(".groupswidget-overlay .c-PermitDuration-Container .c-TimerSelectBox").hasClass("active")) {
			ClosePermitDateInputBox($(".groupswidget-overlay .c-PermitDuration-Container .c-TimerSelectBox"), $TimerSelectBox, $CircleOption, event);
		} else {
			var parentOffset = $CircleOption.parent().offset();
			//or $(this).offset(); if you really just want the current element's offset
			/*if (event) {
			 var relX = event.pageX - parentOffset.left;
			 var relY = event.pageY - parentOffset.top;
			 }
			 var $top = ((parseInt($lastSet) * 11) * ($set - 1));
			 console.log($top);
			 
			 var $lastOption = $(".groupswidget .c-PermitDuration-Container .c-Circle-Option").last(),
			 $lastSet = $lastOption.attr("data-set"),
			 $lastFloat = $lastOption.attr("data-float");*/
			var $float = $CircleOption.attr("data-float"),
					$set = $CircleOption.attr("data-set"),
					//$top = (33 * ($set - 1)),
					//$top = (relY - 63.5),
					$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");

				// 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);
				}

				// 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 ClosePermitDateInputBox($TimerSelectBox, $Open, $CircleOption, event) {
		var $TimerSelectBox = $(".groupswidget-overlay .c-PermitDuration-Container .c-TimerSelectBox"),
				$startInput = $TimerSelectBox.find(".c-TimerSelect-StartTime"),
				$endInput = $TimerSelectBox.find(".c-TimerSelect-EndTime");

		$(".groupswidget-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();
				$(".groupswidget-overlay .c-PermitDuration-Container .c-Circle-Option").not($CircleOption).removeClass("c-hasActive");
				if ($Open) {
					OpenPermitDateInputBox($Open, $CircleOption, event);
				}
			}, 500);
		});
	}

	var lastSlideClose = function (setNext) {
		if (setNext.hasClass("c-option-done")) {
			$('.groupswidget-overlay .navigation-overlay').removeClass("active");
			setTimeout(function () {
				setNext.fadeOut(800, function () {
					removeOverlay();
				});
			}, 1000);
		} else {
		}
	};
	var existing_groupswidget_names_actions = function () {
		$('.groupswidget .existing-names a').unbind("click").bind('click', function () {

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

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

			$(".groupswidget .c-CirclesOption .c-Circles[data-step='" + $set + "']").parents(".c-CircleOption-Empty").removeClass("c-CircleOption-Empty");

			if ($this.parents('.options').hasClass("c-DayTime-Options")) {
				$(".groupswidget .c-CirclesOption .c-Circles[data-step='" + $set + "'] .c-CirclesText").html('<span class="groups-startDate"></span><span class="groups-endDate"></span>');

				if ($this.hasClass("u-DateTime")) {
					var $startDateButton = $parents.find(".c-PermitDuration-StartEndSelection #groups-StartDate"),
							$endDateButton = $parents.find(".c-PermitDuration-StartEndSelection #groups-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);

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

					$(".groupswidget .c-CirclesOption .c-Circles[data-step='" + $set + "'] .c-CirclesText .groups-startDate").html($StartMonth_str + " " + $StartDay + " " + $StartYear);
					$(".groupswidget .c-CirclesOption .c-Circles[data-step='" + $set + "'] .c-CirclesText .groups-endDate").html($EndMonth_str + " " + $EndDay + " " + $EndYear);
					checkDateTimeFooterButton();
				} else {
					$(".groupswidget .c-CirclesOption .c-Circles[data-step='" + $set + "'] .c-CirclesText .groups-startDate").html($val);
					$(".groupswidget .c-CirclesOption .c-Circles[data-step='" + $set + "'] .c-CirclesText .groups-endDate").html('');
				}
				checkNavigationStatus();
			} else if ($this.parents('.set-box').hasClass("c-option-location")) {

				$parents.find(".groupswidget-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");
					$(".groupswidget-overlay .c-Footer a").addClass("active");
				}


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

				var $activeNames = $(".groupswidget .c-CirclesOption .c-Circles[data-step='" + $set + "']").find(".c-CircleOption-Filled");
				$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(".groupswidget-Box-overlay").removeClass("active");
				$this.parents('.options').find(".option").removeClass("active");
				$this.addClass("active");
				var setOne = $(".groupswidget .options.active");
				var setNext = $(".groupswidget .options.active").next(".options");

				$(".groupswidget-overlay .u-navigationCircle.active").addClass("u-setDone");

				$(".groupswidget-overlay .u-navigationCircle.active").addClass("u-setDone");
				if ($this.parents('.options').hasClass("active")) {
					$('.groupswidget-overlay .navigation-overlay a.u-navigationCircle.c-GroupsSummaryCircle').trigger("click");
				}
				checkNavigationStatus();

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

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

			}
			return false;

		});
	};
	existing_groupswidget_names_actions();

	$('.groupswidget .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(".groupswidget-Box-overlay").removeClass("active");
		if (existing_div.find("#group-add").length) {
			$(".groupswidget-overlay .c-Setone-Option-Selection .c-BtnSave").attr("saved", false);
		}
		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) {
				$(".groupswidget .u-DateTime").removeClass("active");
			}
		} else {
			$this.parents(".option-container").find(".c-BtnClear").hide();
			existing_name.removeClass("c-found").show();
		}

	});

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

	$(".groupswidget-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();
			$this.parents(".option-container").find(".existing-names .active").removeClass("active");
			$(".groupswidget-overlay .u-navigationCircle[data-set='" + $set + "']").removeClass("u-setDone hovered");
			$(".u-setDone.active").removeClass("u-setDone");
		}
		$(".groupswidget-overlay .c-Setone-Option-Selection .c-BtnSave").attr("saved", false);
		return false;
	});

	hideGroupInactiveElements = function () {
		fullScreenOverlay.find('.c-Setone-Option-Selection').find(".c-CircleOption-Hide, .c-CircleOption-Padlock").fadeOut();
		var circles = fullScreenOverlay.find('.c-Setone-Option-Selection').find('.c-CircleOption-Hide.icon-hide2');
		fullScreenOverlay.find(".u-navigationCircle").removeClass("c-LastNavigationNode");
		$.each(circles, function (i, $th) {
			var $this = $($th),
					$step = $this.parents(".c-CirclesOption").find(".c-Circles").attr("data-step");
			$this.parents(".c-CirclesOption").hide();
			if ($step) {
				fullScreenOverlay.find(".u-navigationCircle[data-set='" + $step + "']").hide();
				if (!fullScreenOverlay.find(".u-navigationCircle[data-set='" + $step + "']").next().length) {
					fullScreenOverlay.find(".u-navigationCircle[data-set='" + $step + "']").prev(".u-navigationCircle").addClass("c-LastNavigationNode");
				}
			}
		});

		fullScreenOverlay.find(".c-Setone-Option-Selection .c-Done-Footer").removeClass("c-Edit-Footer");
	};
	showGroupActiveElements = function () {
		fullScreenOverlay.find('.c-Setone-Option-Selection').find(".c-CircleOption-Hide, .c-CircleOption-Padlock").fadeIn();
		fullScreenOverlay.find('.c-CirclesOption').show();
		fullScreenOverlay.find(".u-navigationCircle").show();
		fullScreenOverlay.find(".u-navigationCircle").removeClass("c-LastNavigationNode");
		fullScreenOverlay.find(".c-Setone-Option-Selection .c-Done-Footer").addClass("c-Edit-Footer");
	};

	$('.groupswidget .c-BtnSave').on('click', function () {
		var $this = $(this),
				existing_div = $this.parents(".option-container"),
				options = $this.parents(".options"),
				existing_name = existing_div.find(".existing-names"),
				$name = existing_div.find('.c-add-input').val();
		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 $from = $(".groupswidget-overlay").attr("from");
						if (undefined != $from && "" != $from && "" !== $isbackTofirst && undefined !== $isbackTofirst) {
							fullScreenOverlay.attr('backTofirst', "");
							openFromWidget($from);
						} else {
							fullScreenOverlay.find(".navigation-overlay").hide();
							$('.groupswidget .set-one').css({display: 'none', top: 0});
							var $comfirmed = fullScreenOverlay.attr("data-comfirmed");

							var innerTarget = fullScreenOverlay.find('.c-Setone-Option-Selection');
							if ((undefined == $comfirmed || "" == $comfirmed)) {
								if (undefined != $isUpdate && "" != $isUpdate) {
									innerTarget = fullScreenOverlay.find('.c-Confirmation-Option');
								}
							}
							var getTopOffset = 60;

							$this.parents('.options').find(".option").removeClass("active");
							$this.addClass("active");

							var setOne = $(".groupswidget-overlay .options.active");
							var setNext = innerTarget;
							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").hide();
									setNext.addClass("active");
								});
							});
						}
					} else {
						inputErrors(existing_div, "Ooops! Your pin is not correct, pls try again.");
					}
				}, 'json');

			} else {
				inputErrors(existing_div, "Ooops! Pls add in your pin…");
			}
		} else {

			if ("" !== $name) {

				if (existing_div.parents(".c-Setone-Option-Selection").length) {
					//alert("saved!");
					inputSuccess(existing_div, "GROUP NAME SAVED");
					$this.attr("saved", true);
				} else {
					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="#" class="c-newAdded">' + $name + '</a>');
						//$('.groupswidget .c-add-input').val("").trigger("keyup");
						existing_groupswidget_names_actions();
						existing_div.find('.c-add-input').val("").trigger("keyup");

					} else {
						inputErrors(existing_div, $name + " already exists, pls select from list, it’s shaking for your attention!");
					}
					existing_name.find("a.c-newAdded").trigger("click");
					existing_name.find("a.c-newAdded").removeClass("c-newAdded");
				}
			} else {
				if (existing_div.parents(".c-Setone-Option-Selection").length) {
					inputErrors(existing_div, "Ooops! Pls add in your group name …");
				} else {
					inputErrors(existing_div, "Oops! We Can't add nothing, please try again..");
				}
			}
		}
		return false;
	});

	$('.groupswidget .c-Footer .c-Footer-Done-Button, .groupswidget .c-Options-Location .c-Footer .c-Footer-Button').on('click', function () {
		fullScreenOverlay.find(".navigation-overlay").show().find(".c-GroupsSummaryCircle").trigger("click");
		return false;
	});
	$('.groupswidget .c-Confirmation-Option .c-Confirmation-Box').on('click', function () {
		var $this = $(this);
		if ($this.hasClass("c-Confirmation-EditPermit")) {
			fullScreenOverlay.find(".option-container-singlebox").find("#group-add").attr("disabled", true);
			fullScreenOverlay.find(".option-container-singlebox").find(".c-Clear-Handle, .c-BtnSave").addClass("unseen");
		}
		fullScreenOverlay.find(".navigation-overlay").show().find(".c-GroupsSummaryCircle").trigger("click");
		var $type = (($(this).hasClass("c-Confirmation-EditGroup")) ? "group" : "permit");
		fullScreenOverlay.attr("data-comfirmed", $type);
		if($type == "permit"){
			fullScreenOverlay.find(".c-Setone-Option-Selection .c-Done-Footer").removeClass("c-Edit-Footer").find(".c-Footer-Delete-Button").removeClass("active");
		}
		return false;
	});
	$('.groupswidget .c-Confirmation-Option .c-Confirmation-Footer .c-Footer-Cancel-Button').on('click', function () {
		$(".groupswidget .c-Done-Footer .c-Footer-Cancel-Button").trigger("click");
		return false;
	});

	$('.groupswidget .c-Pin-Option-Selection .c-Pin-Footer .c-Footer-Cancel-Button').on('click', function () {
		if (parseInt($("body").attr("data-GroupMasterPinAccepted")) === 1 && parseint($("body").attr("data-GroupReviewPinAccepted")) === 1) {
			fullScreenOverlay.find(".navigation-overlay").show().find(".c-GroupsSummaryCircle").trigger("click");
		} else {
			fullScreenOverlay.find(".c-Setone-Option-Selection").find(".c-Footer-Cancel-Button").trigger("click");
		}
		return false;
	});


	$(".widget-Box-overlay").unbind("click").bind("click", function () {
		$(this).removeClass("active");
		if ($(this).parents(".widgetpopup-Box")) {
			$(this).parents(".option-container").find('.c-add-input').trigger("focus").trigger("keyup");
		}
	});

	function inputSuccess(existing_div, $error) {
		existing_div.find(".groupswidget-Success-Box-overlay").addClass("active").find(".c-Contentbox span").html($error);
		setTimeout(function () {
			existing_div.find(".groupswidget-Success-Box-overlay").removeClass("active");
		}, 4000);
	}
	function inputErrors(existing_div, $error) {
		existing_div.find(".groupswidget-Box-overlay").addClass("active").find(".c-Contentbox span").html($error);
		setTimeout(function () {
			existing_div.find(".groupswidget-Box-overlay").removeClass("active");
		}, 4000);
	}

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

	function removeOverlay() {
		$('body').removeClass('activate-full-screen');
		$('.groupswidget-overlay').attr("data-comfirmed", "");
		$('.groupswidget-overlay').removeClass('activate-overlay-screen').hide();
		$(".groupswidget-overlay .groupswidget").removeClass("active");
		$(".groupswidget-overlay").find(".active").removeClass("active");
		$(".groupswidget-overlay").find(".c-found").removeClass("c-found");
		$(".groupswidget-overlay").find(".navigation-overlay").show();
		$(".groupswidget-overlay .navigation-overlay a").not(".u-gotOSummary").show();
		$('.groupswidget-overlay .groupswidget').hide();
		$('.groupswidget-overlay .c-TimerSelectBox, .groupswidget-overlay .c-TimerSelection-Inputs-Box').hide();
		$(".groupswidget-overlay .c-DayTimes .c-hasActive").removeClass("c-hasActive");
		$(".groupswidget-overlay .groupswidget .c-PermitDuration-StartEndSelection-BtnAddNew").trigger("click");
		$(".groupswidget-overlay .groupswidget input").val("");
		$(".groupswidget-overlay .groupswidget .option-container-firstbox").show();
		$(".groupswidget-overlay .groupswidget .option-container-secondbox").hide();
		$(".groupswidget-overlay .navigation-overlay .u-setDone").removeClass("u-setDone");
		$(".groupswidget-overlay .navigation-overlay .active").removeClass("active");
		$(".groupswidget-overlay .c-Setone-Option-Selection .c-CirclesOption").addClass("c-CircleOption-Empty");
		$(".groupswidget-overlay .hovered").removeClass("hovered");
		$(".groupswidget-overlay .c-Pin-Option-Selection").attr("action", "");
		fullScreenOverlay.find(".c-Setone-Option-Selection .c-Done-Footer").removeClass("c-Edit-Footer");
	}

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

	function addPermitDateCircleToGroup($startDate, $endDate) {
		$(".groupswidget-overlay .c-PermitDuration-CirclesOption").removeClass("c-CircleOption-Empty");
		$(".groupswidget-overlay .c-PermitDuration-CirclesOption.c-CirclesOption .c-CircleOption-Filled .groups-startDate").text($startDate);
		$(".groupswidget-overlay .c-PermitDuration-CirclesOption.c-CirclesOption .c-CircleOption-Filled .groups-endDate").text($endDate);
		checkPermitDateFooterButton();
	}
	function removePermitDateCircleToGroup() {
		$(".groupswidget-overlay .c-PermitDuration-CirclesOption.c-CirclesOption").addClass("c-CircleOption-Empty");
		checkPermitDateFooterButton();
	}

	$(".groupswidget-overlay .c-TimerSelection-Input").hover(function (e) {
		var $this = $(this),
				$tooltip = $this.parents(".options").find(".tooltip"),
				$hasActive = $this.parents(".option-container").find(".c-Circle-Option.c-hasActive"),
				$set = $hasActive.attr("data-set");
		if ($this.hasClass("c-TimerSelect-StartTime")) {
			offsets = $this.parents(".option-container").find(".c-TimerSelect-StartTime").offset();
		} else if ($this.hasClass("c-TimerSelect-EndTime")) {
			offsets = $this.parents(".option-container").find(".c-TimerSelect-EndTime").offset();
		}
		var offsets = $this.offset(),
				$top = offsets.top,
				$left = offsets.left;
		if ($set == 1) {
			$top = -65;
		} else if ($set == 2) {
			$top = 65;
		} else if ($set == 3) {
			$top = 200;
		}

		$tooltip.css({"display": "block", "left": $left, "top": $top}).toggleClass('animation bounceIn');

		$offsetsTimer = setTimeout(function () {
			$tooltip.css("display", "none").removeClass('animation bounceIn');
		}, 5000);
	}, function () {
		clearTimeout($offsetsTimer);
		var $this = $(this),
				$tooltip = $this.parents(".options").find(".tooltip");
		$tooltip.css("display", "none").removeClass('animation bounceIn');
	});


	$('.groupswidget .c-CirclesOption .c-CircleOption-Clear').bind({
		click: function () {
			var $this = $(this),
					$parents = $this.parents(".c-CirclesOption"),
					$step = $this.attr("data-step");
			$parents.addClass("c-CircleOption-Empty");

			if ($step == "permit") {
				$parents.find(".groups-startDate, .groups-endDate").text("");
				$(".groupswidget .c-PermitDuration-Container .c-Circle-Option-Box").find(".active").removeClass("active");
				$(".groupswidget-overlay .u-navigationCircle[data-set='two']").removeClass("u-setDone");
			} else if ($step == "daytime") {
				$parents.find(".active").removeClass("active");
				$(".groupswidget .options .c-DayTimes").find(".active").removeClass("active");
				$(".groupswidget-overlay .u-navigationCircle[data-set='three']").removeClass("u-setDone");
			} else {
				var $options = $(".groupswidget .options[data-set='" + $step + "']");
				$options.find(".c-BtnClear").trigger("click");
				$parents.find(".c-CircleOption-Filled").text("");
			}
			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 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");
				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;
	};

	$('.groupswidget .c-CirclesOption .c-CircleOption-Hide').bind({
		click: function () {
			var $this = $(this),
					$parents = $this.parents(".c-CirclesOption"),
					$step = $this.attr("data-step");
			if ($this.hasClass("icon-hide1")) {
				$this.removeClass("icon-hide1").addClass("icon-hide2");
			} else {
				$this.addClass("icon-hide1").removeClass("icon-hide2");
			}
			return false;
		}
	});
	$('.groupswidget .c-CirclesOption .c-CircleOption-Padlock').bind({
		click: function () {
			var $this = $(this);
			var $valid = validateCircleOptionLinks();
			if ($valid) {
				$this.toggleClass("c-Padlock-Active");
			}
			return false;
		}
	});

	setInterval(function () {
		$(".c-PermitDuration-Container .c-Circle-Option .c-Circle-NewAdd").toggleClass("icon-add");
	}, 3000);


	var summaryLocationTimeInterval = setInterval(function () {
		var $summaryOptions = fullScreenOverlay.find(".c-Setone-Option-Selection"),
				$locationCirclesOption = $summaryOptions.find(".c-Location-CirclesOption"),
				$locationCircleOptionFilled = $locationCirclesOption.find(".c-CircleOption-Filled"),
				$locationCirclesText = $locationCircleOptionFilled.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);

});