localforage.config({
    name: 'eventstorage'
});
var kaamKaajKoyta = '0';
localforage.length(function(e, v) {
  kaamKaajKoyta = v;
});
var indexErKhoma = document.querySelector('.event-home');
var dashboardErChehara = document.querySelector('.event-dashboard');
var dataInputKorarJayga = document.querySelector('.event-screen');

document.querySelector('.ajaira-event').addEventListener('click', function(){
  indexErKhoma.style.display = 'none';
  dashboardErChehara.style.display = 'block';
});
document.querySelector('.event-add-koren').addEventListener('click', function(){
  indexErKhoma.style.display = 'none';
  dashboardErChehara.style.display = 'none';
  dataInputKorarJayga.style.display = 'block';
});
document.querySelector('.event-e-jamuna').addEventListener('click', function(){
  indexErKhoma.style.display = 'none';
  dashboardErChehara.style.display = 'block';
  dataInputKorarJayga.style.display = 'none';
  var eventList = "";
  localforage.iterate(function(value, key) {
    console.log([key, value]);
    eventList += '<div class="item item-button-right"><h2>' + value['title'] +'</h2><p><i class="icon ion-ios7-clock-outline"> ' + value['time'] +'</i>&nbsp;&nbsp;<i class="icon ion-calendar"> '+ value['date'] +'</i></p><button data-id="' + key + '" class="gayebi-button button button-clear"><i class="icon ion-ios7-trash"></i></button></div>';
    document.querySelector('.eventListFucks').innerHTML = eventList;
  }, function(v, k) {
    var eventsGayeb = document.getElementsByClassName('gayebi-button');
    console.log(eventsGayeb);
    for (var i = 0; i < eventsGayeb.length; i++) {
      eventsGayeb[i].onclick = function() {
        localforage.removeItem(this.getAttribute('data-id'), function(err) {
          console.log('Key is cleared!');
          alert('Done!');
          location.reload(true);
        });
      }
    console.log('Iteration has completed');
  }
  });
});
var eventTitle = '';
document.querySelector('.event-name').addEventListener('input', function() {
  eventTitle = document.querySelector('.event-name').value;
  console.log(eventTitle);
});
var eventDate = '';
document.querySelector('.event-date').addEventListener('input', function() {
  eventDate = document.querySelector('.event-date').value;
  console.log(eventDate);
});
var eventTime = '';
document.querySelector('.event-time').addEventListener('input', function() {
  eventTime = document.querySelector('.event-time').value;
  console.log(eventTime);
});
document.querySelector('.event-e-jamu').addEventListener('click', function() {
  var myDate = new Date(eventDate + 'T' + eventTime)
  var alarmData = eventTitle;
  var request = navigator.mozAlarms.add(myDate, "honorTimezone", alarmData);

request.onsuccess = function () {
  new Notification("Reminder scheduled");
};

request.onerror = function () { 
  new Notification("An error occurred: " + this.error.name);
};
  localforage.setItem(Math.random() + kaamKaajKoyta + 1, {"title" : eventTitle, "date" : eventDate, "time" : eventTime}, function(e, v) {
    console.log('item saved');
    alert('item saved successfully');
    indexErKhoma.style.display = 'none';
    dashboardErChehara.style.display = 'block';
    dataInputKorarJayga.style.display = 'none';
    var eventList = "";
    localforage.iterate(function(value, key) {
      console.log([key, value]);
      eventList += '<div class="item item-button-right"><h2>' + value['title'] +'</h2><p><i class="icon ion-ios7-clock-outline">' + value['time'] +'</i>&nbsp;&nbsp;<i class="icon ion-calendar">'+ value['date'] +'</i></p><button data-id="' + key + '" class="gayebi-button button button-clear"><i class="icon ion-ios7-trash"></i></button></div>';
      document.querySelector('.eventListFucks').innerHTML = eventList;
    }, function(v, k) {
      var eventsGayeb = document.getElementsByClassName('gayebi-button');
      console.log(eventsGayeb);
      for (var i = 0; i < eventsGayeb.length; i++) {
        eventsGayeb[i].onclick = function() {
          localforage.removeItem(this.getAttribute('data-id'), function(err) {
            console.log('Key is cleared!');
            alert('Done!');
            location.reload(true);
          });
        }
      console.log('Iteration has completed');
    }
    });
  })
});
var eventList = "";
localforage.iterate(function(value, key) {
  console.log([key, value]);
  eventList += '<div class="item item-button-right"><h2>' + value['title'] +'</h2><p><i class="icon ion-ios7-clock-outline">' + value['time'] +'</i>&nbsp;&nbsp;<i class="icon ion-calendar">'+ value['date'] +'</i></p><button data-id="' + key + '" class="gayebi-button button button-clear"><i class="icon ion-ios7-trash"></i></button></div>';
  document.querySelector('.eventListFucks').innerHTML = eventList;
}, function(v, k) {
  var eventsGayeb = document.getElementsByClassName('gayebi-button');
  console.log(eventsGayeb);
  for (var i = 0; i < eventsGayeb.length; i++) {
    eventsGayeb[i].onclick = function() {
      localforage.removeItem(this.getAttribute('data-id'), function(err) {
        console.log('Key is cleared!');
        alert('Done!');
        location.reload(true);
      });
    }
  console.log('Iteration has completed');
}
});
navigator.mozSetMessageHandler("alarm", function (mozAlarm) { 
  new Notification(mozAlarm.data);
});