if ($) {
	$(document).ready(function () {
		reportdata = new Array();
		var $tabReports = $(".tab-reports");
		
		animateScrollTop = function($top, $duration){
			if(undefined == $duration){
				$duration = 800;
			}
			$('html, body').stop().animate({scrollTop: $top}, $duration);
		};
		
		//INITIALIZE SPARKLINE CHARTS
		intializeChart = function () {
			if ($(".sparkline").length) {
				$(".sparkline").each(function () {
					var $this = $(this);
					var range_map = $.range_map({
						10: 'red', 8: '#F05C52', 14: '#F4C63C', 13: '#D07B04', 11: '#453C3F', 15: '#59922B', 18: '#0743D7', 12: '#767E81'
					});
					$this.sparkline([10, 8, 14, 13, 11, 15, 18, 12], {type: $this.attr("data-type"), width: '100%', height: $this.attr("data-height"), 'barWidth': $this.attr("data-bar-Width"), 'barSpacing': $this.attr("data-bar-Spacing"), colorMap: range_map});
				});
			}
		};

		$(window).resize(function(e) {
			intializeChart();
		});

		closeSaveActionPopup = function () {

			if (reportdata.deletereport) {
				$.each(reportdata.deletereport, function (i, data) {
					if (undefined != data && '' != data) {
						var block = $(".c-Reports-Create-Block-Main").eq(i);
						block.html(data);
						intializeChart();
					}
				});
			}

			var activeAjaxReportRab = $(".c-Reports-Create-Block-Detail-Ajax.active");
			if (activeAjaxReportRab.length) {
				if (reportdata.name) {
					activeAjaxReportRab.find(".u-ReportName-Field").val(reportdata.name);
				}
				if (undefined != reportdata.checkedstatus) {
					if (reportdata.checkedstatus == 0) {
						activeAjaxReportRab.find(".c-Report-Status-Button-Off").trigger("click");
					} else if (reportdata.checkedstatus == 1) {
						activeAjaxReportRab.find(".c-Report-Status-Button-On").trigger("click");
					}
				}

				if (undefined != reportdata.schedule) {
					activeAjaxReportRab.find(".c-Report-Schedule-Block").not(".c-Report-Schedule-Period-Block").fadeOut();
					activeAjaxReportRab.find(".c-Report-Schedule-Block-Selection").removeClass("active");
					activeAjaxReportRab.find(".c-Report-Schedule-Block").find("input").val('');
					activeAjaxReportRab.find(".u-ReportTimePicker").val("");
				}

				if (reportdata.reportdeletemini) {
					$.each(reportdata.reportdeletemini, function (i, data) {
						if (undefined != data && '' != data) {
							console.log('delete min', i);
							var $reportBlockMini = activeAjaxReportRab.find(".c-Reports-Section-Block").eq(i);
							$reportBlockMini.html(data).removeClass("u-Report-Static-Create").addClass("u-Report-Static-Created");
						}
					});
				}
				if (reportdata.reportdeletebig) {
					$.each(reportdata.reportdeletebig, function (i, data) {
						if (undefined != data && '' != data) {
							console.log('delete big', i);
							var $reportBlockBig = activeAjaxReportRab.find(".c-Reports-Add-Section").eq(i);
							$reportBlockBig.html($("#u-AddReport-Template").html()).removeClass("u-Report-Static-Create").addClass("u-Report-Static-Created");
						}
					});
					intializeChart();
				}

				if (reportdata.reportaddmini) {
					$.each(reportdata.reportaddmini, function (i, data) {
						if (undefined != data && '' != data) {
							console.log('add mini', i);
							var $reportBlockMini = activeAjaxReportRab.find(".c-Reports-Section-Block").eq(i);
							$reportBlockMini.html(data).removeClass("u-Report-Static-Created").addClass("u-Report-Static-Create");
						}
					});
				}
				if (reportdata.reportaddbig) {
					$.each(reportdata.reportaddbig, function (i, data) {
						if (undefined != data && '' != data) {
							console.log('add big', i);
							var $reportBlockBig = activeAjaxReportRab.find(".c-Reports-Add-Section").eq(i);
							console.log($reportBlockBig);
							$reportBlockBig.html($("#u-AddStaticsBig-Template").html()).removeClass("u-Report-Static-Created").addClass("u-Report-Static-Create");
						}
					});
				}

				if (reportdata.subscriptionsstatus) {
					$.each(reportdata.subscriptionsstatus, function (i, status) {
						console.log('subscription status', i, status);
						if (undefined != status && 0 <= status) {
							var $subscriptionblock = activeAjaxReportRab.find(".c-Reports-Subscriptions-Sections-Blocks .c-Reports-Subscriptions-Block[data-id='" + i + "']");
							var $checked = false;
							if (status == 1) {
								$checked = true;
							}
							$subscriptionblock.find(".c-Reports-Subscriptions-Details-Text .u-Switch-Checkbox").attr("checked", $checked);
						}
					});
				}

				if (reportdata.deletesub) {
					$.each(reportdata.deletesub, function (i, data) {
						if (undefined != data && '' != data) {
							console.log('deletesub', data);
							var block = activeAjaxReportRab.find(".c-Reports-Subscriptions-Block-Main").eq(i);
							block.html(data);
						}
					});
				}
				if (reportdata.addsub) {
					$.each(reportdata.addsub, function (i, data) {
						if (undefined != data && '' != data) {
							console.log('deletesub', data);
							var block = activeAjaxReportRab.find(".c-Reports-Subscriptions-Block-Main").eq(i);
							block.html(data);
						}
					});
				}
			}
			$("#report-saveaction").removeClass("active").fadeOut(function () {
				reportdata = new Array();
			});

		};
		openSaveActionPopup = function () {
			$("#report-saveaction").addClass("active").fadeIn();
		};

		$tabReports
				.on("click", ".u-Reports-OpenDetail", function (e, $scrollTo) {
					var $this = $(this),
							container = $this.closest('.tab-container'),
							$parentsBlocks = $this.parents(".c-Reports-Create-Blocks"),
							$parentsMain = $this.parents(".c-Reports-Create-Block-Main"),
							$block = $parentsMain.find(".c-Reports-Create-Block"),
							$ajaxBlock = $parentsMain.find(".c-Reports-Create-Block-Detail-Ajax"),
							$id = $block.attr("data-id");
					var $activeTabs = $parentsBlocks.find(".c-Reports-Create-Block-Detail-Ajax.active");
					if (!$activeTabs.length) {
						$activeTabs = $ajaxBlock;
					}
					$activeTabs.slideUp(function () {
						container.css('height', 'auto');
						if (!$ajaxBlock.hasClass("active")) {
							$.post("", {"get-report": true, "report-id": $id}, function (data) {
								if (data) {
									$ajaxBlock.html(data);
									container.css('height', 'auto');

									if ($id) {
										var reportName = $block.find(".c-Reports-Create-Text").text(),
												reportSendDate = $block.attr("data-date-send"),
												reportSendTime = $block.attr("data-time-send"),
												reportTriggerDate = $block.attr("data-date-trigger"),
												reportCapture = $block.attr("data-capture");

										$ajaxBlock.find(".u-ReportName-Field").val(reportName);
										$ajaxBlock.find(".u-SendReportDate").val(reportSendDate);
										$ajaxBlock.find(".u-SendReportTime").val(reportSendTime);
										$ajaxBlock.find(".u-CaptureReportDate").val(reportTriggerDate);
										$ajaxBlock.find(".c-Form-Block-Field-Block-Button").eq(reportCapture).addClass("u-Selected");

									}

									$ajaxBlock.slideDown(function () {
										$ajaxBlock.addClass("active");
										intializeChart();

										container.css('height', 'auto');
										setTimeout(function () {
											container.css('height', 'auto');
										}, 200);

										if ($ajaxBlock.find('.' + $scrollTo).length) {
											animateScrollTop($ajaxBlock.find('.' + $scrollTo).offset().top - 100);
										}
									});
								}
							});
						} else {
							closeSaveActionPopup();
							$(".c-Reports-Create-Block-Detail-Ajax").removeClass("active");
						}
					});
					return false;
				})
				.on("click", ".c-Reports-Created-Details-Left", function (e, $scrollTo) {
					var $this = $(this),
							$parentsBlocks = $this.parents(".c-Reports-Created");
					$parentsBlocks.find(".u-Reports-OpenDetail").trigger("click");
					return false;
				})
				.on("click", ".u-Reports-DeleteDetail", function () {
					var $this = $(this),
							container = $this.closest('.tab-container'),
							$parentsBlocks = $this.parents(".c-Reports-Create-Blocks"),
							$parentsMain = $this.parents(".c-Reports-Create-Block-Main"),
							$block = $parentsMain.find(".c-Reports-Create-Block"),
							$ajaxBlock = $parentsMain.find(".c-Reports-Create-Block-Detail-Ajax"),
							$id = $block.attr("data-id");

					openDeletePoup();
					deleteOverlay.find(".c-DeleteConfirm-Button-Yes").unbind("click")
						.on("click", function(){
							if (undefined == reportdata.deletereport) {
								reportdata.deletereport = new Array();
							}
							reportdata.deletereport[$parentsMain.index()] = $parentsMain.html();

							var $activeTabs = $parentsBlocks.find(".c-Reports-Create-Block-Detail-Ajax.active");
							if (!$activeTabs.length) {
								$activeTabs = $ajaxBlock;
							}

							$block.html($("#u-ReportHeaderAdd-Template").html()).removeClass("c-Reports-Created");
							$block.attr({"data-id": "", "data-date-send": "", "data-time-send": "", "data-capture": "", "data-date-trigger": ""});
							$block.find(".c-Reports-Create-Number").text($parentsMain.index() + 1);
							if ($ajaxBlock.hasClass("active")) {
								$ajaxBlock.slideUp(function () {
									$ajaxBlock.html("");
									$ajaxBlock.removeClass("active");
								});
							}
							openSaveActionPopup();
							$(".u-CreatedReportsNumber").text($(".c-Reports-Create-Block-Main .c-Reports-Create-Block.c-Reports-Created").length);
						});			
					
					return false;
				})
				.on("click", ".c-Btn-Delete-Report", function () {
					var $this = $(this),
							container = $this.closest('.tab-container'),
							$parentsBlocks = $this.parents(".c-Reports-Create-Blocks"),
							$parentsMain = $this.parents(".c-Reports-Create-Block-Main");

					$parentsMain.find(".u-Reports-DeleteDetail").trigger("click");
					return false;
				})
				.on("click", ".u-Reports-Created-SubscribedTab", function () {
					var $this = $(this),
							$parentMain = $this.parents(".c-Reports-Create-Block-Main"),
							$parent = $this.parents(".c-Reports-Created-Details"),
							$openDetails = $parent.find(".u-Reports-OpenDetail"),
							$ajaxBlock = $parentMain.find(".c-Reports-Create-Block-Detail-Ajax");
					if ($ajaxBlock.hasClass("active")) {
						if ($ajaxBlock.find('.c-Reports-Subscriptions-Section').length) {
							animateScrollTop($ajaxBlock.find('.c-Reports-Subscriptions-Section').offset().top - 100);
						}
					} else {
						$openDetails.trigger("click", 'c-Reports-Subscriptions-Section');
					}
					return false;
				})
				.on("click", ".c-Reports-Create-Navigations-Button", function () {
					var $this = $(this),
							$id = $this.attr("href"),
							$parentMain = $this.parents(".c-Reports-Create-Block-Main"),
							$parent = $this.parents(".c-Reports-Created-Details"),
							$ajaxBlock = $parentMain.find(".c-Reports-Create-Block-Detail-Ajax");
					if ($($id).length) {
						animateScrollTop($ajaxBlock.find($id).offset().top - 50);
						$(".c-Reports-Create-Navigations-Button").removeClass("c-Reports-Create-Navigations-Active");
						$this.addClass("c-Reports-Create-Navigations-Active");
					}
					return false;
				})
				.on("click", ".u-Reports-Created-ReportsTab", function () {
					var $this = $(this),
							$parentMain = $this.parents(".c-Reports-Create-Block-Main"),
							$parent = $this.parents(".c-Reports-Created-Details"),
							$openDetails = $parent.find(".u-Reports-OpenDetail"),
							$ajaxBlock = $parentMain.find(".c-Reports-Create-Block-Detail-Ajax");
					if ($ajaxBlock.hasClass("active")) {
						if ($ajaxBlock.find('.c-Reports-Sections').length) {
							animateScrollTop($ajaxBlock.find('.c-Reports-Sections').offset().top - 100);
						}
					} else {
						$openDetails.trigger("click", 'c-Reports-Sections');
					}
					return false;
				})
				.on("click", ".u-Report-Static-Create", function () {
					var $this = $(this),
							container = $this.closest('.tab-container'),
							$parentsSection = $this.parents(".c-Reports-Statistics-Section"),
							$createBlock = $parentsSection.find(".c-Reports-Statistic-CreateBlock"),
							$reportBlock = $parentsSection.find(".c-Reports-Statistics");

					var fullScreenOverlay = $('.reportstatics-overlay');

					removeReportStaticsOverlay();
					addReportStaticsOverlay();

					$(".u-ReportsStatic-Active").removeClass("u-ReportsStatic-Active");
					$this.addClass("u-ReportsStatic-Active");
					/*extendOverlay('contactupload');*/
					var outerTarget = fullScreenOverlay.find('.overlay-item').css({display: 'block'});

					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;
				})
				.on("click", ".c-Form-Block-Field-Block-Big .c-Form-Block-Field-Block-Buttons .c-Form-Block-Field-Block-Button", function () {
					var $this = $(this),
							$parents = $this.parents(".c-Form-Block-Field-Block-Buttons");
					$parents.find(".u-Selected").removeClass("u-Selected");
					$this.addClass("u-Selected");
					return false;
				})
				.on("click", ".c-Reports-Statistic-CreateBlock .c-Reports-Add-Section .u-Report-Static-Created", function () {
					var $this = $(this),
							container = $this.closest('.tab-container'),
							$mainParents = $this.parents(".c-Reports-Create-Section"),
							$parentsSection = $this.parents(".c-Reports-Statistics-Section"),
							$createBlock = $parentsSection.find(".c-Reports-Statistic-CreateBlock"),
							$reportBlock = $parentsSection.find(".c-Reports-Statistics"),
							$index = $createBlock.attr("data-index"),
							$addSectionReportsBlock = $parentsSection.find(".c-Reports-Add-Section-ReportsBlock"),
							$header = $addSectionReportsBlock.find(".c-Reports-Add-Section-ReportsBlock-Header"),
							$number = $header.find(".c-Reports-Add-Section-ReportsBlock-Details-Number").html();
					$mainParents.find(".u-Report-Static-Created.u-Selected").removeClass("u-Selected");
					$this.addClass("u-Selected");
					var $reportBlockMini = $parentsSection.find(".c-Reports-Section-Block.u-ReportsStatic-Active"),
							$reportBlockBig = $parentsSection.find(".c-Reports-Add-Section .u-ReportsStatic-Active");

					if ($reportBlockMini.length) {
						$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");
						$reportBlockBig.attr({"data-report": $this.index(), "data-reportcat": $(".u-Statistic-Button-Selected").index()});
					} else if ($reportBlockBig.length) {
						$this.parents(".c-Reports-Add-Section").find(".c-Reports-Add-Section-ReportsBlock-ChartReport-HoverIcon").hide();
						$reportBlockBig.html($("#u-AddReport-Template").html()).removeClass("u-Report-Static-Create").addClass("u-Report-Static-Created");
						$reportBlockBig.find(".c-Reports-Add-Section-ReportsBlock-Text").text($this.parents(".c-Reports-Add-Section").find(".c-Reports-Add-Section-ReportsBlock-Text").text());
						$reportBlockBig.attr({"data-report": $parentsSection.find(".c-Reports-Add-Section").index(), "data-reportcat": $(".u-Statistic-Button-Selected").index()});
					}

					$createBlock.fadeOut(function () {
						$reportBlock.fadeIn(function () {
							container.css('height', 'auto');
							$createBlock.attr("data-index", $this.index());
							intializeChart();
							setTimeout(function () {
								$this.removeClass("u-Selected");
							}, 1000);
						});
						setTimeout(function () {
							container.css('height', 'auto');
						}, 200);
					});
					return false;
				})
				.on("click", ".c-Reports-Statistic-CreateBlock .c-Reports-Statistic-CreateBlock-Close", function () {
					var $this = $(this),
							container = $this.closest('.tab-container'),
							$parentsSection = $this.parents(".c-Reports-Statistics-Section"),
							$createBlock = $parentsSection.find(".c-Reports-Statistic-CreateBlock"),
							$reportBlock = $parentsSection.find(".c-Reports-Statistics");

					$createBlock.fadeOut(function () {
						$reportBlock.fadeIn(function () {
							container.css('height', 'auto');
							$createBlock.attr("data-index", $this.index());
						});
						setTimeout(function () {
							container.css('height', 'auto');
						}, 200);
					});
					return false;
				})
				.on("click", ".c-Reports-Statistics .u-DeleteReportStatic", function () {
					var $this = $(this),
							container = $this.closest('.tab-container'),
							$parentsSection = $this.parents(".c-Reports-Section-Group"),
							$parentsSelection = $this.parents(".u-Report-Static-Created"),
							$index = $parentsSelection.index();

					openDeletePoup();
					deleteOverlay.find(".c-DeleteConfirm-Button-Yes").unbind("click")
						.on("click", function(){		
						if ($this.parents(".c-Reports-Add-Section").length) {
							$parentsSelection = $this.parents(".c-Reports-Add-Section");
							$index = $parentsSelection.index() - 1;
						}

						if ($this.parents(".c-Reports-Section-Block").length) {
							if (undefined == reportdata.reportdeletemini) {
								reportdata.reportdeletemini = new Array();
							}
							if (undefined == reportdata.reportaddmini) {
								reportdata.reportaddmini = new Array();
							}
							var $reportBlockMini = $parentsSection.find(".c-Reports-Section-Block").eq($index);
							reportdata.reportdeletemini[$index] = $reportBlockMini.html();
							reportdata.reportaddmini[$index] = '';
							$reportBlockMini.html($("#u-AddStaticsMini-Template").html()).removeClass("u-Report-Static-Created").addClass("u-Report-Static-Create");
						} else if ($this.parents(".c-Reports-Add-Section-ReportsBlock").length) {
							if (undefined == reportdata.reportdeletebig) {
								reportdata.reportdeletebig = new Array();
							}
							if (undefined == reportdata.reportaddbig) {
								reportdata.reportaddbig = new Array();
							}
							var $reportBlockBig = $parentsSection.find(".c-Reports-Add-Section").eq($index);
							reportdata.reportdeletebig[$index] = $reportBlockBig.html();
							reportdata.reportaddbig[$index] = '';
							$reportBlockBig.html($("#u-AddStaticsBig-Template").html()).removeClass("u-Report-Static-Created").addClass("u-Report-Static-Create");
						}

						container.css('height', 'auto');

						openSaveActionPopup();
					});
					return false;
				})
				.on("click", ".u-ReportNameCreate-Button", function () {
					var $this = $(this),
							$parentField = $this.parents(".c-Form-Block-Fields"),
							$input = $parentField.find(".u-ReportName-Field"),
							$name = $.trim($input.val()),
							$parentsBlockMain = $this.parents(".c-Reports-Create-Block-Main"),
							$parents = $this.parents(".c-Reports-Create-Block-Detail"),
							$id = $parentsBlockMain.index() + 1,
							$createBlockHeader = $parentsBlockMain.find(".c-Reports-Create-Block"),
							$reportName = $parents.find(".c-Reports-Section-Name span");

					$parentsBlockMain.find(".c-Form-Error").removeClass("c-Form-Error");
					$input.attr("placeholder", "Enter Report Name");
					if ('' !== $name) {

						$createBlockHeader.attr({"data-id": $id, "data-date-send": $parentsBlockMain.find(".u-ReportDatePicker").val(), "data-time-send": $parentsBlockMain.find(".u-ReportTimePicker").val(), "data-capture": $parentsBlockMain.find(".u-CaptureReportDate").val(), "data-date-trigger": $parentsBlockMain.find(".c-Form-Block-Field-Block-Button.u-Selected").index()});

						if ($createBlockHeader.hasClass("c-Reports-Created")) {

						} else {
							$parentsBlockMain.removeClass("c-Reports-Create");
							$createBlockHeader.html($("#u-ReportHeader-Template").html()).removeClass("c-Reports-Create").addClass("c-Reports-Created");
							$createBlockHeader.find(".c-Reports-Create-Number").text($id);
							$createBlockHeader.find(".c-Reports-Section-Name span").html($name);
						}
						$parentsBlockMain.find(".c-Reports-Create-Text").html($name);
						if ($reportName.length) {
							$reportName.html($name);
						}
					} else {
						$parentField.addClass("c-Form-Error");
						$input.attr("placeholder", "Please Enter Report Name");
					}
					$(".u-CreatedReportsNumber").text($(".c-Reports-Create-Block-Main .c-Reports-Create-Block.c-Reports-Created").length);
					return false;
				})
				.on("focus", ".u-ReportName-Field", function () {
					var $this = $(this),
							$name = $.trim($this.val());
					$this.attr("data-value", $name);
				})
				.on("keyup", ".u-ReportName-Field", function () {
					var $this = $(this),
							$name = $.trim($this.val());
					if ("" != $name) {
						reportdata.name = $this.attr("data-value");
						openSaveActionPopup();
					} else {
						closeSaveActionPopup();
					}
				})
				.on("keyup", ".u-SearchReports", function () {
					var $this = $(this),
							$search = $this.parents(".control-search"),
							tab_container = $this.parents(".tab-container"),
							$createBlock = tab_container.find(".c-Reports-Create-Block-Main"),
							$createdReports = $createBlock.find(".c-Reports-Create-Block.c-Reports-Created"),
							$createdReportsSubscriptions = $createdReports.parents(".c-Reports-Create-Block-Main").find(".c-Reports-Subscriptions-Block.c-Subscriptions-Created"),
							$val = $this.val();

					if ("" !== $val) {
						$search.find(".c-search-Clear-Handle").show();

						var foundReports = $createdReports.filter(function () {
							var string = $(this).find(".c-Reports-Create-Text").text().trim().replace(/ +/g, "").toLowerCase();
							if (string.indexOf($val.trim().replace(/ +/g, "").toLowerCase()) > -1) {
								if (string == $val.trim().replace(/ +/g, "").toLowerCase()) {
									//$(this).parents(".summary-bar").addClass("c-contact-found");
								} else {
									//$(this).parents(".summary-bar").removeClass("c-contact-found");
								}
								return $(this);
							}
						});
						var foundSubscriptions = $createdReportsSubscriptions.filter(function () {
							var string = $(this).find(".c-Reports-Subscriptions-Text").text().trim().replace(/ +/g, "").toLowerCase();
							if (string.indexOf($val.trim().replace(/ +/g, "").toLowerCase()) > -1) {
								if (string == $val.trim().replace(/ +/g, "").toLowerCase()) {
									//$(this).parents(".summary-bar").addClass("c-contact-found");
								} else {
									//$(this).parents(".summary-bar").removeClass("c-contact-found");
								}
								return $(this);
							}
						});

						$createBlock.hide();
						if (foundReports.length) {
							foundReports.parents(".c-Reports-Create-Block-Main").show();
						}
						if (foundSubscriptions.length) {
							foundSubscriptions.parents(".c-Reports-Create-Block-Main").show();
						}

					} else {
						$createBlock.show();
					}
				})
				.on("click", ".c-Reports-Subscriptions-Details-Text .c-Switch", function () {
					var $this = $(this),
							$parents = $this.parents(".c-Reports-Subscriptions-Block-Main"),
							$id = $parents.find(".c-Reports-Subscriptions-Block").attr("data-id"),
							$checkbox = $this.find(".u-Switch-Checkbox");

					if (undefined == reportdata.subscriptionsstatus) {
						reportdata.subscriptionsstatus = new Array();
					}
					if ($checkbox.attr("checked")) {
						reportdata.subscriptionsstatus[$id] = 1;
						$checkbox.removeAttr("checked");
					} else {
						reportdata.subscriptionsstatus[$id] = 0;
						$checkbox.attr("checked", 'checked');
					}
					openSaveActionPopup();
					return false;
				})
				.on("click", ".u-Subscription-OpenDetail, .c-Subscriptions-Created .c-Reports-Subscriptions-Details-Left", function (e, $scrollTo) {
					var $this = $(this),
							container = $this.closest('.tab-container'),
							$parentsBlocks = $this.parents(".c-Reports-Subscriptions-Block"),
							$parentsMain = $this.parents(".c-Reports-Subscriptions-Block-Main"),
							$ajaxBlock = $parentsMain.find(".c-Subscribed-Form-Main");

					var $activeTabs = $parentsBlocks.find(".c-Subscribed-Form-Main.active");
					if (!$activeTabs.length) {
						$activeTabs = $ajaxBlock;
					}
					$ajaxBlock.removeClass("c-Subscriptions-Create-Form");
					if($this.parents(".c-Subscriptions-Create").length){
						$ajaxBlock.addClass("c-Subscriptions-Create-Form");
					}

					$activeTabs.slideUp(function () {
						container.css('height', 'auto');
						if (!$ajaxBlock.hasClass("active")) {

							$ajaxBlock.html($("#u-SubscribedForm-Template").html());
							$ajaxBlock.find("input").val("");
							if ($parentsBlocks.hasClass("c-Subscriptions-Created")) {
								var $name = $.trim($parentsBlocks.attr("data-name")),
										$email = $.trim($parentsBlocks.attr("data-email"));
								$ajaxBlock.find(".u-SubscribedName-Field").val($name);
								$ajaxBlock.find(".u-SubscribedEmail-Field").val($email);
							} else {
								$ajaxBlock.find(".u-Subscribed-Button-Delete").hide();
							}

							$ajaxBlock.slideDown(function () {
								$ajaxBlock.addClass("active");
								container.css('height', 'auto');
								setTimeout(function () {
									container.css('height', 'auto');
								}, 200);
							});
						} else {
							$(".c-Subscribed-Form-Main").removeClass("active");
						}
					});
					return false;
				})
				.on("click", ".u-Subscription-Delete", function () {
					var $this = $(this),
							container = $this.closest('.tab-container'),
							$parentsBlocks = $this.parents(".c-Reports-Subscriptions-Block"),
							$parentsMain = $this.parents(".c-Reports-Subscriptions-Block-Main"),
							$index = $parentsMain.index(),
							$ajaxBlock = $parentsMain.find(".c-Subscribed-Form-Main");

					openDeletePoup();
					deleteOverlay.find(".c-DeleteConfirm-Button-Yes").unbind("click")
						.on("click", function(){		
							if (undefined == reportdata.deletesub) {
								reportdata.deletesub = new Array();
							}
							reportdata.deletesub[$index] = $parentsMain.html();

							$parentsBlocks.html($("#u-NotSubscribed-Template").html());
							$parentsBlocks.find(".c-Reports-Subscriptions-Number").html($parentsBlocks.index() + 1);
							$parentsBlocks.removeClass("c-Subscriptions-Created").addClass("c-Subscriptions-Create");
							$ajaxBlock.html("").hide();
							container.css('height', 'auto');

							openSaveActionPopup();
						});
					return false;
				})
				.on("click", ".u-Subscribed-Button-Delete", function () {
					var $this = $(this),
							container = $this.closest('.tab-container'),
							$parentsMain = $this.parents(".c-Reports-Subscriptions-Block-Main");

					$parentsMain.find(".u-Subscription-Delete").trigger("click");
					container.css('height', 'auto');

					openSaveActionPopup();
					return false;
				})
				.on("click", ".u-Subscribed-Button-Save", function () {
					var $this = $(this),
							$formBlock = $this.parents(".c-Form"),
							container = $this.closest('.tab-container'),
							$parentsMain = $this.parents(".c-Reports-Subscriptions-Block-Main"),
							$index = $parentsMain.index() + 1,
							$parentsBlocks = $parentsMain.find(".c-Reports-Subscriptions-Block"),
							$nameField = ($formBlock.find(".u-SubscribedName-Field")),
							$name = $.trim($nameField.val()),
							$emailField = ($formBlock.find(".u-SubscribedEmail-Field")),
							$email = $.trim($emailField.val());

					$formBlock.find(".c-Form-Error").removeClass("c-Form-Error");
					if ('' == $name) {
						$nameField.parents(".c-Form-Block-Fields").addClass("c-Form-Error");
					}
					if ('' == $email) {
						$emailField.parents(".c-Form-Block-Fields").addClass("c-Form-Error");
					}

					if (!$formBlock.find(".c-Form-Error").length) {
						var tmp = $parentsMain.clone();
						tmp.find(".active").removeClass("active").hide();
						if (undefined == reportdata.addsub) {
							reportdata.addsub = new Array();
						}
						reportdata.addsub[$parentsMain.index()] = tmp.html();

						$parentsBlocks.attr({"data-id": $index, "data-name": $name, "data-email": $email})
						$parentsBlocks.html($("#u-Subscribed-Template").html());
						$parentsBlocks.find(".c-Reports-Subscriptions-Number").text($index);
						$parentsBlocks.find(".c-Reports-Subscriptions-Text").text($name);
						$parentsBlocks.removeClass("c-Subscriptions-Create").addClass("c-Subscriptions-Created");
						$parentsMain.find(".u-Subscription-OpenDetail").trigger("click");

						openSaveActionPopup();
					}
					container.css('height', 'auto');
					return false;
				})
				.on("click", ".c-Report-Status-Block .c-Report-Status-Button", function () {
					var $this = $(this),
							$parentsButtons = $this.parents(".c-Report-Status-Buttons"),
							$parentsMain = $this.parents(".c-Reports-Create-Block-Main"),
							$checkbox = $parentsMain.find(".u-Switch-Checkbox");
					$parentsButtons.find(".active").removeClass("active");
					$this.addClass("active");
					if ($this.hasClass("c-Report-Status-Button-On")) {
						$checkbox.attr("checked", false);
						reportdata.checkedstatus = 0;
					} else if ($this.hasClass("c-Report-Status-Button-Off")) {
						$checkbox.attr("checked", true);
						reportdata.checkedstatus = 1;
					}
					openSaveActionPopup();
					return false;
				})
				.on("click", ".c-ReviewReport-Button .c-Switch", function () {
					var $this = $(this),
							$checkbox = $this.find(".u-Switch-Checkbox"),
							$parentsMain = $checkbox.parents(".c-Reports-Create-Block-Main"),
							$parentsButtons = $parentsMain.find(".c-Report-Status-Buttons");
					$parentsButtons.find(".active").removeClass("active");
					if ($checkbox.attr("checked")) {
						$parentsButtons.find(".c-Report-Status-Button-On").addClass("active");
						reportdata.checkedstatus = 0;
					} else {
						$parentsButtons.find(".c-Report-Status-Button-Off").addClass("active");
						reportdata.checkedstatus = 1;
					}
					openSaveActionPopup();
				})
				.on("click", ".c-Report-Schedule-Block-Selections .c-Report-Schedule-Block-Selection", function () {
					var $this = $(this),
							$block = $this.parents(".c-Report-Schedule-Block"),
							$parents = $this.parents(".c-Report-Schedule-Section");
					
						if(!$this.parents(".c-Report-Schedule-Select-Multi").length){
							if ($this.hasClass("active")) {
								return false;
							}
							$block.find(".c-Report-Schedule-Block-Selection").removeClass("active");
							$this.addClass("active");
							
							$block.nextAll(".c-Report-Schedule-Block").find(".c-Report-Schedule-Footer-Button").removeClass("active");
							$block.nextAll(".c-Report-Schedule-Block").find("input").val('');
						} else{
							$this.toggleClass("active");
							if(!$block.find(".c-Report-Schedule-Block-Selection.active").length){
								$block.nextAll(".c-Report-Schedule-Block").fadeOut();
								$block.nextAll(".c-Report-Schedule-Block").find(".c-Report-Schedule-Block-Selection").removeClass("active");
								$block.nextAll(".c-Report-Schedule-Block").find(".c-Report-Schedule-Footer-Button").removeClass("active");
								$block.nextAll(".c-Report-Schedule-Block").find("input").val('');
								return false;
							}
						}

						if ($block.hasClass("c-Report-Schedule-Period-Block")) {
							$parents.find(".c-Report-Schedule-Block").not($block).hide();
							$parents.find(".c-Report-Schedule-Block").not($block).find(".c-Report-Schedule-Block-Selection").removeClass("active");
							if($this.hasClass("u-Selection-Once")){
								$parents.find(".c-Report-Schedule-TimeOnce-Block").fadeIn();
							} else if($this.hasClass("u-Selection-Hour")){
								$parents.find(".c-Report-Schedule-EveryHours-Block").fadeIn();
							} else if($this.hasClass("u-Selection-Month")){
								$parents.find(".c-Report-Schedule-Month-Block").fadeIn();
							} else {
								$parents.find(".c-Report-Schedule-WeekDay-Block").fadeIn();
							}
						} else if ($block.hasClass("c-Report-Schedule-WeekDay-Block")) {
							if($parents.find(".u-Selection-Day.active").length){
								$parents.find(".c-Report-Schedule-EveryDays-Block").fadeIn();
								$parents.find(".c-Report-Schedule-EveryHoursTime-Block").fadeIn();
							} else if($parents.find(".u-Selection-Month.active").length){
								$parents.find(".c-Report-Schedule-Time-Block").fadeIn();
							} else if($parents.find(".u-Selection-Week.active").length){
								$parents.find(".c-Report-Schedule-EveryWeeks-Block").fadeIn();
								$parents.find(".c-Report-Schedule-EveryHoursTime-Block").fadeIn();
							} else {
								$parents.find(".c-Report-Schedule-Time-Block").fadeIn();
							}
						} else if ($block.hasClass("c-Report-Schedule-Month-Block")) {
							$parents.find(".c-Report-Schedule-MonthWeek-Block").fadeIn();
						} else if ($block.hasClass("c-Report-Schedule-MonthWeek-Block")) {
							$parents.find(".c-Report-Schedule-WeekDay-Block").fadeIn();
							$parents.find(".u-CalendatDay-Button").removeClass("active");
							$parents.find(".c-Report-Schedule-CalendarDay-Block").fadeOut().find(".active").removeClass("active");
							$parents.find(".c-Report-Schedule-Time-Block").fadeOut();
						} else if ($block.hasClass("c-Report-Schedule-CalendarDay-Block")) {
							$parents.find(".c-Report-Schedule-Time-Block").fadeIn();
						}
					
					animateScrollTop($block.offset().top - 20);
					
					reportdata.schedule = true;
					openSaveActionPopup();
					return false;
				})
				.on("click", ".u-CalendatDay-Button", function () {
					var $this = $(this),
							$block = $this.parents(".c-Report-Schedule-Block"),
							$parents = $this.parents(".c-Report-Schedule-Section");
			
					$this.toggleClass("active");
					$parents.find(".c-Report-Schedule-WeekDay-Block").fadeOut().find(".active").removeClass("active");
					$parents.find(".c-Report-Schedule-Time-Block").fadeOut();
					$block.find(".c-Report-Schedule-Block-Selection.active").removeClass("active");
					$parents.find(".c-Report-Schedule-CalendarDay-Block").fadeIn();
					animateScrollTop($block.offset().top - 20);

					return false;
				})
				.on("keyup", ".c-Report-Schedule-TimerSelection-Input", function () {
					var $this = $(this),
							$block = $this.parents(".c-Report-Schedule-Block"),
							$parents = $this.parents(".c-Report-Schedule-Section"),
							$val = $.trim($this.val());
					if($val.length == 5){
						$parents.find(".c-Report-Schedule-StartEndDate-Block").fadeIn();
					} else{
						$parents.find(".c-Report-Schedule-StartEndDate-Block").fadeOut();
					}
					$parents.find(".c-Report-Schedule-StartEndDate-Block").find("input").trigger("keyup");
					animateScrollTop($block.offset().top - 20);
				})
				.on("click", ".u-StartEndDate-Button", function () {
					var $this = $(this),
							$block = $this.parents(".c-Report-Schedule-Block"),
							$parents = $this.parents(".c-Report-Schedule-Section");
					if($block.find(".c-Report-Schedule-StartEndDate-StartDate").val() == ""){
						alert("Please enter start date!");
					} else{
						$this.toggleClass("active");
						$block.find(".c-Report-Schedule-StartEndDate-EndDate").removeClass("filled").val("");
						animateScrollTop($block.offset().top - 20);
					}
					return false;
				})
				.on("keyup change", ".c-Report-Schedule-StartEndDate-Block input", function () {
					var $this = $(this),
							$block = $this.parents(".c-Report-Schedule-Block");
					if($this.val() != ""){		
						$block.find(".u-StartEndDate-Button").removeClass("active");
						$this.addClass("filled");
					} else {
						$this.removeClass("filled");

						if($this.hasClass("c-Report-Schedule-StartEndDate-EndDate")){
							$block.find(".u-StartEndDate-Button").removeClass("active");
						}
					}
					return false;
				})
				.on("keyup", ".c-Report-Schedule-TimerSelection-Input", function () {
					var $this = $(this),
							$val = $this.val(),
							$block = $this.parents(".c-Report-Schedule-Block"),
							$parents = $this.parents(".c-Report-Schedule-Section");
					if (5 == $val.length) {
						//$parents.find(".c-Report-Schedule-Frequency-Block").fadeIn();
						//animateScrollTop($block.offset().top - 20);
					} else {
						//$parents.find(".c-Report-Schedule-Frequency-Block").fadeOut();
						//animateScrollTop($(".c-Report-Schedule-Time-Block").offset().top - 20);
					}
					reportdata.schedule = true;
					openSaveActionPopup();
					return false;
				})
				.on("keyup", ".c-Report-Schedule-EveryHours-Block .c-Report-Schedule-Multi-Input", function () {
					var $this = $(this),
							$block = $this.parents(".c-Report-Schedule-Block"),
							$parents = $this.parents(".c-Report-Schedule-Section"),
							$hrs = parseInt($block.find(".c-Report-Schedule-EveryHours-Hours").val()),
							$min = parseInt($block.find(".c-Report-Schedule-EveryHours-Minutes").val());
					if ($hrs > 0 && $min > 0) {
						$parents.find(".c-Report-Schedule-EveryHoursTime-Block").fadeIn();
						animateScrollTop($(".c-Report-Schedule-EveryHours-Block").offset().top - 20);
					} else {
						$parents.find(".c-Report-Schedule-EveryHoursTime-Block").fadeOut();
						animateScrollTop($(".c-Report-Schedule-EveryHours-Block").offset().top - 20);
					}
					reportdata.schedule = true;
					openSaveActionPopup();
					return false;
				})
				.on("keyup", ".c-Report-Schedule-EveryWeeks-Block .c-Report-Schedule-Single-Input", function () {
					var $this = $(this),
							$val = parseInt($this.val()),
							$block = $this.parents(".c-Report-Schedule-Block"),
							$parents = $this.parents(".c-Report-Schedule-Section");
							
					if ($val > 0) {
						$parents.find(".c-Report-Schedule-EveryHoursTime-Block").fadeIn();
						animateScrollTop($(".c-Report-Schedule-EveryWeeks-Block").offset().top - 20);
					} else {
						//$parents.find(".c-Report-Schedule-EveryHoursTime-Block").fadeOut();
						//animateScrollTop($(".c-Report-Schedule-EveryHours-Block").offset().top - 20);
					}
					reportdata.schedule = true;
					openSaveActionPopup();
					return false;
				});


		var $offsetsReportTimer;
		reportDateandTime = function () {
			$(".u-ReportDatePicker").datepicker({
				dateFormat: "yy-mm-dd",
				'autoclose': true,
				beforeShow: function (input, event) {
				},
				onSelect: function (dateText, inst) {
					var $this = $(this);
					$this.addClass("filled").attr("data-date", moment(dateText).format('YYYY/MM/DD')).val(moment(dateText).format("Do MMM YYYY"));
					if($this.hasClass("c-Report-Schedule-StartEndDate-EndDate")){
						$this.parents(".c-Report-Schedule-Block").find(".u-StartEndDate-Button").removeClass("active");
					}
				}
			});
			$(".u-ReportTimePicker").inputmask("h:s", {"placeholder": "", "numeric": {allowPlus: true, allowMinus: true}});

			$(".c-Form-Block-Field-Block-Input, .u-ReportTimePicker").hover(function (e) {
				var $this = $(this),
						$tooltip = $this.attr("data-tooltip");
				if ('' !== $tooltip && undefined !== $tooltip) {
					var offsetTop = $this.attr("data-offset-top"),
							offsetLeft = $this.attr("data-offset-left"),
							$tooltipBlock = $this.parents(".c-Form-Block-Fields, .c-Report-Schedule-Select-Time").find(".tooltip." + $tooltip),
							w = $(window).width();
					
					if(w <= 431){
						offsetLeft =0;
					}

					if ($tooltipBlock.length) {
						$tooltipBlock.css({"display": "block", "top": offsetTop + "px", "left": offsetLeft + "px"}).toggleClass('animation bounceIn');
						$offsetsReportTimer = setTimeout(function () {
							$tooltipBlock.css("display", "none").removeClass('animation bounceIn');
						}, 5000);
					}
				}

				if ($this.hasClass("u-ReportTimePicker")) {
					$this.get(0).setSelectionRange(0, 0);
					$this.trigger("focus");
				}
			}, function () {
				clearTimeout($offsetsReportTimer);
				var $this = $(this),
						$tooltip = $this.attr("data-tooltip");
				if ('' !== $tooltip && undefined !== $tooltip) {
					var $tooltipBlock = $this.parents(".c-Form-Block-Fields, .c-Report-Schedule-Select-Time").find(".tooltip." + $tooltip);
					if ($tooltipBlock.length) {
						$tooltipBlock.css("display", "none").removeClass('animation bounceIn');
					}
				}
			});
		};

		$("#report-saveaction")
				.on("click", ".report-saveaction-button-save", function () {
					var $this = $(this),
							$parents = $this.parents(".report-saveaction");
					reportdata = new Array();
					$(".c-Reports-Create-Block-Detail-Ajax.active").find(".u-ReportNameCreate-Button").trigger("click");
					closeSaveActionPopup();
					return false;
				})
				.on("click", ".report-saveaction-button-cancel", function () {
					var $this = $(this),
							$parents = $this.parents(".report-saveaction");
					closeSaveActionPopup();
					return false;
				});
	});
}
