$(function(){
  nw.process.mainModule.exports.broadcaster.subscribe('init', () => {
    gotoPage('init');
  });
})

window.onbeforeunload = function(){
  nw.process.mainModule.exports.broadcaster.clear('UPDATE_CART');
  nw.process.mainModule.exports.broadcaster.clear('USER_LOGIN');
  nw.process.mainModule.exports.broadcaster.clear('EXIT_CART');
  nw.process.mainModule.exports.broadcaster.clear('IN_PAY');
  nw.process.mainModule.exports.broadcaster.clear('FINISH_PAY');
  nw.process.mainModule.exports.broadcaster.clear('init');
};

const gotoPage =function (type) {
  switch (type){
    case 'ad':
      if (location.href.indexOf('ad.html') < 0) {
        location.href = location.origin + '/assets/html/vice-screen/ad.html';
      }
      break;
    case 'pay':
      if (location.href.indexOf('payment.html') < 0) {
        location.href = location.origin + '/assets/html/vice-screen/payment.html';
      }
      break;
    case 'init':
      if (location.href.indexOf('index.html') < 0) {
      location.href =location.origin +'/assets/html/vice-screen/index.html';
      }
      break;
  }
};

