
var XB_VER = "0.1";							// Cross-browser fuggvenygyujtemeny verziószáma

function getXBVer()
{
}

function toAbsXPos(x)
{
    if( document.documentElement ) x = x - 2;
    x = x + getScrollLeft();

    return x;
}

function toAbsYPos(y)
{
    if( document.documentElement ) y = y - 2;
    y = y + getScrollTop();

    return y;
}

function pointInRect(point,rect)
{
    return ( (point.x >= rect.left) && (point.x <= rect.right) && (point.y >= rect.top) && (point.y <= rect.bottom) );
}

function isIntersect(rect1,rect2)
{
    return (
                pointInRect( {"x":rect1.left, "y":rect1.top}, rect2 ) ||
                pointInRect( {"x":rect1.right,"y":rect1.top}, rect2 ) ||
                pointInRect( {"x":rect1.left, "y":rect1.bottom}, rect2 ) ||
                pointInRect( {"x":rect1.right,"y":rect1.bottom}, rect2 ) ||

                pointInRect( {"x":rect2.left, "y":rect2.top}, rect1 ) ||
                pointInRect( {"x":rect2.right,"y":rect2.top}, rect1 ) ||
                pointInRect( {"x":rect2.left, "y":rect2.bottom}, rect1 ) ||
                pointInRect( {"x":rect2.right,"y":rect2.bottom}, rect1 )
            );
}

function isEqualRect(rect1,rect2)
{
	return (
				(rect1.left == rect2.left) &&
				(rect1.top == rect2.top) &&
				(rect1.right == rect2.right) &&
				(rect1.bottom == rect2.bottom)
			);
}

function getScrollLeft()
{
    if (self.pageXOffset) // all except Explorer
    	return self.pageXOffset;
    if (document.documentElement && document.documentElement.scrollLeft)
    	return document.documentElement.scrollLeft;
    if (document.body) // all other Explorers
        return document.body.scrollLeft;

    return 0;
}

function getScrollTop()
{
    if (self.pageYOffset) // all except Explorer
    	return self.pageYOffset;
    if (document.documentElement && document.documentElement.scrollTop)
    	return document.documentElement.scrollTop;
    if (document.body) // all other Explorers
    	return document.body.scrollTop;

    return 0;
}

function getBlockLeft(node)
{
	var bcr = node.getBoundingClientRect();
	return toAbsXPos(bcr.left);
}

function getBlockTop(node)
{
	var bcr = node.getBoundingClientRect();
	return toAbsYPos(bcr.top);
}

function getBlockWidth(node)
{
    return node.offsetWidth;
}

function getBlockHeight(node)
{
	var bcr = node.getBoundingClientRect();
	return (bcr.bottom - bcr.top);
}

function getBlockRect(node)
{
   var result = {
                    "left"   : getBlockLeft(node),
                    "top"    : getBlockTop(node),
                    "right"  : getBlockLeft(node) + getBlockWidth(node),
                    "bottom" : getBlockTop(node) + getBlockHeight(node)
                 };

    return result;
}


function setBlockLeft(node,left)
{
    node.style.left = left + "px";
}

function setBlockTop(node,top)
{
    node.style.top = top + "px";
}


function setBlockWidth(node,width)
{
    var temp = node.offsetWidth - node.clientWidth;
    temp += intVal(node.currentStyle.paddingLeft);
    temp += intVal(node.currentStyle.paddingRight);
	var w = width - temp; if( w < 0 ) w = 0;
	node.style.width = w + "px";
}

function setBlockHeight(node,height)
{
    var temp = node.offsetHeight - node.clientHeight;
    temp += intVal(node.currentStyle.paddingTop);
    temp += intVal(node.currentStyle.paddingBottom);
	var h = height - temp; if( h < 0 ) h = 0;
    node.style.height = h + "px";
}

function showBlock(node,bShow)
{
	node.style.display = (bShow) ? 'block' : 'none';
	node.style.visibility = (bShow) ? 'visible' : 'hidden';
}

function displayBlock(node,bShow)
{
	node.style.visibility = (bShow) ? 'visible' : 'hidden';
}

function isBlockVisible(node)
{
	return ( (node.style.visibility != "hidden") && (node.style.display != "none") )
}

function setBlockHTML(node,html)
{
	node.innerHMTL = html;
}

function getScrollLeft()
{
    if (self.pageXOffset) // all except Explorer
    	return self.pageXOffset;
    if (document.documentElement && document.documentElement.scrollLeft)
    	return document.documentElement.scrollLeft;
    if (document.body) // all other Explorers
        return document.body.scrollLeft;

    return 0;
}

