var dialog_last_opened = false;
var dialog_zIndex = 3000;
var dialogStack = new Array();
var dialogOptions = {modal:true,toTop: true,overlayClass: 'whiteOverlay'};

function dialog_show(id,url,callback)
{
	$("*").not('.thisisabuttondisabler').each( function()
	{
		var zi = $(this).css("z-index");
		if( zi > dialog_zIndex )
				dialog_zIndex = parseInt(zi, 10);
	});
	dialog_zIndex = parseInt(dialog_zIndex) + 100;
//	Debug("dialog_show: " + id + " zIndex = "+dialog_zIndex);

    // save for later usage
    if(!dialogStack[id])
    {
        dialogStack[id] = true;
        dialogStack.length ++;
    }

// changed to wmode=opaque (see http://kb.adobe.com/selfservice/viewContent.do?externalId=tn_15523&sliceId=2)
//    $("object,embed").each( function()
//	{
//		$(this).parent().css("visibility", "hidden");
//	});

	if( url )
	{
		$.get(url,function(d)
		{
			$('#'+id+'_content').html(d);
			dialog_last_opened = $('#'+id).css({zIndex:dialog_zIndex}).prependTo('body').jqm(dialogOptions).jqmShow();
		});
	}
	else
		dialog_last_opened = $('#'+id).css({zIndex:dialog_zIndex}).prependTo('body').jqm(dialogOptions).jqmShow();

	if( callback )
		callback();
}

function dialog_hide(id)
{
    if(!id && dialog_last_opened[0])
        id = dialog_last_opened[0].id;
    if(dialogStack[id])
    {
        delete(dialogStack[id]);
        dialogStack.length --;
    }
//    Debug("dialog_hide: " + id + " stacklen: " + dialogStack.length);
    //Debug(dialog_hide.caller);
	if( id )
		$('#'+id).jqmHide();
	else if( dialog_last_opened )
		dialog_last_opened.jqmHide();
//    if(dialogStack.length == 0)
//    {
//        $("object,embed").each( function()
//        {
//            $(this).parent().css("visibility", "visible");
//        });
//    }
}

function Message(text,direkt)
{
	if( direkt )
	{
		try
		{
            msg = unescape(text); // + "<br><br><br><div align=center><button onclick='dialog_hide()'>Ok</button></div>";
			$('#global_alert_dlg_content').html(msg);
			dialog_show('global_alert_dlg');
		}
		catch(ex)
		{
			alert(unescape(text));
		}
	}
	else
		setTimeout("Message('"+escape(text)+"',true);",1);
}

function Confirm(text,okcallback)
{
	var result = confirm(unescape(text));
	if( result && okcallback )
	{
		if( typeof(okcallback) == "function" )
			okcallback();
		else
		{
			//Debug(unescape(okcallback));
			eval(unescape(okcallback));
		}
		return;
	}
	if( !okcallback )
		return result;
}

function ShowLoadingMessage(txt)
{
	if(!txt)
		txt = TEXT_LOADING_MESSAGE;
	var progress_dlg = waiting_dlg;
	//if(dlg_ajax_processing)
	$("#spWaitDlgMessage").html(txt);
	dialog_show(progress_dlg);
}

function HideLoadingMessage()
{
	var progress_dlg = waiting_dlg;
	dialog_hide(progress_dlg);
}