/*
Script: MooEditable.Extras.js
	Extends MooEditable to include more (simple) toolbar buttons.

License:
	MIT-style license.
*/

MooEditable.Actions.extend({

	formatBlock: {
		title: 'Block Formatting',
		type: 'menu-list',
		options: {
			list: [
				{text: 'Paragraphe', value: 'p'},
				{text: 'Titre', value: 'h1'},
				{text: 'Citation', value: 'h2'},
				{text: 'Légende', value: 'h3'}
			]
		},
		states: {
			tags: ['p', 'h1', 'h2', 'h3']
		},
		command: function(menulist, name){
			var argument = '<' + name + '>';
			this.execute('formatBlock', false, argument);
			this.focus();
		}
	},
	
	justifyleft:{
		title: 'Align Left',
		states: {
			css: {'text-align': 'left'}
		}
	},
	
	justifyright:{
		title: 'Align Right',
		states: {
			css: {'text-align': 'right'}
		}
	},
	
	justifycenter:{
		title: 'Align Center',
		states: {
			tags: ['center'],
			css: {'text-align': 'center'}
		}
	},
	
	justifyfull:{
		title: 'Align Justify',
		states: {
			css: {'text-align': 'justify'}
		}
	},

	rubber: {
		title: 'Remove formatting',
		shortcut: 'k',
		command: function() {
		this.execute('removeformat',false,'');
		}
	},

	increasefontsize: {
		title: 'Increase font size',
		shortcut: '+',
		command: function() {

			// webkit doesnt do this natively
   if(Browser.Engine.webkit || Browser.Engine.trident) {

				var inserted;

				var node = new Element('body',{ html: this.selection.getContent() }).getChildren();
				if(node.length > 0) {
					node = node[0];
					if(node.get('tag') == 'small') {
						this.selection.insertContent( node.get('html') );
						inserted = true;
					}
				}

				if(!inserted) {
					this.selection.insertContent(new Element('big', { html : this.selection.getContent() }));
				}
			}
			else {
				this.execute('increasefontsize',false,[]);
			}
			//this.focus();
		},
		toggle: function(mode,source) {
			if(mode == 'iframe') {
				source = source.replace(/<big><small>/gi,'');
				source = source.replace(/<big>([^<]*)<\/big>/gi,'');
				source = source.replace(/<\/big><\/small>/gi,'');
				source = source.replace(/<small><big>/gi,'');
				source = source.replace(/<\/small><\/big>/gi,'');
			}
			return source;
		}
	},

	decreasefontsize: {
		title: 'Decrease font size',
		shortcut: '-',
		command: function() {

			// webkit doesnt do this natively
			if(Browser.Engine.webkit || Browser.Engine.trident) {

				var inserted;

				var node = new Element('body',{ html: this.selection.getContent() }).getChildren();
				if(node.length > 0) {
					node = node[0];
					if(node.get('tag') == 'big') {
						this.selection.insertContent( node.get('html') );
						inserted = true;
					}
				}

				if(!inserted) {
					this.selection.insertContent(new Element('small', { html : this.selection.getContent() }));
				}
			}
			else {
				this.execute('decreasefontsize',false,[]);
			}

		},
		toggle: function(mode,source) {
			if(mode == 'iframe') {
				source = source.replace(/<big><small>/gi,'');
				source = source.replace(/<small>([^<]*)<\/small>/gi,'');
				source = source.replace(/<\/big><\/small>/gi,'');
				source = source.replace(/<small><big>/gi,'');
				source = source.replace(/<\/small><\/big>/gi,'');
			}
			return source;
		}
	}	

});

