/* ============================================== *\
	Varia
	
	version:	1.2
	copyright:	(C) 2006-2007 Maciej Jaros
	license:	GNU General Public License v2,
				http://opensource.org/licenses/gpl-license.php
\* ============================================== */

/* ===================================================== *\
	winPop

	Simple window popup maker.
	DO NOT USE THIS IN HREF! HREF is for simple URL.
	Use this in onclick e.g. like this:
		onclick="winPop(this.href)"

	Params
	------
	 url - url to be opened in popuped up window
	 nazwa [optional] - a name of the window
\* ===================================================== */
function winPop(url, nazwa)
{
	if (!nazwa)	{nazwa = 'nowe_okienko'}
	window.open(url, nazwa, 'resizable=yes,scrollbars=yes');
}

/* ===================================================== *\
	maximizeBlock

	Maximizing width of some block element.

	Params
	------
	 element_id - text id of some block element 
		(like thingy in <div id="thingy">)
\* ===================================================== */
function maximizeBlock(element_id)
{
	var objBlock = document.getElementById(element_id);
	if (objBlock)
	{
		//
		// If tagged
		// 	then bring back saved width
		//
		if (objBlock.tag)
		{
			objBlock.tag = 0;
			objBlock.style.width = objBlock.defWidth + 'px';
		}
		//
		// If untagged
		// 	then tag, save width and set maximum width
		//
		else
		{
			objBlock.tag = 1;
			objBlock.defWidth = objBlock.clientWidth;

			var documentWidth;
			var objDocumentElem = document.documentElement;
			if (!objDocumentElem)
			{
				objDocumentElem = document.body;
			}
			documentWidth = objDocumentElem.offsetWidth - 2;

			objBlock.style.width = documentWidth + 'px';
		}
	}
	return false;
}

/* ===================================================== *\
	minimizeBlock

	Minimizing some block element. This includes hidding
	all child elements so to preserve some (like the bring back
	element) tag it with class="preserve_me"

	Params
	------
	 element_id - text id of some block element 
		(like thingy in <div id="thingy">)
\* ===================================================== */
function minimizeBlock(element_id)
{
	var objBlock = document.getElementById(element_id);
	if (objBlock)
	{
		//
		// If tagged by minimize
		// 	then bring back saved width and hidden elements
		//
		if (objBlock.tag==2)
		{
			objBlock.tag = 0;
			objBlock.style.width = objBlock.defWidth + 'px';
			
			for (var i=objBlock.childNodes.length-1; i>=0; i--)
			{
				if (objBlock.childNodes.item(i).nodeType == 1 && objBlock.childNodes.item(i).className!='preserve_me')
				{
					if (objBlock.childNodes.item(i).nodeName=='DIV')
					{
						objBlock.childNodes.item(i).style.display = 'block';
					}
					else
					{
						objBlock.childNodes.item(i).style.display = 'inline';
					}
				}
			}
		}
		//
		// If untagged
		// 	then save width, tag and do the rest
		// If tagged by maximize
		// 	then just tag and hide child elements 
		// 	which class is different then preserve_me
		//
		else
		{
			if (!objBlock.tag)
			{
				objBlock.defWidth = objBlock.clientWidth;
			}
			objBlock.tag = 2;
			for (var i=objBlock.childNodes.length-1; i>=0; i--)
			{
				if (objBlock.childNodes.item(i).nodeType == 1 && objBlock.childNodes.item(i).className!='preserve_me')
				{
					objBlock.childNodes.item(i).style.display = 'none';
				}
			}
			
			objBlock.style.width = 16 + 'px';
		}
	}
	return false;
}

/* ===================================================== *\
	addOnLoadFun

	Adding some extra onLoad function (without overwriting some 
	already set).

	Params
	------
	 fun - the name of the function (not as a string!)
		function example_gratia() {return;}
		addOnLoadFun(example_gratia);
\* ===================================================== */
function addOnLoadFun(fun)
{
	if (window.attachEvent)
	{
		window.attachEvent ("onload", fun)
	}
	else
	{
		window.addEventListener ("load", fun, false)
	}
}

/* ===================================================== *\
	dbgJS

	Debuging of JS by displaying some text.

	Params
	------
	 txt - some text to display
\* ===================================================== */
function dbgJS(txt)
{
	document.getElementById('JSdebug').innerHTML += txt;
	return false;
}

/* ===================================================== *\
	dbgJSinit
	
	Initialization of debuging.
\* ===================================================== */
addOnLoadFun(dbgJSinit);
function dbgJSinit()
{
	if (document.getElementById('debug_msgtext'))
	{
		document.getElementById('debug_msgtext').innerHTML += '<div id="JSdebug"></div>';
	}
}
