// escape: hotkey to close dialog
registerKey(27, 'dialogClose()');

function dialog(id, url)
{
	// clear things up and start loading the url
	logi('<p><small class="help">'+getTimestamp()+'</small> robin.dialog.js: <i>'+url+'</i></p>');
	$('#dialogContent').html('');
	$('#dialog').hide();
	//$('#dialog').css('visibility', 'hidden');
	//$('#dialog').draggable("enable");
	//$('#dialog').resizable("enable");
	$('#dialogContent').html(
		$.ajax
		(
			{
				url: url+"&ajax&html",
				async: false,
				dataType: "html",
				error: function(request, msg) { alert('dialog '+msg+': '+entities(request.responseText)); },
				success: function(request) { updateDialog(id); }
			}
		).responseText
	);
}

function updateDialog(id)
{
	$('#dialog').show();

	// proxy animation on/off
	var animation = false;
	var center = true;

	// position of clicked element
	if( typeof id == 'string' )
	{
		var t = $('#'+id);
		var offset = t.offset();
		var w = t.width();
		var h = t.height();
	}
	else
	{
		var t = id;
		var offset = rect(id);
		var w = offset.width;
		var h = offset.height;
	}
	
	if( !offset ) // check that this id really exists
	{
		alert('dialog.js::updateDialog() - id not found: '+id);
		return;
	}
	
	var top = offset.top;
	var left = offset.left;

	// put proxy div to it
	if( animation )
	{
		$('#dialogProxy').width( w );
		$('#dialogProxy').height( h );
		$('#dialogProxy').css('top', top+'px');
		$('#dialogProxy').css('left', left+'px');
		$('#dialogProxy').show();
	}

	if( center )
	{
        var pos = {
                sTop : window.pageYOffset || document.documentElement && document.documentElement.scrollTop ||	document.body.scrollTop,
                wHeight : window.innerHeight || document.documentElement && document.documentElement.clientHeight || document.body.clientHeight
              };
        $('#dialog').css('position', 'absolute' );
        $('#dialog').css('top', (pos.sTop + 120) + 'px' );
        $('#dialog').css('left', '20px' );
		// center dialog
		//$('#dialog').positionCenter();
		//$('#dialog').css('position', 'fixed');
		/*var winWidth = Math.round( $(window).width() / 2 );
		var winHeight = Math.round( $(window).height() / 2 );
		var leftcenter = Math.round( $('#dialog').width() / 2 );
		var topcenter = Math.round( $('#dialog').height() / 2 );
		$('#dialog').css('top', w+topcenter+'px');
		$('#dialog').css('left', h+leftcenter+'px');*/
	}
	else
	{
		// check that we are inside screen
		if( left + $('#dialog').width() > $(window).width() )
			left = $(window).width() - $('#dialog').width();
	
		if( top + $('#dialog').height() > $(window).height() )
			left = $(window).height() - $('#dialog').height();
	
		// align dialog to the screen already and save top/left position
		$('#dialog').css('top', top+'px');
		$('#dialog').css('left', left+'px');
	}

	// offset of dialog and centering
	var a = $('#dialog').offset();

	// set dialog more nicely on the center
	//$('#dialog').css('top', a.top - 200 + 'px');
	//$('#dialog').css('left', $(window).width() / 2 - $('#dialog').width() / 2 + 'px');

	if( animation )
	{
		// offset of dialog again with the changed values
		var a = $('#dialog').offset();

		// animate proxy
		$('#dialogProxy').attr('w', $('#dialog').width() - 80);
		$('#dialogProxy').attr('h', $('#dialog').height() - 80);
		$('#dialogProxy').attr('top', a.top + 40);
		$('#dialogProxy').attr('left', a.left + 40);

		$('#dialogProxy').animate
		(
			{
				top: $('#dialogProxy').attr('top'),
				left: $('#dialogProxy').attr('left'),
				width: $('#dialogProxy').attr('w'),
				height: $('#dialogProxy').attr('h')
			},
			300,
			function()
			{
				$('#dialogProxy').fadeOut(300);
				$('#dialog').css('visibility', 'visible');
				$('#dialog').hide();
				$('#dialog').fadeIn(300);
			}
		);
	}
	else
	{
		//$('#dialog').fadeIn(300);
		$('#dialog').css('visibility', 'visible');
		$('#dialog').show();
	}
}

// Close dialog (with hotkey or such)
$forceCloseDialog = false; // for universal use
function dialogClose()
{
	if
	(
	 	($('#dialog').css('display') != 'none' &&
		$('#tooltip').css('display') == 'none' &&
		$('#alertscreen').css('display') == 'none') ||
		$forceCloseDialog
	)
	{
		//$('#dialog').draggable("disable");
		//$('#dialog').resizable("disable");

		if( $('#xToolbar').html() && !gup('s') ) // if FCK was loaded, refresh whole page
		{
			window.location.reload();
		}
		else
		{
			//$('#dialog').hide();
			$('#dialog').fadeOut(300);
		}
	}
}

// close
$('#dialogClose').bind('click',	function() { dialogClose(); });

// draggable
$('#dialog').draggable({ handle: 'td.dialogTop' });
//$('#dialog').draggable("disable");

// resizable [DISABLED]
/*$('#dialog').resizable
(
	{
		minWidth:	350,
		minHeight:	100,
		maxWidth:	2000,
		maxHeight:	2000,
		handles:	{ se: '#dialogResize' }
	} 
);
$('#dialog').resizable("disable");*/
