function editable(o, data)
{
  if($.widget.isOwner)
  {
    postData = {
      'widget[profile_layout_tab_id]'     : data.tabId,
      'widget[profile_layout_slot_id]'    : data.slotId,
      '_method'                           : 'post'
    }
  
    o.editable('/space/title', {
      select      : true,
      submit      : 'ok',
      cancel      : 'cancel',
      submitdata  : postData,
      cssclass    : 'text'
    });
  }
}

$.widget.slot   = false;

$.widget.window = function()
                  {
                    this.list             = $('ul.widgets');
                    
                    // prevent enter key from submitting form
                    this.list.find('form :input').live('keypress', function(event)
                    {
                      if(event.keyCode == 13)
                      {
                        return false;
                      }
                    });
                    
                    this.saveSlot         = function(item)
                                            {
                                              item.find('ul.error_list').remove();
                                              
                                              var tabId     = $('ul.slots').attr('id').replace(/^tab\-/, '');
                                              var slotId    = $.widget.slot.attr('id').replace(/^(slot|add)\-(space\-)?/, '');
                                              var widgetId  = item.attr('id').replace(/^widget\-/, '');
                                              
                                              var data      = {
                                                                'widget[widget_id]'                 : widgetId, 
                                                                'widget[profile_layout_tab_id]'     : tabId,
                                                                'widget[profile_layout_slot_id]'    : slotId
                                                              };
                                              
                                              if(item.find('form').length > 0)
                                              {
                                                item.find('form :input').each(function() {
                                                  data[$(this).attr('name')] = $(this).val();
                                                });
                                              }
                                              
                                              $.ajax({
                                                url       : '/space/save',
                                                dataType  : 'json',
                                                type      : 'POST',
                                                data      : data,
                                                success   : function(json) {
                                                              if(json['error'] != undefined)
                                                              {
                                                                $.each(json['error'], function(k, v) {
                                                                  item.find('#' + k + ':visible').before('<ul class="error_list"><li>' + v + '</li></ul>');
                                                                });
                                                              }
                                                              else
                                                              {
                                                                $this.reloadSlot(item.attr('rel'));
                                                              }
                                                            }
                                              });
                                            };
                                            
                    this.reloadSlot       = function(widget)
                                            {
                                              var space   = $.widget.slot;
                                              
                                              var params  = {
                                                              slotId  : space.attr('id').replace(/^(slot|add)\-(space\-)?/, ''),
                                                              tabId   : $('ul.slots').attr('id').replace(/^tab\-/, '')
                                                            };
                                              
                                              var url     = '/widget/' + widget + '/' + params.slotId + '/' + params.tabId;
                                              
                                              $.get(url, function(html) {
                                                space.find('div.empty').html(html);
                                                
                                                editable(space.find('h3'), params);
                                                
                                                $.fancybox.close();
                                              });
                                            };
                                            
                    this._initialize      = function()
                                            {
                                              $('ul.widgets > li').click(function(event) {
                                                $this.list.children().removeClass('active');
                                                
                                                $(this).addClass('active');
                                              });
                                              
                                              $('ul.widgets > li .red-button').click(function(event) {
                                                event.preventDefault();
                                                
                                                $this.saveSlot($(this).parent());
                                              });
                                            };
                                            
                    var $this = this;
                    
                    this._initialize();
                  };

$.fn.widget     = function(settings)
                  {
                    this.settings         = {
                                              'baseURL'   : null,
                                              'windowURL' : null,
                                              'widgets'   : {}
                                            };
                    
                    this._initialize      = function()
                                            {
                                              $this._bindSpaces();
                                              $this._loadSpaces();
                                              $this._bindForms();
                                              
                                            };
                                            
                    this._bindForms       = function()
                                            {
                                              $('ul.slots li form input[type=submit]').live('click', function(event) {
                                                event.preventDefault();
                                                
                                                $(this).unbind();
                                                
                                                var data    = {};
                                                var form    = $(this).parent();
                                                
                                                form.find(':input').each(function() {
                                                  $(this).attr('disabled', 'disabled');
                                                  
                                                  data[$(this).attr('name')] = $(this).val();
                                                });
                                                
                                                $.post(form.attr('action'), data, function(html) {
                                                  var space = form.parent().parent().parent();
                                                  
                                                  space.html(html);
                                                });
                                              });
                                            };
                                            
                    this._loadSpaces      = function()
                                            {
                                              $.each($this.settings.widgets, function(k, v) {
                                                var space   = $('ul.slots li[rel=' + k + ']');
                                                
                                                var params  = {
                                                                slotId  : space.attr('id').replace(/^slot\-/, ''),
                                                                tabId   : $('ul.slots').attr('id').replace(/^tab\-/, '')
                                                              };
                                                
                                                var url     = '/widget/' + v + '/' + params.slotId + '/' + params.tabId;
                                                
                                                $.get(url, function(html) {
                                                  space.find('div.empty').html(html);
                                                  
                                                  editable(space.find('h3'), params);
                                                });
                                              });
                                            };
                    
                    this._bindSpaces      = function()
                                            {
                                              // add
                                              $this.find('a.add, a.empty-space-add').live('click', function(event) {
                                                event.preventDefault();
                                                
                                                var item    = $(this);
                                                var params  = {
                                                                slotId  : $(this).attr('id').replace(/^add\-(space\-)?/, ''),
                                                                tabId   : $('ul.slots').attr('id').replace(/^tab\-/, '')
                                                              };
                                                
                                                $.fancybox('',
                                                {
                                                  'href'            : '/widget?profile_layout_slot_id=' + params.slotId + '&profile_layout_tab_id=' + params.tabId,
                                                  'type'            : 'ajax',
                                                  'autoDimensions'  : false,
                                                  'autoScale'       : false,
                                                  'width'           : 800,
                                                  'height'          : 500,
                                                  'transitionIn'    : 'none',
                                                  'transitionOut'   : 'none',
                                                  'onComplete'      : function()
                                                                      {
                                                                        $.widget.slot = $('li#slot-' + params.slotId);
                                                                        $.widget.window();
                                                                      }
                                                });
                                              });
                                              
                                              // delete
                                              $this.find('a.delete').live('click', function(event) {
                                                event.preventDefault();
                                                
                                                var item    = $(this);
                                                var slotId  = $(this).attr('id').replace(/^delete\-/, '');
                                                var params  = {
                                                                'widget[profile_layout_slot_id]'  : slotId,
                                                                'widget[profile_layout_tab_id]'   : $('ul.slots').attr('id').replace(/^tab\-/, '')
                                                              };
                                                
                                                $.post('/space/delete', params, function(html) {
                                                  $('li#slot-' + slotId).find('div.dropped-item').fadeOut('slow', function() {
                                                    $(this).remove();
                                                    
                                                    $('li#slot-' + slotId).find('div.empty').html(html);
                                                  });
                                                });
                                                
                                              });
                                            };
                    
                    var $this = this;
                    
                    $.each(settings, function(k, v) {
                      $this.settings[k] = v;
                    });
                    
                    this._initialize();
                  };
