/**
 * MW_Content Javascript Library
 * Requires: jQuery
 */
 
(function($){ // })(jQuery);
  
  $.mw = $.extend($.mw?$.mw:{}, {  // add .content to $.mw , creating $.mw if it doesn't exist.
    content: {
      
      editors: [],
      registerEditor: function(editor)
      {
        this.editors.push(editor);
      },
      // bind(element) - binds editors to element.
      bind: function(elem) {  
        if (elem == undefined) elem = document;
        var $elem = $(elem); 
        $.each(this.editors, function(k, v) {
          v.bind($elem);
        });
      }
    }
  });
  
  // MW_Content_Editor class:
  $.mw.content.editor = function(opts) {
    $.extend(this, opts);
    $.mw.content.registerEditor(this);
  };
  
  $.mw.content.editor.prototype.bind = function($elem) {
    alert('Unbound editor!');
  };
  
  $.mw.content.editor.prototype.popEditor = function() {
    var editor = this;
    var args = arguments;
    if (args.length == 1) args = args[0];
    var url = args.url?args.url:args[0];
    $.ajax({
      type: "GET",
      dataType: 'json',
      url: url,
      success: function(data) {
        $.extend(args, data);
        editor.showEditor(data.form, args);
      },
      error: function(data) {
        console.debug(data);
        window.location = url;
      }
    });
  };
  
  var statictext = new $.mw.content.editor({
    showEditor: function(html, args) {
      var editor = this;
      $.prompt(html, {
        prefix: 'content_edit',
        buttons: args.buttons,
        submit: function(btn, text) {
          if (btn == 'cancel') {
          } else if (btn == 'save') {
            var html = FCKeditorAPI.GetInstance('text').GetHTML(true);
            $('input[name="text"]', text).val(html);
            $('form',text).ajaxSubmit({
              dataType: 'json',
              success: function(data) {
                args.div.css({'background-color': ''});
                if (data.html)
                {
                  args.div.html(data.html);
                  if (args.div.text().replace(/\s+/, '') ==0) args.div.text('Empty Static Text: '+(args.div.attr('id')));                  
                } else if (data.form) {
                  editor.showEditor(data.form, args);
                }
              },
              error: function(data) {
                if (data.status == 200)
                {
                  editor.showEditor(data.responseText, args);
                }
              }
            
            });
            
          }
          return true;
        }
      });
      $.mw.bind(".content_edit");
      
    },
    bind: function($elem) {
      var editor = this;
      $(".mw_content_st_edit_link").mwbind('st_edit', function() {
        var url = this.href;
        var $editdiv = $(this).parent();
        $editdiv.wrap("<div>");
        var $container = $editdiv.parent().addClass('mw_content_container').css({'position': 'relative'});
        var $editlink = $(this);
        var clickHandle = function(e) {
          editor.popEditor({url: url, div:$editdiv, link:$editlink});
          e.preventDefault();
          e.stopPropagation();
          return false;
        };
        $editlink.css({'display': 'block', 'position': 'absolute', 'bottom': '1em', 'right': '1em'});

        $container.append($editlink);

        $editlink.click(clickHandle).hide();
        

        $container.hover(function() {
            $editdiv.css({'background-color': '#abcdef'});
            $editlink.show();
          }, function() {
            $editdiv.css({'background-color': ''});
            $editlink.hide();
          });
          
        if ($editdiv.text().replace(/\s+/, '').length == 0) {
          $editdiv.text('Empty static text: '+$editdiv.attr('id'));
        }
        return true;
      });
    }
  });
  
  content = new $.mw.binder({
    bind: function(elem) {
      $.mw.content.bind(elem);
    }
  });
})(jQuery);
 
