audi_ngw.namespace(audi_ngw, 'question');

// Options:
// posCallback, negCallback, posCallbackBeforeClose, negCallbackBeforeClose
audi_ngw.question.showDialog = function(sUrl, options) {
	audi_ngw.event.bind('ajaxModalContentReady.questionBinder', audi_ngw.question.onAjaxModalContentReady);
	audi_ngw.question.posCallback = options.posCallback ? options.posCallback : function(){};
	audi_ngw.question.posCallbackBeforeClose = options.posCallbackBeforeClose ? options.posCallbackBeforeClose : function(){};
	audi_ngw.question.negCallback = options.negCallback ? options.negCallback : function(){};
	audi_ngw.question.negCallbackBeforeClose = options.negCallbackBeforeClose ? options.negCallbackBeforeClose : function(){};
	
	audi_ngw.question.autoHide = options.noAutoHide ? options.noAutoHide : false;
	audi_ngw.api.openModal(sUrl, {});
};

audi_ngw.question.showModalForm = function(sUrl, options) {
	if (options.sErrorClass) {
		audi_ngw.question.sErrorClass = options.sErrorClass;
	}
	oldposCallbackBeforeClose = options.posCallbackBeforeClose ? options.posCallbackBeforeClose : function() {};
	options.posCallbackBeforeClose = function() {
		oldposCallbackBeforeClose();
		var _form = jQuery('#audi_modal_container form');
		jQuery.ajax({
			complete: function(xhr, msg) {
				if (msg === 'success') {
					audi_ngw.question.modalform = 2;
					audi_ngw.layer._doSetContent(audi_ngw.layer.modal._$elLayer, null, 'modal', {sContent: xhr.responseText, sContentType: 'html', sXHRObj: xhr});
					audi_ngw.question.bindListeners();
				}
			},
			data: _form.serializeArray(),
			url: _form.attr('action')
		});
	};
	options.noAutoHide = true;
	audi_ngw.question.showDialog(sUrl, options);
	audi_ngw.question.modalform = 1;
};

audi_ngw.question.hide = function() {
	audi_ngw.question.modalform = false;
	audi_ngw.layer.modal.hide();
};

audi_ngw.question.bindListeners = function() {
	var yesButton = jQuery('#audi_modal_body button.dialog_yes_button');
	var cancelButton = jQuery('#audi_modal_body button.dialog_no_button');
	if (yesButton) {
		yesButton.bind('click.questionButton', function(evt) {
			audi_ngw.event.unbind('ajaxModalContentReady.questionBinder');
			audi_ngw.question.posCallbackBeforeClose();
			if (!audi_ngw.question.autoHide) {
				audi_ngw.question.hide();
			}
			audi_ngw.question.posCallback();
			return false;
		});
	}
	if (cancelButton) {
		cancelButton.bind('click.questionButton', function(evt) {
			audi_ngw.event.unbind('ajaxModalContentReady.questionBinder');
			audi_ngw.question.negCallbackBeforeClose();
			audi_ngw.question.hide();
			audi_ngw.question.negCallback();
			return false;
		});
	}
	if ((audi_ngw.question.modalform === 2) && 
		((jQuery('#audi_modal_body .' + audi_ngw.question.sErrorClass).length === 0) ||
			jQuery('#audi_modal_body form').length === 0)
	) {
		if (jQuery('#audi_modal_body button.cancel').length > 0) {
			audi_ngw.iframe.contentRedirects(jQuery('#audi_modal_body'));
		}
		window.setTimeout('audi_ngw.question.autoHideConfirmation();', audi_ngw.config.myAudiFormConfirmationDelay);
	}
};

audi_ngw.question.autoHideConfirmation = function() {
	if (jQuery('#audi_modal_body button.cancel').length > 0) {
		jQuery('#audi_modal_body button.cancel').click();
	} else {
		audi_ngw.question.hide();
	}
};

audi_ngw.question.showConfirmation = function(sUrl) {
	audi_ngw.question.showDialog(sUrl, {});
};

audi_ngw.question.onAjaxModalContentReady = function(event,data){
	audi_ngw.question.bindListeners();
	audi_ngw.event.trigger('ajaxModalContentPrepared');
};

