dmaApp.controller('dmaPageController', [ 
	'$scope', 
	'dmaPageService',
	'Constants',
	'mixpanelService',
	function($scope, dmaPageService, Constants, mixpanelService) {
		var marketingUrl = Constants.marketingUrl;
				
		$scope.dmaData = [];
		$scope.companyStatus = "";
		$scope.isCompanyStatus = false;
		$scope.isIndustry = false;
		$scope.isRegion = false;
		$scope.isDivision = false;
		$scope.isOverall = false;
		$scope.isQuery = false;
		$scope.searchTitle = "";
		$scope.industryTitle = "";
		$scope.divisionTitle = "";
		$scope.overallTitle = "";
		$scope.statusTitle = "";
		$scope.searchTitle = "";
		$scope.pageNumber = pageNumber;
		$scope.numberOfPages = numberOfPages
		$scope.queryTerm = "";

		$scope.showHide = function(isShowed) {
			if (isShowed) {
				return "- Hide all cities";
			} else {
				return "+ Show all cities";
			}
		}
		
		init();
		
		function init() {
			if (query && query != "null") {
				trackDmaPageQuery(query)
				fetchCompaniesFromSearchQuery(query, pageNumber);
				$scope.isQuery = true;
				$scope.searchTitle = "Search Results of " + query;
			}  else if (companyStatus && companyStatus != "null") {
				$scope.statusTitle = "Top " + companyStatus + " Companies";
				fetchCompaniesByType(companyStatus, pageNumber);
				$scope.isCompanyStatus = true;
			} else if (isIndustry != "null" && isIndustry == "true") {
				fetchCompaniesByIndustry(pageNumber);
				$scope.isIndustry = true;
				$scope.industryTitle = "Top Industries";
			}  else if (isRegion != "null" && isRegion == "true") {
				fetchCompaniesByRegion(pageNumber);
				$scope.isRegion = true;
				$scope.regionTitle = "Top Regions";
			}  else if (isDivision != "null" && isDivision == "true") {
				fetchCompaniesByDivision(pageNumber);
				$scope.isDivision = true;
				$scope.divisionTitle = "Top Divisions";
			} else {
				fetchAllCompanies(pageNumber);
				$scope.overallTitle = "Top Companies Overall"
				$scope.isOverall = true;
			}
		}
		
		$scope.fetchAllCompanies = fetchAllCompanies;
		$scope.fetchCompaniesByType = fetchCompaniesByType;
		$scope.fetchCompaniesByIndustry = fetchCompaniesByIndustry;
		$scope.fetchCompaniesByRegion = fetchCompaniesByRegion;
		$scope.fetchCompaniesByDivision = fetchCompaniesByDivision;
		$scope.fetchCompaniesFromSearchQuery = fetchCompaniesFromSearchQuery;
		
		function fetchCompaniesFromSearchQuery(query, pageNumber) {
		    var promise = dmaPageService.getCompanySearchResult(query, pageNumber);
		    promise.then(function(response) {
		    	if(response.data) {
		    		$scope.dmaData = response.data;
		    	}
		    });
		}
		
		function fetchAllCompanies(pageNumber) {
			var promise = dmaPageService.getAllCompanies(pageNumber);
		    promise.then(function(response) {
		    	if(response.data) {
		    		$scope.dmaData = response.data;
		    	}
		    });
		}
		
		function fetchCompaniesByType(companyStatus, pageNumber) {
			var promise = dmaPageService.getCompaniesByType(companyStatus, pageNumber);
		    promise.then(function(response) {
		    	if(response.data) {
		    		$scope.dmaData = response.data;
		    	}
		    });
		}
		
		function fetchCompaniesByIndustry(pageNumber) {
		    var promise = dmaPageService.getMatchedCompaniesByIndustry(pageNumber);
		    promise.then(function(response) {
		    	if(response.data) {
		    		$scope.dmaData = response.data;
		    	}
		    });
		}
		
		function fetchCompaniesByRegion(pageNumber) {
		    var promise = dmaPageService.getMatchedCompaniesByRegion(pageNumber);
		    promise.then(function(response) {
		    	if(response.data) {
		    		$scope.dmaData = response.data;
		    	}
		    });
		}
		
		function fetchCompaniesByDivision(pageNumber) {
			var promise = dmaPageService.getMatchedCompaniesByDivision(pageNumber);
			promise.then(function(response) {
				if(response.data) {
					$scope.dmaData = response.data;
				}
			});
		}
		
	
	    $scope.sortType     = 'dmaRating'; // set the default sort type
	    $scope.sortReverse  = true;  // set the default sort order
	    
	    $scope.goToCompaniesByType = function(companyStatus) {
	    	var url = marketingUrl + "/leadership-diversity.htm?companyStatus=" + companyStatus ;
	    	return url;
	    }
	    
	    $scope.goToIndustry = function() {
	    	var url = marketingUrl + "/leadership-diversity.htm?isIndustry=true";
	    	return url;
	    }
	    
	    $scope.goToRegion = function() {
	    	var url = marketingUrl + "/leadership-diversity.htm?isRegion=true";
	    	return url;
	    }
	    
	    $scope.goToDivision = function() {
	    	var url = marketingUrl + "/leadership-diversity.htm?isDivision=true";
	    	return url;
	    }
	    
	    $scope.trackPreviousButton = function() {
			var prop ={};
			prop["page"] = $scope.pageNumber; 
 			mixpanelService.trackEvent( "Previous page navigation" ,prop);
	    }
	    
	    $scope.trackNextButton = function() {
			var prop ={};
			prop["page"] = $scope.pageNumber; 
 			mixpanelService.trackEvent( "Next page navigation" ,prop);
	    }
	    
		$scope.trackOverallPage = function(){
			var prop ={};
			prop["Overall"] = "true"; 
 			mixpanelService.trackEvent( "All Companies" ,prop);
		}
		
		$scope.trackPrivateCompaniesPage = function(){
			var prop ={};
			prop["Private"] = "true"; 
 			mixpanelService.trackEvent( "Top Private Companies" ,prop);
		}
		
		$scope.trackPublicCompaniesPage = function(){
			var prop ={};
			prop["Public"] = "true"; 
 			mixpanelService.trackEvent( "Top Public Companies" ,prop);
		}
		
		$scope.trackDmaPageIndustry = function(){
			var prop ={};
			prop["Industry"] = "true"; 
 			mixpanelService.trackEvent( "Top Companies in industry" ,prop);
		}
		
		$scope.trackDmaPageRegion = function(){
			var prop ={};
			prop["Region"] = "true"; 
 			mixpanelService.trackEvent( "Top Companies in region" ,prop);
		}
		
		$scope.trackDmaPageDivision = function(){
			var prop ={};
			prop["Division"] = "true"; 
 			mixpanelService.trackEvent( "Top Companies in division" ,prop);
		}
		
		function trackDmaPageQuery(query){
			var prop ={};
			prop["Query"] = query; 
 			mixpanelService.trackEvent( "Track searching term" ,prop);
		}
		
	}]
);