
(function(container) {

	var $ = jQuery;

	// this is our object
	function ActionDialog(formID, options)	{

		var self = {
			options:	{}
		};

		this.options = {};
		// possible options:
		// noConfirm:  true|false      if false, don't ask for confirm

		// 	inits the form, give formID and options for
		this.init =	function(formID, options)	{
			self.form = formID;
			self.formID = '#' + formID;

			// mix options
			$.extend(self.options, options);

			// generate instance of modalForm
			var modalOptions = {noSubmitBind: true};
			self.modalForm = container.modalForm.init(self.form, modalOptions);

			// bind submitElements onClick and ask for confirmation
			$(self.formID).bind('submit', self.submit);
			$(self.formID + ' .formSubmit').bind('click', self.submit);

		};

		this.submit = function(event)	{
			if (event)	event.preventDefault();

			// check if we should ask for a confirm? (default)
			if (!options.noConfirm)	{
				self.askConfirm();
			} else {
				// directly submit the form
				self.modalForm.formSubmit(event);
			}
		};

		this.askConfirm = function(){
			var header = text_are_you_sure;
			var message = text_do_you_really;

			if (self.options.header)	header = self.options.header;
			if (self.options.message)	message = self.options.message;

			var html = self.getModalHtml(message, header);

			$.modal(html, {onShow: self.modalShow, onClose: self.closeModal});
			// bind event for modalFormConfirm
			$('#modalFormConfirm').bind('click', self.confirmed);
			$(document).bind('keypress',self.keyPress);

		};

		this.unbindKeyPress = function()	{
			$(document).unbind('keypress',self.keyPress);
		};

		this.closeModal = function(dialog)	{
			self.unbindKeyPress();
			$.modal.close();
		};

		this.keyPress = function(event)	{
			var keyCode = event.which;
			if (keyCode == 0)	{
				self.unbindKeyPress();
				$.modal.close();
			}
		};

		this.confirmed = function(event)	{
			// close modal window
			self.unbindKeyPress();
			$.modal.close();
			self.modalForm.formSubmit(event);
		};

		this.getModalHtml = function(message, messageHeader)	{
			// build simple html-code as container for simple text message/error
			if (!messageHeader)	messageHeader = '';
			//var html = '<div class="modalFormMessage"><h3>' + messageHeader + '</h3><p class="modalFormMessageContent">' + message + '</p><p align=center class="modalFormConfirm">'
			//			+ '<input type=button id="modalFormConfirm" value="Ja">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input class="modalClose" type="button" value="Nein"></p></div>';
			var html = '<div class="modalFormMessage modalFormMessageFixedHeight"><h3>' + messageHeader + '</h3><p class="modalFormMessageContent">' + message + '</p></div>'
						+ '<div class="buttonRow" style="margin: auto; width: 150px"><div class="btnSmall btnWidthSmall" id="modalFormConfirm"><a href="#">' + text_Yes + '</a></div><div class="btnSmall btnWidthSmall modalClose"><a href="#">' + text_No + '</a></div></div>';
			return html;
		};

		this.modalShow = function(dialog)	{
			//dialog.container.css('width', 800);
		};



		// merge self with this, so we can call externally AutoForm.init and internally self.init;
		$.extend(self, this);

		// call constructor
		this.init(formID, options);

	};

	// create and return new timecounter object
	var self = {
		instances:	{},
		init:	function(formID,  options)	{
			var obj =  new ActionDialog(formID, options);
			self.instances[formID] = obj;				// store in local array, for later use
			return obj;
		},
		message:	function(message, header, onClose)	{
			// show just a simple modal message, do nothing. static stuff only!
			if (!header)	header = '';
			var html = '<div class="modalFormMessage modalFormMessageFixedHeight"><h3>' + header + '</h3><p class="modalFormMessageContent">' + message + '</p></div>'
						+ '<div class="modalClose modalFormClose btnSmall btnWidthSmall"><a href="#">' + text_ok + '</a></div>';
			var options = {};
			if (onClose)	options['onClose'] = function()	{ $.modal.close(); onClose()};		// close modal and call onClose function
			$.modal(html, options);
		},
		showContainer:	function(containerID, showOptions)	{
			if (showOptions == undefined) showOptions = {};
			// just show a static container as modal stuff.
			var options = {persist: true, onShow: function(dialog) {self.showContainerOnShow.apply(self, [dialog, containerID] )}};

			// check if we should skip close button
			if (showOptions['noClose']) options['close'] = false;

			$('#' + containerID).modal(options);
		},
		showContainerOnShow:	function(dialog, containerID)	{
			// read out real with and height of displaying container
			var width = $('#' + containerID).outerWidth();
			var height = $('#' + containerID).outerHeight();
			var newMarginLeft = 0 - Math.round((width / 2));
			// correct size/pos of container
			dialog.container.css({height: height, width: width, marginLeft: newMarginLeft});
		}

	}
	container.game.actionDialog = self;


})(_meef);








