/* 	File:	inlineEdit.v3.js
	Title: 	Mootools inlineEdit Plugin
	Author:	Justin Maier
	Url:	http://justinmaier.com
	Date:	2008-06-06
	Ver:	1*/

var inlineEdit = new Class({
	Implements: [Options,Events],
	options: {
		onComplete:$empty,//passes element
		onCancel:$empty,//passes element
		onLoad:$empty,//passes element,input
		onKeyup:$empty,//passes element, input, event
		inputClass:'inlineedit',
		stripHtml:true,
		updateOnBlur: true,
		saveElement: $empty,
		elementType: 'textarea'
	},
	initialize: function(element,options){
		this.setOptions(options);
		this.element = element;
		this.originalText = element.get('html');
		this.originalText.replace(/<br\/>/gi,"\n");
		this.originalText.replace(/<br \/>/gi,"\n");
		this.originalText.replace(/<br>/gi,"\n");
		this.input = new Element(this.options.elementType,{
			'class':this.options.inputClass,
			//'styles':this.element.getStyles('width','padding-top','padding-right','padding-bottom','padding-left','margin-top','margin-right','margin-bottom','margin-left','font-family','font-size','font-weight','line-height','border-top','border-right','border-bottom','border-left','background-color','color'),
			'events':{
				'keyup':this.keyup.bind(this)
			},
			'value':this.originalText.clean()
		});
		if(this.options.updateOnBlur) {
			this.input.addEvent('blur', this.complete.bind(this));
		}else if(this.options.saveElement){
			this.options.saveElement.removeEvents('click');
			this.options.saveElement.addEvent('click', this.complete.bind(this));
		}
		if(!Browser.Engine.trident) {
			this.input.setStyle('margin-left',this.input.getStyle('margin-left').toInt()-1);
		}
		this.originalWidth = this.element.getStyle('width');
		this.element.setStyles({'visibility':'hidden','position':'absolute','width':this.element.offsetWidth});
		
		this.input.inject(this.element,'after');
		this.input.focus();
		this.fireEvent('onLoad', [this.element,this.input]);
	},
	keyup: function(e){
		if(this.options.elementType=='textarea'){
			if(e)e=new Event(e);
			else return;
			this.fireEvent('onKeyup', [this.element,this.input,e])
			this.element.set('html',(e.key=='enter')?this.getContent()+"&nbsp;":this.getContent());
			if(e.key=='enter')this.input.addEvent('keydown',this.newLine.bind(this));
			this.input.setStyle('height',this.element.offsetHeight);
			if(e.key=='esc'){
				this.cancel();
			}
		}

	},
	getContent: function(){
		var content = this.input.value
		if(this.options.stripHtml)content = content.replace(/(<([^>]+)>)/ig,"");
		return(content.replace(/\n/gi,"<br>"));
	},
	cancel: function(){
		this.element.set('html', this.originalText);
		this.fireEvent('onCancel', this.element);
		this.end();
	},
	newLine: function(){
		this.element.innerHTML=this.element.innerHTML.replace("&nbsp;","");
		this.input.removeEvents('keydown');
	},
	complete: function(){
		this.element.set('html',this.getContent());
		this.fireEvent('onComplete', this.element)
		this.end();
	},
	end: function(){
		this.input.destroy();
		this.element.setStyles({'visibility':'visible','position':'relative','width':this.originalWidth});
	}
});

Element.implement({
	inlineEdit: function(options) {
		return new inlineEdit(this, options);
	}
});
