/* BOFHnet rjs@biuro.net.pl */

(function($) {
	$.sfWindow = {
		alert: function(title, message, settings) {
			var config = $.extend( {
				dialogClass: 'info',
				width: 450,
				minHeight: 50,
				bgiframe: true,
				modal: true,
				resizable: false,
				close: null,
				buttons: {
					OK: function() {
						$(this).dialog('destroy');
					}
				}
			}, settings);
			$('<div title="'+title+'" class="sfModalAlert"><span class="ui-icon ui-icon-'+config.dialogClass+'" /><div class="alertContent">'+message+'</div></div>').dialog(config);
		},
		open: function(url, settings) {
			var config = $.extend ({
				bgiframe: true,
				minWidth: 350,
				minHeight: 50,
				resizable: false,
				afterOpen: null,
				open: function(event, ui) {
					$.sfWindow.overlayDestroy();
					var firstChild = $(this).find('*:first');
					firstChild.removeClass('hidden');
					var width = firstChild.width() + (2 * parseInt($('.ui-dialog .ui-dialog-content').css('padding-left')));
					firstChild.css('width', 'auto');
					firstChild.data('width', width);
					width = (width > 350)?width:350;
					var height = $(this).height();
					var maxHeight = $(window).height()-100;
					$(this)
						.dialog('option', 'title', firstChild.attr('title'))
						.dialog('option', 'width', width)
						.dialog('option', 'position', $(this).dialog('option', 'position'));
					if (height > maxHeight) {
						$(this)
							.css({
								height: maxHeight,
								width: 'auto'
							})
							.dialog('option', 'position', 'center');
					}
					if (jQuery.isFunction($(this).dialog('option', 'afterOpen')))
						$(this).dialog('option', 'afterOpen')();
				},
				close: function() {
					if ($(this).data('appendedId')) {
						$('#'+$(this).data('appendedId'))
							.appendTo(document.body)
							.css('width', $('#'+$(this).data('appendedId')).data('width'))
							.hide();
						$(this).removeData('appendedId');
					}
					else
						$(this).html('');
					$(this)
						.dialog('destroy')
						.remove();
				}
			}, settings);
			var anchor = (url.match(/#/g))?url.split('#')[1]:url;
			load(anchor, url);

			function container() {
				return $('<div>', {
					'class': 'sfModalWindow'
				}).hide().appendTo('body');
			}
			function load(anchor, url) {
				if (anchor != url && $('#'+anchor).length) {
					container()
						.data('appendedId', anchor)
						.append($('#'+anchor).show())
						.dialog(config);
				} else {
					if (url != '#') {
						$.sfWindow.overlay();
						container()
							.load(url, null, function(responseText){
								$(this).dialog(config);
							});
					}
				}
			}
		},
		modal: function(url, settings) {
			var config = $.extend( {
				modal: true
			}, settings);
			this.open(url, config);
		},
		overlayResize: function () {
			$('#sfOverlay')
				.css({
					width: 0,
					height: 0
				})
				.css({
					width: $.ui.dialog.overlay.width(),
					height: $.ui.dialog.overlay.height()
				});
		},
		overlay: function (message, settings) {
			if (!$('#sfOverlay').length) {
				var config = $.extend( {
					title: 'proszę czekać...'
				}, settings);
				var el = $('<div>',{
					id: 'sfOverlay',
					'class': 'ui-widget-overlay'
				}).appendTo(document.body);
				($.fn.bgiframe && el.bgiframe());
				this.overlayResize();
				$(window).resize(function(){
					$.sfWindow.overlayResize();
				});
				$('<div>', {
					id: 'sfOverlayInfo',
					'class': 'ui-dialog-titlebar ui-widget-header ui-corner-all',
					text: config.title,
					css: {
						'top': ($(window).height()-$('#sfOverlayInfo').height())/2+$(document).scrollTop(),
						'left': ($(window).width()-$('#sfOverlayInfo').width())/2+$(document).scrollLeft()
					}
				}).appendTo(document.body);
			}
		},
		overlayDestroy: function() {
			$('#sfOverlay').remove();
			$('#sfOverlayInfo').remove();
		}
	}
})(jQuery);

$(function() {
	$('a.actionOpenWindow').click(function(event) {
		event.preventDefault();
		$.sfWindow.open($(this).attr('href'));
	});
	$('a.actionHashModalWindow').click(function(event) {
		event.preventDefault();
		var url = $(this).attr('href').split('#');
		$.sfWindow.modal(url[0] + ' #' + url[1]);
	});
	$('a.actionModalWindow').click(function(event) {
		event.preventDefault();
		$.sfWindow.modal($(this).attr('href'));
	});
});