function getWindowWidth()
{
    if (self.innerHeight)
        return x = self.innerWidth;
    if (document.documentElement && document.documentElement.clientHeight)
	    return document.documentElement.clientWidth-4;
    if (document.body)
        return document.body.clientWidth;

    return 0;
}

function getWindowHeight()
{
    if (self.innerHeight)
	    return self.innerHeight;
    if (document.documentElement && document.documentElement.clientHeight)
	    return document.documentElement.clientHeight;
    if (document.body)
	    return document.body.clientHeight;

    return 0;
}

function getScrollTop()
{
    if (self.pageYOffset) // all except Explorer
    	return self.pageYOffset;
    if (document.documentElement && document.documentElement.scrollTop)
    	return document.documentElement.scrollTop;
    if (document.body) // all other Explorers
    	return document.body.scrollTop;

    return 0;
}

function intVal(val)
{
    var result = parseInt(val);
    if( isNaN(result) )
        return 0;

    return result;
}

function addEvent(target,eventName,handlerObj,handlerName)
{
    if( handlerObj )
        target.attachEvent("on" + eventName, function(e){return handlerObj[handlerName](e);});
    else
        target.attachEvent("on" + eventName, handlerName);
}

function getEventProp(evt,prop)
{
    return evt[prop];
}

function loadJavascript(src)
{
	fileref = document.createElement('script')
	fileref.setAttribute("type","text/javascript");
	fileref.setAttribute("src", src);
	document.getElementsByTagName("head").item(0).appendChild(fileref)
}

function loadCSS(src)
{
	fileref = document.createElement('link')
	fileref.setAttribute("rel", "stylesheet");
	fileref.setAttribute("type", "text/css");
	fileref.setAttribute("href", src);
	document.getElementsByTagName("head").item(0).appendChild(fileref)
}

//  param:
//	 to_name
//	 to_email
//   subject
//   body
//	 cc
//	 bcc
function composeEmailTo(param)
{
	try
	{
		var opt_params = ['subject','body','cc','bcc'];

		if( param.to_name )
			var res = "mailto:" + param.to_name + "<"+param.to_email+">";
		else
			var res = "mailto:" + param.to_email;

		var res_params = "";

		if( param['body'] )
			param['body'] = param['body'].replace(/\r/g,"").replace(/\n/g,"\x0d\x0a");

		for(var i = 0; i < opt_params.length; i++)
			if( param[opt_params[i]] != undefined )
				res_params += opt_params[i] + "=" + encodeURIComponent(param[opt_params[i]]) + "&";

		if( res_params != "" )
			res = res + "?" + res_params;

		return res;
	} catch (e) {alert("composeEmailTo ["+e+"]")};
}

// paramStr:
//  left
//  top
//  center [yes/no]

//  fullscreen [yes/no]
//  location [yes/no]
//  menubar [yes/no]
//  resizable [yes/no]
//  scrollbars [yes/no]
//  status [yes/no]
//  titlebar [yes/no]
//  toolbar [yes/no]
function openPopupPage(url,wndName,width,height,paramStr, returnWithInstane)
{
	var res 	= [];
	var left	= 10;
	var top		= 10;
	var sb		= false;
	var mb		= false;
	var op = (navigator.userAgent.search("Opera")!=-1);
	var ms = (navigator.userAgent.search("MSIE")!=-1) && (!op);

	var	param = paramStr.split(";");
	for(var i in param)
	{
		var nam = param[i].split(":")[0].toLowerCase();
		var val = param[i].split(":")[1];

		switch( nam )
		{
			case "fullscreen" 	: res.push("fullscreen="+val);	break;
			case "location" 	: res.push("location="+val);	break;
			case "menubar" 		: res.push("menubar="+val);
								  mb=(val == 'yes');			break;
			case "resizable" 	: res.push("resizable="+val);	break;
			case "scrollbars" 	: res.push("scrollbars="+val);
								  sb=(val == 'yes');			break;
			case "status" 		: res.push("status="+val);		break;
			case "titlebar" 	: res.push("titlebar="+val);	break;
			case "status" 		: res.push("status="+val);		break;
			case "toolbar" 		: res.push("toolbar="+val);		break;
			case "left"			: left	= val; break;
			case "top"			: top 	= val; break;
			case "center"		: if( (val == 'yes') && op )
								  {
								  	left= Math.round((screen.availWidth - width)/2);
									top	= Math.round((screen.availHeight - height)/2);
								  }
								  else if( val == 'yes' )
					 			  {
									left= Math.round((screen.width - width)/2);
									top	= Math.round((screen.height - height)/2);
								  }
								  break;
		}
	}

	if( ms )
	{
		width	+= (sb) ? 13 : -4;
		height	+= (sb) ? 0 : -4;
		if( mb ) height -= 20;
	}

	res.push("width="+width);
	res.push("height="+height);
	res.push("left="+left);
	res.push("top="+top);

	var win = window.open(url, wndName, res.join(","));
	win.focus();

	if( returnWithInstane )
		return win;
}

