// =========================================================================
var Ajax = new JQueryAjax(true);
var FieldAjax = new JQueryAjax(false);
// =========================================================================
function JQueryAjax(KillPopUps)
{
	// ---------- deklaracje --------
	this.Go = Go;
	this.ScriptGo = ScriptGo;
	this.FormGo = FormGo;
	this.ShowMarker = ShowMarker;
	this.ShowSnow = ShowSnow;
	this.HideMarker = HideMarker;
	this.CreateAndGo = PopUpWin; // dla kompatybilności wstecz
	this.PopUpMenu = PopUpMenu;
	this.PopUpWin = PopUpWin;
	var NotKill;
	var KillPopUpWin = KillPopUps;
	NotKill = new Array;
	// -------------------------------------------------------------------------
	function HideMarker()
	{
		$("#snow").remove();
		$("#xyzLoading").remove();
	}
	// ---------- implementacje -----
	function ShowSnow(ObjId)
	{
		if (!$('#' + ObjId + '_snow').length)
		{
			$("body:first").append("<div id='" + ObjId + "_snow' style='background-color:black' class='a h'></div>");
			$('#' + ObjId + '_snow').width($("body:first").outerWidth(true));
			$('#' + ObjId + '_snow').height($(document).height());
			$('#' + ObjId + '_snow').css("left", "0px");
			$('#' + ObjId + '_snow').css("top", "0px");
			$('#' + ObjId + '_snow').css("opacity", "0.7");
			$('#' + ObjId + '_snow').fadeIn("fast");
			flash = document.getElementsByTagName('embed');
			for ( var i = 0; i < flash.length; i++)
			{
				flash[i].style.visibility = 'hidden';
			}
		}
	}
	// -------------------------------------------------------------------------
	function ShowMarker()
	{
		if (!$('#snow').length)
		{
			$("body:first").append("<div id='snow' class='a ui-widget-overlay'></div>");
			$("#snow").width($("body:first").outerWidth(true));
			$("#snow").height($(document).height());
			$("#snow").css("left", "0px");
			$("#snow").css("top", "0px");
			$("#snow").css("opacity", "-0.3");
			$("#snow").fadeTo(5000, 0.7);
		}
		if (!$('#xyzLoading').length)
		{
			$("body:first").append("<div id='xyzLoading' class='a h ui-state-error ui-priority-primary ui-widet-content ui-corner-br' style='padding:4px;font-size:20px'>Loading...</div>");
		}
		$("#xyzLoading").css("z-index", "10");
		$("#xyzLoading").fadeIn("slow");
		$("#xyzLoading").css("left", "0px");
		$("#xyzLoading").css("top", $(window).scrollTop() + "px");
	}
	// -------------------------------------------------------------------------
	function PopUpMenu(NewObjId, e, Sender)
	{
		Obj = document.getElementById(NewObjId);
		if (Obj)
		{
			DestroyContener(NewObjId);
		}
		else
		{
			CreateContenerFor(e, NewObjId);
			Go(Sender);
		}
		return false;
	}
	// -------------------------------------------------------------------------
	function PopUpWin(NewObjId, e, Sender, Title)
	{
		if (typeof Title == 'undefined')
		{
			Title = "&nbsp;";
		}
		Obj = document.getElementById(NewObjId);
		if (Obj)
		{
			DestroyContener(NewObjId);
			return false;
		}
		else
		{
			CreateWindowFor(NewObjId, Title);
			if (Sender != null)
			{
				Go(Sender);
			}
		}
		return false;
	}
	// --------------------------------------------------------------------------
	function ScriptGo(url)
	{
		ShowMarker();
		var Get = url;
		Get += "&js=true";
		$.get(Get, function(data, textStatus)
		{
			OdbierzDane(data, textStatus)
		})
		return false;
	}
	// --------------------------------------------------------------------------
	function Go(SenderObj)
	{
		ShowMarker();
		var Get = SenderObj.href;
		Get += "&js=true";
		$.get(Get, function(data, textStatus)
		{
			OdbierzDane(data, textStatus)
		})
		return false;
	}
	// -----------------------------
	function FormGo(SenderObj)
	{
		if (BeforeSubmitUI(SenderObj))
		{
			this.ShowMarker();
			Get = SenderObj.getAttribute("action", 2);
			if (Get == null)
			{
				Get = "./";
			}
			Post = $(SenderObj).serialize() + "&js=true";
			$.post(Get, Post, function(data, textStatus)
			{
				OdbierzDane(data, textStatus)
			})
		}
		else
		{
			HideMarker();
		}
		return false;
	}
	// --------------------------------------------------------------------------
	function KillPopUp()
	{
		PopUps = document.getElementsByTagName('div');
		for (a = 0; a < PopUps.length; a++)
		{
			Obj = document.getElementsByTagName('div')[a];
			if ($(Obj).hasClass("PopUpWin") || $(Obj).hasClass("PopUpMenu"))
			{
				tmp = true;
				// alert("Find:"+$(Obj).attr("id"));
				for (b = 0; b < NotKill.length; b++)
				{
					if ($(Obj).attr("id") == NotKill[b])
					{
						tmp = false;
					}
				}
				if (tmp)
				{
					// alert(Obj.className);
					if ($(Obj).hasClass("PopUpWin")) // to nie zamyka PopUpMenu
					{
						// alert("Close:"+$(Obj).attr("id"));
						DestroyContener($(Obj).attr("id"))
						// $(Obj).dialog('close');
						a--; // to cofa licznik divów bo po usunięciu jednego zmniejsza się ich liczba
					}
				}
				else
				{
					if ($(Obj).hasClass("PopUpWin"))
					{
						if ($(Obj).attr("ajax_centered") == "0")
						{
							// centruj dialog
							var parid = $(Obj).parent().parent().parent().parent().parent().attr("id");
							SetCenterPosition(document.getElementById(parid));
							$(Obj).attr("ajax_centered", "1");
							ResizeSnow(Obj);
							// var szer = $(Obj).parent().outerWidth();
							// var wyso = $(Obj).parent().outerHeight();
							// alert("Center "+$(Obj).parent().attr("id"));
							//
							// var p = $(Obj).parent().position();
							// var ax = p.left;
							// var ay = p.top;
							//							
							// var winWidth = $(window).width();
							// var winHeight = $(window).height();
							//							
							// var vx = (winWidth - szer) /2;
							// if(vx <0 )
							// vx = 0;
							// var vy = (winHeight - wyso) /3;
							// if(vy <0 )
							// vy = 0;
							// vy += $(window).scrollTop();
							// var
							// SetCenterPosition();
						}
					}
				}
			}
		}
	}
	// --------------------------------------------------------------------------
	function OdbierzDane(data, textStatus)
	{
		NotKill = new Array;
		if (textStatus == "success")
		{
			// try
			{
				var Okna = data.getElementsByTagName("popupwin");
				for (a = 0; a < Okna.length; a++)
				{
					var Okno = Okna[a];
					var ObjId = Okno.getAttribute("id");
					var Title = Okno.getAttribute("title");
					PopUpWin(ObjId, null, null, Title);
				}
				var Atrybuty = data.getElementsByTagName("atrybut");
				for (a = 0; a < Atrybuty.length; a++)
				{
					var Atrybut = Atrybuty[a];
					var ObjId = Atrybut.getAttribute("id");
					var attrbName = Atrybut.getAttribute("name");
					var attrbValue = Atrybut.getAttribute("value");
					$("#" + ObjId).attr(attrbName, attrbValue);
				}
				var Zmiany = data.getElementsByTagName('zmiana');
				for (a = 0; a < Zmiany.length; a++)
				{
					Zmiana = Zmiany[a];
					Obj = Zmiana.childNodes[0];
					NotKill[a] = Zmiana.getAttribute("id");
					NotKill[a + Zmiany.length] = Zmiana.getAttribute("parent");
					tmp = document.getElementById(Zmiana.getAttribute("id"));
					if (tmp)
					{
						if (Obj.nodeValue != "")
						{
							$("#" + Zmiana.getAttribute("id")).html(Obj.nodeValue);
							$("#" + Zmiana.getAttribute("id")).slideDown("fast");
						}
						else
						{
							$("#" + Zmiana.getAttribute("id")).slideUp("fast", function()
							{
								$(this).html("");
							});
						}
					}
					// else
					// {
					// AlertBox("Błąd","Nie ma obiektu o id: "+Zmiana.getAttribute("id"));
					// }
				}
				if (KillPopUpWin)
				{
					KillPopUp();
				}
				HideMarker();
			}
			// catch(err)
			// {
			// AlertBox(data,"Błąd serwera:<br>"+err);
			// }
			HideMarker();
		}
		else
		{
			HideMarker();
			AlertBox(data, textStatus);
		}
	}
	// --------------------------------------------------------------------------
	function AlertBox(Text, Title)
	{
		// if(typeof(Title) == 'undefined')
		// Title = "&nbsp;";
		//		
		// ContBody = "" +
		// "<div id='MessageBox' class='h PopUpWin' title='"+Title+"' ajax_centered='0' >" +
		// "</div>";
		// $("body:first").append(ContBody);
		// $("#MessageBox").text(Text);
		// $("#MessageBox").dialog({
		// bgiframe: false,
		// modal: true,
		// dialogClass: 'alert',
		// autoOpen: true,
		// width: 600,
		// resizable: true,
		// buttons:{ "Ok": function() { $(this).dialog("close")} } ,
		// close: function(event, ui) {$("#MessageBox").remove();return true}
		// });
		CreateWindowFor("PopUpMsgBox", Title, Text);
	}
	// --------------------------------------------------------------------------
	function CreateWindowFor(ObjId, Title, Body)
	{
		// AddSnow(ObjId);
		ShowSnow(ObjId);
		if (typeof Title == 'undefined')
		{
			Title = "&nbsp;";
		}
		if (typeof Body == 'undefined')
		{
			Body = "<div class='ImageBoxIndicator'></div>";
		}
		$(".PopUpMenu").each(function()
		{
			$(this).remove()
		});
		Obj = document.getElementById(ObjId);
		if (Obj)
		{
			$("#" + ObjId + "_contener").remove();
		}
		ContBody = "" + "<table class='sto'>" + "<tr>" + "<td class='Frame_lt'></td>" + "<td class='Frame_t'>" + "<p class='b color_white'>" + Title + "</p>" + "</td>" + "<td class='Frame_t r'>" + "<img alt='close' title='Zamknij' OnMouseOver='$(this).attr(\"src\",\"/img/cross_a.png\")' OnMouseOut='$(this).attr(\"src\",\"/img/cross_p.png\")' class='hand' src='/img/cross_p.png' OnClick='DestroyContener(\"" + ObjId + "\")' >" + "</td>" + "<td class='Frame_rt'></td>" + "</tr>" + "<tr>" + "<td class='Frame_l'></td>" + "<td class='Frame_m' colspan=2>" + "<div id='" + ObjId + "' class='PopUpWin' ajax_centered='0' >" + Body + "</div>" + "</td>" + "<td class='Frame_r'></td>" + "</tr>" + "</table>";
		$("body:first").append("<div id='" + ObjId + "_contener' class='a'></div>");
		$("#" + ObjId + "_contener").append(ContBody);
		SetCenterPosition(document.getElementById(ObjId + "_contener"));
	}
	// --------------------------------------------------------------------------
}
// ------------------------------------------------------------------------------
function SetClickPosition(Zdarzenie, TTObj)
{
	if (ie)
	{
		X = Zdarzenie.clientX + document.documentElement.scrollLeft + 18;
		// X = X - (TTObj.offsetWidth /2 );
		Y = Zdarzenie.clientY + document.documentElement.scrollTop + 5;
	}
	else
	{
		Y = Zdarzenie.pageY + 18;
		X = Zdarzenie.pageX + 5;
	}
	TTObj.setAttribute("style", "top:" + Y + "px;left:" + X + "px;");
}
// -------------------------------------------------------------------------
function DestroyContener(ObjId)
{
	var tmp = ObjId.substr(ObjId.length - 9);
	if (tmp == "_contener")
	{
		ObjId = ObjId.substr(0, ObjId.length - 9)
	}
	Obj = document.getElementById(ObjId + "_contener");
	if (Obj)
	{
		BodyObj = document.getElementsByTagName("body");
		BodyObj[0].removeChild(Obj);
		Obj = document.getElementById(ObjId + "_snow");
		if (Obj)
		{
			BodyObj = document.getElementsByTagName("body");
			BodyObj[0].removeChild(Obj);
		}
	}
	$(".PopUpMenu").each(function()
	{
		$(this).remove();
	});
	flash = document.getElementsByTagName('embed');
	for ( var i = 0; i < flash.length; i++)
	{
		flash[i].style.visibility = 'visible';
	}
}
// -------------------------------------------------------------------------
function SetCenterPosition(Obj)
{
	var szer = $(Obj).outerWidth();
	var wyso = $(Obj).outerHeight();
	var winWidth = $(window).width();
	var winHeight = $(window).height();
	var vx = (winWidth - szer) / 2;
	if (vx < 0)
	{
		vx = 0;
	}
	var vy = (winHeight - wyso) / 3;
	if (vy < 0)
	{
		vy = 8;
	}
	vy += $(window).scrollTop();
	$(Obj).attr("style", "top:" + vy + "px;left:" + vx + "px;");
}
// -------------------------------------------------------------------------
function CenterImgBox(Obj)
{
	var szer = $(Obj).outerWidth();
	var wyso = $(Obj).outerHeight();
	var p = $(Obj).position();
	var ax = p.left;
	var ay = p.top;
	var winWidth = $(window).width();
	var winHeight = $(window).height();
	var vx = (winWidth - szer) / 2;
	if (vx < 0)
	{
		vx = 0;
	}
	var vy = (winHeight - wyso) / 3;
	if (vy < 0)
	{
		vy = 0;
		// vy += $(window).scrollTop();
	}
	if (vx - ax > 50 || vx - ax < -50 || vy - ay > 50 || vy - ay < -50)
	{
		$(Obj).animate(
		{
			left : vx,
			top : vy
		}, "fast", "linear", function()
		{
			ResizeSnow(Obj)
		});
	}
	else
	{
		ResizeSnow(Obj);
	}
}
// -------------------------------------------------------------------------
function ResizeSnow(Obj)
{
	var ObjId = $(Obj).attr("id");
	var tmp = ObjId.substr(ObjId.length - 9);
	if (tmp == "_contener")
	{
		ObjId = ObjId.substr(0, ObjId.length - 9)
	}
	$("#" + ObjId + "_snow").width($("body:first").outerWidth(true));
	$("#" + ObjId + "_snow").height($(document).height());
}
// -------------------------------------------------------------------------
function MoveBox(e, ObjId)
{
	Y = e.pageY - 10;
	X = e.pageX - $("#" + ObjId + "_contener").outerWidth() / 2;
	var TTObj = document.getElementById(ObjId + "_contener");
	TTObj.setAttribute("style", "top:" + Y + "px;left:" + X + "px;");
}
// -------------------------------------------------------------------------
function LeaveBox()
{
	$("body:first").unbind('mousemove');
}
// -------------------------------------------------------------------------
function ReadyToMove(Sender, ObjId)
{
	$("#" + ObjId + "_contener").addClass("moved");
	var p = $("#" + ObjId + "_contener").position();
	var ax = p.left - 2;
	var ay = p.top - 2;
	$("#" + ObjId + "_contener").css("left", ax + "px");
	$("#" + ObjId + "_contener").css("top", ay + "px");
	$("#VVV").text("przed: " + p.left + " Po: " + ax);
	$("body:first").mousemove(function(e)
	{
		MoveBox(e, ObjId)
	});
}
// -------------------------------------------------------------------------
function CreateContenerFor(e, ObjId)
{
	$(".PopUpMenu").each(function()
	{
		$(this).remove()
	});
	$("body:first").append("<div id='" + ObjId + "_contener' class='a PopUpMenu' style='z-index:2000'></div>");
	// if (!div)
	// {
	retval = "<table id='TableToolTip'>";
	retval += "<tr>";
	retval += "<td id='LeftTopToolTip'></td>";
	retval += "<td id='TopToolTip' class='r d' >" +
	// "<img style='cursor:pointer' OnClick='DestroyContener(\""+ObjId+"\")' alt='' src='/img/cancel.png'>" +
			"</td>";
	retval += "<td id='RightTopToolTip'></td>";
	retval += "</tr>";
	retval += "<tr>";
	retval += "<td id='LeftToolTip'></td>";
	retval += "<td id='ToolTip'><div id='" + ObjId + "'>Loading...</div></td>";
	retval += "<td id='RightToolTip'></td>";
	retval += "</tr>";
	retval += "<tr>";
	retval += "<td id='LeftBottomToolTip'></td>";
	retval += "<td id='BottomToolTip'></td>";
	retval += "<td id='RightBottomToolTip'></td>";
	retval += "</tr>";
	retval += "</table>";
	// div = document.createElement('div');
	// div.setAttribute("id",);
	// div.className = "a PopUpMenu";
	// BodyObj = document.getElementsByTagName("body");
	// BodyObj[0].appendChild(div);
	// div.innerHTML = retval;
	// }
	$("#" + ObjId + "_contener").html(retval);
	var ie = document.getElementById && document.all;
	if (ie)
	{
		vx = e.clientX + document.documentElement.scrollLeft + 2;
		vy = e.clientY + document.documentElement.scrollTop + 2;
	}
	else
	{
		vy = e.pageY + 2;
		vx = e.pageX + 2;
	}
	// if($("#"+ObjId+"_contener").outerHeight() + vy > $(window).height())
	// vy = vy -$("#"+ObjId+"_contener").outerHeight();
	//	
	$("#" + ObjId + "_contener").show();
	$("#" + ObjId + "_contener").css("left", vx + "px");
	$("#" + ObjId + "_contener").css("top", vy + "px");
	// return div;
}
// =========================================================================

