var field;
var control;
var ignoreDocumentClick = false;
function showControl(fieldID, controlID)
{
	if (control)
		closeControl();

	field = document.getElementById(fieldID);
	control = document.getElementById(controlID);
	
	// Display the control
	control.style.display = "block";

	// Set position if it pops up off screen
	setControlPosition();

	// Set document onclick event so control will close if user clicks anywhere outside control	
	document.onclick = documentClick;
	
	window.onresize = setControlPosition;
	
	// Since i just set a document onclick event, i need to cancel the bubble so control
	// isn't closed (obviously user just clicked outside control)
	window.event.cancelBubble = true;
	
	// Focus the field
	field.focus();
}
function setControlPosition()
{
	// Get x and y coordinate for control display (bottom left of the field)	
	var x = getFieldOffsetLeft(field);
	var y = getFieldOffsetTop(field) + field.offsetHeight;
	
	var controlRight = x + control.clientWidth;
	var controlBottom = y + control.clientHeight;
	var fieldTop = getFieldOffsetTop(field);
	var fieldLeft = getFieldOffsetLeft(field);
	var fieldBottom = fieldTop + field.offsetHeight;
	var fieldRight = fieldLeft + field.offsetWidth;
	var bodyRight = document.body.offsetLeft + document.body.offsetWidth - 30; // -30 for the scroll bar;
	var bodyBottom = document.body.offsetTop + document.body.offsetHeight - 30; // -30 for the scroll bar;
	var bodyLeft = document.body.offsetLeft;
	var bodyTop = document.body.offsetTop;
	
	var availableTop = fieldTop - bodyTop;
	var availableBottom = bodyBottom - fieldBottom;
	var availableLeft = fieldLeft - bodyLeft;
	var availableRight = bodyRight - fieldRight;
	
	// Calculate difference in control width and field width
	var controlWidthOverflow = control.offsetWidth > field.offsetWidth ? control.offsetWidth - field.offsetWidth : 0;
	
	// If control right is beyond body right align control with the right side of field
	if (controlRight > bodyRight && availableLeft >= controlWidthOverflow)
	{
		x = fieldLeft - controlWidthOverflow;
	}
	else
	{
		x = fieldLeft;
	}
	
	// If control bottom is beyond body bottom align control with the top side of field
	if (controlBottom > bodyBottom && availableTop >= control.offsetHeight)
	{
		y = fieldTop - control.offsetHeight;
	}
	else
	{
		y = fieldBottom;
	}
	
	// Set control position
	control.style.posLeft = x;
	control.style.posTop = y;
	
  // Hide <select> controls that intersect the control
  var allSelectFields = document.getElementsByTagName("select");
  
  for( i=0;i<allSelectFields.length;i++)
  {
	var selectField = allSelectFields(i);
	if (selectField.id == "year" || selectField.id == "month") continue;

	// only hide select field if its at least partially covered by the menu
	var selectFieldX = getPageOffsetLeft( selectField );
	var selectFieldY = getPageOffsetTop( selectField ) + selectField.offsetHeight;
	var selectFieldRight = 0;
	var selectFieldBottom = 0;
	
	if( browser.isIE )
	{
		selectFieldX += selectField.offsetParent.clientLeft;
		selectFieldY += selectField.offsetParent.clientTop;
		
		selectFieldRight += selectFieldX + selectField.clientWidth;
		selectFieldBottom += selectFieldY + selectField.clientHeight;		
	}
	
//alert("X: "+selectFieldX+"\t"+menuOffsetLeft+"\r\nR: "+selectFieldRight+"\t"+menuRight+"\r\nY: "+selectFieldY+"\t"+menuOffsetTop+"\r\nB: "+selectFieldBottom+"\t"+menuBottom);
	if(	squareIntersectsSquare( selectFieldX, selectFieldRight, selectFieldY, selectFieldBottom, x, controlRight, y, controlBottom ) )
	{
		selectField.style.visibility = "hidden";
	}
  }		
}
function documentClick()
{
    if (ignoreDocumentClick)
    {
        ignoreDocumentClick = false;
        return;
    }
        
	var x = window.event.x;
	var y = window.event.y;

	if (control.componentFromPoint(x,y) == "outside")
	{
		closeControl(control);
	}
}
function documentResize()
{
	adjustControlPosition();
}
function closeControl()
{
	if (control && control.style.display != "none")
		control.style.display="none";
	
	document.onclick = null;
	window.onresize = null;
	control = null;
	field = null;
	
	var allSelectFields = document.getElementsByTagName("select");
	for( i=0;i<allSelectFields.length;i++)
	{
		allSelectFields(i).style.visibility = "visible";
	}
}
var prevClassNameHover;
var prevClassNameClick;
function cellMouseOver()
{
	var dayCell = window.event.srcElement;
	prevClassNameHover = dayCell.className;
	dayCell.className = "dayHover";
}
function cellMouseOut()
{
	var dayCell = window.event.srcElement;
	dayCell.className = prevClassNameHover;
	prevClassNameHover = null;
}
function cellMouseDown()
{
	var dayCell = window.event.srcElement;
	prevClassNameClick = dayCell.className;
	dayCell.className = "selected";
}
function cellMouseUp()
{
	var dayCell = window.event.srcElement;
	dayCell.className = prevClassNameClick;
	prevClassNameClick = null;
}
function getFieldOffsetLeft(el) {

  var x;

  // Return the x coordinate of an element relative to the page.

  x = el.offsetLeft;
  
  // MODIFIED - R.Wenger - absolute positioning messes up the calculation so only continue if not using absolute positioning
  if (el.offsetParent != null && el.offsetParent.style.position != 'absolute')
    x += getFieldOffsetLeft(el.offsetParent);

  return x;
}

function getFieldOffsetTop(el) {

  var y;

  // Return the x coordinate of an element relative to the page.

  y = el.offsetTop;
  // MODIFIED - R.Wenger - absolute positioning messes up the calculation so only continue if not using absolute positioning
  if (el.offsetParent != null && el.offsetParent.style.position != 'absolute')
    y += getFieldOffsetTop(el.offsetParent);

  return y;
}
