summaryrefslogtreecommitdiff
path: root/chromium/chrome/browser/resources/ntp_android/ntp_android.js
blob: 5de0633c7b6a4a162c255c8995c8d2ef59152bfc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// File Description:
//     Contains all the necessary functions for rendering the NTP on mobile
//     devices.

/**
 * The event type used to determine when a touch starts.
 * @type {string}
 */
var PRESS_START_EVT = 'touchstart';

/**
 * The event type used to determine when a touch finishes.
 * @type {string}
 */
var PRESS_STOP_EVT = 'touchend';

/**
 * The event type used to determine when a touch moves.
 * @type {string}
 */
var PRESS_MOVE_EVT = 'touchmove';

cr.define('ntp', function() {
  /**
   * Constant for the localStorage key used to specify the default bookmark
   * folder to be selected when navigating to the bookmark tab for the first
   * time of a new NTP instance.
   * @type {string}
   */
  var DEFAULT_BOOKMARK_FOLDER_KEY = 'defaultBookmarkFolder';

  /**
   * Constant for the localStorage key used to store whether or not sync was
   * enabled on the last call to syncEnabled().
   * @type {string}
   */
  var SYNC_ENABLED_KEY = 'syncEnabled';

  /**
   * The time before and item gets marked as active (in milliseconds).  This
   * prevents an item from being marked as active when the user is scrolling
   * the page.
   * @type {number}
   */
  var ACTIVE_ITEM_DELAY_MS = 100;

  /**
   * The CSS class identifier for grid layouts.
   * @type {string}
   */
  var GRID_CSS_CLASS = 'icon-grid';

  /**
   * The element to center when centering a GRID_CSS_CLASS.
   */
  var GRID_CENTER_CSS_CLASS = 'center-icon-grid';

  /**
   * Attribute used to specify the number of columns to use in a grid.  If
   * left unspecified, the grid will fill the container.
   */
  var GRID_COLUMNS = 'grid-columns';

  /**
   * Attribute used to specify whether the top margin should be set to match
   * the left margin of the grid.
   */
  var GRID_SET_TOP_MARGIN_CLASS = 'grid-set-top-margin';

  /**
   * Attribute used to specify whether the margins of individual items within
   * the grid should be adjusted to better fill the space.
   */
  var GRID_SET_ITEM_MARGINS = 'grid-set-item-margins';

  /**
   * The CSS class identifier for centered empty section containers.
   */
  var CENTER_EMPTY_CONTAINER_CSS_CLASS = 'center-empty-container';

  /**
   * The CSS class identifier for marking list items as active.
   * @type {string}
   */
  var ACTIVE_LIST_ITEM_CSS_CLASS = 'list-item-active';

  /**
   * Attributes set on elements representing data in a section, specifying
   * which section that element belongs to. Used for context menus.
   * @type {string}
   */
  var SECTION_KEY = 'sectionType';

  /**
   * Attribute set on an element that has a context menu. Specifies the URL for
   * which the context menu action should apply.
   * @type {string}
   */
  var CONTEXT_MENU_URL_KEY = 'url';

  /**
   * The list of main section panes added.
   * @type {Array.<Element>}
   */
  var panes = [];

  /**
   * The list of section prefixes, which are used to append to the hash of the
   * page to allow the native toolbar to see url changes when the pane is
   * switched.
   */
  var sectionPrefixes = [];

  /**
   * The next available index for new favicons.  Users must increment this
   * value once assigning this index to a favicon.
   * @type {number}
   */
  var faviconIndex = 0;

  /**
   * The currently selected pane DOM element.
   * @type {Element}
   */
  var currentPane = null;

  /**
   * The index of the currently selected top level pane.  The index corresponds
   * to the elements defined in {@see #panes}.
   * @type {number}
   */
  var currentPaneIndex;

  /**
   * The ID of the bookmark folder currently selected.
   * @type {string|number}
   */
  var bookmarkFolderId = null;

  /**
   * The current element active item.
   * @type {?Element}
   */
  var activeItem;

  /**
   * The element to be marked as active if no actions cancel it.
   * @type {?Element}
   */
  var pendingActiveItem;

  /**
   * The timer ID to mark an element as active.
   * @type {number}
   */
  var activeItemDelayTimerId;

  /**
   * Enum for the different load states based on the initialization of the NTP.
   * @enum {number}
   */
  var LoadStatusType = {
    LOAD_NOT_DONE: 0,
    LOAD_IMAGES_COMPLETE: 1,
    LOAD_BOOKMARKS_FINISHED: 2,
    LOAD_COMPLETE: 3  // An OR'd combination of all necessary states.
  };

  /**
   * The current loading status for the NTP.
   * @type {LoadStatusType}
   */
  var loadStatus_ = LoadStatusType.LOAD_NOT_DONE;

  /**
   * Whether the loading complete notification has been sent.
   * @type {boolean}
   */
  var finishedLoadingNotificationSent_ = false;

  /**
   * Whether the page title has been loaded.
   * @type {boolean}
   */
  var titleLoadedStatus_ = false;

  /**
   * Whether the NTP is in incognito mode or not.
   * @type {boolean}
   */
  var isIncognito = false;

  /**
   * Whether incognito mode is enabled. (It can be blocked e.g. with a policy.)
   * @type {boolean}
   */
  var isIncognitoEnabled = true;

  /**
   * Whether the initial history state has been replaced.  The state will be
   * replaced once the bookmark data has loaded to ensure the proper folder
   * id is persisted.
   * @type {boolean}
   */
  var replacedInitialState = false;

  /**
   * Stores number of most visited pages.
   * @type {number}
   */
  var numberOfMostVisitedPages = 0;

  /**
   * Whether there are any recently closed tabs.
   * @type {boolean}
   */
  var hasRecentlyClosedTabs = false;

  /**
   * Whether promo is not allowed or not (external to NTP).
   * @type {boolean}
   */
  var promoIsAllowed = false;

  /**
   * Whether promo should be shown on Most Visited page (externally set).
   * @type {boolean}
   */
  var promoIsAllowedOnMostVisited = false;

  /**
   * Whether promo should be shown on Open Tabs page (externally set).
   * @type {boolean}
   */
  var promoIsAllowedOnOpenTabs = false;

  /**
   * Whether promo should show a virtual computer on Open Tabs (externally set).
   * @type {boolean}
   */
  var promoIsAllowedAsVirtualComputer = false;

  /**
   * Promo-injected title of a virtual computer on an open tabs pane.
   * @type {string}
   */
  var promoInjectedComputerTitleText = '';

  /**
   * Promo-injected last synced text of a virtual computer on an open tabs pane.
   * @type {string}
   */
  var promoInjectedComputerLastSyncedText = '';

  /**
   * The different sections that are displayed.
   * @enum {number}
   */
  var SectionType = {
    BOOKMARKS: 'bookmarks',
    FOREIGN_SESSION: 'foreign_session',
    FOREIGN_SESSION_HEADER: 'foreign_session_header',
    MOST_VISITED: 'most_visited',
    PROMO_VC_SESSION_HEADER: 'promo_vc_session_header',
    RECENTLY_CLOSED: 'recently_closed',
    SNAPSHOTS: 'snapshots',
    UNKNOWN: 'unknown',
  };

  /**
   * The different ids used of our custom context menu. Sent to the ChromeView
   * and sent back when a menu is selected.
   * @enum {number}
   */
  var ContextMenuItemIds = {
    BOOKMARK_EDIT: 0,
    BOOKMARK_DELETE: 1,
    BOOKMARK_OPEN_IN_NEW_TAB: 2,
    BOOKMARK_OPEN_IN_INCOGNITO_TAB: 3,
    BOOKMARK_SHORTCUT: 4,

    MOST_VISITED_OPEN_IN_NEW_TAB: 10,
    MOST_VISITED_OPEN_IN_INCOGNITO_TAB: 11,
    MOST_VISITED_REMOVE: 12,

    RECENTLY_CLOSED_OPEN_IN_NEW_TAB: 20,
    RECENTLY_CLOSED_OPEN_IN_INCOGNITO_TAB: 21,
    RECENTLY_CLOSED_REMOVE: 22,

    FOREIGN_SESSIONS_REMOVE: 30,

    PROMO_VC_SESSION_REMOVE: 40,
  };

  /**
   * The URL of the element for the context menu.
   * @type {string}
   */
  var contextMenuUrl = null;

  var contextMenuItem = null;

  var currentSnapshots = null;

  var currentSessions = null;

  /**
   * The possible states of the sync section
   * @enum {number}
   */
  var SyncState = {
    INITIAL: 0,
    WAITING_FOR_DATA: 1,
    DISPLAYING_LOADING: 2,
    DISPLAYED_LOADING: 3,
    LOADED: 4,
  };

  /**
   * The current state of the sync section.
   */
  var syncState = SyncState.INITIAL;

  /**
   * Whether or not sync is enabled. It will be undefined until
   * setSyncEnabled() is called.
   * @type {?boolean}
   */
  var syncEnabled = undefined;

  /**
   * The current most visited data being displayed.
   * @type {Array.<Object>}
   */
  var mostVisitedData_ = [];

  /**
   * The current bookmark data being displayed. Keep a reference to this data
   * in case the sync enabled state changes.  In this case, the bookmark data
   * will need to be refiltered.
   * @type {?Object}
   */
  var bookmarkData;

  /**
   * Keep track of any outstanding timers related to updating the sync section.
   */
  var syncTimerId = -1;

  /**
   * The minimum amount of time that 'Loading...' can be displayed. This is to
   * prevent flashing.
   */
  var SYNC_LOADING_TIMEOUT = 1000;

  /**
   * How long to wait for sync data to load before displaying the 'Loading...'
   * text to the user.
   */
  var SYNC_INITIAL_LOAD_TIMEOUT = 1000;

  /**
   * An array of images that are currently in loading state. Once an image
   * loads it is removed from this array.
   */
  var imagesBeingLoaded = new Array();

  /**
   * Flag indicating if we are on bookmark shortcut mode.
   * In this mode, only the bookmark section is available and selecting
   * a non-folder bookmark adds it to the home screen.
   * Context menu is disabled.
   */
  var bookmarkShortcutMode = false;

  function setIncognitoMode(incognito) {
    isIncognito = incognito;
    if (!isIncognito) {
      chrome.send('getMostVisited');
      chrome.send('getRecentlyClosedTabs');
      chrome.send('getForeignSessions');
      chrome.send('getPromotions');
      chrome.send('getIncognitoDisabled');
    }
  }

  function setIncognitoEnabled(item) {
    isIncognitoEnabled = item.incognitoEnabled;
  }

  /**
   * Flag set to true when the page is loading its initial set of images. This
   * is set to false after all the initial images have loaded.
   */
  function onInitialImageLoaded(event) {
    var url = event.target.src;
    for (var i = 0; i < imagesBeingLoaded.length; ++i) {
      if (imagesBeingLoaded[i].src == url) {
        imagesBeingLoaded.splice(i, 1);
        if (imagesBeingLoaded.length == 0) {
          // To send out the NTP loading complete notification.
          loadStatus_ |= LoadStatusType.LOAD_IMAGES_COMPLETE;
          sendNTPNotification();
        }
      }
    }
  }

  /**
   * Marks the given image as currently being loaded. Once all such images load
   * we inform the browser via a hash change.
   */
  function trackImageLoad(url) {
    if (finishedLoadingNotificationSent_)
      return;

    for (var i = 0; i < imagesBeingLoaded.length; ++i) {
      if (imagesBeingLoaded[i].src == url)
        return;
    }

    loadStatus_ &= (~LoadStatusType.LOAD_IMAGES_COMPLETE);

    var image = new Image();
    image.onload = onInitialImageLoaded;
    image.onerror = onInitialImageLoaded;
    image.src = url;
    imagesBeingLoaded.push(image);
  }

  /**
   * Initializes all the UI once the page has loaded.
   */
  function init() {
    // Special case to handle NTP caching.
    if (window.location.hash == '#cached_ntp')
      document.location.hash = '#most_visited';
    // Special case to show a specific bookmarks folder.
    // Used to show the mobile bookmarks folder after importing.
    var bookmarkIdMatch = window.location.hash.match(/#bookmarks:(\d+)/);
    if (bookmarkIdMatch && bookmarkIdMatch.length == 2) {
      localStorage.setItem(DEFAULT_BOOKMARK_FOLDER_KEY, bookmarkIdMatch[1]);
      document.location.hash = '#bookmarks';
    }
    // Special case to choose a bookmark for adding a shortcut.
    // See the doc of bookmarkShortcutMode for details.
    if (window.location.hash == '#bookmark_shortcut')
      bookmarkShortcutMode = true;
    // Make sure a valid section is always displayed.  Both normal and
    // incognito NTPs have a bookmarks section.
    if (getPaneIndexFromHash() < 0)
      document.location.hash = '#bookmarks';

    // Initialize common widgets.
    var titleScrollers =
        document.getElementsByClassName('section-title-wrapper');
    for (var i = 0, len = titleScrollers.length; i < len; i++)
      initializeTitleScroller(titleScrollers[i]);

    // Initialize virtual computers for the sync promo.
    createPromoVirtualComputers();

    setCurrentBookmarkFolderData(
        localStorage.getItem(DEFAULT_BOOKMARK_FOLDER_KEY));

    addMainSection('incognito');
    addMainSection('most_visited');
    addMainSection('bookmarks');
    addMainSection('open_tabs');

    computeDynamicLayout();

    scrollToPane(getPaneIndexFromHash());
    updateSyncEmptyState();

    window.onpopstate = onPopStateHandler;
    window.addEventListener('hashchange', updatePaneOnHash);
    window.addEventListener('resize', windowResizeHandler);

    if (!bookmarkShortcutMode)
      window.addEventListener('contextmenu', contextMenuHandler);
  }

  function sendNTPTitleLoadedNotification() {
    if (!titleLoadedStatus_) {
      titleLoadedStatus_ = true;
      chrome.send('notifyNTPTitleLoaded');
    }
  }

  /**
   * Notifies the chrome process of the status of the NTP.
   */
  function sendNTPNotification() {
    if (loadStatus_ != LoadStatusType.LOAD_COMPLETE)
      return;

    if (!finishedLoadingNotificationSent_) {
      finishedLoadingNotificationSent_ = true;
      chrome.send('notifyNTPReady');
    } else {
      // Navigating after the loading complete notification has been sent
      // might break tests.
      chrome.send('NTPUnexpectedNavigation');
    }
  }

  /**
   * The default click handler for created item shortcuts.
   *
   * @param {Object} item The item specification.
   * @param {function} evt The browser click event triggered.
   */
  function itemShortcutClickHandler(item, evt) {
    // Handle the touch callback
    if (item['folder']) {
      browseToBookmarkFolder(item.id);
    } else {
      if (bookmarkShortcutMode) {
        chrome.send('createHomeScreenBookmarkShortcut', [item.id]);
      } else if (!!item.url) {
        window.location = item.url;
      }
    }
  }

  /**
   * Opens a recently closed tab.
   *
   * @param {Object} item An object containing the necessary information to
   *     reopen a tab.
   */
  function openRecentlyClosedTab(item, evt) {
    chrome.send('openedRecentlyClosed');
    chrome.send('reopenTab', [item.sessionId]);
  }

  /**
   * Creates a 'div' DOM element.
   *
   * @param {string} className The CSS class name for the DIV.
   * @param {string=} opt_backgroundUrl The background URL to be applied to the
   *     DIV if required.
   * @return {Element} The newly created DIV element.
   */
  function createDiv(className, opt_backgroundUrl) {
    var div = document.createElement('div');
    div.className = className;
    if (opt_backgroundUrl)
      div.style.backgroundImage = 'url(' + opt_backgroundUrl + ')';
    return div;
  }

  /**
   * Helper for creating new DOM elements.
   *
   * @param {string} type The type of Element to be created (i.e. 'div',
   *     'span').
   * @param {Object} params A mapping of element attribute key and values that
   *     should be applied to the new element.
   * @return {Element} The newly created DOM element.
   */
  function createElement(type, params) {
    var el = document.createElement(type);
    if (typeof params === 'string') {
      el.className = params;
    } else {
      for (attr in params) {
        el[attr] = params[attr];
      }
    }
    return el;
  }

  /**
   * Adds a click listener to a specified element with the ability to override
   * the default value of itemShortcutClickHandler.
   *
   * @param {Element} el The element the click listener should be added to.
   * @param {Object} item The item data represented by the element.
   * @param {function(Object, string, BrowserEvent)=} opt_clickCallback The
   *     click callback to be triggered upon selection.
   */
  function wrapClickHandler(el, item, opt_clickCallback) {
    el.addEventListener('click', function(evt) {
      var clickCallback =
          opt_clickCallback ? opt_clickCallback : itemShortcutClickHandler;
      clickCallback(item, evt);
    });
  }

  /**
   * Create a DOM element to contain a recently closed item for a tablet
   * device.
   *
   * @param {Object} item The data of the item used to generate the shortcut.
   * @param {function(Object, string, BrowserEvent)=} opt_clickCallback The
   *     click callback to be triggered upon selection (if not provided it will
   *     use the default -- itemShortcutClickHandler).
   * @return {Element} The shortcut element created.
   */
  function makeRecentlyClosedTabletItem(item, opt_clickCallback) {
    var cell = createDiv('cell');

    cell.setAttribute(CONTEXT_MENU_URL_KEY, item.url);

    var iconUrl = item.icon;
    if (!iconUrl) {
      iconUrl = 'chrome://touch-icon/size/16@' + window.devicePixelRatio +
          'x/' + item.url;
    }
    var icon = createDiv('icon', iconUrl);
    trackImageLoad(iconUrl);
    cell.appendChild(icon);

    var title = createDiv('title');
    title.textContent = item.title;
    cell.appendChild(title);

    wrapClickHandler(cell, item, opt_clickCallback);

    return cell;
  }

  /**
   * Creates a shortcut DOM element based on the item specified item
   * configuration using the thumbnail layout used for most visited.  Other
   * data types should not use this as they won't have a thumbnail.
   *
   * @param {Object} item The data of the item used to generate the shortcut.
   * @param {function(Object, string, BrowserEvent)=} opt_clickCallback The
   *     click callback to be triggered upon selection (if not provided it will
   *     use the default -- itemShortcutClickHandler).
   * @return {Element} The shortcut element created.
   */
  function makeMostVisitedItem(item, opt_clickCallback) {
    // thumbnail-cell          -- main outer container
    //   thumbnail-container   -- container for the thumbnail
    //     thumbnail           -- the actual thumbnail image; outer border
    //     inner-border        -- inner border
    //   title                 -- container for the title
    //     img                 -- hack align title text baseline with bottom
    //     title text          -- the actual text of the title
    var thumbnailCell = createDiv('thumbnail-cell');
    var thumbnailContainer = createDiv('thumbnail-container');
    var backgroundUrl = item.thumbnailUrl || 'chrome://thumb/' + item.url;
    if (backgroundUrl == 'chrome://thumb/chrome://welcome/') {
      // Ideally, it would be nice to use the URL as is.  However, as of now
      // theme support has been removed from Chrome.  Instead, load the image
      // URL from a style and use it.  Don't just use the style because
      // trackImageLoad(...) must be called with the background URL.
      var welcomeStyle = findCssRule('.welcome-to-chrome').style;
      var backgroundImage = welcomeStyle.backgroundImage;
      // trim the "url(" prefix and ")" suffix
      backgroundUrl = backgroundImage.substring(4, backgroundImage.length - 1);
    }
    trackImageLoad(backgroundUrl);
    var thumbnail = createDiv('thumbnail');
    // Use an Image object to ensure the thumbnail image actually exists.  If
    // not, this will allow the default to show instead.
    var thumbnailImg = new Image();
    thumbnailImg.onload = function() {
      thumbnail.style.backgroundImage = 'url(' + backgroundUrl + ')';
    };
    thumbnailImg.src = backgroundUrl;

    thumbnailContainer.appendChild(thumbnail);
    var innerBorder = createDiv('inner-border');
    thumbnailContainer.appendChild(innerBorder);
    thumbnailCell.appendChild(thumbnailContainer);
    var title = createDiv('title');
    title.textContent = item.title;
    var spacerImg = createElement('img', 'title-spacer');
    spacerImg.alt = '';
    title.insertBefore(spacerImg, title.firstChild);
    thumbnailCell.appendChild(title);

    var shade = createDiv('thumbnail-cell-shade');
    thumbnailContainer.appendChild(shade);
    addActiveTouchListener(shade, 'thumbnail-cell-shade-active');

    wrapClickHandler(thumbnailCell, item, opt_clickCallback);

    thumbnailCell.setAttribute(CONTEXT_MENU_URL_KEY, item.url);
    thumbnailCell.contextMenuItem = item;
    return thumbnailCell;
  }

  /**
   * Creates a shortcut DOM element based on the item specified item
   * configuration using the favicon layout used for bookmarks.
   *
   * @param {Object} item The data of the item used to generate the shortcut.
   * @param {function(Object, string, BrowserEvent)=} opt_clickCallback The
   *     click callback to be triggered upon selection (if not provided it will
   *     use the default -- itemShortcutClickHandler).
   * @return {Element} The shortcut element created.
   */
  function makeBookmarkItem(item, opt_clickCallback) {
    var holder = createDiv('favicon-cell');
    addActiveTouchListener(holder, 'favicon-cell-active');

    holder.setAttribute(CONTEXT_MENU_URL_KEY, item.url);
    holder.contextMenuItem = item;
    var faviconBox = createDiv('favicon-box');
    if (item.folder) {
      faviconBox.classList.add('folder');
    } else {
      var iconUrl = item.icon || 'chrome://touch-icon/largest/' + item.url;
      var faviconIcon = createDiv('favicon-icon');
      faviconIcon.style.backgroundImage = 'url(' + iconUrl + ')';
      trackImageLoad(iconUrl);

      var image = new Image();
      image.src = iconUrl;
      image.onload = function() {
        var w = image.width;
        var h = image.height;
        if (Math.floor(w) <= 16 || Math.floor(h) <= 16) {
          // it's a standard favicon (or at least it's small).
          faviconBox.classList.add('document');

          faviconBox.appendChild(
              createDiv('color-strip colorstrip-' + faviconIndex));
          faviconBox.appendChild(createDiv('bookmark-border'));
          var foldDiv = createDiv('fold');
          foldDiv.id = 'fold_' + faviconIndex;
          foldDiv.style['background'] =
              '-webkit-canvas(fold_' + faviconIndex + ')';

          // Use a container so that the fold it self can be zoomed without
          // changing the positioning of the fold.
          var foldContainer = createDiv('fold-container');
          foldContainer.appendChild(foldDiv);
          faviconBox.appendChild(foldContainer);

          // FaviconWebUIHandler::HandleGetFaviconDominantColor expects
          // an URL that starts with chrome://favicon/size/.
          // The handler always loads 16x16 1x favicon and assumes that
          // the dominant color for all scale factors is the same.
          chrome.send('getFaviconDominantColor',
              [('chrome://favicon/size/16@1x/' + item.url), '' + faviconIndex]);
          faviconIndex++;
        } else if ((w == 57 && h == 57) || (w == 114 && h == 114)) {
          // it's a touch icon for 1x or 2x.
          faviconIcon.classList.add('touch-icon');
        } else {
          // It's an html5 icon (or at least it's larger).
          // Rescale it to be no bigger than 64x64 dip.
          var max = 64;
          if (w > max || h > max) {
            var scale = (w > h) ? (max / w) : (max / h);
            w *= scale;
            h *= scale;
          }
          faviconIcon.style.backgroundSize = w + 'px ' + h + 'px';
        }
      };
      faviconBox.appendChild(faviconIcon);
    }
    holder.appendChild(faviconBox);

    var title = createDiv('title');
    title.textContent = item.title;
    holder.appendChild(title);

    wrapClickHandler(holder, item, opt_clickCallback);

    return holder;
  }

  /**
   * Adds touch listeners to the specified element to apply a class when it is
   * selected (removing the class when no longer pressed).
   *
   * @param {Element} el The element to apply the class to when touched.
   * @param {string} activeClass The CSS class name to be applied when active.
   */
  function addActiveTouchListener(el, activeClass) {
    if (!window.touchCancelListener) {
      window.touchCancelListener = function(evt) {
        if (activeItemDelayTimerId) {
          clearTimeout(activeItemDelayTimerId);
          activeItemDelayTimerId = undefined;
        }
        if (!activeItem) {
          return;
        }
        activeItem.classList.remove(activeItem.dataset.activeClass);
        activeItem = null;
      };
      document.addEventListener('touchcancel', window.touchCancelListener);
    }
    el.dataset.activeClass = activeClass;
    el.addEventListener(PRESS_START_EVT, function(evt) {
      if (activeItemDelayTimerId) {
        clearTimeout(activeItemDelayTimerId);
        activeItemDelayTimerId = undefined;
      }
      activeItemDelayTimerId = setTimeout(function() {
        el.classList.add(activeClass);
        activeItem = el;
      }, ACTIVE_ITEM_DELAY_MS);
    });
    el.addEventListener(PRESS_STOP_EVT, function(evt) {
      if (activeItemDelayTimerId) {
        clearTimeout(activeItemDelayTimerId);
        activeItemDelayTimerId = undefined;
      }
      // Add the active class to ensure the pressed state is visible when
      // quickly tapping, which can happen if the start and stop events are
      // received before the active item delay timer has been executed.
      el.classList.add(activeClass);
      el.classList.add('no-active-delay');
      setTimeout(function() {
        el.classList.remove(activeClass);
        el.classList.remove('no-active-delay');
      }, 0);
      activeItem = null;
    });
  }

  /**
   * Creates a shortcut DOM element based on the item specified in the list
   * format.
   *
   * @param {Object} item The data of the item used to generate the shortcut.
   * @param {function(Object, string, BrowserEvent)=} opt_clickCallback The
   *     click callback to be triggered upon selection (if not provided it will
   *     use the default -- itemShortcutClickHandler).
   * @return {Element} The shortcut element created.
   */
  function makeListEntryItem(item, opt_clickCallback) {
    var listItem = createDiv('list-item');
    addActiveTouchListener(listItem, ACTIVE_LIST_ITEM_CSS_CLASS);
    listItem.setAttribute(CONTEXT_MENU_URL_KEY, item.url);
    var iconSize = item.iconSize || 64;
    var iconUrl = item.icon ||
        'chrome://touch-icon/size/' + iconSize + '@1x/' + item.url;
    listItem.appendChild(createDiv('icon', iconUrl));
    trackImageLoad(iconUrl);
    var title = createElement('div', {
      textContent: item.title,
      className: 'title session_title'
    });
    listItem.appendChild(title);

    listItem.addEventListener('click', function(evt) {
      var clickCallback =
          opt_clickCallback ? opt_clickCallback : itemShortcutClickHandler;
      clickCallback(item, evt);
    });
    if (item.divider == 'section') {
      // Add a child div because the section divider has a gradient and
      // webkit doesn't seem to currently support borders with gradients.
      listItem.appendChild(createDiv('section-divider'));
    } else {
      listItem.classList.add('standard-divider');
    }
    return listItem;
  }

  /**
   * Creates a DOM list entry for a remote session or tab.
   *
   * @param {Object} item The data of the item used to generate the shortcut.
   * @param {function(Object, string, BrowserEvent)=} opt_clickCallback The
   *     click callback to be triggered upon selection (if not provided it will
   *     use the default -- itemShortcutClickHandler).
   * @return {Element} The shortcut element created.
   */
  function makeForeignSessionListEntry(item, opt_clickCallback) {
    // Session item
    var sessionOuterDiv = createDiv('list-item standard-divider');
    addActiveTouchListener(sessionOuterDiv, ACTIVE_LIST_ITEM_CSS_CLASS);
    sessionOuterDiv.contextMenuItem = item;

    var icon = createDiv('session-icon ' + item.iconStyle);
    sessionOuterDiv.appendChild(icon);

    var titleContainer = createElement('div', 'title');
    sessionOuterDiv.appendChild(titleContainer);

    // Extra container to allow title & last-sync time to stack vertically.
    var sessionInnerDiv = createDiv('session_container');
    titleContainer.appendChild(sessionInnerDiv);

    var title = createDiv('session-name');
    title.textContent = item.title;
    title.id = item.titleId || '';
    sessionInnerDiv.appendChild(title);

    var lastSynced = createDiv('session-last-synced');
    lastSynced.textContent =
        templateData.opentabslastsynced + ': ' + item.userVisibleTimestamp;
    lastSynced.id = item.userVisibleTimestampId || '';
    sessionInnerDiv.appendChild(lastSynced);

    sessionOuterDiv.addEventListener('click', function(evt) {
      var clickCallback =
          opt_clickCallback ? opt_clickCallback : itemShortcutClickHandler;
      clickCallback(item, evt);
    });
    return sessionOuterDiv;
  }

  /**
   * Saves the number of most visited pages and updates promo visibility.
   * @param {number} n Number of most visited pages.
   */
  function setNumberOfMostVisitedPages(n) {
    numberOfMostVisitedPages = n;
    updatePromoVisibility();
  }

  /**
   * Saves the recently closed tabs flag and updates promo visibility.
   * @param {boolean} anyTabs Whether there are any recently closed tabs.
   */
  function setHasRecentlyClosedTabs(anyTabs) {
    hasRecentlyClosedTabs = anyTabs;
    updatePromoVisibility();
  }

  /**
   * Updates the most visited pages.
   *
   * @param {Array.<Object>} List of data for displaying the list of most
   *     visited pages (see C++ handler for model description).
   * @param {boolean} hasBlacklistedUrls Whether any blacklisted URLs are
   *     present.
   */
  function setMostVisitedPages(data, hasBlacklistedUrls) {
    setNumberOfMostVisitedPages(data.length);
    // limit the number of most visited items to display
    if (isPhone() && data.length > 6) {
      data.splice(6, data.length - 6);
    } else if (isTablet() && data.length > 8) {
      data.splice(8, data.length - 8);
    }

    data.forEach(function(item, index) {
      item.mostVisitedIndex = index;
    });

    if (equals(data, mostVisitedData_))
      return;

    var clickFunction = function(item) {
      chrome.send('openedMostVisited');
      chrome.send('metricsHandler:recordInHistogram',
          ['NewTabPage.MostVisited', item.mostVisitedIndex, 8]);
      window.location = item.url;
    };
    populateData(findList('most_visited'), SectionType.MOST_VISITED, data,
        makeMostVisitedItem, clickFunction);
    computeDynamicLayout();

    mostVisitedData_ = data;
  }

  /**
   * Updates the recently closed tabs.
   *
   * @param {Array.<Object>} List of data for displaying the list of recently
   *     closed tabs (see C++ handler for model description).
   */
  function setRecentlyClosedTabs(data) {
    var container = $('recently_closed_container');
    if (!data || data.length == 0) {
      // hide the recently closed section if it is empty.
      container.style.display = 'none';
      setHasRecentlyClosedTabs(false);
    } else {
      container.style.display = 'block';
      setHasRecentlyClosedTabs(true);
      var decoratorFunc = isPhone() ? makeListEntryItem :
          makeRecentlyClosedTabletItem;
      populateData(findList('recently_closed'), SectionType.RECENTLY_CLOSED,
          data, decoratorFunc, openRecentlyClosedTab);
    }
    computeDynamicLayout();
  }

  /**
   * Updates the bookmarks.
   *
   * @param {Array.<Object>} List of data for displaying the bookmarks (see
   *     C++ handler for model description).
   */
  function bookmarks(data) {
    bookmarkFolderId = data.id;
    if (!replacedInitialState) {
      history.replaceState(
          {folderId: bookmarkFolderId, selectedPaneIndex: currentPaneIndex},
          null, null);
      replacedInitialState = true;
    }
    if (syncEnabled == undefined) {
      // Wait till we know whether or not sync is enabled before displaying any
      // bookmarks (since they may need to be filtered below)
      bookmarkData = data;
      return;
    }

    var titleWrapper = $('bookmarks_title_wrapper');
    setBookmarkTitleHierarchy(
        titleWrapper, data, data['hierarchy']);

    var filteredBookmarks = data.bookmarks;
    if (!syncEnabled) {
      filteredBookmarks = filteredBookmarks.filter(function(val) {
        return (val.type != 'BOOKMARK_BAR' && val.type != 'OTHER_NODE');
      });
    }
    if (bookmarkShortcutMode) {
      populateData(findList('bookmarks'), SectionType.BOOKMARKS,
          filteredBookmarks, makeBookmarkItem);
    } else {
      var clickFunction = function(item) {
        if (item['folder']) {
          browseToBookmarkFolder(item.id);
        } else if (!!item.url) {
          chrome.send('openedBookmark');
          window.location = item.url;
        }
      };
      populateData(findList('bookmarks'), SectionType.BOOKMARKS,
          filteredBookmarks, makeBookmarkItem, clickFunction);
    }

    var bookmarkContainer = $('bookmarks_container');

    // update the shadows on the  breadcrumb bar
    computeDynamicLayout();

    if ((loadStatus_ & LoadStatusType.LOAD_BOOKMARKS_FINISHED) !=
        LoadStatusType.LOAD_BOOKMARKS_FINISHED) {
      loadStatus_ |= LoadStatusType.LOAD_BOOKMARKS_FINISHED;
      sendNTPNotification();
    }
  }

  /**
   * Checks if promo is allowed and MostVisited requirements are satisfied.
   * @return {boolean} Whether the promo should be shown on most_visited.
   */
  function shouldPromoBeShownOnMostVisited() {
    return promoIsAllowed && promoIsAllowedOnMostVisited &&
        numberOfMostVisitedPages >= 2 && !hasRecentlyClosedTabs;
  }

  /**
   * Checks if promo is allowed and OpenTabs requirements are satisfied.
   * @return {boolean} Whether the promo should be shown on open_tabs.
   */
  function shouldPromoBeShownOnOpenTabs() {
    var snapshotsCount =
        currentSnapshots == null ? 0 : currentSnapshots.length;
    var sessionsCount = currentSessions == null ? 0 : currentSessions.length;
    return promoIsAllowed && promoIsAllowedOnOpenTabs &&
        (snapshotsCount + sessionsCount != 0);
  }

  /**
   * Checks if promo is allowed and SyncPromo requirements are satisfied.
   * @return {boolean} Whether the promo should be shown on sync_promo.
   */
  function shouldPromoBeShownOnSync() {
    var snapshotsCount =
        currentSnapshots == null ? 0 : currentSnapshots.length;
    var sessionsCount = currentSessions == null ? 0 : currentSessions.length;
    return promoIsAllowed && promoIsAllowedOnOpenTabs &&
        (snapshotsCount + sessionsCount == 0);
  }

  /**
   * Records a promo impression on a given section if necessary.
   * @param {string} section Active section name to check.
   */
  function promoUpdateImpressions(section) {
    if (section == 'most_visited' && shouldPromoBeShownOnMostVisited())
      chrome.send('recordImpression', ['most_visited']);
    else if (section == 'open_tabs' && shouldPromoBeShownOnOpenTabs())
      chrome.send('recordImpression', ['open_tabs']);
    else if (section == 'open_tabs' && shouldPromoBeShownOnSync())
      chrome.send('recordImpression', ['sync_promo']);
  }

  /**
   * Updates the visibility on all promo-related items as necessary.
   */
  function updatePromoVisibility() {
    var mostVisitedEl = $('promo_message_on_most_visited');
    var openTabsVCEl = $('promo_vc_list');
    var syncPromoLegacyEl = $('promo_message_on_sync_promo_legacy');
    var syncPromoReceivedEl = $('promo_message_on_sync_promo_received');
    mostVisitedEl.style.display =
        shouldPromoBeShownOnMostVisited() ? 'block' : 'none';
    syncPromoReceivedEl.style.display =
        shouldPromoBeShownOnSync() ? 'block' : 'none';
    syncPromoLegacyEl.style.display =
        shouldPromoBeShownOnSync() ? 'none' : 'block';
    openTabsVCEl.style.display =
        (shouldPromoBeShownOnOpenTabs() && promoIsAllowedAsVirtualComputer) ?
            'block' : 'none';
  }

  /**
   * Called from native.
   * Clears the promotion.
   */
  function clearPromotions() {
    setPromotions({});
  }

  /**
   * Set the element to a parsed and sanitized promotion HTML string.
   * @param {Element} el The element to set the promotion string to.
   * @param {string} html The promotion HTML string.
   * @throws {Error} In case of non supported markup.
   */
  function setPromotionHtml(el, html) {
    if (!el) return;
    el.innerHTML = '';
    if (!html) return;
    var tags = ['BR', 'DIV', 'BUTTON', 'SPAN'];
    var attrs = {
      class: function(node, value) { return true; },
      style: function(node, value) { return true; },
    };
    try {
      var fragment = parseHtmlSubset(html, tags, attrs);
      el.appendChild(fragment);
    } catch (err) {
      console.error(err.toString());
      // Ignore all errors while parsing or setting the element.
    }
  }

  /**
   * Called from native.
   * Sets the text for all promo-related items, updates
   * promo-send-email-target items to send email on click and
   * updates the visibility of items.
   * @param {Object} promotions Dictionary used to fill-in the text.
   */
  function setPromotions(promotions) {
    var mostVisitedEl = $('promo_message_on_most_visited');
    var openTabsEl = $('promo_message_on_open_tabs');
    var syncPromoReceivedEl = $('promo_message_on_sync_promo_received');

    promoIsAllowed = !!promotions.promoIsAllowed;
    promoIsAllowedOnMostVisited = !!promotions.promoIsAllowedOnMostVisited;
    promoIsAllowedOnOpenTabs = !!promotions.promoIsAllowedOnOpenTabs;
    promoIsAllowedAsVirtualComputer = !!promotions.promoIsAllowedAsVC;

    setPromotionHtml(mostVisitedEl, promotions.promoMessage);
    setPromotionHtml(openTabsEl, promotions.promoMessage);
    setPromotionHtml(syncPromoReceivedEl, promotions.promoMessageLong);

    promoInjectedComputerTitleText = promotions.promoVCTitle || '';
    promoInjectedComputerLastSyncedText = promotions.promoVCLastSynced || '';
    var openTabsVCTitleEl = $('promo_vc_title');
    if (openTabsVCTitleEl)
      openTabsVCTitleEl.textContent = promoInjectedComputerTitleText;
    var openTabsVCLastSyncEl = $('promo_vc_lastsync');
    if (openTabsVCLastSyncEl)
      openTabsVCLastSyncEl.textContent = promoInjectedComputerLastSyncedText;

    if (promoIsAllowed) {
      var promoButtonEls =
          document.getElementsByClassName('promo-button');
      for (var i = 0, len = promoButtonEls.length; i < len; i++) {
        promoButtonEls[i].onclick = executePromoAction;
        addActiveTouchListener(promoButtonEls[i], 'promo-button-active');
      }
    }
    updatePromoVisibility();
  }

  /**
   * On-click handler for promo email targets.
   * Performs the promo action "send email".
   * @param {Object} evt User interface event that triggered the action.
   */
  function executePromoAction(evt) {
    evt.preventDefault();
    chrome.send('promoActionTriggered');
  }

  /**
   * Called by the browser when a context menu has been selected.
   *
   * @param {number} itemId The id of the item that was selected, as specified
   *     when chrome.send('showContextMenu') was called.
   */
  function onCustomMenuSelected(itemId) {
    if (contextMenuUrl != null) {
      switch (itemId) {
        case ContextMenuItemIds.BOOKMARK_OPEN_IN_NEW_TAB:
        case ContextMenuItemIds.BOOKMARK_OPEN_IN_INCOGNITO_TAB:
          chrome.send('openedBookmark');
          break;

        case ContextMenuItemIds.MOST_VISITED_OPEN_IN_NEW_TAB:
        case ContextMenuItemIds.MOST_VISITED_OPEN_IN_INCOGNITO_TAB:
          chrome.send('openedMostVisited');
          if (contextMenuItem) {
            chrome.send('metricsHandler:recordInHistogram',
                ['NewTabPage.MostVisited',
                 contextMenuItem.mostVisitedIndex,
                 8]);
          }
          break;

        case ContextMenuItemIds.RECENTLY_CLOSED_OPEN_IN_NEW_TAB:
        case ContextMenuItemIds.RECENTLY_CLOSED_OPEN_IN_INCOGNITO_TAB:
          chrome.send('openedRecentlyClosed');
          break;
      }
    }

    switch (itemId) {
      case ContextMenuItemIds.BOOKMARK_OPEN_IN_NEW_TAB:
      case ContextMenuItemIds.MOST_VISITED_OPEN_IN_NEW_TAB:
      case ContextMenuItemIds.RECENTLY_CLOSED_OPEN_IN_NEW_TAB:
        if (contextMenuUrl != null)
          chrome.send('openInNewTab', [contextMenuUrl]);
        break;

      case ContextMenuItemIds.BOOKMARK_OPEN_IN_INCOGNITO_TAB:
      case ContextMenuItemIds.MOST_VISITED_OPEN_IN_INCOGNITO_TAB:
      case ContextMenuItemIds.RECENTLY_CLOSED_OPEN_IN_INCOGNITO_TAB:
        if (contextMenuUrl != null)
          chrome.send('openInIncognitoTab', [contextMenuUrl]);
        break;

      case ContextMenuItemIds.BOOKMARK_EDIT:
        if (contextMenuItem != null)
          chrome.send('editBookmark', [contextMenuItem.id]);
        break;

      case ContextMenuItemIds.BOOKMARK_DELETE:
        if (contextMenuUrl != null)
          chrome.send('deleteBookmark', [contextMenuItem.id]);
        break;

      case ContextMenuItemIds.MOST_VISITED_REMOVE:
        if (contextMenuUrl != null)
          chrome.send('blacklistURLFromMostVisited', [contextMenuUrl]);
        break;

      case ContextMenuItemIds.BOOKMARK_SHORTCUT:
        if (contextMenuUrl != null)
          chrome.send('createHomeScreenBookmarkShortcut', [contextMenuItem.id]);
        break;

      case ContextMenuItemIds.RECENTLY_CLOSED_REMOVE:
        chrome.send('clearRecentlyClosed');
        break;

      case ContextMenuItemIds.FOREIGN_SESSIONS_REMOVE:
        if (contextMenuItem != null) {
          chrome.send(
              'deleteForeignSession', [contextMenuItem.sessionTag]);
          chrome.send('getForeignSessions');
        }
        break;

      case ContextMenuItemIds.PROMO_VC_SESSION_REMOVE:
        chrome.send('promoDisabled');
        break;

      default:
        log.error('Unknown context menu selected id=' + itemId);
        break;
    }
  }

  /**
   * Generates the full bookmark folder hierarchy and populates the scrollable
   * title element.
   *
   * @param {Element} wrapperEl The wrapper element containing the scrollable
   *     title.
   * @param {string} data The current bookmark folder node.
   * @param {Array.<Object>=} opt_ancestry The folder ancestry of the current
   *     bookmark folder.  The list is ordered in order of closest descendant
   *     (the root will always be the last node).  The definition of each
   *     element is:
   *     - id {number}: Unique ID of the folder (N/A for root node).
   *     - name {string}: Name of the folder (N/A for root node).
   *     - root {boolean}: Whether this is the root node.
   */
  function setBookmarkTitleHierarchy(wrapperEl, data, opt_ancestry) {
    var title = wrapperEl.getElementsByClassName('section-title')[0];
    title.innerHTML = '';
    if (opt_ancestry) {
      for (var i = opt_ancestry.length - 1; i >= 0; i--) {
        var titleCrumb = createBookmarkTitleCrumb_(opt_ancestry[i]);
        title.appendChild(titleCrumb);
        title.appendChild(createDiv('bookmark-separator'));
      }
    }
    var titleCrumb = createBookmarkTitleCrumb_(data);
    titleCrumb.classList.add('title-crumb-active');
    title.appendChild(titleCrumb);

    // Ensure the last crumb is as visible as possible.
    var windowWidth =
        wrapperEl.getElementsByClassName('section-title-mask')[0].offsetWidth;
    var crumbWidth = titleCrumb.offsetWidth;
    var leftOffset = titleCrumb.offsetLeft;

    var shiftLeft = windowWidth - crumbWidth - leftOffset;
    if (shiftLeft < 0) {
      if (crumbWidth > windowWidth)
        shifLeft = -leftOffset;

      // Queue up the scrolling initially to allow for the mask element to
      // be placed into the dom and it's size correctly calculated.
      setTimeout(function() {
        handleTitleScroll(wrapperEl, shiftLeft);
      }, 0);
    } else {
      handleTitleScroll(wrapperEl, 0);
    }
  }

  /**
   * Creates a clickable bookmark title crumb.
   * @param {Object} data The crumb data (see setBookmarkTitleHierarchy for
   *     definition of the data object).
   * @return {Element} The clickable title crumb element.
   * @private
   */
  function createBookmarkTitleCrumb_(data) {
    var titleCrumb = createDiv('title-crumb');
    if (data.root) {
      titleCrumb.innerText = templateData.bookmarkstitle;
    } else {
      titleCrumb.innerText = data.title;
    }
    titleCrumb.addEventListener('click', function(evt) {
      browseToBookmarkFolder(data.root ? '0' : data.id);
    });
    return titleCrumb;
  }

  /**
   * Handles scrolling a title element.
   * @param {Element} wrapperEl The wrapper element containing the scrollable
   *     title.
   * @param {number} scrollPosition The position to be scrolled to.
   */
  function handleTitleScroll(wrapperEl, scrollPosition) {
    var overflowLeftMask =
        wrapperEl.getElementsByClassName('overflow-left-mask')[0];
    var overflowRightMask =
        wrapperEl.getElementsByClassName('overflow-right-mask')[0];
    var title = wrapperEl.getElementsByClassName('section-title')[0];
    var titleMask = wrapperEl.getElementsByClassName('section-title-mask')[0];
    var titleWidth = title.scrollWidth;
    var containerWidth = titleMask.offsetWidth;

    var maxRightScroll = containerWidth - titleWidth;
    var boundedScrollPosition =
        Math.max(maxRightScroll, Math.min(scrollPosition, 0));

    overflowLeftMask.style.opacity =
        Math.min(
            1,
            (Math.max(0, -boundedScrollPosition)) + 10 / 30);

    overflowRightMask.style.opacity =
        Math.min(
            1,
            (Math.max(0, boundedScrollPosition - maxRightScroll) + 10) / 30);

    // Set the position of the title.
    if (titleWidth < containerWidth) {
      // left-align on LTR and right-align on RTL.
      title.style.left = '';
    } else {
      title.style.left = boundedScrollPosition + 'px';
    }
  }

  /**
   * Initializes a scrolling title element.
   * @param {Element} wrapperEl The wrapper element of the scrolling title.
   */
  function initializeTitleScroller(wrapperEl) {
    var title = wrapperEl.getElementsByClassName('section-title')[0];

    var inTitleScroll = false;
    var startingScrollPosition;
    var startingOffset;
    wrapperEl.addEventListener(PRESS_START_EVT, function(evt) {
      inTitleScroll = true;
      startingScrollPosition = getTouchEventX(evt);
      startingOffset = title.offsetLeft;
    });
    document.body.addEventListener(PRESS_STOP_EVT, function(evt) {
      if (!inTitleScroll)
        return;
      inTitleScroll = false;
    });
    document.body.addEventListener(PRESS_MOVE_EVT, function(evt) {
      if (!inTitleScroll)
        return;
      handleTitleScroll(
          wrapperEl,
          startingOffset - (startingScrollPosition - getTouchEventX(evt)));
      evt.stopPropagation();
    });
  }

  /**
   * Handles updates from the underlying bookmark model (calls originate
   * in the WebUI handler for bookmarks).
   *
   * @param {Object} status Describes the type of change that occurred.  Can
   *     contain the following fields:
   *     - parent_id {string}: Unique id of the parent that was affected by
   *                           the change.  If the parent is the bookmark
   *                           bar, then the ID will be 'root'.
   *     - node_id {string}: The unique ID of the node that was affected.
   */
  function bookmarkChanged(status) {
    if (status) {
      var affectedParentNode = status['parent_id'];
      var affectedNodeId = status['node_id'];
      var shouldUpdate = (bookmarkFolderId == affectedParentNode ||
          bookmarkFolderId == affectedNodeId);
      if (shouldUpdate)
        setCurrentBookmarkFolderData(bookmarkFolderId);
    } else {
      // This typically happens when extensive changes could have happened to
      // the model, such as initial load, import and sync.
      setCurrentBookmarkFolderData(bookmarkFolderId);
    }
  }

  /**
   * Loads the bookarks data for a given folder.
   *
   * @param {string|number} folderId The ID of the folder to load (or null if
   *     it should load the root folder).
   */
  function setCurrentBookmarkFolderData(folderId) {
    if (folderId != null) {
      chrome.send('getBookmarks', [folderId]);
    } else {
      chrome.send('getBookmarks');
    }
    try {
      if (folderId == null) {
        localStorage.removeItem(DEFAULT_BOOKMARK_FOLDER_KEY);
      } else {
        localStorage.setItem(DEFAULT_BOOKMARK_FOLDER_KEY, folderId);
      }
    } catch (e) {}
  }

  /**
   * Navigates to the specified folder and handles loading the required data.
   * Ensures the current folder can be navigated back to using the browser
   * controls.
   *
   * @param {string|number} folderId The ID of the folder to navigate to.
   */
  function browseToBookmarkFolder(folderId) {
    history.pushState(
        {folderId: folderId, selectedPaneIndex: currentPaneIndex},
        null, null);
    setCurrentBookmarkFolderData(folderId);
  }

  /**
   * Called to inform the page of the current sync status. If the state has
   * changed from disabled to enabled, it changes the current and default
   * bookmark section to the root directory.  This makes desktop bookmarks are
   * visible.
   */
  function setSyncEnabled(enabled) {
    try {
      if (syncEnabled != undefined && syncEnabled == enabled) {
        // The value didn't change
        return;
      }
      syncEnabled = enabled;

      if (enabled) {
        if (!localStorage.getItem(SYNC_ENABLED_KEY)) {
          localStorage.setItem(SYNC_ENABLED_KEY, 'true');
          setCurrentBookmarkFolderData('0');
        }
      } else {
        localStorage.removeItem(SYNC_ENABLED_KEY);
      }
      updatePromoVisibility();

      if (bookmarkData) {
        // Bookmark data can now be displayed (or needs to be refiltered)
        bookmarks(bookmarkData);
      }

      updateSyncEmptyState();
    } catch (e) {}
  }

  /**
   * Handles adding or removing the 'nothing to see here' text from the session
   * list depending on the state of snapshots and sessions.
   *
   * @param {boolean} Whether the call is occuring because of a schedule
   *     timeout.
   */
  function updateSyncEmptyState(timeout) {
    if (syncState == SyncState.DISPLAYING_LOADING && !timeout) {
      // Make sure 'Loading...' is displayed long enough
      return;
    }

    var openTabsList = findList('open_tabs');
    var snapshotsList = findList('snapshots');
    var syncPromo = $('sync_promo');
    var syncLoading = $('sync_loading');
    var syncEnableSync = $('sync_enable_sync');

    if (syncEnabled == undefined ||
        currentSnapshots == null ||
        currentSessions == null) {
      if (syncState == SyncState.INITIAL) {
        // Wait one second for sync data to come in before displaying loading
        // text.
        syncState = SyncState.WAITING_FOR_DATA;
        syncTimerId = setTimeout(function() { updateSyncEmptyState(true); },
            SYNC_INITIAL_LOAD_TIMEOUT);
      } else if (syncState == SyncState.WAITING_FOR_DATA && timeout) {
        // We've waited for the initial info timeout to pass and still don't
        // have data.  So, display loading text so the user knows something is
        // happening.
        syncState = SyncState.DISPLAYING_LOADING;
        syncLoading.style.display = '-webkit-box';
        centerEmptySections(syncLoading);
        syncTimerId = setTimeout(function() { updateSyncEmptyState(true); },
            SYNC_LOADING_TIMEOUT);
      } else if (syncState == SyncState.DISPLAYING_LOADING) {
        // Allow the Loading... text to go away once data comes in
        syncState = SyncState.DISPLAYED_LOADING;
      }
      return;
    }

    if (syncTimerId != -1) {
      clearTimeout(syncTimerId);
      syncTimerId = -1;
    }
    syncState = SyncState.LOADED;

    // Hide everything by default, display selectively below
    syncEnableSync.style.display = 'none';
    syncLoading.style.display = 'none';
    syncPromo.style.display = 'none';

    var snapshotsCount =
        currentSnapshots == null ? 0 : currentSnapshots.length;
    var sessionsCount = currentSessions == null ? 0 : currentSessions.length;

    if (!syncEnabled) {
      syncEnableSync.style.display = '-webkit-box';
      centerEmptySections(syncEnableSync);
    } else if (sessionsCount + snapshotsCount == 0) {
      syncPromo.style.display = '-webkit-box';
      centerEmptySections(syncPromo);
    } else {
      openTabsList.style.display = sessionsCount == 0 ? 'none' : 'block';
      snapshotsList.style.display = snapshotsCount == 0 ? 'none' : 'block';
    }
    updatePromoVisibility();
  }

  /**
   * Called externally when updated snapshot data is available.
   *
   * @param {Object} data The snapshot data
   */
  function snapshots(data) {
    var list = findList('snapshots');
    list.innerHTML = '';

    currentSnapshots = data;
    updateSyncEmptyState();

    if (!data || data.length == 0)
      return;

    data.sort(function(a, b) {
      return b.createTime - a.createTime;
    });

    // Create the main container
    var snapshotsEl = createElement('div');
    list.appendChild(snapshotsEl);

    // Create the header container
    var headerEl = createDiv('session-header');
    snapshotsEl.appendChild(headerEl);

    // Create the documents container
    var docsEl = createDiv('session-children-container');
    snapshotsEl.appendChild(docsEl);

    // Create the container for the title & icon
    var headerInnerEl = createDiv('list-item standard-divider');
    addActiveTouchListener(headerInnerEl, ACTIVE_LIST_ITEM_CSS_CLASS);
    headerEl.appendChild(headerInnerEl);

    // Create the header icon
    headerInnerEl.appendChild(createDiv('session-icon documents'));

    // Create the header title
    var titleContainer = createElement('span', 'title');
    headerInnerEl.appendChild(titleContainer);
    var title = createDiv('session-name');
    title.textContent = templateData.receivedDocuments;
    titleContainer.appendChild(title);

    // Add support for expanding and collapsing the children
    var expando = createDiv();
    var expandoFunction = createExpandoFunction(expando, docsEl);
    headerInnerEl.addEventListener('click', expandoFunction);
    headerEl.appendChild(expando);

    // Support for actually opening the document
    var snapshotClickCallback = function(item) {
      if (!item)
        return;
      if (item.snapshotId) {
        window.location = 'chrome://snapshot/' + item.snapshotId;
      } else if (item.printJobId) {
        window.location = 'chrome://printjob/' + item.printJobId;
      } else {
        window.location = item.url;
      }
    }

    // Finally, add the list of documents
    populateData(docsEl, SectionType.SNAPSHOTS, data,
        makeListEntryItem, snapshotClickCallback);
  }

  /**
   * Create a function to handle expanding and collapsing a section
   *
   * @param {Element} expando The expando div
   * @param {Element} element The element to expand and collapse
   * @return {function()} A callback function that should be invoked when the
   *     expando is clicked
   */
  function createExpandoFunction(expando, element) {
    expando.className = 'expando open';
    return function() {
      if (element.style.height != '0px') {
        // It seems that '-webkit-transition' only works when explicit pixel
        // values are used.
        setTimeout(function() {
          // If this is the first time to collapse the list, store off the
          // expanded height and also set the height explicitly on the style.
          if (!element.expandedHeight) {
            element.expandedHeight =
                element.clientHeight + 'px';
            element.style.height = element.expandedHeight;
          }
          // Now set the height to 0.  Note, this is also done in a callback to
          // give the layout engine a chance to run after possibly setting the
          // height above.
          setTimeout(function() {
            element.style.height = '0px';
          }, 0);
        }, 0);
        expando.className = 'expando closed';
      } else {
        element.style.height = element.expandedHeight;
        expando.className = 'expando open';
      }
    }
  }

  /**
   * Initializes the promo_vc_list div to look like a foreign session
   * with a desktop.
   */
  function createPromoVirtualComputers() {
    var list = findList('promo_vc');
    list.innerHTML = '';

    // Set up the container and the "virtual computer" session header.
    var sessionEl = createDiv();
    list.appendChild(sessionEl);
    var sessionHeader = createDiv('session-header');
    sessionEl.appendChild(sessionHeader);

    // Set up the session children container and the promo as a child.
    var sessionChildren = createDiv('session-children-container');
    var promoMessage = createDiv('promo-message');
    promoMessage.id = 'promo_message_on_open_tabs';
    sessionChildren.appendChild(promoMessage);
    sessionEl.appendChild(sessionChildren);

    // Add support for expanding and collapsing the children.
    var expando = createDiv();
    var expandoFunction = createExpandoFunction(expando, sessionChildren);

    // Fill-in the contents of the "virtual computer" session header.
    var headerList = [{
      'title': promoInjectedComputerTitleText,
      'titleId': 'promo_vc_title',
      'userVisibleTimestamp': promoInjectedComputerLastSyncedText,
      'userVisibleTimestampId': 'promo_vc_lastsync',
      'iconStyle': 'laptop'
    }];

    populateData(sessionHeader, SectionType.PROMO_VC_SESSION_HEADER, headerList,
        makeForeignSessionListEntry, expandoFunction);
    sessionHeader.appendChild(expando);
  }

  /**
   * Called externally when updated synced sessions data is available.
   *
   * @param {Object} data The snapshot data
   */
  function setForeignSessions(data, tabSyncEnabled) {
    var list = findList('open_tabs');
    list.innerHTML = '';

    currentSessions = data;
    updateSyncEmptyState();

    // Sort the windows within each client such that more recently
    // modified windows appear first.
    data.forEach(function(client) {
      if (client.windows != null) {
        client.windows.sort(function(a, b) {
          if (b.timestamp == null) {
            return -1;
          } else if (a.timestamp == null) {
            return 1;
          } else {
            return b.timestamp - a.timestamp;
          }
        });
      }
    });

    // Sort so more recently modified clients appear first.
    data.sort(function(aClient, bClient) {
      var aWindows = aClient.windows;
      var bWindows = bClient.windows;
      if (bWindows == null || bWindows.length == 0 ||
          bWindows[0].timestamp == null) {
        return -1;
      } else if (aWindows == null || aWindows.length == 0 ||
          aWindows[0].timestamp == null) {
        return 1;
      } else {
        return bWindows[0].timestamp - aWindows[0].timestamp;
      }
    });

    data.forEach(function(client, clientNum) {

      var windows = client.windows;
      if (windows == null || windows.length == 0)
        return;

      // Set up the container for the session header
      var sessionEl = createElement('div');
      list.appendChild(sessionEl);
      var sessionHeader = createDiv('session-header');
      sessionEl.appendChild(sessionHeader);

      // Set up the container for the session children
      var sessionChildren = createDiv('session-children-container');
      sessionEl.appendChild(sessionChildren);

      var clientName = 'Client ' + clientNum;
      if (client.name)
        clientName = client.name;

      var iconStyle;
      var deviceType = client.deviceType;
      if (deviceType == 'win' ||
          deviceType == 'macosx' ||
          deviceType == 'linux' ||
          deviceType == 'chromeos' ||
          deviceType == 'other') {
        iconStyle = 'laptop';
      } else if (deviceType == 'phone') {
        iconStyle = 'phone';
      } else if (deviceType == 'tablet') {
        iconStyle = 'tablet';
      } else {
        console.error('Unknown sync device type found: ', deviceType);
        iconStyle = 'laptop';
      }
      var headerList = [{
        'title': clientName,
        'userVisibleTimestamp': windows[0].userVisibleTimestamp,
        'iconStyle': iconStyle,
        'sessionTag': client.tag,
      }];

      var expando = createDiv();
      var expandoFunction = createExpandoFunction(expando, sessionChildren);
      populateData(sessionHeader, SectionType.FOREIGN_SESSION_HEADER,
          headerList, makeForeignSessionListEntry, expandoFunction);
      sessionHeader.appendChild(expando);

      // Populate the session children container
      var openTabsList = new Array();
      for (var winNum = 0; winNum < windows.length; winNum++) {
        win = windows[winNum];
        var tabs = win.tabs;
        for (var tabNum = 0; tabNum < tabs.length; tabNum++) {
          var tab = tabs[tabNum];
          // If this is the last tab in the window and there are more windows,
          // use a section divider.
          var needSectionDivider =
              (tabNum + 1 == tabs.length) && (winNum + 1 < windows.length);
          tab.icon = tab.icon || 'chrome://favicon/size/16@1x/' + tab.url;

          openTabsList.push({
            timestamp: tab.timestamp,
            title: tab.title,
            url: tab.url,
            sessionTag: client.tag,
            winNum: winNum,
            sessionId: tab.sessionId,
            icon: tab.icon,
            iconSize: 16,
            divider: needSectionDivider ? 'section' : 'standard',
          });
        }
      }
      var tabCallback = function(item, evt) {
        var buttonIndex = 0;
        var altKeyPressed = false;
        var ctrlKeyPressed = false;
        var metaKeyPressed = false;
        var shiftKeyPressed = false;
        if (evt instanceof MouseEvent) {
          buttonIndex = evt.button;
          altKeyPressed = evt.altKey;
          ctrlKeyPressed = evt.ctrlKey;
          metaKeyPressed = evt.metaKey;
          shiftKeyPressed = evt.shiftKey;
        }
        chrome.send('openedForeignSession');
        chrome.send('openForeignSession', [String(item.sessionTag),
            String(item.winNum), String(item.sessionId), buttonIndex,
            altKeyPressed, ctrlKeyPressed, metaKeyPressed, shiftKeyPressed]);
      };
      populateData(sessionChildren, SectionType.FOREIGN_SESSION, openTabsList,
          makeListEntryItem, tabCallback);
    });
  }

  /**
   * Updates the dominant favicon color for a given index.
   *
   * @param {number} index The index of the favicon whose dominant color is
   *     being specified.
   * @param {string} color The string encoded color.
   */
  function setFaviconDominantColor(index, color) {
    var colorstrips = document.getElementsByClassName('colorstrip-' + index);
    for (var i = 0; i < colorstrips.length; i++)
      colorstrips[i].style.background = color;

    var id = 'fold_' + index;
    var fold = $(id);
    if (!fold)
      return;
    var zoom = window.getComputedStyle(fold).zoom;
    var scale = 1 / window.getComputedStyle(fold).zoom;

    // The width/height of the canvas.  Set to 24 so it looks good across all
    // resolutions.
    var cw = 24;
    var ch = 24;

    // Get the fold canvas and create a path for the fold shape
    var ctx = document.getCSSCanvasContext(
        '2d', 'fold_' + index, cw * scale, ch * scale);
    ctx.beginPath();
    ctx.moveTo(0, 0);
    ctx.lineTo(0, ch * 0.75 * scale);
    ctx.quadraticCurveTo(
        0, ch * scale,
        cw * .25 * scale, ch * scale);
    ctx.lineTo(cw * scale, ch * scale);
    ctx.closePath();

    // Create a gradient for the fold and fill it
    var gradient = ctx.createLinearGradient(cw * scale, 0, 0, ch * scale);
    if (color.indexOf('#') == 0) {
      var r = parseInt(color.substring(1, 3), 16);
      var g = parseInt(color.substring(3, 5), 16);
      var b = parseInt(color.substring(5, 7), 16);
      gradient.addColorStop(0, 'rgba(' + r + ', ' + g + ', ' + b + ', 0.6)');
    } else {
      // assume the color is in the 'rgb(#, #, #)' format
      var rgbBase = color.substring(4, color.length - 1);
      gradient.addColorStop(0, 'rgba(' + rgbBase + ', 0.6)');
    }
    gradient.addColorStop(1, color);
    ctx.fillStyle = gradient;
    ctx.fill();

    // Stroke the fold
    ctx.lineWidth = Math.floor(scale);
    ctx.strokeStyle = color;
    ctx.stroke();
    ctx.strokeStyle = 'rgba(0, 0, 0, 0.1)';
    ctx.stroke();

  }

  /**
   * Finds the list element corresponding to the given name.
   * @param {string} name The name prefix of the DOM element (<prefix>_list).
   * @return {Element} The list element corresponding with the name.
   */
  function findList(name) {
    return $(name + '_list');
  }

  /**
   * Render the given data into the given list, and hide or show the entire
   * container based on whether there are any elements.  The decorator function
   * is used to create the element to be inserted based on the given data
   * object.
   *
   * @param {holder} The dom element that the generated list items will be put
   *     into.
   * @param {SectionType} section The section that data is for.
   * @param {Object} data The data to be populated.
   * @param {function(Object, boolean)} decorator The function that will
   *     handle decorating each item in the data.
   * @param {function(Object, Object)} opt_clickCallback The function that is
   *     called when the item is clicked.
   */
  function populateData(holder, section, data, decorator,
      opt_clickCallback) {
    // Empty other items in the list, if present.
    holder.innerHTML = '';
    var fragment = document.createDocumentFragment();
    if (!data || data.length == 0) {
      fragment.innerHTML = '';
    } else {
      data.forEach(function(item) {
        var el = decorator(item, opt_clickCallback);
        el.setAttribute(SECTION_KEY, section);
        el.id = section + fragment.childNodes.length;
        fragment.appendChild(el);
      });
    }
    holder.appendChild(fragment);
    if (holder.classList.contains(GRID_CSS_CLASS))
      centerGrid(holder);
    centerEmptySections(holder);
  }

  /**
   * Given an element containing a list of child nodes arranged in
   * a grid, this will center the grid in the window based on the
   * remaining space.
   * @param {Element} el Container holding the grid cell items.
   */
  function centerGrid(el) {
    var childEl = el.firstChild;
    if (!childEl)
      return;

    // Find the element to actually set the margins on.
    var toCenter = el;
    var curEl = toCenter;
    while (curEl && curEl.classList) {
      if (curEl.classList.contains(GRID_CENTER_CSS_CLASS)) {
        toCenter = curEl;
        break;
      }
      curEl = curEl.parentNode;
    }
    var setItemMargins = el.classList.contains(GRID_SET_ITEM_MARGINS);
    var itemWidth = getItemWidth(childEl, setItemMargins);
    var windowWidth = document.documentElement.offsetWidth;
    if (itemWidth >= windowWidth) {
      toCenter.style.paddingLeft = '0';
      toCenter.style.paddingRight = '0';
    } else {
      var numColumns = el.getAttribute(GRID_COLUMNS);
      if (numColumns) {
        numColumns = parseInt(numColumns);
      } else {
        numColumns = Math.floor(windowWidth / itemWidth);
      }

      if (setItemMargins) {
        // In this case, try to size each item to fill as much space as
        // possible.
        var gutterSize =
            (windowWidth - itemWidth * numColumns) / (numColumns + 1);
        var childLeftMargin = Math.round(gutterSize / 2);
        var childRightMargin = Math.floor(gutterSize - childLeftMargin);
        var children = el.childNodes;
        for (var i = 0; i < children.length; i++) {
          children[i].style.marginLeft = childLeftMargin + 'px';
          children[i].style.marginRight = childRightMargin + 'px';
        }
        itemWidth += childLeftMargin + childRightMargin;
      }

      var remainder = windowWidth - itemWidth * numColumns;
      var leftPadding = Math.round(remainder / 2);
      var rightPadding = Math.floor(remainder - leftPadding);
      toCenter.style.paddingLeft = leftPadding + 'px';
      toCenter.style.paddingRight = rightPadding + 'px';

      if (toCenter.classList.contains(GRID_SET_TOP_MARGIN_CLASS)) {
        var childStyle = window.getComputedStyle(childEl);
        var childLeftPadding = parseInt(
            childStyle.getPropertyValue('padding-left'));
        toCenter.style.paddingTop =
            (childLeftMargin + childLeftPadding + leftPadding) + 'px';
      }
    }
  }

  /**
   * Finds and centers all child grid elements for a given node (the grids
   * do not need to be direct descendants and can reside anywhere in the node
   * hierarchy).
   * @param {Element} el The node containing the grid child nodes.
   */
  function centerChildGrids(el) {
    var grids = el.getElementsByClassName(GRID_CSS_CLASS);
    for (var i = 0; i < grids.length; i++)
      centerGrid(grids[i]);
  }

  /**
   * Finds and vertically centers all 'empty' elements for a given node (the
   * 'empty' elements do not need to be direct descendants and can reside
   * anywhere in the node hierarchy).
   * @param {Element} el The node containing the 'empty' child nodes.
   */
  function centerEmptySections(el) {
    if (el.classList &&
        el.classList.contains(CENTER_EMPTY_CONTAINER_CSS_CLASS)) {
      centerEmptySection(el);
    }
    var empties = el.getElementsByClassName(CENTER_EMPTY_CONTAINER_CSS_CLASS);
    for (var i = 0; i < empties.length; i++) {
      centerEmptySection(empties[i]);
    }
  }

  /**
   * Set the top of the given element to the top of the parent and set the
   * height to (bottom of document - top).
   *
   * @param {Element} el Container holding the centered content.
   */
  function centerEmptySection(el) {
    var parent = el.parentNode;
    var top = parent.offsetTop;
    var bottom = (
        document.documentElement.offsetHeight - getButtonBarPadding());
    el.style.height = (bottom - top) + 'px';
    el.style.top = top + 'px';
  }

  /**
   * Finds the index of the panel specified by its prefix.
   * @param {string} The string prefix for the panel.
   * @return {number} The index of the panel.
   */
  function getPaneIndex(panePrefix) {
    var pane = $(panePrefix + '_container');

    if (pane != null) {
      var index = panes.indexOf(pane);

      if (index >= 0)
        return index;
    }
    return 0;
  }

  /**
   * Finds the index of the panel specified by location hash.
   * @return {number} The index of the panel.
   */
  function getPaneIndexFromHash() {
    var paneIndex;
    if (window.location.hash == '#bookmarks') {
      paneIndex = getPaneIndex('bookmarks');
    } else if (window.location.hash == '#bookmark_shortcut') {
      paneIndex = getPaneIndex('bookmarks');
    } else if (window.location.hash == '#most_visited') {
      paneIndex = getPaneIndex('most_visited');
    } else if (window.location.hash == '#open_tabs') {
      paneIndex = getPaneIndex('open_tabs');
    } else if (window.location.hash == '#incognito') {
      paneIndex = getPaneIndex('incognito');
    } else {
      // Couldn't find a good section
      paneIndex = -1;
    }
    return paneIndex;
  }

  /**
   * Selects a pane from the top level list (Most Visited, Bookmarks, etc...).
   * @param {number} paneIndex The index of the pane to be selected.
   * @return {boolean} Whether the selected pane has changed.
   */
  function scrollToPane(paneIndex) {
    var pane = panes[paneIndex];

    if (pane == currentPane)
      return false;

    var newHash = '#' + sectionPrefixes[paneIndex];
    // If updated hash matches the current one in the URL, we need to call
    // updatePaneOnHash directly as updating the hash to the same value will
    // not trigger the 'hashchange' event.
    if (bookmarkShortcutMode || newHash == document.location.hash)
      updatePaneOnHash();
    computeDynamicLayout();
    promoUpdateImpressions(sectionPrefixes[paneIndex]);
    return true;
  }

  /**
   * Updates the pane based on the current hash.
   */
  function updatePaneOnHash() {
    var paneIndex = getPaneIndexFromHash();
    var pane = panes[paneIndex];

    if (currentPane)
      currentPane.classList.remove('selected');
    pane.classList.add('selected');
    currentPane = pane;
    currentPaneIndex = paneIndex;

    setScrollTopForDocument(document, 0);

    var panelPrefix = sectionPrefixes[paneIndex];
    var title = templateData[panelPrefix + '_document_title'];
    if (!title)
      title = templateData['title'];
    document.title = title;

    sendNTPTitleLoadedNotification();

    // TODO (dtrainor): Could potentially add logic to reset the bookmark state
    // if they are moving to that pane.  This logic was in there before, but
    // was removed due to the fact that we have to go to this pane as part of
    // the history navigation.
  }

  /**
   * Adds a top level section to the NTP.
   * @param {string} panelPrefix The prefix of the element IDs corresponding
   *     to the container of the content.
   * @param {boolean=} opt_canBeDefault Whether this section can be marked as
   *     the default starting point for subsequent instances of the NTP.  The
   *     default value for this is true.
   */
  function addMainSection(panelPrefix) {
    var paneEl = $(panelPrefix + '_container');
    var paneIndex = panes.push(paneEl) - 1;
    sectionPrefixes.push(panelPrefix);
  }

  /**
   * Handles the dynamic layout of the components on the new tab page.  Only
   * layouts that require calculation based on the screen size should go in
   * this function as it will be called during all resize changes
   * (orientation, keyword being displayed).
   */
  function computeDynamicLayout() {
    // Update the scrolling titles to ensure they are not in a now invalid
    // scroll position.
    var titleScrollers =
        document.getElementsByClassName('section-title-wrapper');
    for (var i = 0, len = titleScrollers.length; i < len; i++) {
      var titleEl =
          titleScrollers[i].getElementsByClassName('section-title')[0];
      handleTitleScroll(
          titleScrollers[i],
          titleEl.offsetLeft);
    }

    updateMostVisitedStyle();
    updateMostVisitedHeight();
  }

  /**
   * The centering of the 'recently closed' section is different depending on
   * the orientation of the device.  In landscape, it should be left-aligned
   * with the 'most used' section.  In portrait, it should be centered in the
   * screen.
   */
  function updateMostVisitedStyle() {
    if (isTablet()) {
      updateMostVisitedStyleTablet();
    } else {
      updateMostVisitedStylePhone();
    }
  }

  /**
   * Updates the style of the most visited pane for the phone.
   */
  function updateMostVisitedStylePhone() {
    var mostVisitedList = $('most_visited_list');
    var childEl = mostVisitedList.firstChild;
    if (!childEl)
      return;

    // 'natural' height and width of the thumbnail
    var thumbHeight = 72;
    var thumbWidth = 108;
    var labelHeight = 25;
    var labelWidth = thumbWidth + 20;
    var labelLeft = (thumbWidth - labelWidth) / 2;
    var itemHeight = thumbHeight + labelHeight;

    // default vertical margin between items
    var itemMarginTop = 0;
    var itemMarginBottom = 0;
    var itemMarginLeft = 20;
    var itemMarginRight = 20;

    var listHeight = 0;

    var screenHeight =
        document.documentElement.offsetHeight -
        getButtonBarPadding();

    if (isPortrait()) {
      mostVisitedList.setAttribute(GRID_COLUMNS, '2');
      listHeight = screenHeight * .85;
      // Ensure that listHeight is not too small and not too big.
      listHeight = Math.max(listHeight, (itemHeight * 3) + 20);
      listHeight = Math.min(listHeight, 420);
      // Size for 3 rows (4 gutters)
      itemMarginTop = (listHeight - (itemHeight * 3)) / 4;
    } else {
      mostVisitedList.setAttribute(GRID_COLUMNS, '3');
      listHeight = screenHeight;

      // If the screen height is less than targetHeight, scale the size of the
      // thumbnails such that the margin between the thumbnails remains
      // constant.
      var targetHeight = 220;
      if (screenHeight < targetHeight) {
        var targetRemainder = targetHeight - 2 * (thumbHeight + labelHeight);
        var scale = (screenHeight - 2 * labelHeight -
            targetRemainder) / (2 * thumbHeight);
        // update values based on scale
        thumbWidth = Math.round(thumbWidth * scale);
        thumbHeight = Math.round(thumbHeight * scale);
        labelWidth = thumbWidth + 20;
        itemHeight = thumbHeight + labelHeight;
      }

      // scale the vertical margin such that the items fit perfectly on the
      // screen
      var remainder = screenHeight - (2 * itemHeight);
      var margin = (remainder / 2);
      margin = margin > 24 ? 24 : margin;
      itemMarginTop = Math.round(margin / 2);
      itemMarginBottom = Math.round(margin - itemMarginTop);
    }

    mostVisitedList.style.minHeight = listHeight + 'px';

    modifyCssRule('body[device="phone"] .thumbnail-cell',
        'height', itemHeight + 'px');
    modifyCssRule('body[device="phone"] #most_visited_list .thumbnail',
        'height', thumbHeight + 'px');
    modifyCssRule('body[device="phone"] #most_visited_list .thumbnail',
        'width', thumbWidth + 'px');
    modifyCssRule(
        'body[device="phone"] #most_visited_list .thumbnail-container',
        'height', thumbHeight + 'px');
    modifyCssRule(
        'body[device="phone"] #most_visited_list .thumbnail-container',
        'width', thumbWidth + 'px');
    modifyCssRule('body[device="phone"] #most_visited_list .title',
        'width', labelWidth + 'px');
    modifyCssRule('body[device="phone"] #most_visited_list .title',
        'left', labelLeft + 'px');
    modifyCssRule('body[device="phone"] #most_visited_list .inner-border',
        'height', thumbHeight - 2 + 'px');
    modifyCssRule('body[device="phone"] #most_visited_list .inner-border',
        'width', thumbWidth - 2 + 'px');

    modifyCssRule('body[device="phone"] .thumbnail-cell',
        'margin-left', itemMarginLeft + 'px');
    modifyCssRule('body[device="phone"] .thumbnail-cell',
        'margin-right', itemMarginRight + 'px');
    modifyCssRule('body[device="phone"] .thumbnail-cell',
        'margin-top', itemMarginTop + 'px');
    modifyCssRule('body[device="phone"] .thumbnail-cell',
        'margin-bottom', itemMarginBottom + 'px');

    centerChildGrids($('most_visited_container'));
  }

  /**
   * Updates the style of the most visited pane for the tablet.
   */
  function updateMostVisitedStyleTablet() {
    function setCenterIconGrid(el, set) {
      if (set) {
        el.classList.add(GRID_CENTER_CSS_CLASS);
      } else {
        el.classList.remove(GRID_CENTER_CSS_CLASS);
        el.style.paddingLeft = '0px';
        el.style.paddingRight = '0px';
      }
    }
    var isPortrait = document.documentElement.offsetWidth <
        document.documentElement.offsetHeight;
    var mostVisitedContainer = $('most_visited_container');
    var mostVisitedList = $('most_visited_list');
    var recentlyClosedContainer = $('recently_closed_container');
    var recentlyClosedList = $('recently_closed_list');

    setCenterIconGrid(mostVisitedContainer, !isPortrait);
    setCenterIconGrid(mostVisitedList, isPortrait);
    setCenterIconGrid(recentlyClosedContainer, isPortrait);
    if (isPortrait) {
      recentlyClosedList.classList.add(GRID_CSS_CLASS);
    } else {
      recentlyClosedList.classList.remove(GRID_CSS_CLASS);
    }

    // Make the recently closed list visually left align with the most recently
    // closed items in landscape mode.  It will be reset by the grid centering
    // in portrait mode.
    if (!isPortrait)
      recentlyClosedContainer.style.paddingLeft = '14px';
  }

  /**
   * This handles updating some of the spacing to make the 'recently closed'
   * section appear at the bottom of the page.
   */
  function updateMostVisitedHeight() {
    if (!isTablet())
      return;
    // subtract away height of button bar
    var windowHeight = document.documentElement.offsetHeight;
    var padding = parseInt(window.getComputedStyle(document.body)
        .getPropertyValue('padding-bottom'));
    $('most_visited_container').style.minHeight =
        (windowHeight - padding) + 'px';
  }

  /**
   * Called by the native toolbar to open a different section. This handles
   * updating the hash url which in turns makes a history entry.
   *
   * @param {string} section The section to switch to.
   */
  var openSection = function(section) {
    if (!scrollToPane(getPaneIndex(section)))
      return;
    // Update the url so the native toolbar knows the pane has changed and
    // to create a history entry.
    document.location.hash = '#' + section;
  }

  /////////////////////////////////////////////////////////////////////////////
  // NTP Scoped Window Event Listeners.
  /////////////////////////////////////////////////////////////////////////////

  /**
   * Handles history on pop state changes.
   */
  function onPopStateHandler(event) {
    if (event.state != null) {
      var evtState = event.state;
      // Navigate back to the previously selected panel and ensure the same
      // bookmarks are loaded.
      var selectedPaneIndex = evtState.selectedPaneIndex == undefined ?
          0 : evtState.selectedPaneIndex;

      scrollToPane(selectedPaneIndex);
      setCurrentBookmarkFolderData(evtState.folderId);
    } else {
      // When loading the page, replace the default state with one that
      // specifies the default panel loaded via localStorage as well as the
      // default bookmark folder.
      history.replaceState(
          {folderId: bookmarkFolderId, selectedPaneIndex: currentPaneIndex},
          null, null);
    }
  }

  /**
   * Handles window resize events.
   */
  function windowResizeHandler() {
    // Scroll to the current pane to refactor all the margins and offset.
    scrollToPane(currentPaneIndex);
    computeDynamicLayout();
    // Center the padding for each of the grid views.
    centerChildGrids(document);
    centerEmptySections(document);
  }

  /*
   * We implement the context menu ourselves.
   */
  function contextMenuHandler(evt) {
    var section = SectionType.UNKNOWN;
    contextMenuUrl = null;
    contextMenuItem = null;
    // The node with a menu have been tagged with their section and url.
    // Let's find these tags.
    var node = evt.target;
    while (node) {
      if (section == SectionType.UNKNOWN &&
          node.getAttribute &&
          node.getAttribute(SECTION_KEY) != null) {
        section = node.getAttribute(SECTION_KEY);
        if (contextMenuUrl != null)
          break;
      }
      if (contextMenuUrl == null) {
        contextMenuUrl = node.getAttribute(CONTEXT_MENU_URL_KEY);
        contextMenuItem = node.contextMenuItem;
        if (section != SectionType.UNKNOWN)
          break;
      }
      node = node.parentNode;
    }

    var menuOptions;

    if (section == SectionType.BOOKMARKS &&
        !contextMenuItem.folder && !isIncognito) {
      menuOptions = [
        [
          ContextMenuItemIds.BOOKMARK_OPEN_IN_NEW_TAB,
          templateData.elementopeninnewtab
        ]
      ];
      if (isIncognitoEnabled) {
        menuOptions.push([
          ContextMenuItemIds.BOOKMARK_OPEN_IN_INCOGNITO_TAB,
          templateData.elementopeninincognitotab
        ]);
      }
      if (contextMenuItem.editable) {
        menuOptions.push(
            [ContextMenuItemIds.BOOKMARK_EDIT, templateData.bookmarkedit],
            [ContextMenuItemIds.BOOKMARK_DELETE, templateData.bookmarkdelete]);
      }
      if (contextMenuUrl.search('chrome://') == -1 &&
          contextMenuUrl.search('about://') == -1 &&
          document.body.getAttribute('shortcut_item_enabled') == 'true') {
        menuOptions.push([
          ContextMenuItemIds.BOOKMARK_SHORTCUT,
          templateData.bookmarkshortcut
        ]);
      }
    } else if (section == SectionType.BOOKMARKS &&
               !contextMenuItem.folder &&
               isIncognito) {
      menuOptions = [
        [
          ContextMenuItemIds.BOOKMARK_OPEN_IN_INCOGNITO_TAB,
          templateData.elementopeninincognitotab
        ]
      ];
    } else if (section == SectionType.BOOKMARKS &&
               contextMenuItem.folder &&
               contextMenuItem.editable &&
               !isIncognito) {
      menuOptions = [
        [ContextMenuItemIds.BOOKMARK_EDIT, templateData.editfolder],
        [ContextMenuItemIds.BOOKMARK_DELETE, templateData.deletefolder]
      ];
    } else if (section == SectionType.MOST_VISITED) {
      menuOptions = [
        [
          ContextMenuItemIds.MOST_VISITED_OPEN_IN_NEW_TAB,
          templateData.elementopeninnewtab
        ],
      ];
      if (isIncognitoEnabled) {
        menuOptions.push([
          ContextMenuItemIds.MOST_VISITED_OPEN_IN_INCOGNITO_TAB,
          templateData.elementopeninincognitotab
        ]);
      }
      menuOptions.push(
        [ContextMenuItemIds.MOST_VISITED_REMOVE, templateData.elementremove]);
    } else if (section == SectionType.RECENTLY_CLOSED) {
      menuOptions = [
        [
          ContextMenuItemIds.RECENTLY_CLOSED_OPEN_IN_NEW_TAB,
          templateData.elementopeninnewtab
        ],
      ];
      if (isIncognitoEnabled) {
        menuOptions.push([
          ContextMenuItemIds.RECENTLY_CLOSED_OPEN_IN_INCOGNITO_TAB,
          templateData.elementopeninincognitotab
        ]);
      }
      menuOptions.push(
        [ContextMenuItemIds.RECENTLY_CLOSED_REMOVE, templateData.removeall]);
    } else if (section == SectionType.FOREIGN_SESSION_HEADER) {
      menuOptions = [
        [
          ContextMenuItemIds.FOREIGN_SESSIONS_REMOVE,
          templateData.elementremove
        ]
      ];
    } else if (section == SectionType.PROMO_VC_SESSION_HEADER) {
      menuOptions = [
        [
          ContextMenuItemIds.PROMO_VC_SESSION_REMOVE,
          templateData.elementremove
        ]
      ];
    }

    if (menuOptions)
      chrome.send('showContextMenu', menuOptions);

    return false;
  }

  // Return an object with all the exports
  return {
    bookmarks: bookmarks,
    bookmarkChanged: bookmarkChanged,
    clearPromotions: clearPromotions,
    init: init,
    setIncognitoEnabled: setIncognitoEnabled,
    onCustomMenuSelected: onCustomMenuSelected,
    openSection: openSection,
    setFaviconDominantColor: setFaviconDominantColor,
    setForeignSessions: setForeignSessions,
    setIncognitoMode: setIncognitoMode,
    setMostVisitedPages: setMostVisitedPages,
    setPromotions: setPromotions,
    setRecentlyClosedTabs: setRecentlyClosedTabs,
    setSyncEnabled: setSyncEnabled,
    snapshots: snapshots
  };
});

/////////////////////////////////////////////////////////////////////////////
//Utility Functions.
/////////////////////////////////////////////////////////////////////////////

/**
 * A best effort approach for checking simple data object equality.
 * @param {?} val1 The first value to check equality for.
 * @param {?} val2 The second value to check equality for.
 * @return {boolean} Whether the two objects are equal(ish).
 */
function equals(val1, val2) {
  if (typeof val1 != 'object' || typeof val2 != 'object')
    return val1 === val2;

  // Object and array equality checks.
  var keyCountVal1 = 0;
  for (var key in val1) {
    if (!(key in val2) || !equals(val1[key], val2[key]))
      return false;
    keyCountVal1++;
  }
  var keyCountVal2 = 0;
  for (var key in val2)
    keyCountVal2++;
  if (keyCountVal1 != keyCountVal2)
    return false;
  return true;
}

/**
 * Alias for document.getElementById.
 * @param {string} id The ID of the element to find.
 * @return {HTMLElement} The found element or null if not found.
 */
function $(id) {
  return document.getElementById(id);
}

/**
 * @return {boolean} Whether the device is currently in portrait mode.
 */
function isPortrait() {
  return document.documentElement.offsetWidth <
      document.documentElement.offsetHeight;
}

/**
 * Determine if the page should be formatted for tablets.
 * @return {boolean} true if the device is a tablet, false otherwise.
 */
function isTablet() {
  return document.body.getAttribute('device') == 'tablet';
}

/**
 * Determine if the page should be formatted for phones.
 * @return {boolean} true if the device is a phone, false otherwise.
 */
function isPhone() {
  return document.body.getAttribute('device') == 'phone';
}

/**
 * Get the page X coordinate of a touch event.
 * @param {TouchEvent} evt The touch event triggered by the browser.
 * @return {number} The page X coordinate of the touch event.
 */
function getTouchEventX(evt) {
  return (evt.touches[0] || e.changedTouches[0]).pageX;
}

/**
 * Get the page Y coordinate of a touch event.
 * @param {TouchEvent} evt The touch event triggered by the browser.
 * @return {number} The page Y coordinate of the touch event.
 */
function getTouchEventY(evt) {
  return (evt.touches[0] || e.changedTouches[0]).pageY;
}

/**
 * @param {Element} el The item to get the width of.
 * @param {boolean} excludeMargin If true, exclude the width of the margin.
 * @return {number} The total width of a given item.
 */
function getItemWidth(el, excludeMargin) {
  var elStyle = window.getComputedStyle(el);
  var width = el.offsetWidth;
  if (!width || width == 0) {
    width = parseInt(elStyle.getPropertyValue('width'));
    width +=
        parseInt(elStyle.getPropertyValue('border-left-width')) +
        parseInt(elStyle.getPropertyValue('border-right-width'));
    width +=
        parseInt(elStyle.getPropertyValue('padding-left')) +
        parseInt(elStyle.getPropertyValue('padding-right'));
  }
  if (!excludeMargin) {
    width += parseInt(elStyle.getPropertyValue('margin-left')) +
        parseInt(elStyle.getPropertyValue('margin-right'));
  }
  return width;
}

/**
 * @return {number} The padding height of the body due to the button bar
 */
function getButtonBarPadding() {
  var body = document.getElementsByTagName('body')[0];
  var style = window.getComputedStyle(body);
  return parseInt(style.getPropertyValue('padding-bottom'));
}

/**
 * Modify a css rule
 * @param {string} selector The selector for the rule (passed to findCssRule())
 * @param {string} property The property to update
 * @param {string} value The value to update the property to
 * @return {boolean} true if the rule was updated, false otherwise.
 */
function modifyCssRule(selector, property, value) {
  var rule = findCssRule(selector);
  if (!rule)
    return false;
  rule.style[property] = value;
  return true;
}

/**
 * Find a particular CSS rule.  The stylesheets attached to the document
 * are traversed in reverse order.  The rules in each stylesheet are also
 * traversed in reverse order.  The first rule found to match the selector
 * is returned.
 * @param {string} selector The selector for the rule.
 * @return {Object} The rule if one was found, null otherwise
 */
function findCssRule(selector) {
  var styleSheets = document.styleSheets;
  for (i = styleSheets.length - 1; i >= 0; i--) {
    var styleSheet = styleSheets[i];
    var rules = styleSheet.cssRules;
    if (rules == null)
      continue;
    for (j = rules.length - 1; j >= 0; j--) {
      if (rules[j].selectorText == selector)
        return rules[j];
    }
  }
}

/////////////////////////////////////////////////////////////////////////////
// NTP Entry point.
/////////////////////////////////////////////////////////////////////////////

/*
 * Handles initializing the UI when the page has finished loading.
 */
window.addEventListener('DOMContentLoaded', function(evt) {
  ntp.init();
  $('content-area').style.display = 'block';
});