/*!
 * jQuery SuperCookie v1 -- https://github.com/tantau-horia/jquery-SuperCookie -- Copyright 2012, Tantau Horia -- Dual licensed under the MIT or GPL Version 2 licenses
 * 
 */
(function(b,k,l,i){var h={expires:7,path:"/"},j={};b.super_cookie=function(a){b.extend(h,a);b.extend(this,j);if("undefined"===typeof console||"undefined"===typeof console.log)k.console||(console={log:function(){}});return this};j={create:function(a,d,e){e=b.extend({},h,e);b.cookie(a,JSON.stringify(d),e)},check:function(a){if(null!==a&&a!==i)return null===b.cookie(a)?(console.log("No cookie."),!1):!0;console.log("No cookie selected.");return!1},verify:function(a){if(null!==a&&a!==i){a=b.cookie(a); if(null===a)return console.log("No cookie."),!1;if(jQuery.isEmptyObject(a))return console.log("Invalid values."),!1;try{JSON.parse(a)}catch(d){return console.log("Not JSON."),!1}return!0}console.log("No cookie selected.");return!1},check_index:function(a,d){var e=b.super_cookie().read_JSON(a),c=null;b.each(e,function(a){d===a&&(c="ok")});return null===c?!1:!0},read_values:function(a){return b.super_cookie().verify(a)?b.cookie(a):!1},read_indexes:function(a){var a=b.super_cookie().read_JSON(a),d=[]; b.each(a,function(a){d.push(a)});return d},read_JSON:function(a){return b.super_cookie().verify(a)?JSON.parse(b.cookie(a)):!1},read_value:function(a,d){var e=b.super_cookie().read_JSON(a),c=null;b.each(e,function(a,b){d==a&&(c=b)});return null===c?!1:c},replace_value:function(a,d,e,c){var f=b.super_cookie().read_JSON(a),g=[];b.each(f,function(a,b){field='"'+a+'": "'+b+'"';d===a&&(field='"'+a+'": "'+e+'"');g.push(field)});g="{"+g.join(", ")+"}";f={};f=JSON.stringify(g);c=b.extend({},h,c);b.removeCookie(a); b.cookie(a,JSON.parse(f),c)},add_value:function(a,d,e,c){var f=b.super_cookie().read_JSON(a),g=[];b.each(f,function(a,b){field='"'+a+'": "'+b+'"';g.push(field)});g.push('"'+d+'": "'+e+'"');g="{"+g.join(", ")+"}";d={};d=JSON.stringify(g);c=b.extend({},h,c);b.removeCookie(a);b.cookie(a,JSON.parse(d),c)},remove_value:function(a,d,e){var c=b.super_cookie().read_JSON(a),f=[];b.each(c,function(a,b){field='"'+a+'": "'+b+'"';d!==a&&f.push(field)});f="{"+f.join(", ")+"}";c={};c=JSON.stringify(f);e=b.extend({}, h,e);b.removeCookie(a);b.cookie(a,JSON.parse(c),e)}}})(jQuery,document);

(function($) {
    $(function() {
        // logo from media library with preview

        var logo = $('#smallbiz_select_from_media_library');
        if (logo.length > 0) {

            var senderInput = null;
            logo.focus(function() {
                senderInput = $(this);
                tb_show('Select image', 'media-upload.php?referer=smalbiz-options-select-logo&type=image&TB_iframe=true&post_id=0', false);
            });

            var old_fn = window.send_to_editor;
            window.send_to_editor = function(html) {
                if (senderInput) {
                    var image_url = $('img', '<div>' + html + '</div>').attr('src');
                    senderInput.val(image_url);
                    if (image_url !== '' && image_url) {
                        senderInput.parent().find('.picture-preview').html('<img class="smallbiz-options-logo" src="' + image_url + '">');
                    } else {
                        senderInput.parent().find('.picture-preview').html('');
                    }
                    senderInput = null;
                    tb_remove();
                }
                if (old_fn) {
                    old_fn(html);
                }
            };

            if ($.trim(logo.val()) !== '') {
                logo.parent().find('.picture-preview').html('<img class="smallbiz-options-logo" src="' + logo.val() + '">');
            }

            $('.smallbiz_theme_with_picture_preview').click(function() {
                logo.val('');
                logo.parent().find('.picture-preview').html('');
            });

            $.cookie.json = true;

            // remember opened tabs
            $('.dropdown a').mousedown(function(e) {
                var cookie = $.super_cookie().read_JSON('smallbiz_configuration_pages');
                if (!cookie) {
                    cookie = {};
                }

                var $this = $(this);
                var optionboxid = $this.attr('onmousedown').match(/getElementById\('([^']+)'\)/)[1];
                var optionbox = $('#' + optionboxid);
                var status = 'visible';
                if (optionbox.css('display') === 'none') {
                    status = 'hidden';
                } else {
                    status = 'visible';
                }
                cookie[optionboxid] = status;
                $.super_cookie().create('smallbiz_configuration_pages', cookie);
            });
            
            var cookie = $.super_cookie().read_JSON('smallbiz_configuration_pages');
            for(var id in cookie) {
                if(cookie[id] === 'visible') {
                    $('#' + id).css('display', 'block');
                }
            }
        }
    });
})(jQuery);