/** Assign popup **/
$(document).ready(function () {
	var animationTimeout;
	var getTopOffset = 30;
	var fullScreenOverlay = $('.assignwidget-overlay');
	var innerOffsetBox = fullScreenOverlay.find('.inner-fullscreen-offset');
	$('.assignwidget-overlay .close-overlay').bind({
		click: function () {
			removeAssignOverlay();
			return false;
		}
	});
	$(".assignwidget-overlay .c-Footer.c-Assign-Footer").on("click", ".c-Footer-Button, .c-Footer-Cancel-Button", function () {
		var $this = $(this);
		var setOne = $(".assignwidget-overlay .options.active");
		var setNext = $(".assignwidget-overlay .options.c-option-done");
		/* If pending contacts found open pending step */
		//if (!$this.hasClass("c-Footer-Cancel-Button")) {
		var pendingContacts = $(".c-AssignContact-Contacts .c-AssignContact-Box-Contacts-List .c-AssignContact-Box-Contacts-Item:not(.u-Contact-Assigned)").length;
		if (pendingContacts > 0) {
			fullScreenOverlay.find(".c-Option-ContactPending-Number").text(pendingContacts);
			var setNext = $(".assignwidget-overlay .options.c-Option-ContactPending");
		}
		//}
		setOne.removeClass("active").fadeOut(800, function () {
			var twoHeight = setNext.height();
			var windowHeight = $(window).height();
			var offsetTwo = (windowHeight / 2) - (twoHeight / 2) - getTopOffset; // + windowOffset;
			if (!$this.hasClass("c-Footer-Cancel-Button")) {
				setNext.find(".Done-text").text("SAVED");
			} else {
				setNext.find(".Done-text").text("CANCELLED");
			}
			setNext.css('top', offsetTwo).fadeIn(800, function () {
				setNext.addClass("active");
				$('.assignwidget-overlay .navigation-overlay').removeClass("active");
				lastassignwidgetSlideClose(setNext);
			});
		});
		return false;
	});

	$(".c-AssignContact-Container").on("click", ".c-AssignContact-Groups .c-AssignContact-AddGroup", function () {
		var $this = $(this),
				$groupName = $.trim($this.text()),
				$groupUid = $this.parents(".c-AssignContact-Box-Contacts-Item").attr("data-uid");

		$('.assignwidget-overlay').attr("hidden", true).find(".inner-fullscreen-offset").fadeOut(function () {
			$('.assignwidget-overlay').fadeOut();
			$(".groupswidget-overlay").attr("from", "assignwidget").fadeIn();
			$('.c-Group-Widget-Button:first').trigger("click");
		});
		return false;
	});
	$(".c-AssignContact-Container").on("click", ".c-AssignContact-Groups .c-AssignContact-Box-Contacts-Item a", function () {
		var $this = $(this),
				$groupName = $.trim($this.text()),
				$groupUid = $this.parents(".c-AssignContact-Box-Contacts-Item").attr("data-uid");

		$(".c-AssignContact-Groups .c-AssignContact-Box-Contacts-Item a").removeClass("active");
		$this.addClass("active");
		var durationstart = $this.attr("data-durationStart"),
				durationend = $this.attr("data-durationEnd"),
				daytime = $this.attr("data-daytime"),
				person = $this.attr("data-person"),
				vehicle = $this.attr("data-vehicle"),
				location = $this.attr("data-location"),
				$StepsCriteria = $(".c-AssignContact-GroupDetails .c-Animated .c-AssignContact-GroupDetails-Criteria");
		if (animationTimeout) {
			clearInterval(animationTimeout);
		}
		$StepsCriteria.find("span.c-AssignContact-GroupDetails-Criteria-Item").not("[data-step='1']").remove();
		$StepsCriteria.find("span.c-AssignContact-GroupDetails-Criteria-Item[data-step='1']").addClass("CriteriaActive");
		var $steps = "";
		if (("" !== durationstart && "" !== durationend) && undefined !== durationstart) {
			var $value = "" + (durationstart.replace(',', '')) + " - " + (durationend.replace(',', '')) + "";
			$steps = getStepHtml($StepsCriteria, "DURATION", $value);
			$StepsCriteria.append($steps);
		}
		if ("" !== daytime && undefined !== daytime) {
			var $value = daytime;
			$steps = getStepHtml($StepsCriteria, "DAY & TIME", $value);
			$StepsCriteria.append($steps);
		}
		if ("" !== person && undefined !== person) {
			var $value = person;
			$steps = getStepHtml($StepsCriteria, "PERSON", $value);
			$StepsCriteria.append($steps);
		}
		if ("" !== vehicle && undefined !== vehicle) {
			var $value = vehicle;
			$steps = getStepHtml($StepsCriteria, "VEHICLE", $value);
			$StepsCriteria.append($steps);
		}
		if ("" !== location && undefined !== location) {
			var $value = location;
			$steps = getStepHtml($StepsCriteria, "LOCATION", $value);
			$StepsCriteria.append($steps);
		}
		animateFilterCriteria();
		filterGroupContacts($groupUid);
		$(".c-AssignContact-Container .c-AssignContact-GroupDetails ").removeClass("c-AssignContact-Empty");
		$(".c-AssignContact-Container .c-AssignContact-GroupDetails .c-AssignContact-Box-Title").text($groupName).attr("data-group_uid", $groupUid);
		return false;
	}).on("click", ".c-AssignContact-Contacts .c-AssignContact-Box-Contacts-Item a", function () {
		var $this = $(this),
				$contactName = $.trim($this.text());
		$this.toggleClass("active");
		return false;
	}).on("click", ".c-AssignContact-Groups .c-AssignContact-Clear", function () {
		var $this = $(this),
				$parents = $this.parents(".c-AssignContact-Box-Contacts-Item"),
				$groupName = $.trim($parents.text()),
				$groupUid = $parents.attr("data-uid"),
				Groppeditem = $(".c-AssignContact-GroupDetails .c-AssignContact-Box-Contacts-Item a[data-group_uid='" + $groupUid + "']");
		if (Groppeditem.length) {
			Groppeditem.parents(".c-AssignContact-Box-Contacts-Item").find(".c-AssignContact-Clear").trigger("click");
		}
		$this.parents(".c-AssignContact-Box-Contacts-Item").fadeOut(function () {
			$(this).remove();
			ShowGroupEmptyBox();
		});

		return false;
	}).on("click", ".c-AssignContact-GroupDetails .c-AssignContact-Clear", function () {
		var $this = $(this),
				$parents = $this.parents(".c-AssignContact-Box-Contacts-Item"),
				$uid = $parents.find("a").attr("data-uid"),
				UnGroppeditem = $(".c-AssignContact-Contacts .c-AssignContact-Box-Contacts-Item a[data-uid='" + $uid + "']");

		if (UnGroppeditem.length) {
			UnGroppeditem.parents(".c-AssignContact-Box-Contacts-Item").fadeIn().removeClass("u-Contact-Assigned");
		}
		$this.parents(".c-AssignContact-Box-Contacts-Item").remove();
		//ShowGroupDetailsEmptyBox();
		return false;
	}).on("click", ".c-AssignContact-GroupDetails .c-AssignContact-GroupDetails-Criteria-Box a", function () {
		var $this = $(this),
				$perant = $this.parents(".c-AssignContact-Box-Contacts-Item"),
				$id = $(".c-AssignContact-Container .c-AssignContact-GroupDetails .c-AssignContact-Box-Title").attr("data-group_uid");

		$('.assignwidget-overlay').fadeOut();
		$(".groupswidget-overlay").attr({"from": "assignwidget", "update": $id}).fadeIn();
		$('.c-Group-Widget-Button:first').trigger("click");
		groupwidgetPreperSavedData();
		return false;
	});
	groupwidgetPreperSavedData = function () {
		var $this = $(".c-AssignContact-Groups .c-AssignContact-Box-Contacts-Item a.active");
		var durationstart = $this.attr("data-durationStart"),
				durationend = $this.attr("data-durationEnd"),
				daytime = $this.attr("data-daytime"),
				person = $this.attr("data-person"),
				vehicle = $this.attr("data-vehicle"),
				location = $this.attr("data-location"),
				$StepsCriteria = $(".c-AssignContact-GroupDetails .c-Animated .c-AssignContact-GroupDetails-Criteria"),
				$groupName = $.trim($this.text()),
				$groupsWidget = $(".groupswidget-overlay");

		if (undefined == $groupName) {
			$groupName = $this.attr("data-group_name");
		}
		// set group name first
		$groupsWidget.find("#group-add").val($groupName).trigger("keyup");

		var $steps = "";
		if (("" !== durationstart && "" !== durationend) && undefined !== durationstart) {
			var $permitDuration = $groupsWidget.find(".c-PermitDuration-Container"),
					date_filter = $this.attr("data-durationdatefilter"),
					date_number = $this.attr("data-durationdatenumber"),
					date_start = $this.attr("data-durationstartdate"),
					date_end = $this.attr("data-durationenddate");

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

			if ($permitDurationCricle.length) {
				$permitDurationCricle.attr({"data-start": durationstart, "data-end": durationend, "data-startdate": date_start, "data-enddate": date_end}).trigger("click");
				setTimeout(function () {
					$permitDuration.find(".c-TimerSelectBox .c-TimerSelect-Save").trigger("click");
				}, 100);
			}
		}
		if ("" !== daytime && undefined !== daytime) {
			var $daytimeBlock = $groupsWidget.find(".c-DayTimes"),
					$day = daytime,
					$filterdays = $day.split(",");
			$.each($filterdays, function (i, $t) {
				var dattimeCricleOption = $daytimeBlock.find(".c-Circle-Option[data-day='" + $t + "']");
				if (dattimeCricleOption.length) {
					var $start = $this.attr("data-daytime-" + $t + "-start"),
							$end = $this.attr("data-daytime-" + $t + "-end");
					dattimeCricleOption.attr({"data-start": $start, "data-end": $end}).trigger("click");
					setTimeout(function () {
						$daytimeBlock.find(".c-TimerSelectBox .c-TimerSelect-Save").trigger("click");
					}, 100);
				}
			});
		}
		if ("" !== person && undefined !== person) {
			$groupsWidget.find(".options.set-four .option-container-firstbox-BtnGo").trigger("click");
			$groupsWidget.find(".options.set-four .c-add-input").val(person).trigger("keyup");
			$groupsWidget.find(".options.set-four .existing-names a.c-found").trigger("click");
		}
		if ("" !== vehicle && undefined !== vehicle) {
			$groupsWidget.find(".options.set-five .option-container-firstbox-BtnGo").trigger("click");
			$groupsWidget.find(".options.set-five .c-add-input").val(vehicle).trigger("keyup");
			$groupsWidget.find(".options.set-five .existing-names a.c-found").trigger("click");
		}
		if ("" !== location && undefined !== location) {
			$groupsWidget.find(".options.set-six .option-container-firstbox-BtnGo").trigger("click");
			$groupsWidget.find(".options.set-six .c-add-input").val(location).trigger("keyup");
			$groupsWidget.find(".options.set-six .existing-names a.c-found").trigger("click");
		}
	};
	var ShowGroupEmptyBox = function ($groupName) {
		var $box = $(".c-AssignContact-Groups");
		if (!$box.find(".c-AssignContact-Box-Contacts-Item").length) {
			$box.addClass("c-AssignContact-Empty");
			ShowGroupDetailsEmptyBox();
		}
	};
	var ShowGroupDetailsEmptyBox = function ($groupName) {
		var $box = $(".c-AssignContact-GroupDetails");
		if (!$box.find(".c-AssignContact-Box-Contacts-Item.active").length) {
			$box.addClass("c-AssignContact-Empty");
		}
	};

	function filterGroupContacts($groupUid) {
		var $contactItems = $(".c-AssignContact-GroupDetails .c-AssignContact-GroupDetails-Items .c-AssignContact-Box-Contacts-Item");
		$contactItems.removeClass("active");
		var element = $contactItems.filter(function () {
			var string = $(this).find("a").attr("data-group_uid").trim();
			if (string != $groupUid) {
			} else {
				return $(this);
			}
		});
		element.addClass("active");
	}

	function getStepHtml($StepsCriteria, $CriteriaName, $CriteriaValue) {
		var $step = parseInt($StepsCriteria.find(".c-AssignContact-GroupDetails-Criteria-Step").length) + 1;
		var $active = ($step == 1) ? "CriteriaActive" : "";
		return '<span class="c-AssignContact-GroupDetails-Criteria-Item c-AssignContact-GroupDetails-Criteria-Step ' + $active + '" data-step=' + $step + '><span class="c-AssignContact-GroupDetails-Criteria-Item-Name">' + $CriteriaName + '</span><span class="c-AssignContact-GroupDetails-Criteria-Item-Value">' + $CriteriaValue + '</span></span>';
	}

	$('.c-Assignwidget-Button').bind({
		click: function () {
			removeAssignOverlay();
			addAssignOverlay();
			/*extendOverlay('contactupload');*/
			var outerTarget = fullScreenOverlay.find('.assignwidget').css({display: 'block'});
			$('.assignwidget .set-two').css({display: 'none', top: 0});
			var innerTarget = outerTarget.find('.set-one');
			var targetHeight = innerTarget.height();
			var windowHeight = $(window).height();
			var windowOffset = $(window).scrollTop();
			var offsetTop = (windowHeight / 2) - (targetHeight / 2) - 50; // + windowOffset;

			innerTarget.css({top: offsetTop, display: 'block'});
			outerTarget.fadeIn(800, function () {
				innerTarget.addClass("active");
			});
			return false;
		}
	});
	/*$('.assignwidget').on("click", ".c-Footer.c-ContactTemplateDownload-Footer", function () {
	 $('.assignwidget-overlay .navigation-overlay a.u-navigationCircle.active').addClass("u-setDone").next().trigger("click");
	 return false;
	 });*/
	$('.assignwidget-overlay .navigation-overlay a').not(".u-gotOSummary").bind({
		click: function () {
			var $this = $(this),
					$set = $this.attr("data-set"),
					$navigation = $this.attr("data-navigation"),
					$options = $(".assignwidget-overlay .options"),
					setActive = $(".assignwidget-overlay .options.active"),
					$activeSet = setActive.attr("data-set");

			var setTwo = $(".assignwidget-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");
				});
			});
			return false;
		}
	});
	$('.assignwidget').on("click", ".c-Footer.c-AssignPending-Footer .c-Footer-Button", function () {
		$('.assignwidget-overlay .navigation-overlay a.u-navigationCircle:first').trigger("click");
		return false;
	});
	$('.assignwidget').on("click", ".c-Footer.c-AssignPending-Footer .c-Footer-Cancel-Button", function () {
		var $this = $(this);
		var setOne = $(".assignwidget-overlay .options.active");
		var setNext = $(".assignwidget-overlay .options.c-option-done");

		var movedContacts = $(".c-AssignContact-GroupDetails .c-AssignContact-Box-Contacts-List .c-AssignContact-Box-Contacts-Item").length;
		if (movedContacts > 0) {
			setNext.find(".Done-text").text("SAVED");
		} else {
			setNext.find(".Done-text").text("DONE");
		}
		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");
				$('.assignwidget-overlay .navigation-overlay').removeClass("active");
				lastassignwidgetSlideClose(setNext);
			});
		});
		return false;
	});
	var animateFilterCriteria = function () {
		var $StepsCriteria = $(".c-AssignContact-GroupDetails .c-AssignContact-GroupDetails-Criteria");
		$.each($StepsCriteria, function (ele, th) {
			var $this = $(th);
			animationTimeout = setInterval(function () {
				var $ContactName = $this.find(".c-AssignContact-GroupDetails-ContactName"),
						$CriteriaSteps = $this.find(".c-AssignContact-GroupDetails-Criteria-Step"),
						$LastCriteriaStep = $this.find(".c-AssignContact-GroupDetails-Criteria-Step:last"),
						$CriteriaStep = $this.find(".c-AssignContact-GroupDetails-Criteria-Step.CriteriaActive"),
						$LastStep = $LastCriteriaStep.attr("data-step"),
						$Step = $CriteriaStep.attr("data-step");
				if ($LastStep == $Step) {
					$Step = 1;
					$(".assignwidget-overlay .c-AssignContact-GroupDetails-Criteria-Box.c-Animated a").addClass("u-AssignContact-FirstStepActive");
				} else {
					$Step = parseInt($Step) + 1;
					$(".assignwidget-overlay .c-AssignContact-GroupDetails-Criteria-Box.c-Animated a").removeClass("u-AssignContact-FirstStepActive");
				}
				$CriteriaStep.fadeOut(300, function () {
					$(this).removeClass("CriteriaActive");
					$this.find(".c-AssignContact-GroupDetails-Criteria-Step[data-step='" + $Step + "']").fadeIn(300).addClass("CriteriaActive");
					if ($Step > 1) {
						$this.parents(".c-AssignContact-GroupDetails-Criteria-Box.c-Animated").addClass("c-AssignContact-GroupDetails-Criteria-Animated");
					} else {
						$this.parents(".c-AssignContact-GroupDetails-Criteria-Box.c-Animated").removeClass("c-AssignContact-GroupDetails-Criteria-Animated");
					}
				});
			}, 3000);
		});
	};

	var lastassignwidgetSlideClose = function (setNext) {
		if (setNext.hasClass("c-option-done")) {
			$('.assignwidget-overlay .navigation-overlay').removeClass("active");
			setTimeout(function () {
				setNext.fadeOut(800, function () {
					removeAssignOverlay();
				});
			}, 1000);
		} else {
			//checkassignwidgetArrowStatus();
		}
	};
	function addAssignOverlay() {
		$('body').addClass('activate-full-screen');
		$('.assignwidget-overlay').addClass('activate-overlay-screen').show();
		innerOffsetBox.css({position: 'absolute', top: $(window).scrollTop() + 50});
		$(".assignwidget-overlay").removeClass("active");
		$(".assignwidget-overlay .options").attr("style", "");
		$(".c-TipHelp-Container").fadeOut().removeClass("active");
	}

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


	assignGroupSelection = function () {
		$(".c-AssignContact-GroupDetails-Items .c-AssignContact-GroupDetails-Item a").unbind("click").bind("click", function () {
			var $this = $(this),
					$contactName = $.trim($this.text());
			$this.toggleClass("active");
			return false;
		});
	};
	assignGroupSelection();

	dragAssignHandle = function () {
		var dragInactiveItems = $('.c-AssignContact-Contacts .c-AssignContact-Box-Contacts-List .c-AssignContact-Box-Contacts-Item a:not(.active)');
		var dragActiveItems = $('.c-AssignContact-Contacts .c-AssignContact-Box-Contacts-List .c-AssignContact-Box-Contacts-Item a.active');
		dragItems = $('.c-AssignContact-Contacts .c-AssignContact-Box-Contacts-List .c-AssignContact-Box-Contacts-Item');
		dragAssignedItems = $('.c-AssignContact-GroupDetails .c-AssignContact-Box-Contacts-List .c-AssignContact-GroupDetails-Items .c-AssignContact-GroupDetails-Item');
		dropZones = $('.c-AssignContact-Groups .c-AssignContact-Box-Empty, .c-AssignContact-GroupDetails, .c-AssignContact-Groups .c-AssignContact-Box-Contacts-Item, .c-AssignContact-Contacts,  .c-AssignContact-AddGroup');
		var selectedObjs;

		var offset_start = {};
		dragItems.draggable({
			opacity: 0.7, /*helper: "clone",*/
			tolerance: "",
			cursorAt: {
				top: 34,
				left: 125
			},
			refreshPositions: true,
			helper: function (event, ui) {
				$('.c-AssignContact-Container .c-AssignContact-Contacts .c-AssignContact-Box-Contacts-Item').removeClass("u-Dragged");
				$(this).find("a").addClass("active");
				var selected = $('.c-AssignContact-Container .c-AssignContact-Contacts .c-AssignContact-Box-Contacts-Item a.active').parents(".c-AssignContact-Box-Contacts-Item");
				if (selected.length === 0) {
					selected = $(this);
				}
				
				var reversed = selected.get().reverse();
				var container = $('<div/>').attr('id', 'draggingContainer');
				container.html($(reversed).clone());
				//container.css({position:"absolute", left:ui.offset.left-this.offsetLeft, top:ui.offset.top-this.offsetTop});;

				selected.addClass("u-Dragged");

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

				//console.log("helper => width : ", $width, " height : ", $height);
				var $arr = {
					left: Math.floor($width / 2),
					top: Math.floor($height / 2)
				};
				//console.info("helper => $arr ", $arr);
				$(this).draggable("option", "cursorAt", $arr);
				//}, 0);

				
				
				return container;
			},
			start: function (event, ui) {
				/*console.log("width : ", $(ui.helper).width(), " height : ", $(ui.helper).height());
				 var $arr = {
				 left: Math.floor($(ui.helper).width() / 2),
				 top: Math.floor($(ui.helper).height() / 2)
				 };
				 console.info("$arr ", $arr);
				 $(this).draggable("option", "cursorAt", $arr);*/
			},
			drag: function (event, ui) {
				/*console.log("width : ", $(ui.helper).width(), " height : ", $(ui.helper).height());
				 var $arr = {
				 left: Math.floor($(ui.helper).width() / 2),
				 top: Math.floor($(ui.helper).height() / 2)
				 };
				 console.info("$arr ", $arr);
				 $(this).draggable("option", "cursorAt", $arr);*/
			}
		});
		dragAssignedItems.draggable({
			opacity: 0.7, /*helper: "clone",*/
			tolerance: "",
			helper: function (event, ui) {
				dragAssignedItems.removeClass("u-Dragged");
				$(this).find("a").addClass("active");
				var selected = dragAssignedItems.find("a.active").parents(".c-AssignContact-Box-Contacts-Item");
				if (selected.length === 0) {
					selected = $(this);
				}
				var container = $('<div/>').attr('id', 'draggingContainer');
				container.html(selected.clone());

				selected.addClass("u-Dragged");

				return container;
			},
			start: function (event, ui) {
				//console.log("dragAssignedItems");
			},
			drag: function (event, ui) {
			}
		});

		dropZones.droppable({
			over: function (event, ui) {
				$(this).not(".c-AssignContact-Contacts").css('z-index', 0);
				ui.helper.css('z-index', 1);
				$(this).attr("data-over", true);
			},
			out: function (event, ui) {
				$(this).attr("data-over", false);
			},
			drop: function (event, ui) {
				//console.info($(this));
				$(this).attr("data-over", false);
				var draggedElement = ui.helper.children();
				//console.info(draggedElement);
				if ($(this).parents(".c-AssignContact-Empty").length) {
					//console.log(1);
					$(".c-AssignContact-AddGroup a").trigger("click");
				} else if ($(this).hasClass("c-AssignContact-GroupDetails")) {
					//console.log(2);
					var selectedGroup = $(".c-AssignContact-Groups .c-AssignContact-Box-Contacts-Item a.active").parents(".c-AssignContact-Box-Contacts-Item");
					if (!selectedGroup.length) {
						//console.log(2.1);
						$(".c-AssignContact-AddGroup a").trigger("click");
					} else {
						//console.log(2.2);
						moveContact($(draggedElement), selectedGroup);
					}
				} else if ($(this).hasClass("c-AssignContact-AddGroup")) {
					//console.log(3);
					$(".c-AssignContact-AddGroup a").trigger("click");
				} else if ($(this).hasClass("c-AssignContact-Box-Contacts-Item")) {
					//console.log(4);
					moveContact($(draggedElement), $(this));
				} else if ($(this).hasClass("c-AssignContact-Contacts")) {
					//console.log(5);
					revertContact($(draggedElement), $(this));
				}
				$(".c-AssignContact-Groups").removeClass("c-AssignContact-Empty");

			}
		});


	};
	dragAssignHandle();


	moveContact = function ($draggedElement, $groupEle) {
		$draggedElement.each(function () {
			var $this = $(this),
					$clone = $this.clone(),
					link = $clone.find("a"),
					uid = link.attr("data-uid"),
					$groupUid = $groupEle.attr("data-uid"),
					$groupName = $groupEle.find("a").text();

			if (!$this.hasClass("c-AssignContact-GroupDetails-Item")) {
				var $cloned = $(".c-AssignContact-Contacts .c-AssignContact-Box-Contacts-Item a[data-uid='" + uid + "']").parents(".c-AssignContact-Box-Contacts-Item");
				$(".c-AssignContact-Contacts .c-AssignContact-Box-Contacts-Item").removeClass("u-Dragged");
				$cloned.fadeOut(function () {
					$cloned.addClass("u-Contact-Assigned");
					link.attr({"data-group": $groupName, "data-group_uid": $groupUid, "style": ""}).removeClass("active");
					$this.hide();

					$(".assignwidget-overlay .c-AssignContact-GroupDetails .c-AssignContact-GroupDetails-Items").append(($clone.addClass("c-AssignContact-GroupDetails-Item")));
					dragAssignHandle();
					$groupEle.find("a").trigger("click");
					assignGroupSelection();
				});
			} else {
				var $tmp = $(".c-AssignContact-GroupDetails .c-AssignContact-GroupDetails-Item a[data-uid='" + uid + "']").parents(".c-AssignContact-GroupDetails-Item");

				$(".c-AssignContact-GroupDetails .c-AssignContact-Box-Contacts-Item").removeClass("u-Dragged");
				$tmp.fadeOut(function () {
					$tmp.find("a").attr({"data-group": $groupName, "data-group_uid": $groupUid, "style": ""}).removeClass("active");
					$groupEle.find("a").trigger("click");
					assignGroupSelection();
				});
			}

		});
		dragAssignedItems.find("a.active").removeClass("active");
		dragItems.find("a.active").removeClass("active");



	};

	revertContact = function ($draggedElement, $groupEle) {
		$draggedElement.each(function () {
			var $this = $(this),
					$clone = $this.clone(),
					link = $clone.find("a"),
					uid = link.attr("data-uid");

			if ($this.hasClass("c-AssignContact-GroupDetails-Item")) {
				var $tmp = $(".c-AssignContact-GroupDetails .c-AssignContact-GroupDetails-Item a[data-uid='" + uid + "']");
				if ($tmp.length) {
					$(".c-AssignContact-GroupDetails .c-AssignContact-GroupDetails-Item a[data-uid='" + uid + "']").parents(".c-AssignContact-GroupDetails-Item").find(".c-AssignContact-Clear").trigger("click");
				}
			}
		});

		dragAssignedItems.find("a.active").removeClass("active");
		dragItems.find("a.active").removeClass("active");
		assignGroupSelection();
	};



	$(".assignwidget-overlay .c-AssignContact-Box-Contacts-Search").on("click", ".c-AssignContact-Box-Contacts-Search-Label", function () {
		var $this = $(this),
				$searchBox = $this.parents(".c-AssignContact-Box-Contacts-Search"),
				$searchInputBox = $searchBox.find(".c-AssignContact-Box-Contacts-Search-Input-Box"),
				$searchInput = $searchInputBox.find(".c-AssignContact-Box-Contacts-Search-Input");
		//if(!$this.hasClass("u-hide")){
		$this.hide();
		$searchInputBox.show();
		$searchInput.focus();
		//}
		return false;
	}).on("keyup", ".c-AssignContact-Box-Contacts-Search-Input-Box .c-AssignContact-Box-Contacts-Search-Input", function () {
		var $this = $(this),
				$val = $this.val(),
				$searchBox = $this.parents(".c-AssignContact-Box-Contacts-Search"),
				$searchInputBox = $searchBox.find(".c-AssignContact-Box-Contacts-Search-Input-Box"),
				$clearBtn = $searchInputBox.find(".c-AssignContact-Box-Contacts-Search-Input-Clear"),
				$contactsList = $(".c-AssignContact-Contacts .c-AssignContact-Box-Contacts-List .c-AssignContact-Box-Contacts-Item a").not(".u-Contact-Assigned");
		if ("" != $this.val()) {
			$clearBtn.show();
		}

		var element = $contactsList.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);
			}
		});

		$contactsList.hide();
		if (element.length) {
			element.show();
		} else {
			if ("" == $this.val()) {
				$contactsList.show();
			}
		}
		return false;
	}).on("click", ".c-AssignContact-Box-Contacts-Search-Input-Box .c-AssignContact-Box-Contacts-Search-Input-Clear", function () {
		var $this = $(this),
				$searchBox = $this.parents(".c-AssignContact-Box-Contacts-Search"),
				$searchlabel = $searchBox.find(".c-AssignContact-Box-Contacts-Search-Label"),
				$searchInputBox = $searchBox.find(".c-AssignContact-Box-Contacts-Search-Input-Box"),
				$searchInput = $searchInputBox.find(".c-AssignContact-Box-Contacts-Search-Input");

		$searchInput.val("").trigger("keyup");

		$searchInputBox.hide();
		$searchlabel.show();

		return false;
	});






	$(".c-AssignContact-Box-Footer").on("click", ".c-AssignContact-Box-Footer-Btn", function (e) {
		var $this = $(this),
				$parent = $this.parents('.c-AssignContact-Box'),
				ele = $parent.find('.c-ScrollContent'),
				speed = 25, scroll = ele.height(), scrolling;
		//console.log(ele);
		if ($this.hasClass("c-AssignContact-Box-Footer-Next")) {
			// Scroll the element up
			ele.animate({
				scrollTop: (ele.scrollTop() - scroll) + "px"
			});
		} else {
			// Scroll the element down
			ele.animate({
				scrollTop: (ele.scrollTop() + scroll) + "px"
			});
		}
		return false;
	})


});