(function ($) {
	/* create modal dialog. */
	$.modal = function (data, options) {
		return $.modal.impl.init(data, options);
	};

	/* close modal dialog */
	$.modal.close = function () {
		$.modal.impl.close(true);
	};

	/* Chained function to create modal dialog. */
	$.fn.modal = function (options) {
		return $.modal.impl.init(this, options);
	};

	/* default options *
	 * overlay: opacity 0 - 100
	 * overlayId: id for the overlay div
	 * overlayCss: CSS declaration for the overlay div
	 * containerId: id for the container div
	 * containerCss: CSS declaration for the container div
	 * close: Show the default window close icon? Uses CSS class modalCloseImg
	 * closeTitle: The title value of the default close link. Depends on close
	 * closeClass: CSS class used to bind to the close event
	 * persist: Persist the data across modal calls? Only used for existing
	            DOM elements. If true, the data will be maintained across modal calls, if false,
				the data will be reverted to its original state.
	 * onOpen: The callback function used in place of SimpleModal's open
	 * onShow: The callback function used after the modal dialog has opened
	 * onClose: The callback function used in place of SimpleModal's close
	 */
	$.modal.defaults = {
		overlay: 75,
		overlayId: 'modalOverlay',
		overlayCss: {},
		containerId: 'modalContainer',
		containerCss: {},
		close: true,
		closeTitle: 'Bezárás',
		closeTxt: 'Bezárás',
		closeClass: 'modalClose',
		persist: false,
		onOpen: null,
		onShow: null,
		onClose: null
	};

	/* Main modal object */
	$.modal.impl = {
		/* Modal dialog options */
		opts: null,
		/* Contains the modal dialog elements and is the object passed 
		 * back to the callback (onOpen, onShow, onClose) functions */
		dialog: {},
		/* Initialize the modal dialog */
		init: function (data, options) {
			// don't allow multiple calls
			if (this.dialog.data) {
				return false;
			}

			// merge defaults and user options
			this.opts = $.extend({}, $.modal.defaults, options);

			// determine how to handle the data based on its type
			if (typeof data == 'object') {
				// convert DOM object to a jQuery object
				data = data instanceof jQuery ? data : jQuery(data);

				// if the object came from the DOM, keep track of its parent
				if (data.parent().parent().size() > 0) {
					this.dialog.parentNode = data.parent();

					// persist changes? if not, make a clone of the element
					if (!this.opts.persist) {
						this.dialog.original = data.clone(true);
					}
				}
			}
			else if (typeof data == 'string' || typeof data == 'number') {
				data = jQuery('<div>').html(data);
			}
			else {
				// unsupported data type!
				if (console) {
					console.log('SimpleModal Error: Unsupported data type: ' + typeof data);
				}
				return false;
			}
			this.dialog.data = data.addClass('modalData');
			data = null;
			this.create();
			this.open();
			// useful for adding events/manipulating data in the modal dialog
			if ($.isFunction(this.opts.onShow)) {
				this.opts.onShow.apply(this, [this.dialog]);
			}
			return this;
		},
		/* Create and add the modal overlay and container to the page */
		create: function () {
			// create the overlay
			this.dialog.overlay = jQuery('<div>')
				.attr('id', this.opts.overlayId)
				.addClass('modalOverlay')
				.css($.extend(this.opts.overlayCss, {
					opacity: this.opts.overlay / 100,
					height: '100%',
					width: '100%',
					position: 'fixed',
					left: 0,
					top: 0,
					zIndex: 3000
				}))
				.hide()
				.appendTo('body');

			// create the container
			this.dialog.container = jQuery('<div>')
				.attr('id', this.opts.containerId)
				.addClass('modalContainer')
				.css($.extend(this.opts.containerCss, {
					position: 'fixed', 
					zIndex: 3100
				}))
				.append(this.opts.close 
					? '<a class="modalCloseImg ' 
						+ this.opts.closeClass 
						+ '" title="' 
						+ this.opts.closeTitle 
                        + '" id="modalClose">'
                        + this.opts.closeTxt
                        +'</a>'
					: '')
				.hide()
				.appendTo('body');

			// fix issues with IE and create an iframe
			if ($.browser.msie && ($.browser.version < 7)) {
				this.fixIE();
			}

			// hide the data and add it to the container
			this.dialog.container.append(this.dialog.data.hide());
		},
		
        /* Bind events */
		bindEvents: function () {
			var modal = this;

			// bind the close event to any element with the closeClass class
			jQuery('.' + this.opts.closeClass).click(function (e) {
				e.preventDefault();
				modal.close();
			});
		},
		
        /* Unbind events */
		unbindEvents: function () {
			// remove the close event
			jQuery('.' + this.opts.closeClass).unbind('click');
		},
		
        /* Fix issues in IE 6 */
		fixIE: function () {
			var wHeight = jQuery(document.body).height() + 'px';
			var wWidth = jQuery(document.body).width() + 'px';

			// position hacks
			this.dialog.overlay.css({position: 'absolute', height: wHeight, width: wWidth});
			this.dialog.container.css({position: 'absolute'});

			this.dialog.iframe = jQuery('<iframe src="javascript:false;">')
				.css($.extend(this.opts.iframeCss, {
					opacity: 0, 
					position: 'absolute',
					height: wHeight,
					width: wWidth,
					zIndex: 1000,
					width: '100%',
					top: 0,
					left: 0
				}))
				.hide()
				.appendTo('body');
		},
		/* Open the modal dialog */
		open: function () {
			if (this.dialog.iframe) {
				this.dialog.iframe.show();
			}

			if ($.isFunction(this.opts.onOpen)) {
				this.opts.onOpen.apply(this, [this.dialog]);
			}
			else {
				this.dialog.overlay.show();
				this.dialog.container.show();
				this.dialog.data.show();
			}

			this.bindEvents();
		},
		
        /* Close the modal dialog */
		close: function (external) {
			if (!this.dialog.data) {
				return false;
			}

			if ($.isFunction(this.opts.onClose) && !external) {
				this.opts.onClose.apply(this, [this.dialog]);
			}
			else {
				if (this.dialog.parentNode) {
					if (this.opts.persist) {
						this.dialog.data.hide().appendTo(this.dialog.parentNode);
					}
					else {
						this.dialog.data.remove();
						this.dialog.original.appendTo(this.dialog.parentNode);
					}
				}
				else {
					this.dialog.data.remove();
				}

				this.dialog.container.remove();
				this.dialog.overlay.remove();
				if (this.dialog.iframe) {
					this.dialog.iframe.remove();
				}

				this.dialog = {};
			}

			this.unbindEvents();
		}
	};
})(jQuery);



jQuery(document).ready(function () {
    jQuery('#sendMailAction_js').click(function (e) {
        e.preventDefault();
        jQuery('#basicModalContent').modal();
    });
});
