var dialogBusy = false;

function initDialog(dialogId,dialogTitle,dialogWidth,dialogHeight){
	if (!dialogId) return;
	var dialog = $(dialogId),
		overlay = dialog.parent(),
		overlayWrap = $('<div id="dialog" />').addClass('dialog').prependTo(overlay),
		overlayHeader = $('<div/>').appendTo(overlayWrap),
		overlayHeaderLeft = $('<div/>').addClass('dialog-header-left').appendTo(overlayHeader),
		overlayHeaderRight = $('<div/>').addClass('dialog-header-right').appendTo(overlayHeader),
		overlayHeaderBg = $('<div/>').addClass('dialog-header-bg').appendTo(overlayHeader),
		dialogHeader = $('<div/>').addClass('dialog-header').appendTo(overlayHeaderBg),
		dialogHeaderCopy = $('<div/>').addClass('dialog-header-copy').appendTo(dialogHeader),
		dialogHeaderCopyH2 = $('<h2>'+dialogTitle+'</h2>').appendTo(dialogHeaderCopy),
		overlayContent = $('<div/>').appendTo(overlayWrap),
		overlayContentLeft = $('<div/>').addClass('dialog-height dialog-content-left').appendTo(overlayContent),
		overlayContentRight = $('<div/>').addClass('dialog-height dialog-content-right').appendTo(overlayContent),
		dialogContent = $('<div/>').addClass('dialog-height dialog-content').appendTo(overlayContent),
		dialogContentInner = $('<div/>').addClass('dialog-height dialog-content-inner').appendTo(dialogContent),
		overlayFooter = $('<div/>').appendTo(overlayWrap),
		overlayFooterLeft = $('<div/>').addClass('dialog-footer-left').appendTo(overlayFooter),
		overlayFooterRight = $('<div/>').addClass('dialog-footer-right').appendTo(overlayFooter),
		overlayFooterBg = $('<div/>').addClass('dialog-footer-bg').appendTo(overlayFooter);
	dialog.appendTo(dialogContentInner);
	$(".dialog-height").css({height:(dialogHeight-parseInt(overlayHeaderBg.css('height'))-parseInt(overlayFooterBg.css('height')))+"px"});
	overlay.data("postload", function() {
		dialogBusy = true;
		$('#overlay #dialog').dialog( {
			draggable: false,
			modal:true,
			resizable:false,
			height:dialogHeight,
			width:dialogWidth,
			close:function(event,ui){dialogBusy = false;}
		}); 
	});
}

function showDialog(dialogId,dialogTitle,dialogWidth,dialogHeight){
	initDialog(dialogId,dialogTitle,dialogWidth,dialogHeight);
	callTargetPostload('#overlay');
}

function showContentInOverlay(contentId, cloned, dialogTitle,dialogWidth,dialogHeight) {
	var jc = $(contentId);
	if (!jc.length > 0) return;
	closeDialog();
	if (cloned) jc = jc.clone(true).attr('id',contentId.replace('#','cloned'));
	jc.appendTo("#overlay");
	showDialog('#'+jc.attr('id'),dialogTitle,dialogWidth,dialogHeight);	
}

function closeDialog() {
	//before opening again the overlay we need to clean it to avoid duplication
	$("#dialog").dialog("close");
	$("#overlay").empty();
	$('.ui-dialog').remove();
	$('#dialog').remove();
	dialogBusy = false;
}

// overall switch: set to true to disable overlay notifications
var disableNotifications = false;
var overlayNotificationQueue = [];
	
function queueOverlayNotification(notification) {
	if (disableNotifications || !notification) return;
	overlayNotificationQueue.push(notification);
}
function validateNotification(n) {
	return (n && n.url && n.cmty && n.uid);
}
function showOverlayNotification() {
	if (dialogBusy) return;
	var n = overlayNotificationQueue.shift();
	if (validateNotification(n)) {
		dialogBusy = true;
		var urlParams = $.extend({cmty:n.cmty,userId:n.uid},n.params);
		loadContent('#overlay',$.param.querystring(n.url,urlParams), false, false, true);
	}
}	

if (!disableNotifications) setInterval(showOverlayNotification,500);
	
(function($) {
	$.fn.overlayNotification = function(notificationList) {
		var $jqObj = this, $jqId = '#'+this.attr('id');
		this.addClass('refreshHidden');
		$.each(notificationList,function(nIdx){
			var n = notificationList[nIdx];
			if (n.eventName && n.url) {
				initListener($jqId, n.eventName, function(e,community,userId,eventParams){
					queueOverlayNotification({url:n.url,cmty:community,uid:userId,params:(eventParams.params)});
				});
			}
		});
		return this;
	};
})(jQuery);





