var Side_Column_Offset = 11;
var li_has_click = false;
var id_active_mem;
var id_active;
var activeElem = null;
var is_first = false;
var select_in_Line = false;
var clicked_input = false;

$("document").ready(function() {
	$("fieldset :input")
		.blur(function () {
					$(this).parents("li").removeClass("activeFieldLine");
					$('.focusOn').removeClass("focusOn");
		})

		.focus(function () {
					//$(this).parent().addClass("focused");
					var focused_li = $(this).parents("li.fieldLine").attr("id"),
						id = getNrFromId(focused_li),
						$this = $(this);
					id_active_mem = id;

	                // check if message box exists
	                if (0 != $("*[id='fromMsg__" + id + "']").length) {
	                	showTooltipBox(id);
						id_active = id;
	                } else {
	                	showTooltipBox("none");
	                }

					$("fieldset li.fieldLine").removeClass("activeFieldLine");
					$this.parents("li.fieldLine").addClass("activeFieldLine");

					$('.focusOn').removeClass("focusOn");
					$this.addClass("focusOn");
	});

	$(".fieldLine input").bind("click", function() { clicked_input = true; });
	$(".fieldLine").bind("click", field_Line_Click);

	$("fieldset li[id^='ln__']")
		.mouseover(function(e) {
					$(".toolTip").hide();

					if ($(this).hasClass('descUndField')) {
						showTooltipBox("none");
					} else {
						var id = getNrFromId($(this).attr("id"));
						if (undefined == id) {
							return;
						}

	                    // check if message box exists
	                    if (0 == $("*[id='fromMsg__" + id + "']").length) {
	                    	showTooltipBox("none");
	                    	return;
						} else {
							showTooltipBox(id);
							id_active = id;
						}
					};
		})

		.mouseout(function() {
				$(".toolTip").hide();
				showTooltipBox("none");
				li_has_click = false;
				id_active = "";

				var t2;
				clearTimeout(t2);
				t2 = setTimeout (function () {
					if ('' == id_active) {
						showTooltipBox(id_active_mem);
					};
				}, 600);
		});

		$("body").click(function () {

				if (false == li_has_click) {

					$("fieldset li").removeClass("activeFieldLine");
					$("fieldset li span").removeClass("focused");

					$(".toolTip").hide();
					showTooltipBox("none");
					id_active_mem = "";
				};
		});

		$("#content").find("form").each(function() {
			$(this).find("li[id^='ln__']").each(function() {
				if (false == is_first) {
					$(this).click();
					is_first = true;
				}
			});
		});

		$(".selectbox").click(function() {
				var a = this;
				setTimeout (function() {
					a.focus();
				}
				, 200 );
		});
});

// -----------------------------------------------------------------------------------------------------------------------

function showTooltipBox(id) {
	if (id == "none") {
		$(".toolTip").hide();
		return;
	};

	$(".frmMsg .ct>div").hide();
	if (0 != $("*[id='fromMsg__" + id + "']").length) {
		$("*[id='fromMsg__" + id + "']").show();

		var el = $("li[id='ln__"+ id +"']");

		if (el) {
			var coords = getElementCoordinate(el);
			if (coords) {
				$(".toolTip").css("margin-top", coords[1]-Side_Column_Offset+"px");
				$(".toolTip").show();
			};
		};
	};
};

function getNrFromId (mystring) {
	if (mystring) {
		var myarray = mystring.split(/__/);
		return myarray[1];
	} else {
		return "";
	};
};

function getElementCoordinate(element) {
	if (null == $(element).html()){
		return;
	};

	var coords = [];
		if ($(".mainColumn").size() >= 1) {
			var mainLeft = Math.round(($(".mainColumn").position().left));
			var mainTop = Math.round(($(".mainColumn").position().top));

			coords[0] = Math.round(($(element).position().left)- mainLeft);
			coords[1] = Math.round(($(element).position().top) - mainTop);
		};
	return coords;
};

function field_Line_Click() {
	if ($(this).hasClass("noAction")) {
		if ($(this).hasClass("captcha")) {
			$(this).find(":input").focus();
		};
		return;
	};

	if (false == clicked_input) {
		showTooltipBox("none");
	};

	var id = getNrFromId($(this).attr("id"));
	id_active_mem = id;
	normal_select_in_line = false;

	if (!$(this).hasClass("noBar"))	{
			$(this).find(".focusOn").each(function() {
				normal_select_in_line = true;
			});

			$(this).find("select").each(function() {
				if ($(this).is(':visible') && false === normal_select_in_line) {
					normal_select_in_line = true;
				};
			});

			var d_flag = false;
			var selectObj;

			if (false == normal_select_in_line) {
				$(this).find("input").each(function() {
					if (false === d_flag){
						selectObj = $(this);
						d_flag = true;
					};
				});
				if (true != d_flag) {
					$(this).find("textarea").each(function() {
						if (false == d_flag) {
							selectObj = $(this);
							d_flag = true;
						};
					});
				};
			};

			if (true == d_flag && false === clicked_input) {
				selectObj.focus();
			};

			li_has_click = true;
	} else {
		li_has_click = true;
	};
	clicked_input = false;
	if ($(this).hasClass("captcha")) {
		$(this).find(":input").focus();
	}
};

