//(function () {

angular.module('MobileWebLogistics')
	.service('panelControl', function () {

		var self = this;
		this.showList = function ($scope, event, wlFactory) {

			$("#panel1").removeClass("active");
			$("#panel2").removeClass("active");
			var nav = $(".navbar-nav > li");
			$(nav).removeClass("active");
			$(nav[0]).addClass("active");
			$(".dijitPopup").remove();
			var ua = window.navigator.userAgent;
			var msie = wlFactory.detectIE();
			if (msie != false) {
				$("#listView").children("div").addClass('grayscale-off');
				$("#mapView").children("div").removeClass('grayscale-off');
			}
			else {
				$("#listImg").removeClass("grayscale_list grayscale grayscale-fade");
				$("#mapImg").addClass("grayscale_list grayscale grayscale-fade");
			}

			if ($("#detailPanels").hasClass("active")) {
				$("#detailPanels").removeClass("active");
				setTimeout(function () {
					$("#mapContainer").removeClass("active");
					$("#mapContainer").addClass("inactive");
					$("#map").removeClass("active");
				}, 200);
			}
			else {
				$("#mapContainer").removeClass("active");
				$("#mapContainer").addClass("inactive");
				$("#map").removeClass("active");
			}
		}

		this.showMap = function ($scope, event, wlFactory) {

			$("#panel1").removeClass("active");
			$("#panel2").removeClass("active");
			$("#listResults").removeClass("active");
			$("#mapContainer").removeClass("inactive");
			$("#mapContainer").addClass("active");
			$("#map").addClass("active");
			var nav = $(".navbar-nav > li");
			$(nav).removeClass("active");
			$(nav[1]).addClass("active");
			var msie = wlFactory.detectIE();

			if (msie != false) {
				$("#listView").children("div").removeClass('grayscale-off');
				$("#mapView").children("div").addClass('grayscale-off');
			}
			else {
				$("#mapImg").removeClass("grayscale_list grayscale grayscale-fade");
				$("#listImg").addClass("grayscale_list grayscale grayscale-fade");
			}

			if ($("#detailPanels").hasClass("active")) {
				$("#detailPanels").removeClass("active");
				//setTimeout(function(){
				$("#mapContainer").addClass("active");
				$("#map").addClass("active");
				//}, 100);
			}
			else {
				$("#mapContainer").addClass("active")
				$("#map").addClass("active");

			}
			setTimeout(function () {
				$scope.map.resize();
				$scope.map.reposition();
				$("#layerSelect").removeClass("active");
				$("#openLayerSelect").removeClass("active");
			}, 200);

			$scope.updateVisEye();
		}

		this.showDetail = function ($scope) {
			$("#panel1").removeClass("inactive");
			$("#panel1").addClass("active");
			$("#detailPanels").addClass("active");
			$("#detail1").addClass("active");

			if ($scope.showAdjoiner == false) {
				$scope.switchParcelDetails("detail");
			} else {
				$scope.switchParcelDetails('adjoiner');
				$scope.showAdjoiner = false;
			}

			//$("#panel1").scrollTop(0);

			/*if($(window).innerWidth<651){  
					  
				setTimeout(function(){$scope.refreshIscroll();}, 200); 
				if($scope.showPanel2==false)
					$scope.hidePanel2();       
				else{ //if user clicks show detail btn
					$("#panel1").removeClass("active");
					$("#panel1").addClass("inactive");  //to hide iscroll of panel1
					$("#panel2").addClass("active"); 
					setTimeout(function(){$scope.refreshIscroll();$("#detail2").scrollTop(0);}, 500);
					//$scope.showPanel2=false;
				}  
				$("#panel1").animate({scrollTop: $("#panel1Buttons").offset().top-100}, 2000); 
				$scope.switchParcelDetails('adjoiner');                
			} 
			setTimeout(function(){$scope.refreshIscroll();$("#panel1").scrollTop(0);$("#panel2").scrollTop(0);}, 200);  
			$("#basemapGalleryButton").css("opacity", 0.75);  //temp solution to init change in android app
			$scope.refreshIscroll();
			$scope.refreshIscroll();$("#panel1").scrollTop(0);$("#panel2").scrollTop(0);*/

			if ($(window).innerWidth < 651) {

				if ($scope.showPanel2 == false)
					$scope.hidePanel2();
				else { //if user clicks show detail btn
					$("#panel1").removeClass("active");
					$("#panel1").addClass("inactive");  //to hide iscroll of panel1
					$("#panel2").addClass("active");
				}
				$scope.switchParcelDetails('adjoiner');
			}
			//setTimeout(function(){$scope.refreshIscroll();$("#panel1").scrollTop(0);$("#panel2").scrollTop(0);}, 400);  
			$("#basemapGalleryButton").css("opacity", 0.75);  //temp solution to init change in android app
			//$scope.refreshIscroll();$("#panel1").scrollTop(0);$("#panel2").scrollTop(0);
			if ($(window).innerWidth < 651) {
				//$("#panel1").animate({scrollTop: $("#panel1Buttons").offset().top-10}, 3000); 

			}

		}
		this.showSelections = function ($scope) {
			if ($scope.multiSelectedLayer.graphics.length > 0) {
				$("#clearResultButton").addClass("showbtn");
				var multiSelectedLayerExtent = $scope.graphicsUtils.graphicsExtent($scope.multiSelectedLayer.graphics);
				$scope.map.setExtent(multiSelectedLayerExtent.expand(1.5));
				$scope.multiSelectedLayer.show();
				$scope.showMap();
			}
		}
		this.hideSelections = function ($scope) {
			$("#clearResultButton").removeClass("showbtn");
			$scope.multiSelectedLayer.hide();

		}
		this.hideDetail = function () {
			$("#detailPanels").removeClass("active");
		}

		this.hidePanel2 = function () {

			setTimeout(function () {
				$("#panel2").removeClass("active");
				$("#panel1").removeClass("inactive");
				$("#panel1").addClass("active");
				//document.getElementById('panel1Back').scrollIntoView();
			}, 200);

		}
		this.showP2 = function ($scope) {
			$scope.showPanel2 = true;
			//$scope.queryObjectId($scope.selectedParcel[$scope.parcelUid],'detail');
			var selPanel = 'detail';
			if ($scope.showP2Delay == 0) {
				$scope.showP2Delay = 1;

				if ($scope.noReport == 'true') {
					selPanel = 'adjoiner';
				}
				setTimeout(function () {
					//$scope.getAdjoiners();
					$("#panel2").addClass("active");
					$("#panel1").addClass("inactive");
					$("#panel1").removeClass("active");
					$scope.queryObjectId($scope.selectedParcel[$scope.parcelUid], $scope.selectedParcel[$scope.parcelOID], selPanel, $scope.selObject);
					//document.getElementById('panel1Back').scrollIntoView();$scope.myScroll3.refresh();
				}, 200);

				$scope.switchParcelDetails(selPanel);
				//$scope.queryObjectId($scope.selectedParcel.OBJECTID);
				//$scope.queryObjectId($scope.selectedParcel[$scope.parcelUid]);
				//setTimeout(function(){$scope.queryObjectId($scope.selectedParcel[$scope.parcelUid]);},200);
				setTimeout(function () { $scope.showP2Delay = 0; }, 1000);
			}
		}
		this.refreshIscroll = function ($scope) {
			setTimeout(function () {
				$scope.myScroll2.refresh();
				$scope.myScroll3.refresh();
			}, 100);

		}
		this.refreshIscrollAll = function ($scope) {
			setTimeout(function () {
				$scope.myScroll.refresh();
				$scope.myScroll2.refresh();
				$scope.myScroll3.refresh();
				$scope.myScroll4.refresh();
				$scope.myScroll5.refresh();
			}, 100);
		}
		this.switchParcelDetails = function ($scope, value) {
			var switchers = $(".switcher");
			var panels = $(".detailPanel");
			var downloads = $(".downloadLink");
			$(switchers).removeClass("active");
			$(panels).removeClass("active");
			$(downloads).removeClass("active");
			// selected a single record now, disable any stacking
			$scope.stackedFeature = false;
			if (value == "detail") {
				$(switchers[0]).addClass("active");
				$(panels[0]).addClass("active");
				$(downloads[0]).addClass("active");
			} else if (value == "adjoiner") {
				$(switchers[1]).addClass("active");
				$(panels[1]).addClass("active");
				$(downloads[1]).addClass("active");
				//setTimeout(function(){$("#adjoinerList").css("z-index", 100)}, 500);        
			}
		}
		this.genParcelInfoBack = function ($scope) {
			if ($scope.stackedFeatureStatus == false) {
				$("#detailPanels").removeClass("active");
				$scope.selectedLayer.clear(); $scope.map.graphics.clear();
				var parcelGeom = $scope.selectedParcel.geometry;
				var highlightGraphic = new $scope.Graphic(parcelGeom, $scope.selParcelSymbol);
				$scope.selectedLayer.add(highlightGraphic);
			}
			else {
				setTimeout(function () {
					$scope.stackedFeature = true;
					$scope.$apply(); $scope.refreshIscroll();
				}, 300);
			}
			//setTimeout(function(){$scope.$apply();$scope.refreshIscroll();}, 500); 
		}
		this.hideStackDetail = function ($scope) {
			$("#detailPanels").removeClass("active");
			$scope.selectedLayer.clear(); $scope.map.graphics.clear();
			var parcelGeom = $scope.selectedParcel.geometry;
			var highlightGraphic = new $scope.Graphic(parcelGeom, $scope.selParcelSymbol);
			$scope.selectedLayer.add(highlightGraphic);
		}
		this.getImages = function ($scope, $modal, wlFactory, pid, i) {
			//alert(i);
			var isParcelImg = true;
			if (i == 1) {
				//$scope.parcelImages.push(pid+"_"+i);	//add first image 
			}
			var parcelUrl = $scope.thumbnailUrl.replace("Thumbnails", "Parcels");
			var parcelImgUrl = parcelUrl + pid + "_" + i + ".jpg";				//https://s3.amazonaws.com/timmons-weblogistics/Gloucester/Parcels/0662-52-0237_1.jpg
			wlFactory.imageExists($scope, parcelImgUrl, function (exists) {

				isParcelImg = exists;
				if (isParcelImg == true) {
					if ($scope.parcelImages.indexOf(pid + "_" + i) == -1) {	  //if it's for 2nd or more image and is not in the array      		
						$scope.parcelImages.push(pid + "_" + i); //alert(i);
					}
					i++;
					parcelImgUrl = parcelUrl + pid + "_" + i + ".jpg";
					self.getImages($scope, $modal, wlFactory, pid, i)		//check for next image
				}
				else if (isParcelImg == false && i < 11) {
					self.getImages($scope, $modal, wlFactory, pid, 11)		//check for next skeche image
				}
				else {
					console.log($scope.parcelImages.length);
					if ($scope.parcelImages.length == 0) {
						$scope.parcelImages.push(pid + "_" + i);	//to prevent error and get error gray house image
					}
					if ($scope.openImageDelay == 0) {
						setTimeout(function () { self.openImage($scope, $modal); }, 400);
					}
					setTimeout(function () { $scope.openImageDelay = 0; }, 1000);
				}
			});
		}
		this.openImage = function ($scope, $modal, event) {
			if ($scope.openImageDelay == 0) {
				$scope.openImageDelay = 1;
				var modalInstance = $modal.open({
					templateUrl: 'templates/imageModal.html',
					controller: 'ImageController',
					scope: $scope
				});
			}
			//$('.carousel').carousel('pause');
			//$('.carousel').carousel({
			//    interval: false
			//}); 
		}

		this.openHelp = function ($modal) {
			var modalInstance = $modal.open({
				templateUrl: 'templates/local_helpModal.html'
			});
			$(".modal-dialog").css("width", "200%");
		}

		this.openM1 = function ($scope, $modal) {   //sale date from calendar				
			$scope.modalInstance = $modal.open({
				templateUrl: 'myModalContent1.html',
				controller: 'PopupInstanceController'
			});
			$scope.modalInstance.result.then(function (selected) {
				//$scope.selected = selected;
				//console.log=$("#dateFrom").val();
			}, function () {
				var value = $("#dateFrom").val();
				$("#startDate").val(value);
			});
		}
		this.openM2 = function ($scope, $modal) {   //sale date to calendar    
			$scope.modalInstance = $modal.open({
				templateUrl: 'myModalContent2.html',
				controller: 'PopupInstanceController'
			});
			$scope.modalInstance.result.then(function (selected) {
				//$scope.selected = selected;
				//console.log=$("#dateTo").val();
			}, function () {
				var value = $("#dateTo").val();
				$("#endDate").val(value);
			});
		}
		this.showDisclaimer = function () {
			if ($("#disclaimerContainer").is(":visible")) {
				// Hide 
				$("#pullButton").animate({ bottom: '0px' });
				$("#disclaimerContainer").slideUp(400);
			}
			else {
				// Show           
				$("#pullButton").animate({ bottom: '50%' });
				$("#disclaimerContainer").slideDown(400);
			}
		}
		this.setIscroll = function ($scope) {
			$scope.myScroll = new IScroll('#listResults', {
				mouseWheel: true,
				scrollbars: true,
				interactiveScrollbars: true,
				click: true,
				bounce: false,
				tap: true,
				preventDefault: false   //to make ng-click work in chrome and ff
			});
			$scope.myScroll2 = new IScroll('#panel1', {
				mouseWheel: true,
				scrollbars: true,
				interactiveScrollbars: true,
				click: true,
				bounce: false,
				tap: true,
				preventDefault: false
			});
			$scope.myScroll3 = new IScroll('#detail1', {
				mouseWheel: true,
				scrollbars: true,
				interactiveScrollbars: true,
				click: true,
				bounce: false,
				tap: true,
				preventDefault: false
			});

			$scope.myScroll4 = new IScroll('#adjoinerList', {
				mouseWheel: true,
				scrollbars: true,
				interactiveScrollbars: true,
				click: true,
				bounce: false,
				tap: true,
				preventDefault: false
			});

			$scope.myScroll5 = new IScroll('#bookmarksMenu', {
				mouseWheel: true,
				scrollbars: true,
				interactiveScrollbars: true,
				click: true,
				bounce: false,
				tap: true,
				preventDefault: false
			});
		}
	});

//})();