1/**
2 * Copyright (c) 2010 The Chromium Authors. All rights reserved.  Use of this
3 * source code is governed by a BSD-style license that can be found in the
4 * LICENSE file.
5 */
6
7//Contains true if multiple calendar option is checked, false otherwise.
8var isMultiCalendar;
9
10//adding listener when body is loaded to call init function.
11window.addEventListener('load', init, false);
12
13/**
14 * Sets the value of multiple calendar checkbox based on value from
15 * local storage.
16 */
17 function init() {
18  isMultiCalendar = JSON.parse(localStorage.multiCalendar || false);
19  $('multiCalendar').checked = isMultiCalendar;
20  $('multiCalendarText').innerHTML =
21      chrome.i18n.getMessage('multiCalendarText');
22  $('optionsTitle').innerHTML = chrome.i18n.getMessage('optionsTitle');
23  $('imageTooltip').title = chrome.i18n.getMessage('imageTooltip');
24  $('imageTooltip').alt = chrome.i18n.getMessage('imageTooltip');
25  $('multiCalendarText').title = chrome.i18n.getMessage('multiCalendarToolTip');
26  $('multiCalendar').title = chrome.i18n.getMessage('multiCalendarToolTip');
27  $('extensionName').innerHTML = chrome.i18n.getMessage('extensionName');
28  if (chrome.i18n.getMessage('direction') == 'rtl') {
29    document.querySelector('body').style.direction = 'rtl';
30  }
31};
32
33/**
34 * Saves the value of the checkbox into local storage.
35 */
36function save() {
37  var multiCalendarId = $('multiCalendar');
38  localStorage.multiCalendar = multiCalendarId.checked;
39  if (multiCalendarId) {
40    multiCalendar.disabled = true;
41  }
42  $('status').innerHTML = chrome.i18n.getMessage('status_saving');
43  $('status').style.display = 'block';
44  chrome.extension.getBackgroundPage().onSettingsChange();
45};
46
47/**
48 * Fired when a request is sent from either an extension process or a content
49 * script. Add Listener to enable the save checkbox button on server response.
50 * @param {String} request Request sent by the calling script.
51 * @param {Object} sender Information about the script that sent a message or
52 *     request.
53 * @param {Function} sendResponse Function to call when there is a response.
54 *     The argument should be any JSON-ifiable object, or undefined if there
55 *     is no response.
56 */
57chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
58  if (!request.message)
59    return;
60  switch (request.message) {
61    case 'enableSave':
62      if ($('multiCalendar')) {
63        if ($('multiCalendar').disabled) {
64          $('status').innerHTML = chrome.i18n.getMessage('status_saved');
65          $('status').style.display = 'block';
66          setTimeout("$('status').style.display = 'none'", 1500);
67        }
68        $('multiCalendar').disabled = false;
69      }
70      sendResponse();
71      break;
72  }
73});
74