function enlargeImageEx(url)
{
		try{ appImgZoomWnd.close() } catch(e) {};

		appImgZoomWnd = openPopupPage('','IMGZOOM',195,150,"center:yes", true);

		var src = "";
		src += '<html><title>Kép nagyítva</title>';
		src += '<head>';
		src += ' <script type="text/javascript">';
		src += ' var NS = (navigator.appName=="Netscape")?true:false;';
		src += ' function init() {';
		src += ' iWidth = (NS)?window.innerWidth:document.body.clientWidth;';
		src += ' iHeight = (NS)?window.innerHeight:document.body.clientHeight;';
		src += '  var img=document.images[0];';
		src += '  if( img && img.width && img.height ) {';
		src += '	var dw=img.width-iWidth; var dh=img.height-iHeight;';
		src += '	window.moveBy(-dw/2,-dh/2);';
		src += '	window.resizeBy(dw,dh);';
		src += '  } else setTimeout("init()",50);';
		src += ' }';
		src += ' </'+'script>';
		src += '</head>';
		src += '<body onload="init()" style="margin:0px;padding: 0px;overflow:scroll;text-align:center;"><img onclick="window.close()" title="bezár" src="'+url+'" border="0" style="cursor:pointer;margin:0px;padding:0px;"></body>';
		src += '</html>';
		appImgZoomWnd.document.open("text/html", "replace");
		appImgZoomWnd.document.write(src);
		appImgZoomWnd.document.close();
}

function scaleDim(width,height,maxWidth,maxHeight)
{
	tempWidth = width;
	tempHeight = height;
	tempRatio = tempWidth / tempHeight;

	if( (tempWidth > maxWidth) || (tempHeight > maxHeight) )
	{
		if(tempWidth > maxWidth)
		{
			tempWidth = maxWidth;
			tempHeight = tempWidth / tempRatio;
		}

		if(tempHeight > maxHeight)
		{
			tempHeight = maxHeight;
			tempWidth = tempHeight * tempRatio;
		}
	}

	return { "width" : Math.round(tempWidth), "height" : Math.round(tempHeight) };
}

function createPreviewImage(nodeTempImg,nodeImg,maxWidth,maxHeight)
{
	nodeImg.src = nodeTempImg.src;
	tempWidth = nodeTempImg.width;
	tempHeight = nodeTempImg.height;
	tempRatio = tempWidth / tempHeight;

	if( (tempWidth > maxWidth) || (tempHeight > maxHeight) )
	{
		if(tempWidth > maxWidth)
		{
			tempWidth = maxWidth;
			tempHeight = tempWidth / tempRatio;
		}

		if(tempHeight > maxHeight)
		{
			tempHeight = maxHeight;
			tempWidth = tempHeight * tempRatio;
		}
	}

	nodeImg.style.width = tempWidth;
	nodeImg.style.height = tempHeight;
	nodeImg.style.borderStyle = "solid";
	nodeImg.style.borderWidth = "1px";
	nodeImg.style.borderColor = "#000000";
}

function previewImage(inputFile,idImg,maxWidth,maxHeight)
{
	var nodeImg = document.getElementById(idImg);
	if( nodeImg )
	{
		var filename = "file:///" + inputFile.value;
		var nodeTemp = new Image();
		nodeTemp.onload = function(){createPreviewImage(this,nodeImg,maxWidth,maxHeight);};
		nodeTemp.src = filename;
	}
}

function getUnitFromCSSLength(value,defUnit)
{
	value = value.toString();
	value = value.toUpperCase();
	if( value.indexOf("PX") != -1 )	return "px";
	if( value.indexOf("MM") != -1 )	return "mm";
	if( value.indexOf("CM") != -1 )	return "cm";
	if( value.indexOf("EM") != -1 )	return "em";
	if( value.indexOf("EX") != -1 )	return "ex";
	if( value.indexOf("PC") != -1 )	return "pc";
	if( value.indexOf("PT") != -1 )	return "pt";
	if( value.indexOf("IN") != -1 )	return "in";

	return defUnit;
}