var THEMEMAKERS_APP_SHORTCODES = function() {
    var self = {
        html_buffer: null,
        init: function() {
            if (!jQuery("#thememakers_shortcodes_html_buffer").size()) {
                jQuery('body').append('<div id="thememakers_shortcodes_html_buffer" style="display: none;"></div>');
            }
            self.html_buffer = jQuery("#thememakers_shortcodes_html_buffer");

            //*****

            jQuery(".js_shortcode_checkbox_self_update").live('click', function() {
                if (jQuery(this).is(':checked')) {
                    jQuery(this).val(1);
                } else {
                    jQuery(this).val(0);
                }
            });

            jQuery(".js_shortcode_radio_self_update").live('click', function() {
                jQuery("input[data-shortcode-field=" + jQuery(this).attr('name') + "]").val(jQuery(this).val()).trigger('change');
            });

        },
        insert_html_in_buffer: function(html) {
            jQuery(self.html_buffer).html(html);
        },
        get_html_from_buffer: function() {
            return jQuery(self.html_buffer).html();
        },
        changer: function(shortcode) {
            var items = jQuery("#thememakers_shortcode_template .js_shortcode_template_changer");
            var begin_string = "[" + shortcode;
            var end_string = "[/" + shortcode + "]";
            var content = "";
            jQuery.each(items, function(key, value) {
                var shortcode_field = jQuery(value).attr('data-shortcode-field');
                if (shortcode_field == 'content') {
                    content = jQuery(value).val();
                } else {
                    begin_string = begin_string + " " + shortcode_field + '="' + jQuery(value).val() + '"';
                }
            });
            var shortcode_text = begin_string + ']' + content + end_string;
            self.insert_html_in_buffer(shortcode_text);

        },
        tab_changer: function(shortcode) {

            var items = jQuery("#thememakers_shortcode_template .js_shortcode_template_changer");

            var begin_string = "[" + shortcode;
            var end_string = "[/" + shortcode + "]";
            jQuery.each(items, function(key, value) {
                var shortcode_field = jQuery(value).attr('data-shortcode-field');
                begin_string = begin_string + " " + shortcode_field + '="' + jQuery(value).val() + '"';
            });

            var shortcode_text = begin_string + ' title="__TITLE__"' + ']' + "__CONTENT__" + end_string;

            //*****
            var tmp_title = "";
            var tmp_content = "";

            var shortcode_tabs_list = jQuery("#shortcode_tabs_list li");
            jQuery.each(shortcode_tabs_list, function(key, value) {
                if (key > 0) {
                    tmp_title += "--TITLE--"
                    tmp_content += "--CONTENT--"
                }
                tmp_title += jQuery(value).find('[data-shortcode-field=title]').val();
                tmp_content += jQuery(value).find('[data-shortcode-field=content]').val();
            });


            shortcode_text = shortcode_text.replace(/__TITLE__/gi, tmp_title);
            shortcode_text = shortcode_text.replace(/__CONTENT__/gi, tmp_content);

            self.insert_html_in_buffer(shortcode_text);
        },
        column_changer: function(shortcode) {

            var items = jQuery("#thememakers_shortcode_template .js_shortcode_template_changer");

            var begin_string = "[" + shortcode;
            var end_string = "[/" + shortcode + "]";
            jQuery.each(items, function(key, value) {
                var shortcode_field = jQuery(value).attr('data-shortcode-field');
                begin_string = begin_string + " " + shortcode_field + '="' + jQuery(value).val() + '"';
            });

            var shortcode_text_template = begin_string + ' type="__TYPE__"' + ']' + "__CONTENT__" + end_string;
            var shortcode_text = "";
            //*****
            var tmp_type = "";
            var tmp_content = "";
            var tmp_shortcode_text = "";

            var shortcode_tabs_list = jQuery("#shortcode_column_list li");
            jQuery.each(shortcode_tabs_list, function(key, value) {
                tmp_shortcode_text = shortcode_text_template;
                tmp_type = jQuery(value).find('[data-shortcode-field=type]').val();
                tmp_content = jQuery(value).find('[data-shortcode-field=content]').val();
                tmp_shortcode_text = tmp_shortcode_text.replace(/__TYPE__/gi, tmp_type);
                tmp_shortcode_text = tmp_shortcode_text.replace(/__CONTENT__/gi, tmp_content);
                shortcode_text += tmp_shortcode_text;
            });

            self.insert_html_in_buffer(shortcode_text);

        },
        toggle_changer: function(shortcode) {

            var items = jQuery("#thememakers_shortcode_template .js_shortcode_template_changer");

            var begin_string = "[" + shortcode;
            var end_string = "[/" + shortcode + "]";
            jQuery.each(items, function(key, value) {
                var shortcode_field = jQuery(value).attr('data-shortcode-field');
                begin_string = begin_string + " " + shortcode_field + '="' + jQuery(value).val() + '"';
            });

            var shortcode_text_template = begin_string + ' title="__TITLE__"' + ']' + "__CONTENT__" + end_string;
            var shortcode_text = "";
            //*****
            var tmp_title = "";
            var tmp_content = "";
            var tmp_shortcode_text = "";

            var shortcode_tabs_list = jQuery("#shortcode_tabs_list li");
            jQuery.each(shortcode_tabs_list, function(key, value) {
                tmp_shortcode_text = shortcode_text_template;
                tmp_title = jQuery(value).find('[data-shortcode-field=title]').val();
                tmp_content = jQuery(value).find('[data-shortcode-field=content]').val();
                tmp_shortcode_text = tmp_shortcode_text.replace(/__TITLE__/gi, tmp_title);
                tmp_shortcode_text = tmp_shortcode_text.replace(/__CONTENT__/gi, tmp_content);
                shortcode_text += tmp_shortcode_text;
            });

            self.insert_html_in_buffer(shortcode_text);

        },
        show_static_info_popup: function(text) {
            jQuery(".thememakers_shortcode_info_popup").text(text);
            jQuery(".thememakers_shortcode_info_popup").fadeTo(400, 0.9);
        },
        hide_static_info_popup: function() {
            window.setTimeout(function() {
                jQuery(".thememakers_shortcode_info_popup").fadeOut(400);
            }, hide_delay);
        }
    }

    return self;
}
//*****

var thememakers_app_shortcodes = null;
jQuery(document).ready(function() {
    thememakers_app_shortcodes = new THEMEMAKERS_APP_SHORTCODES();
    thememakers_app_shortcodes.init();
});

