(function()
{
    $(document).ready(function()
    {
        var config = {
            customSearchEngines: []
        };
        
        var genKey = function()
        {
            var s_a = [];
            var chars = "0123456789abcdef";
            for(var i = 0; i < 16; i++)
            {
                s_a.push(chars.charAt(Math.floor(Math.random() * chars.length)));
            }

            var s = new Date().getTime().toString(16);
            for(var i = 0; i < (16 - s.length); i++)
            {
                s_a.push("0");
            }
            s_a.push(s);

            return s_a.join("");
        };
        
        var updateConfig = function()
        {
            if($("#customSearchEngine-checkbox").attr("checked"))
            {
                config.customSearchEngines = [
                    [$("#custom-search-engine-title").attr("value"),
                     $("#custom-search-engine-favcion-url").attr("value"),
                     $("#custom-search-engine-url-input").attr("value")]];
            }
            else
            {
                config.customSearchEngines = [];
            }
        };

        var updateCode = function()
        {
            var code = "" + 
            '<script type="text/javascript">\n' + 
            "var finderfoxConfig = {\n" + 
            '    version: "1.0.0",\n' + 
            '    key: "' + genKey() + '"';
            
            if(config.customSearchEngines.length > 0)
            {
                code = code + ",\n" + 
                '    customSearchEngines: [[\n        "' + 
                    config.customSearchEngines[0][0] + 
                    '",\n        "' + 
                    config.customSearchEngines[0][1] + 
                    '",\n        "' + 
                    config.customSearchEngines[0][2] + 
                    '"]]';
            }
            code = code + 
            '\n};\n' + 
            /*
            'document.getElementsByTagName("head").appendChild(function(){\n' +
            '    var s = document.createElement("script");\n' + 
            '    s.type = "text/javascript";\n' + 
            '    s.src = ("https:" == document.location.protocol ? "https://ssl." : "http://www.") + "finderfox.smarterfox.com/finderfox-widget.js";\n' + 
            '    return s;\n' + 
            ')());\n' + 
            */
            'document.write(unescape(\'%3Cscript type="text/javascript" src="\'+\n' + 
            '(\'https:\'==document.location.protocol?\'https://ssl.\':\'http://\')+\n' + 
            '\'finderfox.smarterfox.com/finderfox.js"%3E%3C/script%3E\'));\n' + 
            '</script>';

            $("#widget-code-text").text(code);
            $("#blogger-widget-content").attr("value", code);//.replace(/</g,"&lt;").replace(/>/g,"&gt;")
            $("#typepad-widget-content").attr("value", code);
            return code;
        };

        var updatePreview = function()
        {
            var extended_config = {
                showPopupBubble: true,
                openNewTab: true,
                forceSingleRow: false,
                animate: true,
                source: "finderfox",

                customSearchEngines: [],

                searchWikipedia: true,
                searchOneRiot: true,
                searchYandex: false,        
                searchDelicious: false,
                tweetThis: true,
                searchTwitter: true,
                searchBing: false,
                searchBaidu: false,
                searchReddit: false,
                searchYouTube: false,
                searchGoogle: true,        
                showHomepageLink: true
            };
            if(config.customSearchEngines && 
               config.customSearchEngines.length > 0)
            {
                extended_config.customSearchEngines = [config.customSearchEngines[0]];
                extended_config.searchTwitter = false;
            }
            
            var $popupBubble = buildPopupSearchBubbleFull(extended_config, "Standards compliant")
                .appendTo($("#popup-bubble-full-preview").empty());
            $popupBubble = buildPopupSearchBubbleLite(extended_config, "Incompatible")
                .appendTo($("#popup-bubble-lite-preview").empty());
        };

        var updateInterface = function()
        {
            updateConfig();
            updateCode();
            updatePreview();
        };
        $("form#widget-config-form input").change(function(event)
        {
            updateInterface();
        });        
        updateInterface();
        
        updatePreview(); //stupid hack for Webkit, for some reason the <span></span>s are not initialized






        
        var guessFaviconURL = function(url)
        {
            var regexp = /https?:\/\/[^\/]*/;
            return regexp.exec(url) + "/favicon.ico";
        };
        
        var select_this = function()
        {
            this.select();
            return false;
        };
        $("#widget-code-text").focus(select_this);//.click(select_this);


        
        
        var set_custom_search_engine_config_state = function()
        {
            if($("#customSearchEngine-checkbox").attr("checked"))
            {
                $("#custom-search-engine-config").slideDown("fast");
            }
            else
            {
                $("#custom-search-engine-config").slideUp();
            }
        };
        set_custom_search_engine_config_state();
        $("#customSearchEngine-checkbox").click(set_custom_search_engine_config_state)
                                         .change(set_custom_search_engine_config_state);
        
        
        $("#custom-search-engine-url-input").change(function()
        {
            if($("#custom-search-engine-favcion-url").attr("value") ==
                "http://example.com/favicon.ico")
            {
                $("#custom-search-engine-favcion-url").attr("value", 
                    guessFaviconURL($("#custom-search-engine-url-input").attr("value")));
                updateInterface();
            }
        });
        
        $("#blogger-submit-button").click(function()
        {
            $("#blogger-submit-form").submit();
            return false;
        });
        $("#typepad-submit-button").click(function()
        {
            $("#typepad-submit-form").submit();
            return false;
        });
        $("#wordpress-submit-button").click(function()
        {
            $("#wordpress-instructions").slideDown();
            return false;
        });
    });
}());
//TypePad API key: 52616e646f6d4956a4b8855d11d958a12129d8a3081a38ff12f7577391f2fd2e213cbda44e4e44fcab9a8cedc051c731a7545ced1c7364bc