/*
var get_localStorage = function(key)
{
    return localStorage[key];
};

var set_localStorage = function(key, value)
{
    localStorage[key] = value;
};
*/



(function()
{
    chrome.extension.onRequest.addListener(function(request, sender, sendResponse)
    {
        if(request.msg_type == "get_localStorage")
        {
            sendResponse({"value": localStorage[request.key]});
        }
        else if(request.msg_type == "show_options")
        {
            var url = chrome.extension.getURL("options.html");
            chrome.tabs.create({
                index: 100000000, //last
                url: url
            });
            sendResponse({});
        }
        else if(request.msg_type == "set_localStorage")
        {
            localStorage[request.key] = localStorage[request.value];
            sendResponse({"status": true});
        }
        else if(request.msg_type == "$.get")
        {
            $.get(request.url, request.data, function(data, textStatus)
            {
                sendResponse({"data": data, "textStatus": textStatus});
            }, request.type);
        }
        else
        {
            sendResponse({});
        }
    });
    
    var pref = function(key, value)
    {
        var key = "pref." + key;
        if(localStorage[key] == undefined)
        {
            localStorage[key] = value;
        }
    };
    

    pref("install-time", "0");
    pref("add_related_articles", true);
    pref("add_related_searches", true);
    pref("add_related_search_results_amazon", true);
    pref("add_related_search_results_oneriot", true);
    pref("add_trending_search_results_oneriot", true);
    pref("show_context_menu_additions", true);
    pref("show_popup_bubble", true);
    pref("search_oneriot", false);
    pref("search_duckduckgo", true);
    pref("search_yandex", false);
    pref("search_delicious", false);
    pref("search_surfcanyon", true);
    pref("search_twitter", false);
    pref("tweet_this", false);
    pref("search_bing", false);
    pref("search_baidu", false);
    pref("search_reddit", false);
    pref("search_youtube", false);
    pref("search_wiktionary", false);
    pref("search_google", true);
    pref("popup_bubble_show_definitions", true);
    pref("popup_bubble_open_new_tab", true);
    pref("popup_bubble_force_single_row", false);
    pref("enhance_urlbar", true);
    pref("num_urlbar_from_history", 4);
    pref("enable_qlauncher", true);
    pref("qlauncher_invoke_key", "CTRL+SPACE");
    pref("qlauncher_open_new_tab", true);
    pref("enable_linkify", true);
    pref("enable_endless_pages", true);
    pref("auto_copy_selected", false);
    pref("enable_middle_click_paste", false);
    pref("enable_right_click_paste", false);
    pref("insert_aff_code", true);
    pref("add_search_refinements", true);
    pref("last-version", "firstrun");


    /*
    //disable OneRiot
    localStorage["pref." + "search_oneriot"] = false;
    localStorage["pref." + "search_twitter"] = false;
    localStorage["pref." + "search_surfcanyon"] = true;
    */



    var onuninstallURL = function(install_time, install_duration)
    {
        return "http://smarterfox.com/smarterwiki/uninstalled/?install_time=" + install_time + "&install_duration=" + install_duration;
    };
    var ondisableURL = function(install_time, install_duration)
    {
        return "http://smarterfox.com/smarterwiki/disabled/?install_time=" + install_time + "&install_duration=" + install_duration;
    };
    var oninstallURL = function(lastVersion, currentVersion)
    {
        return "http://smarterfox.com/smarterwiki/installed/?from_ver=" + lastVersion + "&to_ver=" + currentVersion;
    };
    
    
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function()
    {
        if(xhr.readyState==4)
        {
            var data = JSON.parse(xhr.responseText);
            var current_version = data.version;
            if(localStorage["pref." + "last-version"] != current_version)
            {
                if(localStorage["pref." + "last-version"] == "firstrun")
                {
                    localStorage["pref." + "install-time"] = new Date().getTime();
                }
                else
                {
                }

                if(localStorage["pref." + "last-version"] == "firstrun")
                {
                    chrome.tabs.create({
                        index: 100000000, //last
                        url: oninstallURL(localStorage["pref." + "last-version"], current_version)
                    });
                }//only open new page if first install

                localStorage["pref." + "last-version"] = current_version;
            }
        }
    };
    xhr.open("GET", chrome.extension.getURL("manifest.json"), true);
    xhr.send();
}());