$(document).ready( function() {
	$('#dialog').dialog({
		autoOpen: false,
		position: ['left', 'top'],
		width: 750,
		height: 700,
		zIndex: 100,
		stack: false,
		open: function(event, ui){
			$('#dialog .content').css('height', $('#dialog').height() - $('#dialog .dialog_title').outerHeight());
			$('#dialog .close_img').css('left', $('#dialog').width() - 23);
		}
	});
	$('.ui-dialog-titlebar').hide();
});

function setTitle(html)
{
	$('#dialog .dialog_title').html(html);
}

function closeDialog(dialog)
{
	if(typeof(dialog) == 'undefined')
	{
		dialog = 'dialog';
	}
	dialog = '#' + dialog;
	$(dialog).trigger('dialog_closed');
	$(dialog).dialog('close');
	$(dialog + ' .content').html('');
	window.onDialogOpen = undefined;
}

function positionDialog(dialogId, left, top, width)
{
	dialogId = '#' + dialogId;
	if(left == 'flash_center')
	{
		left = (999 - $(dialogId).dialog('option', 'width')) / 2;
	}
	if(top == 'flash_center')
	{
		top = (645 - $(dialogId).dialog('option', 'height')) / 2;
	}
	$(dialogId).dialog('option', 'position', [left, top]);
}

var isReplaceMode = false;
var isWBVisible   = false;
var wbPX = 0;
var wbPY = 0;

function showDialog(dialog, left, top, width, height, params)
{
	isReplaceMode = true;
	closeDialog();
	isReplaceMode = false;

	$('#dialog .content').css('background-color', '#FAFBC3');

	width  = (isNaN(width) || width <= 0)?'auto': width;
	height = (isNaN(height) || height <= 0)?'auto': height;
	$('#dialog').dialog('option', 'width', width);
	$('#dialog').dialog('option', 'height', height);

	positionDialog('dialog', left, top);

	$('#dialog .content').load
	(
		'/bbtw/dialog.php?req=show_dialog&dialog=' + dialog,
		params,
		function()
		{
			$('#dialog').dialog('open');
			if(typeof(window.onDialogOpen) == 'function')
			{
				window.onDialogOpen();
			}
			//$('.ui-dialog').show('scale', {percent: 100}, 2000, function(){$('#dialog .content').css('height', $('#dialog').height() - $('#dialog #dialog_title').outerHeight());});
//			$('#dialog .content').css('height', $('#dialog').height() - $('#dialog .dialog_title').outerHeight());
//			$('#dialog .close_img').css('left', $('#dialog').width() - 8);
		}
	);
}

function wwMessage(left, top, notification)
{
	var dlg = new Dialog('wild_wire_msg', 725, 128, 230, 170, 200, 'background-color:#FAFBC3;padding:5px;');
	dlg.open('wild_wire', {'show': 'message', 'notification': notification});
}

function wwUpdates(left, top)
{
	showDialog('wild_wire', left, top, 240, 280, {'show': 'updates'});
}

function Dialog(dialogId, left, top, width, height, zindex, css)
{
	this.dialogId = dialogId;
	var selector = this.selector = '#' + dialogId;

	if($(this.selector).length <= 0)
	{
		$("body").append("<div id='" + dialogId + "' style='display:none;font-size:12px;" + css +"'></div>");
		init();
		positionDialog(dialogId, left, top);
	}

	this.setTitle  = setTitle;
	this.open      = open;
	this.close     = close;
	this.init      = init;

	function init()
	{
		$(selector).dialog({
			autoOpen: false,
			width: width,
			height: height,
			zIndex: zindex,
			stack: true,
			open: function(event, ui) {
				$(selector + ' .close_img').css('left', $(selector).innerWidth() - 22);
			},
			close: function(event, ui) {
				$(this).remove();
			}
		});
		$('.ui-dialog-titlebar').hide();
		$(selector).empty().append('<img class="close_img" src="/bbtw/images/close.png" style="z-index:100;position:absolute;cursor:pointer;" width="20"/><div class="dialog_title"></div><div class="content" style="overflow:auto;position:relative;"></div>');
		$(selector + ' .close_img').click(close);
	}

	function setTitle(html)
	{
		$(this.selector + ' .dialog_title').html(html);
	}

	function open(dialog, params)
	{
		$(this.selector + ' .content').load(
			'/bbtw/dialog.php?req=show_dialog&dialog=' + dialog,
			params,
			function(){
					$('#' + dialogId).dialog('open');
					//$('.ui-dialog').show('scale', {percent: 100}, 2000, function(){$('#dialog .content').css('height', $('#dialog').height() - $('#dialog #dialog_title').outerHeight());});
	//				$('#dialog .content').css('height', $('#dialog').height() - $('#dialog .dialog_title').outerHeight());
	//				$('#dialog .close_img').css('left', $('#dialog').width() - 8);
			}
		);
	}

	function close()
	{
		$(selector).dialog('close');
	}
}

