function ClearBrowserDataOverlay(){
    this.prefsTypeEnum = ['browser.clear_data.browsing_history',
           'browser.clear_data.download_history',
           'browser.clear_data.cache',
           'browser.clear_data.cookies',
           'browser.clear_data.passwords',
           'browser.clear_data.form_data',
           'browser.clear_data.hosted_apps_data',
           'browser.clear_data.content_licenses'];
    this.prefList = {}; //{prefsname : {enabled : true, value: true}};
};

ClearBrowserDataOverlay.prototype.initialize = function(jq){
    this.$ = jq;
    var self = this;
    var node = $("#clear_browser_data_modal_dlg");
    $("#clear_data", node).click(function(){
    });

    $("#cancel_clear", node).click(function(){
      $(node).hide();
    });

    $(".close-button",node).click(function(){
      $(node).hide();
    });

    $("#clear-browser-data-commit", node).click(function(){
      $(this).hide();
        $("#cbc_progress").show();
      chrome.send('performClearBrowserData');
    });

    $("#clear-browser-data-time-period", node).change(function(){
      var time_period = $(this).val();
      var pref = $(this).attr("pref");
      Preferences.getInstance().setIntegerPref(pref, time_period, true);
    });

    $(".cbd-options", node).click(function(){
      var enable = $(this).is(":checked");
      var pref = $(this).attr("pref");
      Preferences.getInstance().setBooleanPref(pref, enable, true);
    });
};

ClearBrowserDataOverlay.prototype.dismiss = function(){
    this.setClearingState(false);
    $("#cbc_progress").hide();
    $("#clear-browser-data-commit").show();
    $("#clear_browser_data_modal_dlg").hide();
};

ClearBrowserDataOverlay.updateHistoryCheckboxes = function(allowed){
};

ClearBrowserDataOverlay.doneClearing = function() {
  window.setTimeout(function() {
    ClearBrowserDataOverlay.getInstance().dismiss();
  }, 1000);
};

ClearBrowserDataOverlay.prototype.flattenMap_ = function(prefix, dict){
  for (var prefName in dict) {
    if (typeof dict[prefName] == 'object') {
      this.flattenMap_(prefix + prefName + '.',
          dict[prefName]);
    }else {
      var value = dict[prefName];
      var full_pref_name = prefix.substr(0, prefix.length - 1);
      if(this.prefList[full_pref_name] == undefined){
        this.prefList[full_pref_name] = {};
      }
      this.prefList[full_pref_name][prefName] = dict[prefName];
    }
  }
};

ClearBrowserDataOverlay.prototype.UpdateCheckboxState_ = function(){
  for (var prefName in this.prefList) {
    var temp = "input[pref=";
    temp += prefName;
    temp += "]";
    if(!this.prefList[prefName].disabled){
      $(temp).attr("checked", this.prefList[prefName].value);
      $(temp).attr("disabled", false);
    }else{
      $(temp).attr("disabled", true);
    }
  }
};

ClearBrowserDataOverlay.prefsFetchedCallback = function(dict){
  ClearBrowserDataOverlay.getInstance().flattenMap_('', dict);
  ClearBrowserDataOverlay.getInstance().UpdateCheckboxState_();
};

ClearBrowserDataOverlay.onShowModalDialogCallback = function(){
  ClearBrowserDataOverlay.getInstance().showModalDialog();
}

ClearBrowserDataOverlay.prototype.showModalDialog = function(){
  $("#clear_browser_data_modal_dlg").show();
  Preferences.getInstance().fetchPrefs(this.prefsTypeEnum, "ClearBrowserDataOverlay.prefsFetchedCallback");
};



ClearBrowserDataOverlay.prototype.setClearingState = function(state) {
  $('#delete-browsing-history-checkbox').disabled = state;
  $('#delete-download-history-checkbox').disabled = state;
  $('#delete-cache-checkbox').disabled = state;
  $('#delete-cookies-checkbox').disabled = state;
  $('#delete-passwords-checkbox').disabled = state;
  $('#delete-form-data-checkbox').disabled = state;
  $('#delete-hosted-apps-data-checkbox').disabled = state;
  $('#deauthorize-content-licenses-checkbox').disabled = state;
  $('#clear-browser-data-time-period').disabled = state;
  $("#clear-browser-data-commit").disabled = state;
};
