/**
 * Window for jQuery
 */

/**
 *	<table id="w1" class="wndActive" cellpadding="0" cellspacing="0">
 *
 *		<tr class="wndTitleRow">
 *			<td class="wndHeaderLeft" nowrap="nowrap" />
 *
 *			<td unselectable="on" grip="true" titlegrip="show" style="height: 3px;" class="wndHeaderCenter" nowrap="true" valign="top" width="100%">
 *				<div class="wndHeaderTop"></div>
 *
 *				<table border="0" cellpadding="0" cellspacing="0" width="100%">
 *					<tr>
 *						<td class="wndHeaderCenter">
 *							<img class="wndIcon" src="images/wnd-default-icon.png" align="absmiddle">
 *						</td>
 *
 *						<td class="wndHeaderCenter" nowrap="nowrap" width="100%">
 *							<span class="wndTitle"></span>
 *						</td>
 *
 *						<td title="Pin Off" class="wndHeaderCenter" nowrap="nowrap">
 *							<img class="wndPinOffButton" src="images/wnd-pin-off.png">
 *						</td>
 *
 *						<td class="wndHeaderCenter" nowrap="nowrap">
 *							<img class="wndMinimizeButton" src="images/wnd-minimize.png" title="Minimize">
 *						</td>
 *
 *						<td class="wndHeaderCenter" nowrap="nowrap">
 *							<img class="wndMaximizeButton" src="images/wnd-maximize.png" title="Maximize">
 *						</td>
 *
 *						<td class="wndHeaderCenter" title="Close" nowrap="nowrap">
 *							<img class="wndCloseButton" src="images/wnd-close.png" style="margin-left: 2px;">
 *						</td>
 *					</tr>
 *				</table>
 *			</td>
 *
 *			<td class="wndHeaderRight" nowrap="nowrap" />
 *		</tr>
 *
 *		<tr style="height: 100%;" height="100%">
 *			<td colspan="8" style="width: 100%; height: 100%;" align="left">
 *
 *				<table style="width: 100%; height: 100%;" cellpadding="0" cellspacing="0">
 *
 *					<tr style="height: 100%;" height="100%">
 *						<td class="wndBodyLeft" rowspan="2" nowrap="nowrap" />
 *						<td class="wndBodyCenter" />
 *						<td class="wndBodyRight" rowspan="2" nowrap="nowrap" />
 *					</tr>
 *
 *					<tr>
 *						<td class="wndStatusRow"><div><img src="images/wnd-footer-resize.png" class="wndResize" /></div></td>
 *					</tr>
 *
 *				</table>
 *
 *			</td>
 *		</tr>
 *
 *		<tr>
 *			<td colspan="8" height="1" width="100%">
 *				<table border="0" cellpadding="0" cellspacing="0" height="1" width="100%">
 *					<tr>
 *						<td class="wndFooterLeft" nowrap="nowrap" />
 *						<td class="wndFooterCenter" nowrap="nowrap" />
 *						<td class="wndFooterRight" nowrap="nowrap" />
 *					</tr>
 *				</table>
 *			</td>
 *		</tr>
 *
 *	</table>
 */
