function isNumber(n)
{
  return !isNaN(parseFloat(n)) && isFinite(n);
}

function positionElement(ele, typex, typey, scroll) // (htmlelement reference)ele, (string) typex = 0, center or max, (string) typey = 0, center or max, (boolean)
{
	typex = typex||'0';
	typey = typey||'0';
	scroll = scroll||false;

	var dim = windowDimensions();
	var sdim = scrollDimensions();
	var width = ele.offsetWidth;
	var height = ele.offsetHeight;
	
	var posx = parseInt((dim[0]/2) - (width/2));
	var posy = parseInt((dim[1]/2) - (height/2));
	var maxx = parseInt(dim[0] - width);
	var maxy = parseInt(dim[1] - height);
	
	if (scroll)
	{
	 	ele.style.position='fixed';
/*	 	if (sdim[1] > 0)
	 	{
	 	 	posy += sdim[1];
	 	 	maxy += sdim[1];
	 	}
	 	if (sdim[0] > 0)
	 	{
	 	 	posx += sdim[0];
	 	 	maxx += sdim[0];
	 	}*/
	}

	if(typey == '0')
		ele.style.top = '0px';
	if(typex == '0')
		ele.style.top = '0px';
		
	if(typey == 'center')
		ele.style.top = posy+'px';
	if(typex == 'center')
		ele.style.left = posx+'px';
		
	if(typey == 'max')
		ele.style.top = maxy+'px';
	if(typex == 'max')
		ele.style.left = maxx+'px';
		
	if(isNumber(typey))
		ele.style.top = typey+'px';
	if(isNumber(typex))
		ele.style.left = typex+'px';		
}

function findPos(obj)
{
	var curleft = 0;
	var curtop  = 0;
	do
	{
		curleft += obj.offsetLeft;
		curtop += obj.offsetTop;
	}
	while(obj = obj.offsetParent);

	return [curleft, curtop];
}

function windowDimensions()
{
	var clientWidth = 0, clientHeight = 0;
	if( typeof( window.innerWidth ) == 'number' )
	{
		//Non-IE
		clientWidth = window.innerWidth;
		clientHeight = window.innerHeight;
	}
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
	{
		//IE 6+ in 'standards compliant mode'
		clientWidth = document.documentElement.clientWidth;
		clientHeight = document.documentElement.clientHeight;
	}
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) )
	{
		//IE 4 compatible
		clientWidth = document.body.clientWidth;
		clientHeight = document.body.clientHeight;
	}
	
	return [clientWidth, clientHeight];
}

function scrollDimensions()
{
	var scrollX = 0, scrollY = 0;
	if( typeof( window.pageYOffset ) == 'number' )
	{
		//Netscape compliant
		scrollY = window.pageYOffset;
		scrollX = window.pageXOffset;
	}
	else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) )
	{
		//DOM compliant
		scrollY = document.body.scrollTop;
		scrollX = document.body.scrollLeft;
	}
	else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) )
	{
		//IE6 standards compliant mode
		scrollY = document.documentElement.scrollTop;
		scrollX = document.documentElement.scrollLeft;
	}
	
	return [scrollX, scrollY];
}

function stopEvent(e)
{
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation)
		e.stopPropagation();
		
	return false;
}
