/** Location popup **/
$(document).ready(function () {
	var getTopOffset = 30;
	var xhr;
	var fullScreenOverlay = $('.locationpopup-overlay');
	var innerOffsetBox = fullScreenOverlay.find('.inner-fullscreen-offset');

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

		showDeleteLocationButton();
	};

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

	$('.locationpopup-overlay .navigation-overlay a').not(".u-gotOSummary").bind({
		click: function () {
			var $this = $(this),
					$set = $this.attr("data-set"),
					$navigation = $this.attr("data-navigation"),
					$options = $(".locationpopup-overlay .options"),
					setActive = $(".locationpopup-overlay .options.active"),
					$activeSet = setActive.attr("data-set");
			if ($set == $activeSet) {
				return false;
			} else {
				var validate = validateBlock($this, setActive);
				if (validate) {
					//$(".c-Footer a").removeClass("active");
					var setTwo = $(".locationpopup-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) - 69;// + windowOffset;
						setTwo.css('top', offsetTwo).fadeIn(800, function () {
							setTwo.addClass("active");
							lastLocationPopupSlideClose(setTwo);
							checkLocationPopupArrowStatus();
						});
					});
				}
			}
			return false;
		}
	});
	function validateBlock($this, $activeOption) {
		var $set = $this.attr("data-set"),
				$navigation = $this.attr("data-navigation"),
				$options = $(".locationpopup-overlay .options"),
				setActive = $(".locationpopup-overlay .options.active"),
				$activeSet = setActive.attr("data-set");

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

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

			}

			if ($(".locationpopup-overlay .options.active .c-CirclesOption").length) {
				$result = false;
			}
		}
		return $result;
	}
	var locationpopup_existing_names_actions = function () {
		$('.locationpopup .existing-names a').not(".c-list-delete").unbind('click').bind("click", function () {

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

			if (!$parents.find(".option-container-firstbox").hasClass("isHidden")) {
				$parents.find(".option-container-firstbox").fadeOut(200, function () {
					$parents.find(".option-container-secondbox").fadeIn(200);
				});
			}

			$parents.find(".locationpopup-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");
			}

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

			if ($('.locationpopup .existing-names a.active').length) {
				$(".locationpopup-overlay .u-navigationCircle.active").addClass("u-setDone");
				$(".locationpopup-overlay .c-Footer.c-Location-Footer .c-Footer-Button").addClass("active");
			} else {
				$(".locationpopup-overlay .u-navigationCircle.active").removeClass("u-setDone");
				$(".locationpopup-overlay .c-Footer.c-Location-Footer .c-Footer-Button").removeClass("active");
			}

			//checkLocationPopupArrowStatus();
			return false;
		}).on("click", ".c-contact-delete", function () {
			var $this = $(this),
					$parents = $this.parents("a");
			$parents.fadeOut();
			checkLocationFooterButton();
			return false;
		});
		$('.locationpopup .existing-names a .c-list-delete').unbind("click").bind('click', function () {
			var $this = $(this),
					$parents = $this.parents("a"),
					$isPinNeed = $("body").attr("data-GroupMasterPinAccepted");
			if (undefined == $isPinNeed || "" == $isPinNeed) {
				$this.parents(".c-option-location").find(".option-container-secondbox .c-BtnSave").trigger("click");
			} else {
				$parents.fadeOut(function () {
					$parents.remove();
				});
			}
			return false;
		});
	};
	locationpopup_existing_names_actions();

	$(".locationpopup-overlay .c-Footer.c-Location-Footer").on("click", ".c-Footer-Button", function () {
		var setOne = $(".locationpopup .options.active");
		var setNext = $(".locationpopup .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");
				lastLocationPopupSlideClose(setNext);
			});
		});
		return false;
	});

	$('.c-LocationPopup-Button').bind({
		click: function () {

			removeLocationOverlay();
			addLocationOverlay();
			var outerTarget = fullScreenOverlay.find('.locationpopup').css({display: 'block'});

			var $valid = validateCircleOptionLinks();
			console.log($valid);
			if ($valid) {

				/*extendOverlay('contactupload');*/
				$('.locationpopup .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");
					checkLocationPopupArrowStatus();
				});
			}
			return false;
		}
	});

	$('.locationpopup').on("click", ".c-Footer.c-ContactTemplateDownload-Footer", function () {
		$('.locationpopup-overlay .navigation-overlay a.u-navigationCircle.active').addClass("u-setDone").next().trigger("click");
		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).find(".c-add-input").focus();
			});
		}
		return false;
	});

	var checkLocationFooterButton = function () {
		if (!$(".locationpopup-overlay .set-three .existing-names a.c-namesError").length) {
			$(".c-ContactDownload-Footer").removeClass("c-footerError").find("a").text("UPLOAD");
			$(".locationpopup-overlay .navigation-overlay .u-navigationCircle.active").addClass("u-setDone");
		}
	};

	var lastLocationPopupSlideClose = function (setNext) {
		if (setNext.hasClass("c-option-done")) {
			$('.locationpopup-overlay .navigation-overlay').removeClass("active");
			setTimeout(function () {
				setNext.fadeOut(800, function () {
					removeLocationOverlay();
				});
			}, 3000);
		} else {
			checkLocationPopupArrowStatus();
		}
	};

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

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

	$('.locationpopup-overlay .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 = fullScreenOverlay.attr("from");
						if (undefined != $from && "" != $from && "" !== $isbackTofirst && undefined !== $isbackTofirst) {
							fullScreenOverlay.attr('backTofirst', "");
							openFromWidget($from);
						} else {
							fullScreenOverlay.find(".navigation-overlay").hide();
							var innerTarget = fullScreenOverlay.find('.set-one');
							var getTopOffset = 60;
							var setOne = fullScreenOverlay.find(".options.active");
							var setNext = innerTarget;

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

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

			} else {
				inputErrors(existing_div, "Ooops! Pls add in your pin…");
			}
		} else {
			if ("" !== $name) {
				var element = existing_name.find("a").filter(function () {
					var string = $(this).text().trim().replace(/ +/g, "").toLowerCase();
					if (string != $name.trim().replace(/ +/g, "").toLowerCase()) {
					} else {
						return $(this);
					}
				});
				if (!element.length) {
					// adding delete button is location step list added
					var $btnDelete = '';
					if (options.find(".c-option-location").length) {
						$btnDelete = '<span class="c-list-delete icon-cross c-circle" href="#"></span>';
					}
					existing_name.append('<a href="#" class="c-newAdded">' + $name + $btnDelete + '</a>');
					//$('.personvehiclepermit .c-add-input').val("").trigger("keyup");
					existing_div.find('.c-add-input').val("").trigger("keyup");
					locationpopup_existing_names_actions();
					existing_name.find("a.c-newAdded").trigger("click");
					existing_name.find("a.c-newAdded").removeClass("c-newAdded");
					showDeleteLocationButton();
				} else {
					inputErrors(existing_div, $name + " alread exists, Please enter another location!");
				}
			} else {
				inputErrors(existing_div, "Oops! We Can't add nothing, please try again..");
			}
		}
		return false;
	});
	function inputSuccess(existing_div, $error) {
		existing_div.find(".locationpopup-Success-Box-overlay").addClass("active").find(".c-Contentbox span").html($error);
		setTimeout(function () {
			existing_div.find(".locationpopup-Success-Box-overlay").removeClass("active");
		}, 4000);
	}
	function inputErrors(existing_div, $error) {
		existing_div.find(".locationpopup-Box-overlay").addClass("active").find(".c-Contentbox span").html($error);
		setTimeout(function () {
			existing_div.find(".locationpopup-Box-overlay").removeClass("active");
		}, 4000);
	}

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

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

	function removeLocationOverlay() {
		$('body').removeClass('activate-full-screen');
		$('.locationpopup-overlay').removeClass('activate-overlay-screen');
		$(".locationpopup-overlay .locationpopup").removeClass("active");
		$(".locationpopup-overlay").find(".active").removeClass("active");
		$(".locationpopup-overlay").find(".c-found").removeClass("c-found");
		$(".locationpopup-overlay .navigation-overlay a").not(".u-gotOSummary").show();
		$('.locationpopup-overlay .locationpopup').hide();
		$(".locationpopup-overlay .locationpopup input").val("");
		$(".locationpopup-overlay .locationpopup .option-container-firstbox").show();
		$(".locationpopup-overlay .locationpopup .option-container-secondbox").hide();
		$(".locationpopup-overlay .navigation-overlay .u-setDone").removeClass("u-setDone");
		$(".locationpopup-overlay .navigation-overlay .active").removeClass("active");
		$(".locationpopup-overlay .hovered").removeClass("hovered");
	}

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

	});
	$(".locationpopup-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");
			$(".locationpopup-overlay .u-navigationCircle[data-set='" + $set + "']").removeClass("u-setDone hovered");
			$(".u-setDone.active").removeClass("u-setDone");
		}
		return false;
	});

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