/**
 * Short description for file.
 *
 * Long description for file.
 *
 * JS version $version
 *
 * @license     http://www.opensource.org/licenses/mit-license.php The MIT License
 * @author      Remo Häusler <remo.haeusler@hotmail.com>
 * @version     0.0.1
 */

// {{{ popWin

/**
 * $description
 */
var POPUP_WINDOWS = {}

/**
 * $description
 *
 * @param string
 * @param string
 * @param object
 */
window.popWin = function (url, title, para)
{
	if (POPUP_WINDOWS[title]){
		if (!POPUP_WINDOWS[title].closed) POPUP_WINDOWS[title].close();
		delete POPUP_WINDOWS[title];
	}
	if (para == null) para = {};
	var width = (para.width != null) ? para.width: getOuterWidth();
	var height = (para.height != null) ? para.height: getOuterHeight();
	
	if (para.left != null){
		switch (para.left.charAt(0).toUpperCase()){
		case "L": para.left = 0; break;
		case "C": para.left = (screen.availWidth-width)/2; break;
		case "R": para.left = (screen.availWidth-width); break;
		}
	}
	if (para.top != null){
		switch (para.top.charAt(0).toUpperCase()){
		case "T": para.top = 0; break;
		case "M": para.top = (screen.availHeight-height)/2; break;
		case "B": para.top = (screen.availHeight-height); break;
		}
	}
	var tmp = [];
	for (var i in para){
		if (para[i] !== null) tmp.push(i+"="+para[i]);
	}
	
	POPUP_WINDOWS[title] = window.open(url, title, tmp.join(", "));
	POPUP_WINDOWS[title].focus();
	//return POPUP_WINDOWS[title];
}

// }}}
// {{{ popImg

/**
 * $description
 * @param string
 * @param int
 * @param int
 */
window.popImg = function (source, width, height)
{
	var para = {left:"c", top:"m"};
	if (width != null) para.width = width;
	if (height != null) para.height = height;
	window.popWin("", "POPIMG", para);
	
	with(POPUP_WINDOWS['POPIMG'].document){
		writeln('<html>');
		writeln('<head>');
		writeln('<title>'+source+'</title>');
		writeln('</head>');
		writeln('<body style="margin:0px;" onclick="window.close()">');
		writeln('<img src="'+source+'" width="100%" height="100%">');
		writeln('</body>');
		writeln('</html>');
		close();
	}
}

// }}}
// {{{ Window properties

window.getOuterWidth = function (target)
{
	if (!target) target = window;
	if (target.outerWidth) return target.outerWidth;
	if (target.document.body.offsetWidth) return target.document.body.offsetWidth;
	return null;
}

window.getOuterHeight = function (target)
{
	if (!target) target = window;
	if (target.outerHeight) return target.outerHeight;
	if (target.document.body.offsetHeight) return target.document.body.offsetHeight;
	return null;
}

window.getInnerWidth = function (target)
{
	if (!target) target = window;
	if (target.innerHeight) return target.innerWidth;
	if (target.document.documentElement){
		if (target.document.documentElement.clientWidth) return target.document.documentElement.clientWidth;
	}
	if (target.document.body.offsetWidth) return target.document.body.offsetWidth;
	return null;
}

window.getInnerHeight = function (target)
{
	if (!target) target = window;
	if (target.innerHeight) return target.innerHeight;
	if (target.document.documentElement){
		if (target.document.documentElement.clientHeight) return target.document.documentElement.clientHeight;
	}
	if (target.document.body.offsetHeight) return target.document.body.offsetHeight;
	return null;
}

// }}}
