/// <reference path="jquery-1.4.2.js" />

//
//	Globals.
//
var g_PageCultureID = 9;




//
//	Gets an element object with the ID specified.
//
function GetElement(elementID)
{   
    return $("#" + elementID)[0];
}


//
//	Toggles an element between block and none.
//
function ToggleElement(elementID)
{
	var element = GetElement(elementID);
	if(element == null)
		return;
		
	ToggleElementElement(element);
}

function ToggleElementElement(element)
{
	if(element.style.display == "none")
		element.style.display = "block";
	else
		element.style.display = "none";
}



//
//
//
function WindowOnload(f)
{
	var prev=window.onload;
	window.onload=function(){ if(prev)prev(); f(); }
}


//
//	Cancel any postbacks currently in progress.
//
function CommonPageLoad()
{
	Sys.WebForms.PageRequestManager.getInstance().add_endRequest(OnEndPostBackRequest);
}


var handlePostBackCancel = null;

function OnEndPostBackRequest(sender, args)
{
	args.set_errorHandled(true);
	if(handlePostBackCancel != null)
		handlePostBackCancel();
}



//
//	Sets the text content of an element.
//
function SetElementText(elementID, text)
{
	var element = GetElement(elementID);
	if(element == null)
		return;
		
	var textNode = document.createTextNode(text);
	
	// Remove any existing children.
	if(element.childNodes.length > 0)
		element.removeChild(element.childNodes[0]);
		
	element.appendChild(textNode);
}

// -----------------------------------------------------
//
//	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();
		}
	);
}


// -----------------------------------------------------
//
//	Loading.
//
// -----------------------------------------------------

//var g_loadingEndCallback = null;

//function ShowElementLoading(elementID, endCallback)
//{
//	var element = $("#" + elementID);
//	var loadingFader = $("#LoadingFader").width();
//	loadingFader.width(element.width());
//	loadingFader.height(element.height());
//	loadingFader.scrollLeft(element.scrollLeft());
//	loadingFader.scrollTop(element.scrollTop());
//	loadingFader.fadeIn(500);
//	
//	g_loadingEndCallback = endCallback;
//}

//function EndElementLoading()
//{
//	loadingFader.fadeOut(500);
//	if(g_loadingEndCallback != null)
//		g_loadingEndCallback();
//	
//	g_loadingEndCallback = null;
//}





// -----------------------------------------------------
//
//	Rounded corners.
//
// -----------------------------------------------------
function RoundCorners()
{
	//var toBeRounded = $("div.RoundedCorners,a.RoundedCorners,span.RoundedCorners,li.RoundedCorners");
	//toBeRounded.append($("<div class=\"Corner TopLeft\" /><div class=\"Corner TopRight\" /><div class=\"Corner BottomLeft\" /><div class=\"Corner BottomRight\" />"));
	//toBeRounded.css({"border-width": "0px"});
}

// -----------------------------------------------------
//
//	Error.
//
// -----------------------------------------------------
function HyperFieldValidator_ValidateOnSubmit(obj)
{
	var textBox = $("#" + obj.controltovalidate);
	if(textBox.length == 0)		// Check there is a textbox.
		return true;
		
	if(textBox.val().length > 0)
	{
		textBox.removeClass("TextBox_Invalid");
		HideErrorLabel(obj.errorlableid);
		return true;
	}
	else
	{
		textBox.addClass("TextBox_Invalid");
		ShowErrorLabel(obj.controltovalidate, obj.errorlableid);
		return false;
	}
}

function ShowErrorLabel(targetID, errorLabelID)
{
	var target = $("#" + targetID);
	var errorLabel = $("#" + errorLabelID);
	errorLabel.css({position: "absolute", left: target.position().left + target.outerWidth() + 10, top: (target.position().top + (target.outerHeight()/2)) - (errorLabel.outerHeight()/2)});
	errorLabel.fadeIn(500);
}

function HideErrorLabel(errorLabelID)
{
	var errorLabel = $("#" + errorLabelID);
	if(errorLabel.css("display") != "none")
		errorLabel.fadeOut(500);
}


// -----------------------------------------------------
//
//	Loading indicator.
//
// -----------------------------------------------------






//
//	attributeName: the name of the attribute the element must possess.
//	attributeValue: the value that the attrbute must have.
//	elementThatLoads: the element that will be loaded.
//
function InitLoader(attributeName, attributeValue, elementThatLoads)
{
}

//
//	Creates a jquery object that presents a floating loader box.
//
function CreateFloatingLoader()
{
	$("<div class=\"FloatingLoadingBox\"><div class=\"Indicator\"></div></div>");
}


// -----------------------------------------------------
//
//	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);
}



// -----------------------------------------------------
//
//	Change language functions.
//
// -----------------------------------------------------
function ChangeLanguage(languageID)
{
	var staticTexts = $("span.StaticText");
	for(var i = 0; i < staticTexts.length; i++)
	{
		
	}
}

function ChangeLanguage_Complete(result)
{
	
}

function ChangeLanguage_Error()
{
	
}

// -----------------------------------------------------
//
//	Concept view
//
// -----------------------------------------------------
function GetConceptView(containerElementID, conceptID, onComplete)
{
	$("#" + containerElementID).load("http://localhost:49573/Controls/Concept.aspx",
									 { ConceptID: conceptID },
									 onComplete);
}



// -----------------------------------------------------
//
//	Helper functions.
//
// -----------------------------------------------------
var Helper = 
{
	AppRoot: "",
	
	GetCellIndex: function(cell)
	{
		var cellsInRow = cell.parent().children("td");
		return cellsInRow.index(cell[0]);
	},
	
	GetConceptImageUrl: function(screenerID, conceptID, size)
	{
		var url = this.AppRoot + "Data/" + screenerID.toString() + "/" + conceptID.toString() + "/0_" + size + ".jpg";
		return url;
	},

	ParseIntList: function(strInts)
	{
		/// <summary>
		///	Creates an array of integers from a comma seperated list.
		/// </summary>
		
		var intArray = new Array();
		var strVals = strInts.split(",");
		for(var i = 0; i < strVals.length; i++)
		{
			intArray[i] = parseInt(strVals[i], 10);
		}
		
		return intArray;
	},
	
	ArrayIntersectsAny: function(array1, array2)
	{
		/// <summary>
		///	Returns true if the first array contains any values that are in the second array.
		/// </summary>
		
		
		for(var i1 = 0; i1 < array1.length; i1++)
		{
			for(var i2 = 0; i2 < array2.length; i2++)
			{
				if(array1[i1] == array2[i2])
					return true;
			}
		}
		
		return false;
	},
	
	ClickCheckbox: function(parentElementID)
	{
		var checkbox = $("#" + parentElementID + " input:checkbox");
		//checkbox.click();
		if(checkbox.is(":checked"))
			checkbox.attr('checked', false);
		else
			checkbox.attr('checked', true);
	},
	
	///
	///	Selects/deselectes all the checkboxes in an element.
	///
	ToggleAllCheckboxes: function(parentElementID)
	{	
		if($("#" + parentElementID + " input:checked").length < $("#" + parentElementID + " input:checkbox").length)
			$("#" + parentElementID + " input:not(:checked)").attr("checked", "checked");
		else
			$("#" + parentElementID + " input:checked").removeAttr("checked");
	}
}