/* mmDialog.Ajax */
mmDialog.Ajax = new Class({
	Extends: mmDialog
	, options: {
		data: {}
		, ajaxDelay: 250
		, loadingHtml: '<div class="mlr-10 tM lh-32 tL ptb-15"><h1 class="mb-0 tB"><h3><img src="/assets/img/ajaxloaders/loading_28.gif" class="tM dIB mr-10"/> Loading...</h3></div>'
		, dialogBaseUrl: 'ajax/dialog/'
		, ajaxBaseUrl: 'ajax/'
		, size: {width:640, height:200}
		, sizes: {
			loading: { width: 200, height: 'auto' }
		}
		, minSize: {width:640, height:200}
		, maxSize: {height:600}
		, autosize: true
		, overlay: {
			id: 'mmDialogAjaxOverlay'
			, color: '#000'
			, duration: 200
			, opacity: 0.25
			, zIndex: 9000
		}
		, dialogFx: {
			type: 'tween'
			, open: 1
			, close: 0
			, options: {
				property: 'opacity'
				, duration: 200
			}		
		}					
	}
	, initialize: function(url, options){
// mm.log('ajax dialog INIT OPT:'+options);		
		this.parent(options);
		if (url.charAt(0)=='/') { url = url.substr(1,url.length); }	
		var site_url = (mm.runtime.site_url != undefined) ? mm.runtime.site_url : '/' ;
		this.url = site_url + this.options.dialogBaseUrl+url;
		this.data = this.options.data;		
		this.request = new Request.JSON({
			url: this.url
			// , data: this.data
			, method:'post'
			, onSuccess: function(rJson, rText) {
				this.requestSucces.delay(this.options.ajaxDelay, this, [rJson, rText]);
			}.bind(this)
			, onRequest: function() {
				this.requestStart();
			}.bind(this)
			, onFailure: function() {
				this.requestFailure();
			}.bind(this)					
			
		});
		
	}
	, openDialog:function(data) {
		if (data == undefined) { data = {}; }
		var finaldata = Object.merge(this.data, data);
		this.request.send({data: finaldata});
	}
	, requestSucces:function(rJson, rText) {
		this.fireEvent('requestSuccessBefore');
		if (rJson) {
// mm.log(rJson);				 	
			this.setHeader('');
			this.dialogContainer.setStyles({width:this.options.size.width});		
			this.curSize.width = this.options.size.width;

			if (this.isOpen && this.options.closeable) {
				this.dialogQuit.setStyle('opacity',0).show().fade(1);
			}			
			this.rePosition();				
			if (rJson.html && rJson.html.body) {
				if (rJson.html.footer) {
					this.setFooter(rJson.html.footer);						
				}
				this.setContent(rJson.html.body);						
				
				this.requestSuccessAfter();
				
			} else {
				this.close();
			}
		} else {
			this.close();
		}			
	}
	, requestStart:function() {
		this.fireEvent('requestStart');
		this.setHeader(this.options.loadingHtml);
		this.dialogBody.empty();
		this.dialogFooter.empty();
		this.dialogBody.hide();
		this.dialogFooter.hide();
		// this.options.closeable = false;
		this.dialogContainer.setStyles({width:this.options.sizes.loading.width});
		this.curSize.width = this.options.sizes.loading.width; 
		this.reSize();
		this.rePosition();						
		this.open();
	}
	, requestFailure: function() {
		this.fireEvent('requestFailure');		
		this.close();
	}
	, requestSuccessAfter: function() {	
		this.fireEvent('requestSuccessAfter');	
		if (this.dialogHeader.getFirst()) {
// mm.log('header ajaxready');			
			if (mm.objects) {
				mm.objects.scan(this.dialogHeader);		
			}
		}
		if (this.dialogBody.getFirst()) {		
// mm.log('body ajaxready');
			this.setupDialogBodyElements();
		}
		if (this.dialogFooter.getFirst()) {
// mm.log('footer ajaxready');			
			if (mm.objects) {
				mm.objects.scan(this.dialogFooter);		
			}
			this.setupDialogFooterElements();			
		}
		this.fireEvent('initialized', this.dialogContainer);					
	}
	, setupDialogBodyElements: function() {
		// custom dialog body stuff like dialog forms etc
		this.dialogBodyDefault = this.dialogBody.getElement('.mmDialogBodyDefault');
		this.dialogBodyLoading = this.dialogBody.getElement('.mmDialogBodyLoading');
		this.dialogBodySuccess = this.dialogBody.getElement('.mmDialogBodySuccess');
		this.dialogBodyFailed = this.dialogBody.getElement('.mmDialogBodyFailed');	
						
		var dialogForm = this.dialogBody.getElement('form.mmDialogForm');
		if (dialogForm) {
			this.dialogForm = dialogForm;
			if (this.dialogForm.hasClass('mmDialogFormValidate')) {
				this.dialogFormValidator = new Form.Validator(this.dialogForm, {
					evaluateFieldsOnBlur: false
					, evaluateFieldsOnChange: false
					, scrollToErrorsOnSubmit: false			
					, serial: false
					, onFormValidate: function(passed, form, evt) {
						if (!passed) {
							//mm.noticeError.show({title:'Error', message:'The form did not validate completely.'});
						}
// mm.log('onFormValidate passed='+passed);				
						this.dialogForm.store('validated', passed);				
					}.bind(this)
					, onElementValidate: function(passed, el, validator, warn ) {

					}.bind(this)
					, onElementPass: function(el) {
						var error = el.retrieve('validation_error', false);
						if (!error) {
							error = document.id(el.get('id')+'_error');					
							if (error) {
								el.store('validation_error', error);
							}
						}
						if (error) {
							error.hide();
						}				
					}.bind(this)
					, onElementFail: function(el, validators) {
						var error = el.retrieve('validation_error', false);
						if (!error) {
							error = document.id(el.get('id')+'_error');					
							if (error) {
								el.store('validation_error', error);
							}
						}
						if (error) {
							error.show();
						}
					}.bind(this)
				});
			}
			
			if (this.dialogForm.hasClass('mmAjaxForm')) {
				var action = this.dialogForm.get('action');
				this.dialogForm.addEvent('submit', function(e) {
					e.stop();
				}.bind(this));

				if (action != '') {
					if (action.charAt(0)=='/') { action = action.substr(1,action.length); }	
					var ajaxurl = this.options.ajaxBaseUrl+action;
					this.dialogFormRequest = new Request.JSON({
						url: ajaxurl
						, data: {}
						, method: 'post'
						, onSuccess:function(rJson, rText) {
							this.dialogFormRequestSuccess(rJson, rText);
						}.bind(this)
						, onRequest: function() {
							this.dialogFormRequestStart();
						}.bind(this)
						, onFailure: function() {
							this.dialogFormRequestFailure();
						}.bind(this)						
					});
				}
			}
			
			if (this.dialogForm.hasClass('mmEventForm')) {
				this.dialogForm.addEvent('submit', function(e) {
					e.stop();
				}.bind(this));				
			}
			
		}
	}
	, setupDialogFooterElements: function() {
		// custom dialog footer stuff like automatic close buttons etc
		this.dialogFooterDefault = this.dialogFooter.getElement('.mmDialogFooterDefault');
		this.dialogFooterLoading = this.dialogFooter.getElement('.mmDialogFooterLoading');
		this.dialogFooterSuccess = this.dialogFooter.getElement('.mmDialogFooterSuccess');
		this.dialogFooterFailed = this.dialogFooter.getElement('.mmDialogFooterFailed');		
		
		var footerCloseButtons = this.dialogFooter.getElements('a.mmDialogClose');
		if (footerCloseButtons && footerCloseButtons.length > 0) {
			footerCloseButtons.each(function(btn) {
				btn.addEvent('click', function(e) {
					e.stop();
					this.close();
				}.bind(this));
			},this);
		}
		
		var submitButton = this.dialogFooter.getElement('a.mmDialogSubmit');
		if (submitButton) {
			this.dialogSubmit = submitButton;
			this.dialogSubmit.addEvent('click', function(e) {
				e.stop();
				if (!this.dialogSubmit.hasClass('btn-disabled')) {
// alert('click on submit button');					
					var do_continue = false;
					if (this.dialogFormValidator) {
						// now auto validate on blur (after first submit)
mm.log('validating ..');						
						this.dialogFormValidator.options.evaluateFieldsOnBlur = true;
						this.dialogFormValidator.watchFields(this.dialogFormValidator.getFields());
						if (this.dialogFormValidator.validate()) {
							do_continue = true;
						}
					} else {
						do_continue = true;
					}
					
					if (do_continue) {
						if (this.dialogForm) {
							if (this.dialogForm.hasClass('mmAjaxForm') && this.dialogFormRequest) {
								var qs = this.dialogForm.toQueryString();				
								this.dialogFormData = qs.parseQueryString();
								this.dialogFormRequest.send({data:this.dialogFormData});
							} else if (this.dialogForm.hasClass('mmEventForm')) {
								var qs = this.dialogForm.toQueryString();				
								this.dialogFormData = qs.parseQueryString();
								this.fireEvent('submit', this.dialogFormData);
								this.close();								
							} else {
								this.dialogForm.submit();
							}
						}
					}
				}
			}.bind(this));
		}
	}
	, dialogFormRequestSuccess:function(rJson, rText) {
// mm.log('dialogFormRequestSuccess rJson:');		
// mm.log(rJson);
		if (rJson.success) {
			if (this.dialogBodyDefault) { this.dialogBodyDefault.hide(); }
			if (this.dialogBodyLoading) { this.dialogBodyLoading.hide(); }
			if (this.dialogBodySuccess) { this.dialogBodySuccess.show(); }		
			if (this.dialogBodyFailed) { this.dialogBodyFailed.hide(); }

			if (this.dialogFooterDefault) { this.dialogFooterDefault.hide(); }
			if (this.dialogFooterLoading) { this.dialogFooterLoading.hide(); }
			if (this.dialogFooterSuccess) { this.dialogFooterSuccess.show(); }		
			if (this.dialogFooterFailed) { this.dialogFooterFailed.hide(); }
		} else {
			if (this.dialogBodyDefault) { this.dialogBodyDefault.show(); }
			if (this.dialogBodyLoading) { this.dialogBodyLoading.hide(); }
			if (this.dialogBodySuccess) { this.dialogBodySuccess.hide(); }		
			if (this.dialogBodyFailed) { this.dialogBodyFailed.hide(); }

			if (this.dialogFooterDefault) { this.dialogFooterDefault.show(); }
			if (this.dialogFooterLoading) { this.dialogFooterLoading.hide(); }
			if (this.dialogFooterSuccess) { this.dialogFooterSuccess.hide(); }		
			if (this.dialogFooterFailed) { this.dialogFooterFailed.hide(); }			
		}
	}
	, dialogFormRequestStart:function() {
// mm.log('dialogFormRequestStart');		
		
		if (this.dialogBodyDefault) { this.dialogBodyDefault.hide(); }
		if (this.dialogBodyLoading) { this.dialogBodyLoading.show(); }
		if (this.dialogBodySuccess) { this.dialogBodySuccess.hide(); }		
		if (this.dialogBodyFailed) { this.dialogBodyFailed.hide(); }
		
		if (this.dialogFooterDefault) { this.dialogFooterDefault.hide(); }
		if (this.dialogFooterLoading) { this.dialogFooterLoading.show(); }
		if (this.dialogFooterSuccess) { this.dialogFooterSuccess.hide(); }		
		if (this.dialogFooterFailed) { this.dialogFooterFailed.hide(); }		
		
	}
	, dialogFormRequestFailure:function() {
// mm.log('dialogFormRequestFailure');		
		if (this.dialogBodyDefault) { this.dialogBodyDefault.hide(); }
		if (this.dialogBodyLoading) { this.dialogBodyLoading.hide(); }
		if (this.dialogBodySuccess) { this.dialogBodySuccess.hide(); }		
		if (this.dialogBodyFailed) { this.dialogBodyFailed.show(); }
		
		if (this.dialogFooterDefault) { this.dialogFooterDefault.hide(); }
		if (this.dialogFooterLoading) { this.dialogFooterLoading.hide(); }
		if (this.dialogFooterSuccess) { this.dialogFooterSuccess.hide(); }		
		if (this.dialogFooterFailed) { this.dialogFooterFailed.show(); }		
		
	}	
});
