// -----------------------------------------------------
//
//	Sets an element to follow a target element.
//	Can specify a conditional function that will only display the
//	hover element if the condition returns true.
//
// -----------------------------------------------------
function SetHover(targetElementID, hoverElementID, conditionFunction)
{
	$("#" + targetElementID).hover
	(
		function(e)
		{
			if(conditionFunction())
				$("#" + hoverElementID).show();
		},
		function(e)
		{
			$("#" + hoverElementID).hide();
		}
	);
	
	$("#"+ targetElementID).mousemove
	(
		function(e)
		{
			if(conditionFunction())
			{
				$("#" + hoverElementID).css("left", e.pageX - 5);
				$("#" + hoverElementID).css("top", e.pageY + 25);
			}
		}
	);
}



// -----------------------------------------------------
//
//	Dynamic entry!
//
// -----------------------------------------------------
function DynamicEntry(textboxID, dummyButtonID)
{
	$("#" + textboxID).keyup
	(
		function(ev)
		{
			$("#" + dummyButtonID).click();
		}
	);
}

// -----------------------------------------------------
//
//	Popup.
//
// -----------------------------------------------------
function SetPopup(targetID)
{
	$("#" + targetID).mouseenter(function() { ShowPopup($(this)); });
	$("#" + targetID).mouseleave(function() { HidePopup($(this)); });
	
	$("#" + $("#" + targetID).children("input:last").val()).fadeOut(0);
}

function ShowPopup(target)
{
	var targetPos = target.offset();	
	var popupElement = $("#" + target.children("input:last").val());
	
	popupElement.css({top: targetPos.top - 50 - (popupElement.height() / 2), left: targetPos.left + target.width() + 10})
	popupElement.fadeIn(250);
}

function HidePopup(target)
{
	var popupElement = $("#" + target.children("input:last").val());
	popupElement.fadeOut(250);
}



// -----------------------------------------------------
//
//	
//
// -----------------------------------------------------
function GridTable(tableID)
{
	$("#" + tableID + " td.Selectable").hover(function() { HighlightTable($(this)); },
									  function() { UnHighlightTable($(this)); } );
}

function HighlightTable(cell)
{
	cell.addClass("HighlightCell")
	var value = cell.children("input").attr("value");
	var row = cell.parent();
	//row.addClass("HighlightLine");
	
	cell.prevAll().addClass("HighlightLine");
	
	
	// Find the index of the cell in the table row.
	var cells = row.children();
	var i = 0;
	for(i = 1; i < cells.length; i++)
	{
		if(cells.eq(i).children("input").attr("value") == value)
		{
			// Found the index!
			break;
		}
	}
	
	// Go to the row parent (the table).
	var rows = row.prevAll();//.parent().children("tr");
	var r = 0;
	for(r = 0; r < rows.length; r++)
	{
		rows.eq(r).children("td").eq(i).addClass("HighlightLine");
	}
}

function UnHighlightTable(cell)
{
	cell.removeClass("HighlightCell")
	var value = cell.children("input").attr("value");
	var row = cell.parent();
	//row.removeClass("HighlightLine");
	cell.prevAll().removeClass("HighlightLine");
	
	
	// Find the index of the cell in the table row.
	var cells = row.children();
	var i = 0;
	for(i = 1; i < cells.length; i++)
	{
		if(cells.eq(i).children("input").attr("value") == value)
		{
			// Found the index!
			break;
		}
	}
	
	// Go to the row parent (the table).
	var rows = row.prevAll();//.parent().children("tr");
	var r = 0;
	for(r = 0; r < rows.length; r++)
	{
		rows.eq(r).children("td").eq(i).removeClass("HighlightLine");
	}
}



// -----------------------------------------------------
//
//	
//
// -----------------------------------------------------
function CreateSelectableGrid(listElementID)
{
	var listElement = $("#" + listElementID);
}

// -----------------------------------------------------
//
//	Concept view
//
// -----------------------------------------------------
function GetConceptView(containerElementID, conceptID, onComplete)
{
	$("#" + containerElementID).load("http://localhost:49573/Controls/Concept.aspx",
									 { ConceptID: conceptID },
									 onComplete);
}
