var THEMEMAKERS_APP_MAIL_SUBSCRIBER_TEMPLATE = function() {
    return {
        editing_box_id: null,
        current_template_time: null,
        init: function() {
            var self = this;

            //******************************************************

            //get/set header and footer data
            var data = {
                action: "app_mail_subscriber_get_headfoot_data"
            };
            jQuery.post(mail_subscriber_ajaxurl, data, function(response) {
                var data = jQuery.parseJSON(response);
                self.current_template_time = data.time;
                jQuery("#php_header_info").html(data.header + "<br />" + data.letter_link_html);
                jQuery("#php_footer_info").html(data.footer + "<br />" + data.letter_link_html);
            });

            //*******************************************************************

            jQuery(".content_area").live('click', function() {
                if (self.editing_box_id != null) {
                    return false;
                }
                var id = jQuery(this).attr('id');
                if (self.editing_box_id == id) {
                    return false;
                } else {
                    self.editing_box_id = id;
                }
                self.set_tiny_content(jQuery(this).html());
                return false;
            });



            //*******************************************************************

            jQuery(".content_area_simple").live('click', function() {

                if (self.editing_box_id != null) {
                    return false;
                }

                var id = jQuery(this).attr('id');
                if (self.editing_box_id == id) {
                    return false;
                } else {
                    self.editing_box_id = id;
                }
                self.set_textinput_content(jQuery(this).html());

                return false;
            });


            jQuery(".js_set_simple_active_content").live('click', function() {
                var content = jQuery("#text_" + self.editing_box_id).val();
                jQuery("#" + self.editing_box_id).html(content);
                self.editing_box_id = null;
                return false;
            });





        },
        set_tiny_content: function(content) {
            var textarea_id = 'textarea_' + this.editing_box_id;
            var self = this;
            jQuery("#" + this.editing_box_id).html('<textarea id="' + textarea_id + '">' + content + '</textarea>');
            tinyMCE.init({
                // General options
                mode: "exact",
                elements: textarea_id,
                theme: "advanced",
                plugins: "autolink,lists,pagebreak,layer,table,save,advhr,advimage,advlink,iespell,inlinepopups,preview,media,contextmenu,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
                // Theme options
                theme_advanced_buttons1: "save_changes, bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,formatselect,fontselect,fontsizeselect",
                theme_advanced_buttons2: "bullist,numlist,|,blockquote,|,undo,redo,|,link,unlink,image,code,|,forecolor,backcolor",
                theme_advanced_buttons3: "tablecontrols,|,hr,removeformat,media",
                theme_advanced_toolbar_location: "top",
                theme_advanced_toolbar_align: "left",
                theme_advanced_statusbar_location: "bottom",
                theme_advanced_resizing: true,
                height: "400",
                width: "450",
                // Skin options
                skin: "o2k7",
                skin_variant: "silver",
                relative_urls: false,
                remove_script_host: false,
                convert_urls: false,
                setup: function(ed) {
                    // Register save button
                    ed.addButton('save_changes', {
                        title: 'Save',
                        image: mail_subscriber_app_uri + "/images/save.gif",
                        onclick: function() {
                            //ed.windowManager.alert();
                            var content = ed.getContent();
                            ed.execCommand('mceRemoveControl', true, self.editing_box_id);
                            jQuery("#" + self.editing_box_id).html(content);
                            self.editing_box_id = null;
                            return false;

                        }
                    });
                }
            });


            return false;
        },
        set_textinput_content: function(content) {
            var input_id = 'text_' + this.editing_box_id;
            jQuery("#" + this.editing_box_id).html('<input type="text" id="' + input_id + '" value="' + content + '">&nbsp;<a class="admin-button button-medium button-gray align-btn-right js_set_simple_active_content" href="#">Update</a>');

        }


    }
}

var app_mail_subscriber_template = new THEMEMAKERS_APP_MAIL_SUBSCRIBER_TEMPLATE();
jQuery(document).ready(function() {
    app_mail_subscriber_template.init();
});

