/**
 * Open flyout on click
 */
jQuery.fn.flyout = function(s) {
  var settings = jQuery.extend({
    'width' : null, 
    'href' : null, 
    'title' : null, 
    'success' : null, 
    'error' : null, 
    'success_event' : null, 
    'error_event' : 'async_operation_error',
    'stop_propagation' : false,
    'close' : null
  }, s);
  
  if (settings.width) {
    if (parseInt(settings.width) == settings.width) {
      settings.width = settings.width;      
    } else {
      settings.width_class = settings.width;
      settings.width = 1000;
    } // if
  } else {
    settings.width = 1000;
  } // if
  
  return this.each(function() {
    var element = $(this);
    
    element.click(function(event) {
      if(typeof(settings['href']) == 'function') {
        var url = settings['href']();
      } else if(settings['href']) {
        var url = settings['href'];
      } else {
        var url = element.attr('href');
      } // if
      
      var dialog = App.widgets.FlyoutDialog.show({
        'title' : settings['title'] ? settings['title'] : element.attr('title'), 
        'data' : url, 
        'data_type' : 'url', 
        'min_height' : 'auto', 
        'width' : settings['width'], 
        'width_class' : settings['width_class'],
        'close' : settings['close'],
        'success' : function(response) {
          if(typeof(settings['success']) == 'function') {
            settings['success'].apply(this, [ response ]);
          } // if
          
          if(settings['success_event']) {
            App.Wireframe.Events.trigger(settings['success_event'], [ response ]);
          } // if
        },
        'error' : function(response) {
          if(typeof(settings['error']) == 'function') {
            settings['error'].apply(this, [ response ]);
          } // if
          
          if(settings['error_event']) {
            App.Wireframe.Events.trigger(settings['error_event'], [ App.Wireframe.Utils.responseToErrorMessage(response), response ]);
          } // if
        }, 
        'buttons' : []
      });

      if (settings.stop_propagation) {
        return false;
      } else {
        event.preventDefault();
      } // if
    });
  });
  
};