jQuery.window = function(options)
{
	var cfg = {
		id: options.id || ('wnd' + parseInt(Math.random() * 10000)),
		title: options.title || '',
		text: options.text || null,
		html: options.html || null,
		resizable: (typeof(options.resizable) == 'boolean' ? options.resizable : true),
		style: options.style || null,
		width: options.width || '400px',
		height: options.height || '320px',
		visible: typeof(options.visible) == 'boolean' && !options.visible ? false : true,
		position: options.position || 'default',
		popupFrom: options.popupFrom,
		frameName: options.frameName || null,
		frameSrc: options.frameSrc || null,
		frameSubmit: options.frameSubmit || null,
		frameCancel: options.frameCancel || null,
		frameAjaxResponse: typeof(options.frameAjaxResponse) == 'function' ? options.frameAjaxResponse : null
	};

	var xhtml = '<table id="' + cfg.id + '" class="wndActive" style="background-color: #ffffff; z-index: 3005; width: ' + cfg.width + '; height: ' + cfg.height + '; position: absolute; left: 10px; top: 10px; display: none;' + (cfg.style ? cfg.style : '') + '" cellpadding="0" cellspacing="0"><tr class="wndTitleRow"><td class="wndHeaderLeft" nowrap="nowrap" /><td unselectable="on" grip="true" titlegrip="show" style="height: 3px;" class="wndHeaderCenter" nowrap="true" valign="top" width="100%"><div class="wndHeaderTop"></div>'
	+ '<table border="0" cellpadding="0" cellspacing="0" width="100%"><tr><td class="wndHeaderCenter"><img class="wndIcon" src="images/wnd-default-icon.gif" align="absmiddle"></td>'
	+ '<td class="wndHeaderCenter" nowrap="nowrap" width="100%"><span class="wndTitle">' + cfg.title + '</span></td>'
	//+ '<td title="Pin Off" class="wndHeaderCenter" nowrap="nowrap"><img class="wndPinOffButton" src="images/wnd-pin-off.png"></td>'
	+ '<td class="wndHeaderCenter" nowrap="nowrap"><img class="wndMinimizeButton" src="images/wnd-minimize.png" title="Minimize"></td>'
	+ '<td class="wndHeaderCenter" nowrap="nowrap"><img class="wndMaximizeButton" src="images/wnd-maximize.png" title="Maximize"></td>'
	+ '<td class="wndHeaderCenter" title="Close" nowrap="nowrap"><img class="wndCloseButton" src="images/wnd-close.png" style="margin-left: 2px;"></td></tr></table></td>'
	+ '<td class="wndHeaderRight" nowrap="nowrap" /></tr><tr style="height: 100%;" height="100%"><td colspan="8" style="width: 100%; height: 100%;" align="left">'
	+ '<table style="width: 100%; height: 100%;" cellpadding="0" cellspacing="0" border="0"><tr style="height: 100%;" height="100%"><td class="wndBodyLeft" rowspan="2" nowrap="nowrap" /><td class="wndBodyCenter" /><td class="wndBodyRight" rowspan="2" nowrap="nowrap" /></tr>'
	+ '<tr><td class="wndStatusRow"><div><img src="images/wnd-footer-resize.gif" class="wndResize" /></div></td></tr></table>'
	+ '</td></tr><tr><td colspan="8" height="1" width="100%">'
	+ '<table border="0" cellpadding="0" cellspacing="0" height="1" width="100%"><tr><td class="wndFooterLeft" nowrap="nowrap" /><td class="wndFooterCenter" nowrap="nowrap" /><td class="wndFooterRight" nowrap="nowrap" /></tr></table>'
	+ '</td></tr></table>';

	$(xhtml).prependTo('body');
	$('body').focus();


	var $wnd = $('#' + cfg.id);


	// --------------------------------------------------------------------------
	// Button events
	// --------------------------------------------------------------------------
	$('#' + cfg.id + ' .wndMinimizeButton').bind('mousedown', function(){
		if(this.src.indexOf('minimize') > 0)
			this.src = this.src.replace('minimize', 'minimizing');
	});

	$('#' + cfg.id + ' .wndMinimizeButton').bind('mouseup', function(){
		if(this.src.indexOf('minimizing') == -1) return;

		this.src = this.src.replace('minimizing', 'minimize');
	});

	$('#' + cfg.id + ' .wndMaximizeButton').bind('mousedown', function(){
		if(this.src.indexOf('maximized') > 0)
			this.src = this.src.replace('maximized', 'demaximizing');
		else if(this.src.indexOf('maximize') > 0)
			this.src = this.src.replace('maximize', 'maximizing');
	});

	$('#' + cfg.id + ' .wndMaximizeButton').bind('mouseup', function(){
		$wnd.hide();

		if(this.src.indexOf('demaximizing') > 0)
		{
			this.src = this.src.replace('demaximizing', 'maximize');
			$wnd.css({ 'top' : this.xtop, 'left' : this.xleft, 'width' : this.xwidth, 'height' : this.xheight });
			$wnd.show();
/*
			$wnd.TransferTo({ to: $wnd[0], className: 'transferer2', duration: 100, complete: function(){
					$wnd.show();
				}
			});*/
		}
		else if(this.src.indexOf('maximizing') > 0)
		{
			this.src = this.src.replace('maximizing', 'maximized');
			this.xtop = $wnd.css('top');
			this.xleft = $wnd.css('left');
			this.xwidth = $wnd.css('width');
			this.xheight = $wnd.css('height');
			$wnd.css({ 'top' : 0, 'left' : 0, 'width' : document.body.offsetWidth, 'height' : document.body.offsetHeight });
			$wnd.show();
/*
			$wnd.TransferTo({ to: $('body')[0], className: 'transferer2', duration: 100, complete: function(){
					$wnd.show();
				}
			});*/
		}
	});

	$('#' + cfg.id + ' .wndCloseButton').bind('mousedown', function(){
		if(this.src.indexOf('close') > 0)
			this.src = this.src.replace('close', 'closing');
	});

	$('#' + cfg.id + ' .wndCloseButton').bind('mouseup', function(){
		if(this.src.indexOf('closing') == -1) return;

		this.src = this.src.replace('closing', 'close');
		$wnd.remove();
		$('body').focus();
	});

	// --------------------------------------------------------------------------
	// Move & resize
	// --------------------------------------------------------------------------
	if(cfg.resizable)
	{
		$wnd.Resizable({
			dragHandle: '#' + cfg.id + ' .wndTitle',
			handlers: {
				se:	'#' + cfg.id + ' .wndResize',
				w:		'#' + cfg.id + ' .wndBodyLeft',
				e:		'#' + cfg.id + ' .wndBodyRight',
				ne:	'#' + cfg.id + ' .wndHeaderRight',
				n:		'#' + cfg.id + ' .wndHeaderTop',
				sw:	'#' + cfg.id + ' .wndFooterLeft',
				s:		'#' + cfg.id + ' .wndFooterCenter'
			}
		}); // Resizable
	}
	else
	{
		$wnd.Resizable({ dragHandle: '#' + cfg.id + ' .wndTitle', handlers: {} });
	}

	// --------------------------------------------------------------------------
	//
	// --------------------------------------------------------------------------

	// --------------------------------------------------------------------------
	// Data
	// --------------------------------------------------------------------------
	if(cfg.text != null)
	{
		$('#' + cfg.id + ' .wndBodyCenter').text(cfg.text);
	}
	else if(cfg.html != null)
	{
		$('#' + cfg.id + ' .wndBodyCenter').html(cfg.html);
	}
	else if(cfg.frameSrc != null)
	{
		if(!cfg.frameName) cfg.frameName = cfg.id + '_frame';

		$('#' + cfg.id + ' .wndBodyCenter').html('<iframe name="' + cfg.frameName + '" src="' + cfg.frameSrc + '" style="width: 100%; height: 100%;" />');
	}

	// --------------------------------------------------------------------------
	// Show
	// --------------------------------------------------------------------------
	if(!cfg.visible) return;

	var afterShow = function(){
		if(!cfg.frameSrc) return;
/*
		if($('body', frames[cfg.frameName].document).length == 0)
		{
			window.setTimeout(function(){ afterShow(); }, 400);
			return;
		}
*/
		if(cfg.frameSubmit && $(cfg.frameSubmit, frames[cfg.frameName].document).length == 0)
		{
			window.setTimeout(function(){ afterShow(); }, 400);
			return;
		}
/*
		if($('title', frames[cfg.frameName].document).length == 1)
			$('#' + cfg.id + ' .wndTitle').text($('title', frames[cfg.frameName].document).html());
*/
		if(cfg.frameSubmit)
			$(cfg.frameSubmit, frames[cfg.frameName].document).click(function(){
				var serialized = '';
				var $s_input = $('form input', frames[cfg.frameName].document);
				var $s_select = $('form select', frames[cfg.frameName].document);

				if($s_input.length > 0)
				{
					serialized += $s_input.serialize();
				}

				if($s_select.length > 0)
				{
					if(serialized.length > 0) serialized += '&';
					serialized += $s_select.serialize();
				}

				//$.get(frames[cfg.frameName].document.location.href, serialized, function(data, status){
				//$.post($('form', frames[cfg.frameName].document).attr('action'), serialized, function(data, textStatus){
				$.post(cfg.frameSrc, serialized, function(data, textStatus){
					if(cfg.frameAjaxResponse) cfg.frameAjaxResponse(data, textStatus);
				});
			});

		if(cfg.frameCancel)
			$(cfg.frameCancel, frames[cfg.frameName].document).click(function(){
				$wnd.remove();
				$('body').focus();
			});
	};

	$wnd.css({ 'top' : (document.body.offsetHeight - $wnd.height()) / 2, 'left' : (document.body.offsetWidth - $wnd.width()) / 2 });

	if(!cfg.popupFrom)
	{
		$wnd.show();
		afterShow();
		$wnd.focus();

		return;
	}

	$(cfg.popupFrom).TransferTo({
		to: cfg.id,
		className: 'transferer2',
		duration: 100,
		complete: function(){
			if(cfg.position == 'centered')
				$wnd.css({ 'top' : (document.body.offsetHeight - $wnd.height()) / 2, 'left' : (document.body.offsetWidth - $wnd.width()) / 2 });

			$wnd.show();
			afterShow();
			$wnd.focus();
		}
	});
};

