var myEditor;

function editInPlace()
{
	document.getElementById('edit').style.display='none';
	document.getElementById('save').style.display='block';
	document.getElementById('cancel').style.display='block';

	//strip our div with id of master0 - this is generated by imageFade.js for slideshows.
	var slideShow = document.getElementById('master0');
	if(slideShow)
	{
		slideShow.parentNode.removeChild(slideShow);
	}
	var editable = document.getElementById('editable');
	var editbody = '<textarea name="editbody" class="editbody" id="editbody">'+editable.innerHTML+'</textarea>';
	editable.innerHTML = editbody;
    (function() {
    //Setup some private variables
    var Dom = YAHOO.util.Dom,
        Event = YAHOO.util.Event;

        //The SimpleEditor config
        var myConfig = {
            height: '400px',
            width: '100%',
            dompath: true,
            focusAtStart: true,
            toolbar:
            {
collapse: true,
titlebar: 'Text Editing Tools',
draggable: false,
buttonType: 'advanced',
buttons: [
    { group: 'textstyle', label: 'Font Style',
        buttons: [
            { type: 'push', label: 'Bold CTRL + SHIFT + B', value: 'bold' },
            { type: 'push', label: 'Italic CTRL + SHIFT + I', value: 'italic' },
            { type: 'separator' },
            { type: 'push', label: 'Subscript', value: 'subscript', disabled: true },
            { type: 'push', label: 'Superscript', value: 'superscript', disabled: true },
            { type: 'separator' },
            { type: 'push', label: 'Remove Formatting', value: 'removeformat', disabled: true },
            { type: 'push', label: 'Show/Hide Hidden Elements', value: 'hiddenelements' }
        ]
    },
    { type: 'separator' },
    { group: 'alignment', label: 'Alignment',
        buttons: [
            { type: 'push', label: 'Align Left CTRL + SHIFT + [', value: 'justifyleft' },
            { type: 'push', label: 'Align Center CTRL + SHIFT + |', value: 'justifycenter' },
            { type: 'push', label: 'Align Right CTRL + SHIFT + ]', value: 'justifyright' },
            { type: 'push', label: 'Justify', value: 'justifyfull' }
        ]
    },
    { type: 'separator' },
    { group: 'parastyle', label: 'Paragraph Style',
        buttons: [
        { type: 'select', label: 'Normal', value: 'heading', disabled: true,
            menu: [
                { text: 'Normal', value: 'none', checked: true },
                { text: 'Header 1', value: 'h1' },
                { text: 'Header 2', value: 'h2' },
                { text: 'Header 3', value: 'h3' },
                { text: 'Header 4', value: 'h4' },
                { text: 'Header 5', value: 'h5' },
                { text: 'Header 6', value: 'h6' }
            ]
        }
        ]
    },
    { type: 'separator' },
    { group: 'indentlist', label: 'Indenting and Lists',
        buttons: [
            { type: 'push', label: 'Indent', value: 'indent', disabled: true },
            { type: 'push', label: 'Outdent', value: 'outdent', disabled: true },
            { type: 'push', label: 'Create an Unordered List', value: 'insertunorderedlist' },
            { type: 'push', label: 'Create an Ordered List', value: 'insertorderedlist' }
        ]
    },
    { type: 'separator' },
    { group: 'insertitem', label: 'Insert Item',
        buttons: [
            { type: 'push', label: 'HTML Link CTRL + SHIFT + L', value: 'createlink', disabled: true },
            { type: 'push', label: 'Insert Image', value: 'insertimage' },
            { type: 'push', label: 'Insert Acronym', value: 'insertacronym',title:'ACR' },
        ]
    }
]}            
        };

        var state='off';
    //Now let's load the SimpleEditor..
    myEditor = new YAHOO.widget.Editor('editbody', myConfig);

myEditor.on('toolbarLoaded', function() {
    //Using the Dynamic Event cmdClick where cmd is the value of the button above
    this.toolbar.on('insertacronymClick', function(o) {
        var el = this._getSelectedElement();
        var acronym,meaning;
        if(el && el.tagName=='ACRONYM')
        {
            acronym = prompt('Enter the acronym:',el.innerHTML);
            if(!acronym)    return;
            meaning = prompt('Enter the meaning:',el.getAttribute('title'));
            if(!meaning)    return;
            el.innerHTML = acronym;
            el.setAttribute('title',meaning);
        }
        else
        {
            acronym = prompt('Enter the acronym:');
            if(!acronym)    return;
            meaning = prompt('Enter the meaning:');
            if(!meaning)    return;
            this.execCommand('inserthtml', '<acronym title="'+meaning+'">'+acronym+'</acronym>');
        }
    }, myEditor, true);
    var codeConfig = {
type: 'push', label: 'Edit HTML Code', value: 'editcode'
    };
    this.toolbar.addButtonToGroup(codeConfig, 'insertitem');

    this.toolbar.on('editcodeClick', function() {
            var ta = this.get('element'),
            iframe = this.get('iframe').get('element');

            if (state == 'on') {
            state = 'off';
            this.toolbar.set('disabled', false);
            YAHOO.log('Show the Editor', 'info', 'example');
            YAHOO.log('Inject the HTML from the textarea into the editor', 'info', 'example');
            this.setEditorHTML(ta.value);
            if (!this.browser.ie) {
            this._setDesignMode('on');
            }

            Dom.removeClass(iframe, 'editor-hidden');
            Dom.addClass(ta, 'editor-hidden');
            this.show();
            this._focusWindow();
            } else {
            state = 'on';
            YAHOO.log('Show the Code Editor', 'info', 'example');
            this.cleanHTML();
            YAHOO.log('Save the Editors HTML', 'info', 'example');
            Dom.addClass(iframe, 'editor-hidden');
            Dom.removeClass(ta, 'editor-hidden');
            this.toolbar.set('disabled', true);
            this.toolbar.getButtonByValue('editcode').set('disabled', false);
            this.toolbar.selectButton('editcode');
            this.dompath.innerHTML = 'Editing HTML Code';
            this.hide();
            }
            return false;
    }, this, true);

    this.on('cleanHTML', function(ev) {
            YAHOO.log('cleanHTML callback fired..', 'info', 'example');
            this.get('element').value = ev.html;
            }, this, true);

    this.on('afterRender', function() {
            var wrapper = this.get('editor_wrapper');
            wrapper.appendChild(this.get('element'));
            this.setStyle('width', '100%');
            this.setStyle('height', '100%');
            this.setStyle('visibility', '');
            this.setStyle('top', '');
            this.setStyle('left', '');
            this.setStyle('position', '');

            this.addClass('editor-hidden');
            }, this, true);
}, myEditor, true);

    myEditor.render();
})();
	//jQuery(".editbody").wymeditor({classesItems: [{'name': 'new_window_link', 'title': 'New window link', 'expr': 'a'},{'name':'floatleft','title':'Float left','expr':'img'},{'name':'floatright','title':'Float right','expr':'img'}],editorStyles: [{'name': '.new_window_link','css':'border:1px solid red;'}]});
}

function saveEdit()
{
	//WYM_INSTANCES[0].update();
    myEditor.saveHTML();
	var editBody = document.getElementById('editbody').value;
	var getVars = getUrlVars();
    	var id = getVars['id'];
	if(!id)	id=1;
	loadingImg = document.getElementById('loading');

	loadXMLDoc('utility/save_edit.php?id='+id+'&body='+encodeURIComponent(editBody),null,loadingImg,'POST');
}

function loadPage()
{
	var getVars = getUrlVars();
    	var id = getVars['id'];
	if(!id)	id=1;
	loadingImg = document.getElementById('loading');

	loadXMLDoc('utility/load_page.php?id='+id,null,loadingImg,'POST');
}

function showEdit(div,response)
{
	var editable = document.getElementById('editable');
	if(editable)
	{
		editable.innerHTML = response.content;
	}
	document.getElementById('edit').style.display='block';
	document.getElementById('save').style.display='none';
	document.getElementById('cancel').style.display='none';
    formatLinks();
}

