// Copyright 2011 Google Inc. All Rights Reserved.

/**
 * @fileoverview Options page for Chrome OS CIN.
 * @author zork@google.com (Zach Kuznia)
 */

var kTableUrlsKey = "table_urls";

function init() {
}

function addTableUrl() {
  var url = document.getElementById("cin_table_url_input").value;

  var table_urls = localStorage[kTableUrlsKey];
  if (!table_urls) {
    localStorage[kTableUrlsKey] = {};
    table_urls = localStorage[kTableUrlsKey];
  }

  if (table_urls[url]) {
    setAddUrlStatus("URL already exists", true);
  } else {
    table_urls[url] = {};
    addTableUrlToTable(url);
    setAddUrlStatus("OK", false);
  }
}

function setAddUrlStatus(status, error) {
  var status_field = document.getElementById("add_url_status");
  status_field.innerHTML = status;

  setTimeout(function() {
    status_field.innerHTML = "";
  }, 750);
}

function addTableUrlToTable(url) {
  var table = document.getElementById("cin_table_url_table");

  var row = table.tBodies[0].insertRow(-1);
  var cell = row.insertCell(-1);
  cell.innerHTML = url;
  var cell = row.insertCell(-1);
  var button = document.createElement('input');
  button.type = 'button';
  button.value = 'Remove';
  button.onclick = function () {
    delete localStorage[kTableUrlsKey][url];
    table.tBodies[0].deleteRow(row.sectionRowIndex);
  }
  cell.appendChild(button);
}

function loadTableUrls() {
  var table_urls = localStorage[kTableUrlsKey];
  if (table_urls) {
    for (table_url in table_urls) {
      addTableUrlToTable(table_url);
    }
  }
}

@@ -197,16 +197,35 @@
 nit() %7B%0A
+  loadTableUrls();%0A
 %7D%0A%0Afunct
@@ -334,63 +334,13 @@
 s = 
-localStorage%5BkTableUrlsKey%5D;%0A  if (!table_urls) %7B%0A    l
+readL
 ocal
@@ -338,33 +338,33 @@
 readLocalStorage
-%5B
+(
 kTableUrlsKey%5D =
@@ -364,65 +364,14 @@
 sKey
-%5D =
+,
  %7B%7D
+)
 ;
-%0A    table_urls = localStorage%5BkTableUrlsKey%5D;%0A  %7D
 %0A%0A  
@@ -476,17 +476,77 @@
 url%5D = %7B
-%7D
+%22url%22: url%7D;%0A    writeLocalStorage(kTableUrlsKey, table_urls)
 ;%0A    ad
@@ -753,79 +753,8 @@
 tus;
-%0A%0A  setTimeout(function() %7B%0A    status_field.innerHTML = %22%22;%0A  %7D, 750);
 %0A%7D%0A%0A
@@ -1127,16 +1127,96 @@
     
-delete l
+var table_urls = readLocalStorage(kTableUrlsKey);%0A    delete table_urls%5Burl%5D;%0A    writeL
 ocal
@@ -1214,33 +1214,33 @@
 riteLocalStorage
-%5B
+(
 kTableUrlsKey%5D%5Bu
@@ -1240,14 +1240,21 @@
 sKey
-%5D%5Burl%5D
+, table_urls)
 ;%0A  
@@ -1385,17 +1385,21 @@
 _urls = 
-l
+readL
 ocalStor
@@ -1401,17 +1401,17 @@
 lStorage
-%5B
+(
 kTableUr
@@ -1415,17 +1415,17 @@
 eUrlsKey
-%5D
+)
 ;%0A  if (
@@ -1519,12 +1519,254 @@
     %7D%0A  %7D%0A%7D%0A
+%0Afunction readLocalStorage(key, default_value) %7B%0A  var data = localStorage%5Bkey%5D;%0A  if (!data) %7B%0A    return default_value;%0A  %7D%0A  return JSON.parse(data);%0A%7D%0A%0Afunction writeLocalStorage(key, data) %7B%0A  localStorage%5Bkey%5D = JSON.stringify(data);%0A%7D%0A

Make the options page work correctly