/*
 * jQuery SimpleModal plugin 1.0
 * http://www.ericmmartin.com/projects/simplemodal/
 * http://code.google.com/p/simplemodal/
 *
 * Copyright (c) 2007 Eric Martin - http://ericmmartin.com
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Revision: $Id: jquery.simplemodal.js 32 2007-10-24 02:57:08Z emartin24 $
 *
 */
 (function ($) {
	$.modal = function (content, settings) {
		return $.modal.impl.init(content, settings);
	};
	$.modal.remove = function (dialog) {
		$.modal.impl.remove(dialog);
	};
	$.fn.modal = function (settings) {
		return $.modal.impl.init(this, settings);
	};
	$.modal.defaults = {
		overlay: 50,
		overlayId: 'modalOverlay',
		containerId: 'modalContainer',
		iframeId: 'modalIframe',
		close: true,
		closeTitle: 'Zamknij',
		closeClass: 'modalClose',
		cloneContent: true,
		onOpen: null,
		onShow: null,
		onClose: null
	};
	$.modal.impl = {
		opts: null,
		dialog: {},
		init: function (content, settings) {
			this.opts = $.extend({},
				$.modal.defaults,
				settings
			);
			if (this.dialog.overlay) {
				return false;
			}
			content = content.jquery ? content : $(content);
			this.dialog.content = this.opts.cloneContent ? content.clone() : content;
			content = null;
			this.create();
			this.open();
			if ($.isFunction(this.opts.onShow)) {
				this.opts.onShow.apply(this, [this.dialog]);
			}
			return this;
		},
		create: function () {
			this.dialog.overlay = $('<div></div>')
				.attr('id', this.opts.overlayId)
				.css({opacity: this.opts.overlay / 100})
				.hide()
				.appendTo('body');
			if ($.browser.msie && ($.browser.version < 7)) {
				this.fixIE();
			}
			this.dialog.container = $('<div></div>')
				.attr('id', this.opts.containerId)
				.append(this.opts.close 
					? '<a class="modalCloseImg ' 
						+ this.opts.closeClass 
						+ '" title="' 
						+ this.opts.closeTitle + '"></a>'
					: '')
				.hide()
				.appendTo('body');
			this.dialog.content.appendTo(this.dialog.container);
		},
		bindEvents: function () {
			var modal = this;
			$('.' + this.opts.closeClass).click(function (e) {
				e.preventDefault();
				modal.close();
			});
		},
		unbindEvents: function () {
			$('.' + this.opts.closeClass).unbind('click');
		},
		fixIE: function () {
			this.dialog.overlay.css({position: 'absolute', height: $(document).height() + 'px'});
			this.dialog.iframe = $('<iframe src="javascript:false;"></iframe>')
				.attr('id', this.opts.iframeId)
				.css({opacity: 0, position: 'absolute', height: $(document).height() + 'px'})
				.hide()
				.appendTo('body');
		},
		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.content.show();
			}
			this.bindEvents();
		},
		close: function () {
			if ($.isFunction(this.opts.onClose)) {
				this.opts.onClose.apply(this, [this.dialog]);
			}
			else {
				this.opts.cloneContent ? this.dialog.content.remove() : this.dialog.content.hide();
				this.dialog.container.remove();
				this.dialog.overlay.remove();
				if (this.dialog.iframe) {
					this.dialog.iframe.remove();
				}
			}
			this.dialog = {};
			this.unbindEvents();
		},
		remove: function (dialog) {
			dialog.content.remove();
			dialog.container.remove();
			dialog.overlay.remove();
			if (dialog.iframe) {
				dialog.iframe.remove();
			}
		}
	};
})(jQuery);
