﻿jQuery.fn.editablePanel = function() {
    return this.each(function() {
        var ep = jQuery(this);
        ep.hover(function() {
            jQuery(this).find('._ep_el').each(function() {
                var a = jQuery(this);
                a.css({
                    'width': a.parent().width(),
                    'height': a.parent().height()
                });
                var s = jQuery(a.find('span'));
                s.css('top', ((a.height() - s.height()) / 3) + 'px');
                a.click(function() {

                    jQuery('object').each(function() {
                        jQuery(this).hide();
                    });
                
                    var o = a.siblings('._ep_overlay');
                    o.hover(function() { return false; }, function() { return false; });
                    o.click(function() {
                        tinyMCE.activeEditor.setContent(ep.find('._ep_content').html());
                        o.hide();

                        jQuery('object').each(function() {
                            jQuery(this).show();
                        });
                    });
                    o.show();
                    o.children('._ep_editor').each(function() {
                        var e = jQuery(this);
                        e.click(function() { return false; });
                        e.css({
                            'top': ((o.height() - jQuery(this).height()) / 2) + 'px',
                            'left': ((o.width() - jQuery(this).width()) / 2) + 'px'
                        });
                        e.find('._ep_preview').each(function() {
                            jQuery(this).click(function() {
                                ep.find('._ep_content').html(tinyMCE.activeEditor.getContent());
                                o.hide();

                                jQuery('object').each(function() {
                                    jQuery(this).show();
                                });
                            });
                        });
                        e.find('._ep_cancel').each(function() {
                            jQuery(this).click(function() {
                                tinyMCE.activeEditor.setContent(ep.find('._ep_content').html());
                                o.hide();

                                jQuery('object').each(function() {
                                    jQuery(this).show();
                                });
                            });
                        });
                        e.find('._ep_save').each(function() {
                            jQuery(this).click(function() {
                                return true;
                            });
                        });
                    });
                    a.hide();
                });

                a.show();
            });
        },
        function() {
            jQuery(this).children('._ep_el').each(function() {
                jQuery(this).hide();
            });
        });
    });
};
