(function()
{
    var get_localStorage = function(key, callback)
    {
        var key = "pref." + key;
        var value = chrome.extension.getBackgroundPage().localStorage[key];
        if(callback)
        {
            //alert(value);
            callback(value);
        }
    };
    /*
    var get_localStorage = function(key, callback)
    {    
        chrome.extension.sendRequest({"type": "get_localStorage", "key": key}, function(response)
        {
            if(callback)
            {
                callback(response.value);
            }
        });
    };
    */
    
    var set_localStorage = function(key, value, callback)
    {
        var key = "pref." + key;
        chrome.extension.getBackgroundPage().localStorage[key] = value;
        if(callback)
        {
            callback(value);
        }
    };
    
    /*
    var set_localStorage = function(key, value, callback)
    {
        chrome.extension.sendRequest({"type": "set_localStorage", "key": key, "value": value}, function(response)
        {
            if(callback)
            {
                callback(value);
            }
        });
    };
    */
    
    $(document).ready(function()
    {
        var link_checkbox_to_pref = function($_checkbox, pref_key)
        {
            var recv_value = function()
            {
                get_localStorage(pref_key, function(val)
                {
                    //alert(pref_key + ":" + val + ":" + typeof(val));
                    $_checkbox.attr("checked", val=="true");
                });
            };
            var send_value = function()
            {
                set_localStorage(pref_key, $_checkbox.attr("checked"));
            };
            
            
            recv_value();
            $_checkbox.click(send_value).change(send_value);
        };
        
        link_checkbox_to_pref($("#enable-linkify-checkbox"), "enable_linkify");
        link_checkbox_to_pref($("#add-related-articles-checkbox"), "add_related_articles");
        link_checkbox_to_pref($("#enable-endless-pages-checkbox"), "enable_endless_pages");
        link_checkbox_to_pref($("#add-related-searches-checkbox"), "add_related_searches");
        link_checkbox_to_pref($("#add-related-search-results-amazon-checkbox"), "add_related_search_results_amazon");
        link_checkbox_to_pref($("#add-related-search-results-oneriot-checkbox"), "add_related_search_results_oneriot");
        link_checkbox_to_pref($("#add-trending-search-results-oneriot-checkbox"), "add_trending_search_results_oneriot");
        link_checkbox_to_pref($("#add-search-refinements-checkbox"), "add_search_refinements");
        link_checkbox_to_pref($("#show-popup-bubble-checkbox"), "show_popup_bubble");
        link_checkbox_to_pref($("#search-oneriot-checkbox"), "search_oneriot");
        link_checkbox_to_pref($("#search-duckduckgo-checkbox"), "search_duckduckgo");
        link_checkbox_to_pref($("#search-yandex-checkbox"), "search_yandex");
        link_checkbox_to_pref($("#search-delicious-checkbox"), "search_delicious");
        link_checkbox_to_pref($("#search-twitter-checkbox"), "search_twitter");
        link_checkbox_to_pref($("#search-surfcanyon-checkbox"), "search_surfcanyon");
        link_checkbox_to_pref($("#search-bing-checkbox"), "search_bing");
        link_checkbox_to_pref($("#search-baidu-checkbox"), "search_baidu");
        link_checkbox_to_pref($("#search-youtube-checkbox"), "search_youtube");
        link_checkbox_to_pref($("#search-wiktionary-checkbox"), "search_wiktionary");
        link_checkbox_to_pref($("#search-google-checkbox"), "search_google");
        
        link_checkbox_to_pref($("#popup-bubble-show-definitions-checkbox"), "popup_bubble_show_definitions");
        link_checkbox_to_pref($("#popup-bubble-open-new-tab-checkbox"), "popup_bubble_open_new_tab");
        link_checkbox_to_pref($("#popup-bubble-force-single-row-checkbox"), "popup_bubble_force_single_row");
    });
}());