// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults



/* Submit form on enter */
function submitEnter(myfield,e){
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;
	
	if (keycode == 13) 	   {
	   myfield.form.submit();
	   return false;
	   }
	else
	   return true;
}

/* This is the YUI RTE, customized for toggling HTML */
function yRTE(attach_to, height) {
	if (height==undefined) {
		height=800;		
	}
    var Dom = YAHOO.util.Dom,
        Event = YAHOO.util.Event;
    var myConfig = {
        height: height + 'px',
        width: '580px',
        animate: false,
        dompath: true,
        focusAtStart: true,
	   handleSubmit: true
    };

    var state = 'off';
    var myEditor = new YAHOO.widget.Editor(attach_to, myConfig);
    
    myEditor.on('toolbarLoaded', function() {
        var codeConfig = {
            type: 'push', label: 'Edit HTML Code', value: 'editcode'
        };
        this.toolbar.addButtonToGroup(codeConfig, 'insertitem');
	   this.toolbar.set('titlebar', 'Edit');

    
        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);
                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';
                this.cleanHTML();
                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) {
            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();
    
	return myEditor;
}
