//Note to editor: NOT executed from XUL
//var smarterwiki_$ = SmarterWiki.$;
//var smarterwiki_$ = jQuery.noConflict(true);

(function()
{
    //var $ = smarterwiki_$;
    var getBoolPref = function(pref_name, callback)
    {
        if(callback)
        {
            callback(safe_SW_getBoolPref("extensions.smarterwiki." + pref_name));
        }
    };
    if(window.chrome && window.chrome.extension) // detect chrome
    {
        var get_localStorage = function(key, callback) //only used for Chrome
        {    
            chrome.extension.sendRequest({"msg_type": "get_localStorage", "key": key}, function(response)
            {
                if(callback)
                {
                    callback(response.value);
                }
            });
        };
        var set_localStorage = function(key, value, callback) //only used for Chrome
        {    
            chrome.extension.sendRequest({"msg_type": "set_localStorage", "key": key, "value": value}, function(response)
            {
                if(callback)
                {
                    callback(response.status);
                }
            });
        };

        setBoolPref = function(pref_name, value, callback)
        {
            set_localStorage("pref." + pref_name, value, callback);
        };

        getBoolPref = function(pref_name, callback)
        {
            get_localStorage("pref." + pref_name, function(str)
            {
                if(callback) {
                    callback(str == "true");
                }
            });
        };
        
        SW_$ = $;
        SW_LOG = function(){};
        $.get = function(url, data, callback, type)
        {
            chrome.extension.sendRequest({"msg_type": "$.get", "url": url, "data": data, "type": type}, function(response)
            {
                if(callback)
                {
                    callback(response.data, response.textStatus);
                }
            });            
        };
    }
    
    var safe_SW_getBoolPref = function(name)
    {
        try
        {
            return SW_getBoolPref(name);
        }
        catch(err)
        {
            return null;
        }
    };
    
    getBoolPref("add_related_articles", function(pref_value)
    {
        if(pref_value)
        {
            var doc = document;
            var urlRegExp = new RegExp("(http(?:|s)://en.wikipedia.org/wiki/)(.*)");
            var match = urlRegExp.exec(doc.location.href);
            if(match != null)
            {
                //on a Wikipedia page
                var wgPageName = match[2];//.replace("_", " ");
                var title_match = new RegExp("^(.*) - .*$").exec(doc.title);
                var wgArticleName = ""; //Main Page has no title in that format
                if(title_match)
                {
                    wgArticleName = title_match[1];
                }
                var wgArticlePath = match[1];
                //var locale = SW_getCharPref("general.useragent.locale");
                var locale = navigator.language;

                var addRelatedArticlesBox = function(html, textStatus)
                {
                    var searchPortlet = $('#p-search', doc);

                    if(searchPortlet && searchPortlet.attr("class") == "portlet")
                    {
                        var relatedPortlet = $('<div id="p-smarterwiki"></div>', doc);
                        relatedPortlet.addClass('portlet', doc);
                        relatedPortlet.append(html);
                        relatedPortlet.insertBefore(searchPortlet);  
                        /*
                        relatedPortlet.click(function()
                        {

                        });
                        */
                    }
                    else
                    {
                        var $navPortlet = $("#p-navigation");
                        var $relatedPortlet = $('<div id="p-smarterwiki"></div>', doc).addClass('portal', doc).append(html).insertAfter($navPortlet);
                        $relatedPortlet.find(".pBody").removeClass("pBody").addClass("body");  
                        $relatedPortlet.find("h5").text("Related articles");  
                    }
                };

                SW_$.get("http://static.smarterfox.com/api/related_articles",
                    {'topic': wgArticleName,
                     'locale': locale,
                     'format': 'html_frag'}, addRelatedArticlesBox, "html");
            }
        }
    });
}());