// find_position.js - Find html item position
// uses( Browsers.js);
function IsByWindowPosition( w)
{
	if ( !w)
		w = window;
	if ( typeof( w.pageYOffset) == 'number')
		return true;
	return false;
}
function IsByDocumentElementPosition( doc)
{
	if ( !doc)
		doc = document;
	if (( doc.documentElement)
		&&(( doc.documentElement.clientWidth)
		 ||( doc.documentElement.clientHeight)
			)
		 )
	{
		return true;
	}
	return false;
}
function IsByBodyPosition( doc)
{
	if ( !doc)
		doc = document;
	if (( doc.body)
		&&(( doc.body.clientWidth)
		 ||( doc.body.clientHeight)
			)
		 )
	{
		return true;
	}
	return false;
}

function BrowserWindowWidth( w)
{
	//alert( 'document.documentElement.clientWidth = '+document.documentElement.clientWidth
	//			+' document.body.clientWidth = '+document.body.clientWidth
	//			+' window.innerWidth = '+window.innerWidth
	//			);

	var doc = null;
	if ( w)
		doc = w.document;
	else
		doc = document;

	if ( IsByDocumentElementPosition( doc))
		return doc.documentElement.clientWidth;
	return doc.body.clientWidth;
}

function BrowserWindowHeight( w)
{
	var nHeight = null;
	var doc = null;
	if ( w)
		doc = w.document;
	else
	{
		doc = document;
		w = window;
	}
		

	var bByWindow = IsByWindowPosition( w);
	var bByDocumentElement = IsByDocumentElementPosition( doc);
	var bByBody = IsByBodyPosition( doc);

	if ( bByDocumentElement)
		nHeight = doc.documentElement.clientHeight;
	else
	if ( bByBody)
		nHeight = doc.body.clientHeight;

	if ( bByWindow)
	if ( bByDocumentElement)
	if ( bByBody)
	{
		//ShowBrowserDataInAlert();

		var nBodyHeight = doc.body.clientHeight;
		var nDocumentHeigh = doc.documentElement.clientHeight;
		var nWindowHeight = w.innerHeight;
		
		nHeight = nWindowHeight;// + nBodyHeight - nDocumentHeigh;

		if ( !IsSafari())
		{
			if ( nWindowHeight >= nDocumentHeigh)
			{
				nHeight = nDocumentHeigh;
				//if ( IsOpera())
				if ( nWindowHeight >= nBodyHeight)
				if ( nBodyHeight > nDocumentHeigh)
					nHeight = nBodyHeight;
			}
			else
			if ( nWindowHeight >= nBodyHeight)
			{
				nHeight = nBodyHeight;
			}
		}
	}
	return nHeight;
}

function ScrollWindowPosition( w)
{
	var doc = null;
	if ( w)
		doc = w.document;
	else
	{
		doc = document;
		w = window;
	}

	var nScrollX = 0;
	var nScrollY = 0
	if ( IsByWindowPosition( w))
	{
		nScrollX = w.pageXOffset;
		nScrollY = w.pageYOffset;
	}
	else
	if ( IsByDocumentElementPosition( doc))
	{
		nScrollX = doc.documentElement.scrollLeft;
		nScrollY = doc.documentElement.scrollTop;
	}
	else
	if ( IsByBodyPosition( doc))
	{
		nScrollX = doc.body.scrollLeft;
		nScrollY = doc.body.scrollTop;
	}
	return [nScrollX, nScrollY];
}

function FindPosition(obj, bWithScrolling) 
{
	var curleft = 0;
	var curtop = 0;
	if ( obj)
	{
		//if (obj.offsetParent) 
		//{
			curleft = obj.offsetLeft;
			curtop = obj.offsetTop;
			while (obj.offsetParent) 
			{
				obj = obj.offsetParent;
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
			}
		//}
	
		if ( bWithScrolling)
		{
			var arScrollPosition = ScrollWindowPosition();
			//alert( 'Scroll X = '+arScrollPosition[0]+' Y = '+arScrollPosition[1]);
			curleft -= arScrollPosition[ 0];
			curtop -= arScrollPosition[ 1];
		}

	}
	return [curleft,curtop];
}

function TopPosition( obj)
{
	var arPos = FindPosition(obj);
	return arPos[1];
}

function DocumentScrollHeight( w)
{
	var doc = null;
	if ( w)
		doc = w.document;
	else
		doc = document;
	/*
	var nHeight = null;
	var bByDocumentElement = IsByDocumentElementPosition();
	var bByBody = IsByBodyPosition();

	if ( bByBody)
	{
		nHeight = doc.body.scrollHeight;
	}

	if ( bByDocumentElement)
	if ( !nHeight)
	{
		nHeight = doc.documentElement.scrollHeight;
	}

	//if ( bByDocumentElement)
	//if ( bByBody)
	//{
	//  ShowBrowserDataInAlert( w);
	//}
	//return nHeight;
	*/
	return doc.body.offsetHeight;
}

/*
var g_bFirstAlert = true;
function ShowBrowserDataInAlert( w)
{
	if ( g_bFirstAlert)
	{
		var doc = null;
		if ( w)
			doc = w.document;
		else
		{
			doc = document;
			w = window;
		}
		g_bFirstAlert = false;
		alert( ' document.body.offsetHeight, scrollHeight, clientHeight = '
					+doc.body.offsetHeight
					+', '+doc.body.scrollHeight
					+', '+doc.body.clientHeight
					+"\n"
					+' document.documentElement.offsetHeight, scrollHeight, clientHeight = '
					+doc.documentElement.offsetHeight
					+', '+doc.documentElement.scrollHeight
					+', '+doc.documentElement.clientHeight
					+"\n"
					+' window.innerHeight = '
					+w.innerHeight
					);
	}
}
*/