/** 
 * 	AjaxGroups (c)2007
 *  A. Karasev 
 * 	Use: prototype.js, scriptaculous.js
 */

if(typeof window.Ajax.InPlaceFormEditor == 'undefined'){

	Ajax.InPlaceFormEditor = Class.create();
	Object.extend(Ajax.InPlaceFormEditor.prototype, Ajax.InPlaceEditor.prototype);
	Object.extend(Ajax.InPlaceFormEditor.prototype, {
		//enterHover: function(){},
		//leaveHover: function(){},
				
		enterEditMode: function(evt){
			if (this.saving) return;
			if (this.editing) return;
			this.editing = true;
			
			this.onEnterEditMode();
			if (this.options.externalControl) {
				Element.hide(this.options.externalControl);
			}			
			Element.hide(this.element);
			
			new Insertion.Before(this.element, this.createForm());			
			this.onAfterCreateForm()
			
			if (!this.options.loadTextURL){
				Field.scrollFreeActivate(this.editField);
			}

			// stop the event to avoid a page refresh in Safari
			if (evt) {
				Event.stop(evt);
			}
			
			return false;			
		},
		
		createForm: function(){
			return this.options.formHtml.evaluate(this.options);
		},
		
		onAfterCreateForm: function(){
			this.form = $(this.options.formId);
			
			var okLink = this.form.down('a.editor_ok_button');
			okLink.onclick =  this.onSubmit.bind(this);
			
			var cancelLink = this.form.down('a.editor_cancel');
			cancelLink.onclick = this.onclickCancel.bind(this);
					
			this.options.onAfterCreateForm.bind(this)();
			
			if(!this.element){
				throw "No edit element found";
			}
		},
		
		showSaving: function() {
			this.oldInnerHTML = this.element.innerHTML;
			this.element.innerHTML = this.options.savingText;
			Element.addClassName(this.element, this.options.savingClassName);
			// this.element.style.backgroundColor = this.originalBackground;
			Element.show(this.element);
		}
	});
}