var Landing = {
  Apps : {
    '6' : {name : 'Criminal History Background Check', url : 'https://ecrim.ehawaii.gov'},
	'5' : {name : 'Hawaii Business Express', url : 'https://hbe.ehawaii.gov/BizEx/home.eb', groupWith : '7'},
	'7' : {name : 'Hawaii Business Express', url : 'https://hbe.ehawaii.gov/BizEx/home.eb', groupWith : '5'},
	'8' : {name : 'Hawaii Compliance Express', url : 'https://vendors.ehawaii.gov/hce/splash/welcome.html', groupWith : '9'},
	'9' : {name : 'Hawaii Compliance Express', url : 'https://vendors.ehawaii.gov/hce/splash/welcome.html', groupWith : '8'},
	'36' : {name : 'Hawaii Tax Filing System', url : 'https://www.ehawaii.gov/efile', groupWith : '37'},
	'37' : {name : 'Hawaii Tax Filing System', url : 'https://www.ehawaii.gov/efile', groupWith : '36'},
	'tax' : {name : 'Hawaii Tax Filing System', url : 'https://www.ehawaii.gov/efile', groupWith : '37'},
	'13' : {name : 'Online Hunting Licenses', url : 'https://hunting.ehawaii.gov'},
	'15' : {name : 'Kauai Real Property Tax Payment', url : 'https://payments.ehawaii.gov/propertytax/kauai'},
	'44' : {name : 'Hawaii Water Bill Payments', url : 'https://payments.ehawaii.gov/waterbill/hawaii'},
	'31' : {name : 'Condominium Association Registrations', url : 'http://www.ehawaii.gov/condo'},
	'42' : {name : 'Land Title Records Ordering System', url : 'https://boc.ehawaii.gov/docsearch/nameSearch.html'},
	'30' : {name : 'Pay Charity Annual Fees', url : 'http://ag.ehawaii.gov/charity/welcome.html'},
	'27' : {name : 'Commercial Marine Licensing System', url : 'https://dlnr.ehawaii.gov/cmls-public/app/welcome.html'},
	'21' : {name : 'DCCA Annual Filing', url : 'http://hbe.ehawaii.gov/annuals/welcome.html'},
	'47' : {name : 'Hawaii Motor Vehicle Renewals', url : 'https://mvr.ehawaii.gov/renewals/index.html?county=hawaii'},
	'17' : {name : 'Hawaii Property Tax Payment', url : 'https://payments.ehawaii.gov/propertytax/hawaii/index.html'},
	'19' : {name : 'Hawaii Unemployment Insurance', url : 'http://hui.ehawaii.gov/hui/app/'},
	'24' : {name : 'Purchase Business Documents Online', url : 'http://hbe.ehawaii.gov/documents/search.html'},
	'cogs' : {name : 'Certificate of Good Standing', url : 'http://hbe.ehawaii.gov/documents/search.html', nonLicenseId : true},
	'23' : {name : 'Hawaii State ID', url : 'https://stateid.ehawaii.gov'},
	'38' : {name : 'Teacher License Renewals', url : 'https://htsb.ehawaii.gov/htsb-renewals/welcome.html'},
	'45' : {name : 'Insurance License Renewal', url : 'https://www.ehawaii.gov/dcca/hils/renew/exe/insrenew.cgi'},
	'29' : {name : 'Kauai Motor Vehicle Renewals', url : 'https://mvr.ehawaii.gov/renewals/index.html?county=kauai'},
	'25' : {name : 'Maui Motor Vehicle Renewals', url : 'https://mvr.ehawaii.gov/renewals/index.html?county=maui'},
	'41' : {name : 'Controlled Substances Registration Renewal', url : 'https://ned.ehawaii.gov/renewal/index.html'},
	'13' : {name : 'Online Hunting Licenses', url : 'https://hunting.ehawaii.gov/hunting/license.html'},
	'40' : {name : 'Online Surplus Auctioning System', url : 'https://auction.ehawaii.gov/dcs/auction/index.php'},
	'18' : {name : 'PVL License Renewals', url : 'https://pvl.ehawaii.gov/renewals/index.html'},
	'43' : {name : 'State Parks Online Reservation', url : 'https://camping.ehawaii.gov/camping/welcome.html'},
	'22' : {name : 'Vessel Registration', url : 'http://vessel.ehawaii.gov/vessel'},
	'26' : {name : 'Waste Water Filing', url : 'http://wastewater.ehawaii.gov/wastewater/app/welcome.html'},
	'28' : {name : 'Wedding Permits', url : 'https://dlnr.ehawaii.gov/permits/welcome.html'},
	'12' : {name : 'Kauai Sewer Payment', url : 'https://payments.ehawaii.gov/kauaisewer/search.html'}
  },

  AppIds : [],
  NonLicenseIds : [],
  
  init : function() {
     var today = new Date();
     var currMonth = today.getMonth() + 1;
	 var year = today.getYear();
	 
	 if (year < 1900) {
	   year += 1900;
	 }
	 
	 $('.todays_date').text($.datepicker.formatDate('mm/dd/yy', today));
	 Portal.onInit(function() {
	     WhatsHappening.loadBlock('#whatsHappening');
         News.initGovNewsBlock('#gov-news', 4);
	     setTimeout(function() {
		     Landing._initCalendarWidget(currMonth, year);
			 
			 Portal.onThemeChanged(function() {
			   setTimeout(function() {
					Landing._initCalendarWidget(currMonth, year);
			   }, 1000);
			 });
		 }, 1000);
	 });
	 
	 Landing.popularServicesList('#popular-services');
	 Landing.buildEventList('#todays_events');
         Search.initSearchBoxes();
  },
  
  _initCalendarWidget : function(currMonth, year) {
     var calendar = $('#calendar');
	 
	 if (calendar.find('.ui-datepicker').length > 0) {
	   calendar.datepicker("destroy");
	 }
	 
	 calendar.datepicker({
	   onChangeMonthYear : function(year, month,inst) {
		 Calendar.updateDaysWithEvents(month, year,
			 function() {
				Calendar.markDaysWithEvents('#calendar');
			 }
		 );
	   },
	   
	   onSelect : function(dateText) {
		 var parts = dateText.split('/');
		 location.href='/government/calendar.html#?date=' + parts[0] + "-" + parts[1] + "-" + parts[2];
	   }
	 });
	 
	 Calendar.updateDaysWithEvents(currMonth, year,
		 function() {
			Calendar.markDaysWithEvents('#calendar');
		 }
	 );
  
  },
  
  buildEventList: function(selector) {
    Calendar.buildTodaysEventList(selector);
  },
  
  popularServicesList: function(selector) {
    var elem = $(selector);
	
	var now = new Date();
	var then = new Date();
	then.setDate(then.getDate() - 7);
	
	var from = $.datepicker.formatDate('mm-dd-yy', then);
	var to = $.datepicker.formatDate('mm-dd-yy', now);
	
	Landing.AppIds = [];
	
	elem.find("li").remove();
	$.each(Landing.Apps, function(appId, app) {
	  var isLicenseId = app.nonLicenseId ? false : true;
	  if (isLicenseId) {
	    Landing.AppIds.push(appId);
	  } else {
	    Landing.NonLicenseIds.push(appId);
	  }
	});

    Portal.readPopularServicesFeed(function(data) {
      var appIds = Landing.AppIds;
	  
	  $.each(Landing.NonLicenseIds, function(i, id) {
	    appIds.push(id);
	  });
	  
	  var popularIds = [];

	  for (var i = 0; i < data.length; i++) {
	    var pos = Math.floor(Math.random()* appIds.length);
	    var appId = appIds[pos];
	  
	    var index = $.inArray(appId, popularIds);
	    if (index == -1) {
	    	popularIds.push(appId);
		  var app = Landing.Apps[appId];
	  	  if (app.groupWith) {
		    popularIds.push(app.groupWith);
		  }
	    }
	  }
	
	  var servicesMap = {};
	  var services  = [];
	  $.each(data, function(i, service) {
	    servicesMap[service.id] = service;
	  });
	  
	  var max = 4;
	  var count = 0;
	  
	  $.each(popularIds, function(i, serviceId) {
	    var service = servicesMap[serviceId];
		if (service) {
			var app = Landing.Apps[service.id];
			
			if (app) {
			  if (app.groupWith) {
				var found = false;
				$.each(services, function(i, svc) {
				  if (svc.id == app.groupWith) {
					svc.count += service.count;
					found = true;
				  }
				});
				
				if (!found) {
				  services.push(service);
				}
			  } else if (service.count >= 10){
				services.push(service);
				count++;
			  }
			}
		}
		
		if (count >= max) {
		  return false;
		}
	  });
	  
	  count = 0;
	  $.each(services, function(i, service) {
	      count++;
		  var app = Landing.Apps[service.id];
		  elem.append('<li><span class="service-name"><a target="_blank" href="' + app.url + '">' + app.name + '</a></span><span class="service-trans-count">(' + service.count + ' last week)</span></li>');
		  
		  if (count >= max) {
		    return false;
		  }
	  });
	  
	  setTimeout(function() {
		Portal.alignHeights();
	  }, 500);
	});
  }
  
}
