jQuery(function($){
		
	$( document )
	.addCol()
    .removeCol()
    .cloneCol()
	.jvtinymce()
	.addRow();
    
    $( '#params' ).loadCache();              
    
    var etemplate = CodeMirror.fromTextArea(document.getElementById("jform_template"), {
        lineNumbers: true,
        matchBrackets: true,
        mode: "application/x-httpd-php",
        indentUnit: 4,
        indentWithTabs: true,
        extraKeys: {
            "F11": function(cm) {
              cm.setOption("fullScreen", !cm.getOption("fullScreen"));
            },
            "Esc": function(cm) {
              if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false);
            }
        }
    });
    
    var ecss = CodeMirror.fromTextArea(document.getElementById("jform_customcss"), {
        lineNumbers: true,
        extraKeys: {
            "Ctrl-Space": "autocomplete",
            "F11": function(cm) {
              cm.setOption("fullScreen", !cm.getOption("fullScreen"));
            },
            "Esc": function(cm) {
              if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false);
            }
        },
        mode: {name: "css", globalVars: true}
    });
      
      
    
    var form = document.getElementById('item-form');

	Joomla.submitbutton = function(task)
    {
        if (task == 'item.cancel') {
            Joomla.submitform( task, form );
        }
        else {
            
            if (task != 'item.cancel' && document.formvalidator.isValid( form )) {
                
                if( $.inArray( task, [ 'item.customcss', 'item.template' ] ) > -1 ) {
                    
                    $( '#' + task.replace( '.', '-' ) ).modal( 'show' );
                    
                    return false;
                }
                
                $( form ).find( '[data-tag="row"]' ).each( function(){
                    
                    var w = $( this )
                        ,mc = tinymce.get( w.children( '[data-tag="mce"]' ).attr( 'id' ) )    
                    ;
                    w.children( '[data-field="text"]' ).val( mc.getContent() );
                } );
                
                Joomla.submitform( task, form );
            }
            else {
                alert( Joomla.JText._('JGLOBAL_VALIDATION_FORM_FAILED') );
            }
        }
    }
});