(function($){
    var $document = $(document);

    $document.off('click','#module_swissstyle_init_builder_popup');
    $document.on('click','#module_swissstyle_init_builder_popup', function(event) {

        var $swissstyle_popup = $('#swissstyle_popup');
        var $substrate = $('.sw_popup_substrate');

        $swissstyle_popup.hide();
        $substrate.hide();

        $.coreUtilites.ajaxRequest(
            {
                subject: 'html_generator',
                action: 'render_block',
                params: {
                    block_name: 'builderPopup'
                }
            },
            function(data){
                if (data.status == 'success') {
                    $swissstyle_popup.find('.body').html(data.message);
                    $swissstyle_popup.show();
                    $substrate.show();
                    $document.trigger('initBuilder');
                }
            }
        );

        $document.off('click', '#swissstyle_popup .close_button');
        $document.on('click', '#swissstyle_popup .close_button', function(ev){
            $(this).parents('#swissstyle_popup').hide();
            $substrate.hide();
            ev.stopPropagation();
        });

        $document.off('click', '#sys_cancel_button');
        $document.on('click', '#sys_cancel_button', function(ev){
            ev.stopPropagation();
            $('#swissstyle_popup').hide();
            $substrate.hide();
            return false;
        });

        event.preventDefault ? event.preventDefault() : ev.returnValue = false;
    });

})(SWjQuery);