/** whitelist **/
$(document).ready(function () {
	var xhr;
	var fullScreenOverlay = $('.reportstatics-overlay');
	var innerOffsetBox = fullScreenOverlay.find('.inner-fullscreen-offset');

	var checkReportStaticsArrowStatus = function () {
		//var $top = Math.max(0, (($(window).height() - $(".contactupload-overlay .navigation-overlay").outerHeight()) / 2)) - 69 + "px";
		//$(".contactupload-overlay .navigation-overlay").css("top", $top);
		var $option = $(".contactupload-overlay .options.active"),
				$set = $option.attr("data-set");
		$(".contactupload-overlay .navigation-overlay .active").removeClass("active");
		$(".contactupload-overlay .u-navigationCircle[data-set='" + $set + "']").addClass("active");
		$(".contactupload-overlay .u-navigationCircle.active").prevAll().addClass("u-setDone");
	};

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


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

	removeReportStaticsOverlay = function () {
		$('body').removeClass('activate-full-screen');
		$('.reportstatics-overlay').removeClass('activate-overlay-screen');
		$(".reportstatics-overlay .overlay-item").removeClass("active");
		$(".reportstatics-overlay").find(".active").removeClass("active");
		$(".reportstatics-overlay").find(".c-found").removeClass("c-found");
		$(".reportstatics-overlay .navigation-overlay a").not(".u-gotOSummary").show();
		$('.reportstatics-overlay .overlay-item').hide();
		$(".reportstatics-overlay .overlay-item input").val("");
		$(".reportstatics-overlay .overlay-item .option-container-firstbox").show();
		$(".reportstatics-overlay .overlay-item .option-container-secondbox").hide();
		$(".reportstatics-overlay .navigation-overlay .u-setDone").removeClass("u-setDone");
		$(".reportstatics-overlay .u-Selected").removeClass("u-Selected");
		$(".reportstatics-overlay .u-filled").removeClass("u-filled");
		$(".reportstatics-overlay .hovered").removeClass("hovered");
		$(".reportstatics-overlay input[type='text']").val('');
		$(".reportstatics-overlay .c-ReportsStats-Location").text("LOCATION").removeAttr('data-val');
		$(".reportstatics-overlay .c-Reports-Stats-LocationForm, .reportstatics-overlay .c-Reports-Stats-CaptureForm").hide();
		$(".reportstatics-overlay .c-Reports-Stats-Graph, .reportstatics-overlay .c-Reports-Stats-CaptureForm-Field-Selection").show();
		$(".u-ReportsStatic-Active").removeClass("u-ReportsStatic-Active");
	};
	var GraphFooterButtonsTimer = false;
	var animateGraphFooterButtons = function ($footer) {
		if (!$footer.find(".animate").length) {
			$footer.find(".c-Reports-Stats-Graph-Footer-Block").first().addClass("animate");
		}
		$footer.find(".c-Reports-Stats-Graph-Footer-Block.animate").fadeOut(function () {
			var next = $(this).next(".c-Reports-Stats-Graph-Footer-Block");
			$(this).removeClass("animate");
			if (!next.length) {
				next = $footer.find(".c-Reports-Stats-Graph-Footer-Block").first();
			}
			next.addClass("animate").fadeIn(function () {
				clearTimeout(GraphFooterButtonsTimer);
				GraphFooterButtonsTimer = setTimeout(function () {
					animateGraphFooterButtons($footer);
				}, 3000);
			});
		});
	};
	//animateGraphFooterButtons();

	fullScreenOverlay.on("click", ".c-Reports-Statistic-Create-Buttons .c-Reports-Statistic-Create-Button", function () {
		var $this = $(this),
				$tab = $this.attr("data-tab"),
				container = $this.closest('.tab-container'),
				$parentsTabGroups = $this.parents(".c-Reports-Section-TabsGroup"),
				$tabs = $parentsTabGroups.find(".c-Reports-Statistic-Create-Tabs");
		if (undefined !== $tab || "" !== $tab) {
			$(".c-Reports-Statistic-Create-Buttons .c-Reports-Statistic-Create-Button").removeClass("u-Statistic-Button-Selected");
			$this.addClass("u-Statistic-Button-Selected");
			$parentsTabGroups.find(".c-Reports-Statistic-Create-Tabs.u-Statistic-Tab-Active").fadeOut(function () {
				$tabs.removeClass("u-Statistic-Tab-Active");
				$parentsTabGroups.find("." + $tab).fadeIn(function () {
					$(this).addClass("u-Statistic-Tab-Active");
					container.css('height', 'auto');
				});
			});
		}

		return false;
	}).on("click", ".c-Reports-Stats-CaptureForm-Durations .c-Reports-Stats-CaptureForm-Duration-Slote", function () {
		var $this = $(this),
				$parents = $this.parents(".c-Reports-Add-Section-ReportsBlock");

		$this.addClass('active').siblings().removeClass('active');
		$parents.find(".c-Reports-Stats-CaptureForm-Field-Selection-Button").val("").removeClass("active u-filled");
		return false;
	}).on("click", ".c-Reports-Stats-CaptureForm-Button-Save", function () {
		var $this = $(this),
				$parents = $this.parents(".c-Reports-Add-Section-ReportsBlock"),
				$allow = true;
		var DurationDateRange = '';
		var $duration = $parents.find(".c-Reports-Stats-CaptureForm-Durations .c-Reports-Stats-CaptureForm-Duration-Slote.active");

		$parents.attr({"data-startdate": '', "data-enddate": '', "data-duration": '', "data-location": ""});

		if (!$duration.length) {
			//$allow = false;
		} else {
			$parents.attr("data-duration", $duration.index());
			DurationDateRange = $duration.text();
		}

		if ('' == DurationDateRange) {
			var $startdate = $parents.find(".c-Reports-Stats-StartDate").attr("data-date"),
					$enddate = $parents.find(".c-Reports-Stats-EndDate").attr("data-date");

			if ('' == $startdate || undefined == $startdate) {
				//alert("Please enter valid start date.");
			} else if ('' == $enddate || undefined == $enddate) {
				alert("Please enter valid end date.");
			} else {
				$parents.attr({"data-startdate": $startdate});
				$parents.attr({"data-enddate": $enddate});
				DurationDateRange = moment($startdate).format("Do MMM YY") + ' - ' + moment($enddate).format("Do MMM YY");
			}
		}
		if ('' == DurationDateRange) {
			$allow = false;
			alert("Please select duration or enter valid daterange!");
		}

		var $location = $parents.find(".c-ReportsStats-Location"),
				$location_val = $location.attr("data-val");
		if ($location_val == "" || undefined == $location_val) {
			$allow = false;
			alert("Please enter valid location!");
		} else {
			$parents.attr({"data-location": $location.attr("data-val")});
		}

		if ($allow) {
			$parents.find(".c-Reports-Stats-CaptureForm").fadeOut(function () {
				$parents.find(".c-Reports-Stats-Graph").fadeIn().attr("data-action", "");
				$parents.find(".c-Reports-Stats-Graph-Footer .c-Reports-Stats-Graph-DurationDateRange").html(DurationDateRange);
				$parents.find(".c-Reports-Stats-Graph-Footer .c-Reports-Stats-Graph-Location").html($location.attr("data-val"));
				$parents.addClass("u-Selected");
				animateGraphFooterButtons($parents.find(".c-Reports-Stats-Graph-Footer"));
			});
		}
		return false;
	}).on("click", ".c-Reports-Stats-CaptureForm-Button-Cancel", function () {
		var $this = $(this),
				$parents = $this.parents(".c-Reports-Add-Section-ReportsBlock");
		if ($parents.find(".c-Reports-Stats-CaptureForm").attr("data-action") != "edit") {
			$parents.removeClass("active u-Selected");
		}
		$parents.find(".c-Reports-Stats-CaptureForm").fadeOut(function () {
			$parents.find(".c-Reports-Stats-Graph").fadeIn();
		});
		return false;
	}).on("click", ".c-Reports-Stats-Graph", function (e) {
		var $this = $(this),
				$target = $(e.target),
				$parents = $this.parents(".c-Reports-Add-Section-ReportsBlock");

		if ($parents.hasClass('active')) {
			fullScreenOverlay.find(".c-Reports-Add-Section-ReportsBlock").removeClass("active u-Selected");
		} else {
			if (fullScreenOverlay.find(".c-Reports-Add-Section-ReportsBlock.active").length) {
				fullScreenOverlay.find(".c-Reports-Add-Section-ReportsBlock.active").find(".c-Reports-Stats-LocationForm").hide();
				fullScreenOverlay.find(".c-Reports-Add-Section-ReportsBlock.active").find(".c-Reports-Stats-CaptureForm").fadeOut(function () {
					$(this).parents(".c-Reports-Add-Section-ReportsBlock").find(".c-Reports-Stats-Graph").fadeIn(function () {
						fullScreenOverlay.find(".c-Reports-Add-Section-ReportsBlock").removeClass("active  u-Selected");
						$parents.addClass("active");

						$parents.find(".c-Reports-Stats-Graph").fadeOut(function () {
							$parents.find(".c-Reports-Stats-LocationForm").hide();
							$parents.find(".c-Reports-Stats-CaptureForm").fadeIn();
						});
					});
				});
			} else {
				fullScreenOverlay.find(".c-Reports-Add-Section-ReportsBlock").removeClass("active u-Selected");
				$parents.addClass("active");

				$parents.find(".c-Reports-Stats-Graph").fadeOut(function () {
					$parents.find(".c-Reports-Stats-CaptureForm").fadeIn();
				});
			}
		}
		return false;
	}).on("click", ".c-Reports-Stats-Graph-Footer", function () {
		var $this = $(this),
				$parents = $this.parents(".c-Reports-Add-Section-ReportsBlock");

		$parents.find(".c-Reports-Stats-Graph").fadeOut(function () {
			$parents.find(".c-Reports-Stats-CaptureForm").attr("data-action", 'edit').fadeIn();
		});
		return false;
	}).on("click", ".c-Reports-Stats-CaptureForm .c-ReportsStats-Location", function () {
		var $this = $(this),
				$parents = $this.parents(".u-Report-Static-Created");

		$this.addClass("active");

		$parents.find(".c-Reports-Stats-CaptureForm").fadeOut(function () {
			$parents.find(".c-Reports-Stats-LocationForm").fadeIn();
		});
		return false;
	}).on("click", ".c-Reports-Stats-LocationForm .c-Reports-Stats-LocationForm-Item", function () {
		var $this = $(this);
		$this.find("a").toggleClass("active");
		return false;
	}).on("keyup", ".c-Reports-Stats-LocationForm .c-Reports-Stats-LocationForm-Search-Input", function () {
		var $this = $(this),
				$val = $this.val(),
				$parents = $this.parents(".c-Reports-Stats-LocationForm"),
				$searchBox = $this.parents(".c-Reports-Stats-LocationForm-Search"),
				$searchInputBox = $searchBox.find(".c-Reports-Stats-LocationForm-Search-Input-Box"),
				$clearBtn = $searchInputBox.find(".c-Reports-Stats-LocationForm-Search-Input-Clear"),
				$contactsList = $parents.find(".c-Reports-Stats-LocationForm-List .c-Reports-Stats-LocationForm-Item a");
		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-Reports-Stats-LocationForm-Search-Input-Clear", function () {
		var $this = $(this),
				$searchBox = $this.parents(".c-Reports-Stats-LocationForm-Search"),
				$searchInputBox = $searchBox.find(".c-Reports-Stats-LocationForm-Search-Input-Box"),
				$searchInput = $searchInputBox.find(".c-Reports-Stats-LocationForm-Search-Input");

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

		return false;
	}).on("click", ".c-Reports-Stats-LocationForm .c-Reports-Stats-LocationForm-Footer .c-Reports-Stats-LocationForm-Footer-Btn", function (e) {
		var $this = $(this),
				$parent = $this.parents('.c-Reports-Add-Section-ReportsBlock-Content-Block'),
				ele = $parent.find('.c-ScrollContent'),
				speed = 25, scroll = ele.height(), scrolling;
		//console.log(ele);
		if ($this.hasClass("c-Reports-Stats-LocationForm-Footer-Next")) {
			// Scroll the element up
			ele.animate({
				scrollTop: (ele.scrollTop() - scroll) + "px"
			});
		} else if ($this.hasClass("c-Reports-Stats-LocationForm-Footer-Previous")) {
			// Scroll the element down
			ele.animate({
				scrollTop: (ele.scrollTop() + scroll) + "px"
			});
		} else if ($this.hasClass("c-Reports-Stats-LocationForm-Footer-Close")) {
			var $Created = $this.parents(".u-Report-Static-Created"),
					$locationForm = $Created.find(".c-Reports-Stats-LocationForm");
			//$locationForm.find("active").removeClass("active");
			//$Created.find(".c-Reports-Stats-CaptureForm .c-ReportsStats-Location").removeClass("active").text("LOCATION").attr("data-val", '');
			$Created.find(".c-Reports-Stats-LocationForm").fadeOut(function () {
				$Created.find(".c-Reports-Stats-CaptureForm").fadeIn();
			});
		} else if ($this.hasClass("c-Reports-Stats-LocationForm-Footer-Save")) {
			var $Created = $this.parents(".u-Report-Static-Created"),
					$locationForm = $Created.find(".c-Reports-Stats-LocationForm"),
					$selectedLocatios = $locationForm.find(".c-Reports-Stats-LocationForm-Item a.active");	
			if ($selectedLocatios.length) {

				var selectedArray = $selectedLocatios.map(function () {
					return $(this).text();
				}).get(); // ["bat", "ball"]
				var locations = selectedArray.join(); // "bat, ball"
				$Created.find(".c-Reports-Stats-CaptureForm .c-ReportsStats-Location").addClass("active").text(locations).attr("data-val", locations);
				$Created.find(".c-Reports-Stats-LocationForm").fadeOut(function () {
					$Created.find(".c-Reports-Stats-CaptureForm").fadeIn();
				});
			} else {
				alert("Please select atleast one location please.");
			}
		}
		return false;
	}).on("click", '.set-one .c-Footer-Cancel-Button', function () {
		removeReportStaticsOverlay();
		return false;
	}).on("click", '.set-one .c-Done-Footer', function () {
		var selectedBlock = fullScreenOverlay.find(".c-Reports-Add-Section-ReportsBlock.u-Selected");

		if (!selectedBlock.length) {
			alert("Please select one report!");
		} else {

			var container = $('.tab-reports .tab-container'),
					$mainParents = container.find(".c-Reports-Create-Section"),
					$parentsSection = container.find(".c-Reports-Statistics-Section"),
					$reportBlockMini = $parentsSection.find(".c-Reports-Section-Block.u-ReportsStatic-Active"),
					$reportBlockBig = $parentsSection.find(".c-Reports-Add-Section .u-ReportsStatic-Active");

			console.log(reportdata);
			if ($reportBlockMini.length) {

				if (undefined == reportdata.reportdeletemini) {
					reportdata.reportdeletemini = new Array();
				}
				if (undefined == reportdata.reportaddmini) {
					reportdata.reportaddmini = new Array();
				}
				reportdata.reportaddmini[$reportBlockMini.index()] = $reportBlockMini.html();
				reportdata.reportdeletemini[$reportBlockMini.index()] = '';

				$reportBlockMini.html('<a class="c-Reports-Section-Block-Link" href="#"><span class="c-Reports-Section-Block-Content">50K</span><span class="c-Reports-Section-Block-Title">TOTAL VISITS PER MONTH</span></a><a class="c-Reports-Section-Block-Delete u-DeleteReportStatic icon-cross c-CircleIcon" href="#"></a>').removeClass("u-Report-Static-Create").addClass("u-Report-Static-Created");
				$reportBlockMini.attr({"data-duration": selectedBlock.attr("data-duration"), "data-date": selectedBlock.attr("data-date"), "data-location": selectedBlock.attr("data-location")});
			} else if ($reportBlockBig.length) {
				var bigblock = $reportBlockBig.parents(".c-Reports-Add-Section");

				if (undefined == reportdata.reportdeletebig) {
					reportdata.reportdeletebig = new Array();
				}
				if (undefined == reportdata.reportaddbig) {
					reportdata.reportaddbig = new Array();
				}
				reportdata.reportaddbig[(bigblock.index() - 1)] = bigblock.html();
				reportdata.reportdeletebig[(bigblock.index() - 1)] = '';

				bigblock.html($("#u-CreatedReport-Template").html()).addClass("u-Report-Static-Created");
				bigblock.find(".c-Reports-Add-Section-ReportsBlock-Text").text(selectedBlock.attr("data-location"));
				bigblock.find(".c-Reports-Add-Section-ReportsBlock").attr({"data-duration": selectedBlock.attr("data-duration"), "data-date": selectedBlock.attr("data-date"), "data-location": selectedBlock.attr("data-location")});
				intializeChart();
			}
			removeReportStaticsOverlay();

			openSaveActionPopup();
		}
		return false;
	});

	/*$(".c-ReportsStats-DatRange").on("focus", function () {
	 $(this).val('');
	 return false;
	 }).daterange({
	 dateFormat: "yy-mm-dd",
	 'autoclose': true,
	 rangeSeparator: "#", // the string between first and last date
	 beforeShow: function (input, event) {
	 },
	 onClose: function (dateRangeText) {
	 var $this = $(this);
	 if ('' != dateRangeText) {
	 var date = dateRangeText.split("#");
	 if (date.length == 2) {
	 var start = moment(date[0]).format('YYYY/MM/DD');
	 var end = moment(date[1]).format('YYYY/MM/DD');
	 $this.attr("data-datestart", start).attr("data-dateend", end).val(moment(date[0]).format("Do MMM YY") + " - " + moment(date[1]).format("Do MMM YY"));
	 
	 $this.parents(".c-Reports-Add-Section-ReportsBlock-Content-Block").find(".c-Reports-Stats-CaptureForm-Duration-Slote").removeClass("active");
	 } else {
	 $this.attr("data-datestart", '').attr("data-dateend", '').val('');
	 }
	 } else {
	 $this.attr("data-datestart", '').attr("data-dateend", '').val('');
	 }
	 }
	 });*/

	$(".c-Reports-Stats-StartDate").datepicker({
		dateFormat: "yy-mm-dd",
		'autoclose': true,
		beforeShow: function (input, event) {
		},
		onSelect: function (dateText, inst) {
			var $this = $(this);

			var dt2 = $('.c-Reports-Stats-EndDate');
			var startDate = $this.datepicker('getDate');

			startDate.setDate(startDate.getDate() + 1);

			var minDate = $this.datepicker('getDate');

			//minDate of dt2 datepicker = dt1 selected day
			dt2.datepicker('setDate', startDate).val('');
			//first day which can be selected in dt2 is selected date in dt1
			dt2.datepicker('option', 'minDate', startDate);

			$this.attr("data-date", moment(dateText).format('YYYY/MM/DD')).val(moment(dateText).format("D.M.YY"));
			$this.parents(".c-Reports-Add-Section-ReportsBlock-Content-Block").find(".c-Reports-Stats-CaptureForm-Duration-Slote").removeClass("active");
		}
	});

	$(".c-Reports-Stats-EndDate").datepicker({
		dateFormat: "yy-mm-dd",
		'autoclose': true,
		beforeShow: function (input, event) {
		},
		onSelect: function (dateText, inst) {
			var $this = $(this);

			$this.attr("data-date", moment(dateText).format('YYYY/MM/DD')).val(moment(dateText).format("D.M.YY"));
			$this.parents(".c-Reports-Add-Section-ReportsBlock-Content-Block").find(".c-Reports-Stats-CaptureForm-Duration-Slote").removeClass("active");
		}
	});
});