/**
 *
 * Triggering Script for triggering
 * custom vc_ready event when visualcomposer
 * is ready.
 *
 * @author jason.xie@victheme.com
 */
(function ($) {

  /**
   * VC Iframs Probably is ready at this time
   * using timer will cause the event to be
   * invoked AFTER the vc.build();
   * @see vc_page_editable.js
   */
  if (typeof window.vc_iframe != 'undefined') {

    /**
     * Helper function for acting like PHP strpos
     * @param haystack
     * @param needle
     * @param offset
     * @returns {boolean}
     */
    vc_iframe.strpos = function(haystack, needle, offset) {
      var i = (haystack+'').indexOf(needle, (offset || 0));
      return i === -1 ? false : i;
    }


    /**
     * Extract bootstrap grid classes
     * @param cssClass
     * @returns {string}
     */
    vc_iframe.extractBootstrapClasses = function(cssClass) {

        var arrayClass = [], that = this;

        $.each(cssClass.split(' '), function(key, value) {
          if (that.strpos(value, 'col-') !== false) {
            arrayClass.push(value);
          }
        });

        return arrayClass.join(' ');
    }

    /**
     * Hijack the original setSortable to piggy back
     * custom function and adding trigger so other
     * plugin can hijack into this function easily.
     *
     * @type {Function|vc_iframe.setSortable}
     */
    vc_iframe.origSortable = vc_iframe.setSortable;
    vc_iframe.setSortable = function(app) {
      this.origSortable(app);
      this.setGridSortable();
      $(window).trigger('vc_sortable_callback', this);
    }


    /**
     * Custom sortable modification for grid system
     * This is based on bootstrap grid and all plugin
     * that wishes to use this method must add vtcore-grid-container
     * class and provide the data-connect-with in the sortable main wrapper.
     */
    vc_iframe.setGridSortable = function() {

      var that = this;
      $('.vtcore-grid-container.ui-sortable:not(.processed)')
        .each(function() {
          var dataConnect =  $(this).data('connect-with') || '.vtcore-grid-container',
              self = $(this);

          if (dataConnect != '.vc_element-container') {
            self.removeClass('vc_element-container');
          }

          self
            .addClass('processed')
            .sortable('option', 'connectWith', dataConnect)
            .on('sort.vtcore_grid', function(event, ui) {

              var bsClass = that.extractBootstrapClasses(ui.item.attr('class'));

              if (bsClass.length || self.data('grid-override')) {
                ui.placeholder
                  .attr('style', 'width:' + ui.item.width() + 'px !important')
                  .css('float', 'left')
                  .addClass(bsClass);
              }

              ui.placeholder
                .height(ui.item.height());

            });
        });

    }

    /**
     * Trigger custom event marking the vc_frame is ready
     * and before the vc is build.
     */
    $(window).trigger('vc_frame_ready');
  }

})(window.jQuery);