// In Place Editors
var nameInPlaceEditor = null;
var titleInPlaceEditor = null;
var problemInPlaceEditor = null;
var solutionInPlaceEditor = null;
var notesInPlaceEditor = null;

// ----------------------------------
// Problem
// ----------------------------------
var Problem = Class.create(DeferredModel, {
  initialize: function($super) {
    $super(['id', 'name', 'title', 'problem', 'solution', 'notes', 'lang', 'rendered_problem', 'rendered_notes', 'rendered_solution', 'testset'], 'problem', problemStore);
  }
});

var problemStore = new ModelStore('problem', Problem);


// ----------------------------------
// Problem UI
// ----------------------------------
new DestroyAction('delete_problem', problemStore, 'click');
new NewAction('new_problem', problemStore, 'click');
new ValueObserver('edit_problem_language', problemStore.proxy, 'lang');

new InnerHTMLObserver('edit_problem_name', problemStore.proxy, 'name', function() {
  if(nameInPlaceEditor)
    nameInPlaceEditor.dispose();
  
  nameInPlaceEditor = new Ajax.InPlaceEditor('edit_problem_name', 'api/problems/update/', {
    okText: 'OK',
    cancelText: 'Cancel',
    externalControl: 'edit_problem_name_link',
    highlightColor: '#ffffff',
    callback: function(form, value) {
      return 'problem=' + encodeURIComponent( $H({name: value, id: problemStore.proxy.get('id')}).toJSON() )
    },
    onComplete: function() {
      problemStore.proxy.reload();
    }
  });
});

new InnerHTMLObserver('edit_problem_title', problemStore.proxy, 'title', function() {
  if(titleInPlaceEditor)
    titleInPlaceEditor.dispose();
  
  titleInPlaceEditor = new Ajax.InPlaceEditor('edit_problem_title', 'api/problems/update/', {
    okText: 'OK',
    cancelText: 'Cancel',
    externalControl: 'edit_problem_title_link',
    highlightColor: '#ffffff',
    callback: function(form, value) {
      return 'problem=' + encodeURIComponent( $H({title: value, id: problemStore.proxy.get('id')}).toJSON() )
    },
    onComplete: function() {
      problemStore.proxy.reload();
    }
  });
});

new InnerHTMLObserver('edit_problem_problem', problemStore.proxy, 'rendered_problem', function() {
  if(problemInPlaceEditor)
    problemInPlaceEditor.dispose();
  
  problemInPlaceEditor = new Ajax.InPlaceEditor('edit_problem_problem', 'api/problems/update/', {
    okText: 'OK',
    cancelText: 'Cancel',
    externalControl: 'edit_problem_problem_link',
    highlightColor: '#ffffff',
    rows: 20, cols: 84,
    loadTextURL: 'api/problems/show/?field_name=problem&id=' + problemStore.proxy.get('id'),
    callback: function(form, value) {
      return 'problem=' + encodeURIComponent( $H({problem: value, id: problemStore.proxy.get('id')}).toJSON() )
    },
    onComplete: function() {
      problemStore.proxy.reload();
    },
    ajaxOptions: {evalScripts: false}
  });
  
  MathJax.Hub.Queue(['Typeset', MathJax.Hub, 'edit_problem_problem']);
});

new InnerHTMLObserver('edit_problem_solution', problemStore.proxy, 'rendered_solution', function() {
  if(solutionInPlaceEditor)
    solutionInPlaceEditor.dispose();
  
  solutionInPlaceEditor = new Ajax.InPlaceEditor('edit_problem_solution', 'api/problems/update/', {
    okText: 'OK',
    cancelText: 'Cancel',
    externalControl: 'edit_problem_solution_link',
    highlightColor: '#ffffff',
    rows: 20, cols: 84,
    callback: function(form, value) {
      return 'problem=' + encodeURIComponent( $H({solution: value, id: problemStore.proxy.get('id')}).toJSON() )
    },
    onComplete: function() {
      problemStore.proxy.reload();
    }
  });
});

new InnerHTMLObserver('edit_problem_notes', problemStore.proxy, 'rendered_notes', function() {
  if(notesInPlaceEditor)
    notesInPlaceEditor.dispose();
  
  notesInPlaceEditor = new Ajax.InPlaceEditor('edit_problem_notes', 'api/problems/update/', {
    okText: 'OK',
    cancelText: 'Cancel',
    externalControl: 'edit_problem_notes_link',
    highlightColor: '#ffffff',
    rows: 20, cols: 84,
    loadTextURL: 'api/problems/show/?field_name=notes&id=' + problemStore.proxy.get('id'),
    callback: function(form, value) {
      return 'problem=' + encodeURIComponent( $H({notes: value, id: problemStore.proxy.get('id')}).toJSON() )
    },
    onComplete: function() {
      problemStore.proxy.reload();
    },
    ajaxOptions: {evalScripts: false}
  });
  
  MathJax.Hub.Queue(['Typeset', MathJax.Hub, 'edit_problem_notes']);
});

// Problem list control
new SelectableProxyStoreObserver('problems_list', problemStore, 'id', 'name');


// ----------------------------------
// Problem language control
// ----------------------------------
$('edit_problem_language').observe('change', function(event) {
  problemStore.proxy.set('lang', this.value);
  problemStore.proxy.save();
});
