var	currentDDC = null;
var	timeoutID;
var	currentOldOnClick	=	null;
var	dx=0,dy=0;

var ddMouseOverCounter = 0;
// var ddElements = [];
var ddPauseHide = false;

function DDPopupMouseOut()
{
	ddPauseHide = false;

	timeoutID=window.setTimeout(HideDD, 500);
}

function DDPopupMouseOver()
{
	ddPauseHide = true;
   	window.clearTimeout(timeoutID);
}

function DDBodyOnMouseOut(dd, e)
{
	if (ddPauseHide)
		return;

	if (checkMouseLeave(dd, e))
	{
		timeoutID = window.setTimeout(HideDD,500);
	}
}

function DDBodyOnMouseOver(dd, e)
{
	window.clearTimeout(timeoutID);checkMouseEnter(dd, e);window.clearTimeout(timeoutID);
}

function DDOnMouseOut()
{
	if (ddPauseHide)
		return;

	timeoutID = window.setTimeout(HideDD,100);
}

function DDOnMouseOver(ddcID,activeWord,suspendsubmit)
{
	HideDD();
	ShowDD(ddcID,activeWord,suspendsubmit);
}

function ShowDD(ddcID,activeWord,suspendsubmit)
{
//	var activeWord = document.getElementById(activeWordId);
	var	ddc	=	document.getElementById(ddcID);
	if (ddc	== currentDDC)
	return;
	advance(true);
	HideDD();

		if (navigator.appName!="Netscape")
		{
				dx = findAPosX(document.getElementById('DropDownsTable'));
				dy = findAPosY(document.getElementById('DropDownsTable'));
				//alert(dx);
		}
		else
		{
				if (document.getElementById('BIG_title')!=null)
				{
					dx = document.getElementById('BIG_title').offsetLeft;
					dy = document.getElementById('BIG_title').offsetTop;
				}

		}		 
		var	ddwnd	=	document.getElementById(DropDown_ContentID);
		var	str	=	ddc.innerHTML;
		ddwnd.style.left = findAPosX(activeWord)-	dx;
		if (findAPosX(ddwnd)+ddwnd.offsetWidth>document.body.scrollLeft	+	document.body.clientWidth)
			ddwnd.style.left = document.body.scrollLeft	+	document.body.clientWidth	-	ddwnd.offsetWidth	-	40 - document.body.style.marginLeft;
		ddwnd.style.top	=	 findAPosY(activeWord)+activeWord.offsetHeight - dy;
		
		currentOldOnClick	=	document.getElementById(ddcID.substring(3)).onchange;
		
		ddc.innerHTML	=	"";
		ddwnd.innerHTML	=	str;
		ddc.style.display	=	"";
		
		currentDDC = ddc;

		if (!suspendsubmit)
			document.getElementById(ddcID.substring(3)).onchange	=	function(){window.clearTimeout(timeoutID);if (currentOldOnClick!=null)currentOldOnClick();document.forms[0].submit();};
		else
			document.getElementById(ddcID.substring(3)).onchange	=	function(){window.clearTimeout(timeoutID);if (currentOldOnClick!=null)currentOldOnClick();};
}
function HideDD()
{
		window.clearTimeout(timeoutID);
		if (currentDDC!=null)
		{
			var	ddwnd	=	document.getElementById(DropDown_ContentID);			
			var	str	=	ddwnd.innerHTML;
			ddwnd.innerHTML	=	"";
			currentDDC.innerHTML=	str;			
			document.getElementById(currentDDC.id.substring(3)).onchange = currentOldOnClick;
			currentDDC = null;
			
			
		}
}
function advance(hide)
{
	var	ddt	=	document.getElementById('DropDownTable');
	if (ddt==null)
		return;
	if (hide ||	ddt.style.display!='none')
			ddt.style.display='none';
	else
			ddt.style.display='';

	var	ddt	=	document.getElementById('DropDownTableButtons');
	if (ddt==null)
		return;

	if (hide ||	ddt.style.display!='none')
			ddt.style.display='none';
	else
			ddt.style.display='';			
			
}


//Utils	
function findAPosX(	obj	)
{
		 if( !obj	)			return -1;

		 var		 curLeft		 = 0;

		 if( obj.offsetParent	)			{
					while( obj.offsetParent	)			{
							 curLeft		 +=	obj.offsetLeft;
							 obj					=	obj.offsetParent;
					}
		 } else	if(	obj.x	)
					curLeft			+= obj.x;
		 return	curLeft;
}
/******************************************************************************/
function findAPosY(	obj	)
{
		 if( !obj	)			return -1;

		 var		 curTop			=	0;

		 if( obj.offsetParent	)			{
					while( obj.offsetParent	)			{
							 curTop			+= obj.offsetTop;
							 obj					=	obj.offsetParent;
					}
		 } else	if(	obj.y	)
					curTop		 +=	obj.y;
		 return	curTop;
}


function containsDOM (container, containee)	{
	var	isParent = false;
	do {
		if ((isParent	=	container	== containee))
			break;
		try {containee = containee.parentNode;}
		catch (e) {containee = null} 
	}
	while	(containee !=	null);
	return isParent;
}

function checkMouseEnter (element, evt)	{
	if (element.contains &&	evt.fromElement) {
		return !element.contains(evt.fromElement);
	}
	else if	(evt.relatedTarget)	{
		return !containsDOM(element, evt.relatedTarget);
	}
}

function checkMouseLeave (element, evt)	{
	if (element.contains &&	evt.toElement) {
		return !element.contains(evt.toElement);
	}
	else if	(evt.relatedTarget)	{
		return !containsDOM(element, evt.relatedTarget);
	}
}

function DropDownUpdateControl(controlName,targetId)
{
	UpdateControlEx(controlName,targetId,GetControlParameters(targetId),null,true);
}
