﻿var editor;
var content;

//insert editBar's
function insertEditBars(selector) {
  $(selector).each(function (i) {
    var element = $(this).attr('id');
    $(this).before('<div class="editBar" id="' + element + '_editBar" ><div class="edit" onclick="editPage(\'#' + element + '\')"></div><div class="undo" onclick="undoEdit(\'#' + element + '\')" style="display:none;"></div><div class="save" onclick="saveEdit(\'#' + element + '\')" style="display:none;"></div></div>');
  });
}

//handle editBar events
function editPage(target) {
  content = $(target).html();
  $(target).html(
    '<input type="hidden" name="editItem" value="' + $(target).attr('id') + '"></input>' +
    '<textarea id="editContent" name="editContent" style="width:' + $(target).width() + 'px;">' + content + '</textarea>'
  );

//  editor = new nicEditor({
//    iconsPath: '/images/editBar/nicEditorIcons.gif'
//    , buttonList: ['bold', 'italic', 'strikeThrough', 'subscript', 'superscript', 'link', 'unlink', 'fontFormat', 'ol', 'ul', 'left', 'center', 'right', 'image'/*, 'pasteClean', 'save'*/]
//  }).panelInstance('editContent');
  $('#editContent').ckeditor(function ()
    { /* editor created */
      editor = $('#editContent').ckeditorGet();
    },    
    { /* editor config */
      //      skin: 'office2003'
      contentsCss : '/styles/styles.css',
      bodyClass : 'editorBG',
      toolbar : [
                  ['Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-', 'Undo', 'Redo'],
		              ['Find', 'Replace', '-', 'SelectAll'],
                  [/*'Templates', 'Styles',*/ 'Format', '-', 'Bold', 'Italic', 'Subscript', 'Superscript', '-', 'RemoveFormat'],
                  ['NumberedList', 'BulletedList', '-', 'Image', '-', 'Link', 'Unlink']
                ],
      stylesSet: [
                  // Block-level styles
                  {name: 'Wichtig', element: 'p', styles: { 'color': 'Blue'} },
                  // Inline styles
                  {name: 'Test', element: 'span', attributes: { 'class': 'my_style'} },
                  {name: 'Marker: Yellow', element: 'span', styles: { 'background-color': 'Yellow'} }
                ]/*,
      templates: [
                  {
                    title: 'My Template 1',
                    description: 'Description of My Template 1.',
                    html:
					            '<h2>Template 1</h2>' +
					            '<p><img src="/logo.png" style="float:left" />Type your text here.</p>'
                  },
			            {
			              title: 'My Template 2',
			              description: 'Description of My Template 1.',
			              html:
					            '<h3>Template 2</h3>' +
					            '<p>Type your text here.</p>'
			            }
                ]*/
    }
  );

  $(target + '_editBar .edit').hide();
  $(target + '_editBar .undo, ' + target + '_editBar .save').show();
}
function undoEdit(target) {
  //editor.removeInstance('editContent');
  CKEDITOR.remove(editor);
  $(target).html(content);
  $(target + '_editBar .edit').show();
  $(target + '_editBar .undo, ' + target + '_editBar .save').hide();
}
function saveEdit(target) {
  var data = editor.getData();
  editor.destroy();
  $('#aspnetForm').ajaxSubmit();   //$('#aspnetForm').ajaxSubmit(options);
  //document.forms['aspnetForm'].submit();
  $(target).html(data); //restore Html to read-mode (the form can not be uploadrd after this!)
  $(target + '_editBar .edit').show();
  $(target + '_editBar .undo, ' + target + '_editBar .save').hide();
}
