/**
 * Windows handling
 */
var __windows = new Array();
var __win_count = 0;
var __win_prefix = 'win_';
var __popup_width = 580;
var __popup_height = 420;
var __pic_title = 'vecriga.info';

/**
 * @param string Document URL
 * @param string Window target
 * @param int Window x coordinate
 * @param int Window y coordinate
 * @param int Window width
 * @param int Window height
 * @param bool
 * @param bool
 * @param bool
 * @param bool
 * @param bool
 * @param bool
 */
function openWindow(url, target, left, top, width, height, status, scrollbar, toolbar, menubar, location, resizable, directories, favorites, replace, fullscreen)
{
	var max_width, max_height = 0;
	if (screen.availWidth) {
		max_width = screen.availWidth;
		max_height = screen.availHeight;
	} else {
		max_width = screen.width;
		max_height = screen.height;
	}
	
	if (typeof(width) == 'string' && width.indexOf('%') != -1)
		width = max_width*(parseInt(width)/100);
	if (typeof(height) == 'string' && height.indexOf('%') != -1)
		height = max_height*(parseInt(height)/100);

	left = (left !== null ? left : (screen.availWidth  - width) / 2); 
	top = (top !== null ? top : (screen.availHeight - height) / 2);
	
	// close if open
	if (target !== null) {
		closeWindow(target);
	}
	else {
		target = __win_prefix + __win_count;
	}

	var args = "personalbar='no',locationbar='no',statusbar='no',status=" + status + ",scrollbars=" + scrollbar + ",toolbar=" + toolbar + ",location=" +location + ",menubar=" + menubar + ",resizable=" + resizable + ",left=" + left + ",top=" + top + ",width=" + width + ",height=" + height + ",directories=" + directories + ",favorites=" + favorites;
	var win = window.open(url, target, args, replace) ;
	
	if (!win.opener)
		win.opener = window;
	
	if (window.focus)
		win.focus();
	
	__windows[target] = win;
	
	__win_count++;
	
	return false;
}

/**
 * @param string
 */
function getWindow(target)
{
	if (!target)
		target = __win_prefix + (__win_count - 1);
	
	return __windows[target];
}

/**
 * @param string
 */
function closeWindow(target)
{
	var win = getWindow(target);
	
	if (win)
		win.close();
}

/**
 * @param string
 * @param string
 * @param int
 * @param int
 * @param bool
 * @param bool
 */
function openPopup(url, target, width, height, resizable, scrollbar)
{
	width = (width !== null ? width : __popup_width);
	height = (height !== null ? height : __popup_height);
	resizable = (resizable !== null ? resizable : 0);
	scrollbar = (scrollbar !== null ? scrollbar : 1);

	return openWindow(url, target, null, null, width, height, null, scrollbar, null, null, null, resizable);
}


/**
 * @param string
 */
function openFullScreenPopup(url, target, scrollbar)
{
	scrollbar = scrollbar || 'yes';
	return openWindow(url, target, 0, 0, '100%', '100%', 'no', scrollbar);	
}

/**
 * @param string
 * @param string
 * @param int
 * @param int
 */
function openPicturePopup(url, title, width, height)
{
	title = (title !== null ? title : __pic_title);
	
	openPopup(url, null, width, height);
	
	var popup = getWindow();
	popup.document.writeln('<!DOCTYPE HTML PUBLIC "-\/\/W3C\/\/DTD HTML 4.01 Transitional\/\/EN" "http:\/\/www.w3.org\/TR\/html4\/loose.dtd">');
	popup.document.writeln('<html>');
	popup.document.writeln('<head>');
	popup.document.writeln('<title>' + title + '<\/title>');
	popup.document.writeln('<style>');
	popup.document.writeln('body {');
	popup.document.writeln('margin:0;');
	popup.document.writeln('}');
	popup.document.writeln('<\/style>');
	popup.document.writeln('<\/head>');
	popup.document.writeln('<body topmargin="0" leftmargin="0">');
	popup.document.writeln('<a href="javascript:window.close();">');
	popup.document.writeln('<img src="' + url + '" border="0">');
	popup.document.writeln('</a>');
	popup.document.writeln('<\/body>');
	popup.document.writeln('<\/html>');
	
	return false;
}
