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

    $document.off('initDesignSaver');
    $document.on('initDesignSaver', function() {

        var designs = {};

        $document.trigger('builder.predesign.get',[designs]);


        $document.trigger('getBuilderId',[function(builder_id) {
            $.coreUtilites.ajaxRequest(
                {
                    method: 'post',
                    subject: 'builder',
                    action: 'save_design',
                    params: {
                        builder_id: builder_id,
                        product_id: builder_product_id,
                        design: designs
                    }
                },
                function(){
                    var $popup = $('#swissstyle_popup');
                    $popup.hide();
                    $popup.find('.body').empty();
                    $('.sw_popup_substrate').hide();
                    location.reload();
                    return true;
                }
            );
        }]);

    });

    $document.off('getBuilderId');
    $document.on('getBuilderId', function( ev, callback) {

        var $builder_id = '';

        if ($('#admin_page').val() === '1') {
            callback('00000000000000000000000000000000');
        } else {

            $.coreUtilites.ajaxRequest(
                {
                    subject: 'builder',
                    action: 'get_current_builder_id'
                },
                function (resp) {

                    if (resp.status == 'success') {
                        $builder_id = resp.message;
                        callback($builder_id);
                    }

                }
            );
        }
    });

    $document.off('initDesignSaveButton');
    $document.on('initDesignSaveButton', function() {
        $document.off('click','.design_save_button');
        $document.on('click','.design_save_button', function(){
            $document.trigger('initDesignSaver');
            return false;
        });
    });

})(SWjQuery);