﻿
// ----------------------------------------

function CTopoliszControls (pTopoliszMap)
{
	this.pTopoliszMap = pTopoliszMap;
	this.bFullScreen = false;

	// control boxes
	this.divContent = null;
	this.trTitle = null;
	this.divRouteResult = null;

	this.pRouteSearch = null;
	this.pRouteResult = null;
	this.pExtraInfo = null;
	this.pPoiSearch = null;
	this.divUserData = null;

	this.pLogin = null;
	this.pRegistration = null;

	this.mainMenu = null;
	
	this.aBabelTraffic = null;
	this.aBabelActual = null;
	this.aBabelSearch = null;
	this.aBabelLogin = null;
	
	this.pLayerPoiTreeMemo = null;
	
	this.pLanguageSupporter = new LanguageSupporter ();

	var me = this;
	
	this.CreateMainMenu = function (div)
	{
		me.MainMenu = new Menu(GetBlock('idMainMenu'));
		
		if (g_InitSettings.bShowMenuTraffic)
		    me.aBabelTraffic = me.MainMenu.AddMenuItemLeft(g_Babel.GetWord('mmTraffic2'), me.ShowRouteSearch);
		if (g_InitSettings.bShowMenuExtraInfo)
		    me.aBabelActual = me.MainMenu.AddMenuItemLeft(g_Babel.GetWord('mmActual'), me.ShowActualSettingsBlock);
		if (g_InitSettings.bShowMenuSearch)
		    me.aBabelSearch = me.MainMenu.AddMenuItemLeft(g_Babel.GetWord('mmSearch'), me.ShowSearchBlock);
		me.aBabelLogin = me.MainMenu.AddMenuItemLeft(g_Babel.GetWord("capLogin"), me.ShowLogin);
		me.aBabelGyik = me.MainMenu.AddMenuItemLeft(g_Babel.GetWord("capGYIK"), null, "http://www.utvonalterv.hu/utvonaltervgyik.html");
		me.MainMenu.AddMenuItemLeft("Link", null, "link.html");
		
		me.pLanguageSupporter.Add(me.aBabelLogin, "innerHTML", "capLogin", "", "");
		me.pLanguageSupporter.Add(me.aBabelGyik, "innerHTML", "capGYIK", "", "");
		
		var td = me.MainMenu.CreateTdRight();
		td.style.textAlign = "right";
		td.style.width = "160px";
				
		var img = NewTag ('img', td);
		SetImage (img, "images/hun.gif", 31, 20, "magyar");
		img.style.paddingLeft = "5px";
		img.style.paddingRight = "5px";
		img.onmouseover = function () {Tip("magyar");};
		img.onmouseout = function () {UnTip();};
		img.onclick= function () {me.SetLang('hu');};
		
		img = NewTag ('img', td);		
		SetImage (img, "images/gbr.gif", 31, 20, "english");
		img.style.paddingLeft = "5px";
		img.style.paddingRight = "5px";
		img.onmouseover = function () {Tip("english");};
		img.onmouseout = function () {UnTip();};
		img.onclick= function () {me.SetLang('en');};
		
		
		img = NewTag ('img', td);
		SetImage (img, "images/sk.gif", 31, 20, "slovenské");
		img.style.paddingLeft = "5px";
		img.style.paddingRight = "5px";
		img.onmouseover = function () {Tip("slovenské");};
		img.onmouseout = function () {UnTip();};
		img.onclick= function () {me.SetLang('sk');};

		img = NewTag ('img', td);
		SetImage (img, "images/ro.gif", 31, 20, "român");
		img.style.paddingLeft = "5px";
		img.style.paddingRight = "5px";
		img.onmouseover = function () {Tip("român");};
		img.onmouseout = function () {UnTip();};
		img.onclick= function () {me.SetLang('ro');};
		
				
		//me.MainMenu.AddMenuItemLeft(g_Babel.GetWord('mmToFavourites'), me.AddToFavourites);
	}
	
	this.SetLang = function(strLang)
	{
		if(document.location.search == null || document.location.search == "")
			document.location = document.location.pathname + "?lang=" + strLang;
		else
		{
			var arrParams = location.search.substr(1).split("&");
			var bFound = false;
			for(var i=0; i<arrParams.length; i++)
			{
				var iPos = arrParams[i].indexOf("=");
				if(iPos >= 0)
					strKey = arrParams[i].substr(0, iPos);
				else
					strKey = arrParams[i];
				strKey = strKey.toLowerCase();
				if(strKey == "lang")
				{
					bFound = true;
					arrParams[i] = "lang=" + strLang;
				}
			}
			if(!bFound)
				arrParams[arrParams.length] = "lang=" + strLang;
				
			var aj = arrParams.join('&');
			location.href = location.pathname + "?" + aj;
		}
		
		//document.location = "?lang="
	}

	this.SetContent = function(divContent, trTitle)
	{
		me.divContent = divContent;
		me.trTitle = trTitle;
	}
	this.SetRouteResult = function(divRouteResult)
	{
		me.divRouteResult = divRouteResult;
	}
	
	this.HideLeft = function()
	{
		var div = me.divContent.firstChild;
		while(div)
		{
			if(div.nodeName != "#text")
			{
				div.style.display = "none";
			}
			div = div.nextSibling;
		}
	}

	this.SetTitle = function(strTitle)
	{
		if(me.trTitle)
		{
			if(g_InitSettings.bShowMenuSearch)
			{
				for(var td = me.trTitle.firstChild; td != null; td = td.nextSibling)
				{
					if(td.nodeName.toLowerCase() != "td")
						continue;
					RemoveCssClassPostfix(td, "_on");
					var strTextContent = "";
					var a = td.getElementsByTagName("a");
					if(a != null && a.length > 0)
					{
						strTextContent = a[0].innerHTML;
					}
					if(strTextContent == strTitle)
					{
						td.className += "_on";
					}
				}
			}
			else
			{
				for(var td = me.trTitle.firstChild; td != null;)
				{
					if(td.nodeName.toLowerCase() != "td")
					{
						td = td.nextSibling;
						continue;
					}
					RemoveCssClassPostfix(td, "_on");
					var strTextContent = "";
					var a = td.getElementsByTagName("a");
					if(a != null && a.length > 0)
					{
						strTextContent = a[0].innerHTML;
					}
					if(strTextContent == strTitle)
					{
						td.className = "UI_panel_header";
					}
					else
					{
						var tdDel = td;
						td = td.nextSibling;
						me.trTitle.removeChild(tdDel);
						continue;
					}
					td = td.nextSibling;
				}
				var trCsik = me.trTitle.nextSibling;
				

				 $(document).ready(function() 
				 {
					$("table.UI_panel").find("tr:first").each(function(iIndex, tr)
					{
						if(! $(tr).find("td.UI_panel_top_left").size() 
						  || !$(tr).find("td.UI_panel_top").size()
						  || !$(tr).find("td.UI_panel_top_right").size() )
						{
							$(tr).before("<tr><td class='UI_panel_top_left'></td><td class='UI_panel_top'></td><td class='UI_panel_top_right'></td></tr>");	
						}
						//$(tr).addClass("green");
					})
				 });
				
			}
		}
	}
	
	this.EnablePrinting = function(bEnable)
	{
		var div = GetBlock("tdLeftPart");
		div.className = (bEnable? "" : "noprint");
	}

	this.ShowLastControl = function()
	{
		var strLastPage = getCookie("page");
		if(strLastPage != null)
		{
			strLastPage.toLowerCase();
			
			if(strLastPage == "poi" && g_InitSettings.bShowMenuSearch)
				me.ShowSearchBlock();
			else if(strLastPage == "extra" && g_InitSettings.bShowMenuExtraInfo)
				me.ShowActualSettingsBlock();
			else
				me.ShowRouteSearch();
		}
		else
			me.ShowRouteSearch();
	}
	
	this.SetLastControl = function(strLastPage)
	{
		if(g_InitSettings.strCountryName == "Magyarország")
			setCookie("page", strLastPage);
	}

	this.ShowLogin = function()
	{
		me.RestoreLeftControl();
		me.EnablePrinting(true);
		//var tt = me.SetTitle(g_Babel.GetWord("capLogin"));
		//me.pLanguageSupporter.Add(tt, "nodeValue", "capLogin", "", "");
		
		me.HideLeft();
		if(me.pLogin == null)
		{
			var div = NewTag('div', me.divContent);
			me.pLogin = new LoginBox(div, me);
		}
		me.pLogin.Show();
	}
	
	this.ShowRegistration = function()
	{
		var iframe = NewIframe("Register.aspx?langid=" + g_Babel.GetLang(), 260,340);
		g_PopUpHandler.SetContent(g_Babel.GetWord('mmRegistration'), iframe);
		g_PopUpHandler.SetSize(270, 400);
		g_PopUpHandler.CenterPopup();
		g_PopUpHandler.ShowPopup();
	}
	
	this.ShowForgottenPassword = function()
	{
		var iframe = NewIframe("sendpasswd.aspx?langid=" + g_Babel.GetLang(), 310,100);
		g_PopUpHandler.SetContent(g_Babel.GetWord('sendingForgottenPassword'), iframe);
		g_PopUpHandler.SetSize(320, 150);
		g_PopUpHandler.CenterPopup();
		g_PopUpHandler.ShowPopup();
	}

	this.ShowRouteSearch = function ()
	{
		me.SetLastControl("traffic");
		me.RestoreLeftControl();
		me.EnablePrinting(false);
		me.SetTitle(g_Babel.GetWord('mmTraffic2'));

		me.HideLeft();

		if(me.pRouteSearch == null)
		{
			var div = NewTag('div', me.divContent);
			me.pRouteSearch = new CUtvonaltervRouteSearch(div, me.pTopoliszMap);
			me.pRouteResult = new CUtvonaltervRouteResult(me.divRouteResult, me.pTopoliszMap);
			me.pRouteSearch.SetResultHandler(me.pRouteResult);
			me.pRouteSearch.Show();

		}
		else
			me.pRouteSearch.Unhide ();
	}

	this.ShowActualSettingsBlock = function ()
	{
		me.SetLastControl("extra");
		me.RestoreLeftControl();
		me.EnablePrinting(true);
		me.SetTitle(g_Babel.GetWord('mmActual'));
		
		// a "KERESETT POIK" megjelentítésének törlése
		if (me.pPOIBOX)
			me.pPOIBOX.ResetFindPoi ();

		// Az útvonal törlése
						
		if(me.pRouteSearch)
		{			
			//me.pRouteSearch.Reset();
		}
		else
		{
			me.pTopoliszMap.DrawRoute ();  // pötty rajzolás
		}

		me.HideLeft();

		if (me.pExtraInfo == null)
		{
			var div = NewTag('div', me.divContent);
			me.pExtraInfo = new ExtraInfo(div, me.pTopoliszMap);
			me.pExtraInfo.Show();
		}
		else
		{
			me.pExtraInfo.Unhide();
		}
	}
	

	this.ShowSearchBlock = function ()
	{
		me.SetLastControl("poi");
		me.RestoreLeftControl();
		me.EnablePrinting(true);
		me.SetTitle(g_Babel.GetWord('mmSearch'));		

		// a "RÉTEGES POIK" megjelentítésének törlése
		if(me.pExtraInfo)
			me.pExtraInfo.ResetPoiLayers();

		// Az útvonal törlése
				
		//if(me.pRouteSearch)
		//	me.pRouteSearch.Reset();


		me.HideLeft();

		if (me.pPoiSearch == null)
		{
			var div = NewTag('div', me.divContent);
			me.pPoiSearch = new PoiSearch(div, me.pTopoliszMap);
			me.pPoiSearch.Show();
		}
		else
		{
			me.pPoiSearch.Unhide();
		}
	}


	this.RestoreLeftControl = function()
	{
		var div = GetBlock('tdLeftPart');
		if(GetStyle(div, 'display') == 'none')
		{
			div.style.display = '';			
			Resizer.OnResize();
			me.pTopoliszMap.RedrawWithoutZooming();
		}
	}




	this.SwitchFavouritePlace = function (bChecked)
	{
		if (bChecked == true)
		{
			me.pTopoliszMap.NoSessionServer.CallFunction("GetFavouritePlace", "", 0, me.DrawFavouritePlace);
		}
		else
		{
			me.pTopoliszMap.RemovePinBoxes ("FavouritePlace");
			me.pTopoliszMap.RemovePinLabels ("FavouritePlace");
			me.pTopoliszMap.pMarkers.DeleteAllWithType ("FavouritePlace");
		}
	}

	this.DrawFavouritePlace = function (FavouritePlacelist, pDummy, iCommandID) 
	{
		if (FavouritePlacelist) {
			var length = FavouritePlacelist.length;
			var i = 0;
			for ( i = 0; i < length; i++)
			{
				var dX = FavouritePlacelist[i].x;
				var dY = FavouritePlacelist[i].y;				
				var type = "favplace";
				var marker = new Marker(me.pTopoliszMap, type, dX, dY, null, null);
				marker.SetProperty ("caption", FavouritePlacelist[i].name);
				marker.SetProperty ("pin_img_refx", 5);
				marker.SetProperty ("pin_img_refy", 5);
				marker.SetProperty ("pin_img_url", GetAbsUrl("design/ICON_favourites_on.gif"));
				marker.SetProperty ("pin_img_width", 12);
				marker.SetProperty ("pin_img_height", 12);
				marker.SetProperty ("pin_css_class", "markerFavouritePlace");
				marker.SetProperty ("div_label_sign_class","markerFavouritePlace");
				marker.SetProperty ("start_button", true);
				marker.SetProperty ("through_button", true);
				marker.SetProperty ("stop_button", true);
				marker.SetProperty ("panorama_button", me.pTopoliszMap.bLabelHasPanoramaButton);
				marker.SetProperty ("pinnumber", 0);
				marker.SetProperty ("handle_details", false);
				marker.SetProperty ("delete_marker_on_label_close", false);
				marker.SetProperty ("show_coordinates", me.pTopoliszMap.bDisplayCoords);
				me.pTopoliszMap.pMarkers.Add (marker) ;  // favplaces
			}
			me.pTopoliszMap.DrawRoute ();
		}
	}


	this.OnChangeLang = function ()
	{
		if (me.aBabelTraffic != null) me.aBabelTraffic.innerHTML = g_Babel.GetWord('mmTraffic2');
		if (me.aBabelActual != null) me.aBabelActual.innerHTML = g_Babel.GetWord('mmActual');
		if (me.aBabelSearch != null) me.aBabelSearch.innerHTML = g_Babel.GetWord('mmSearch');
		
		me.pLanguageSupporter.Apply();
	}

	this.Register = function()
	{
		cancelRegistration = function() {
			GetBlock("idModalDialogHolder").style.display = "none";
		}

		GetBlock("idDialogFrame").src = "Register.aspx?langid=" + g_Babel.GetLang();
		GetBlock("idDialogFrame").style.width = "500px";
		GetBlock("idDialogFrame").style.height = "380px";
		GetBlock("idDialogFrame").style.padding = "0px";
		GetBlock("idDialogFrame").style.margin = "0px";
		GetBlock("idModalDialogHolder").style.display = "block";
	}

	this.ShowUserData = function()
	{
		var iframe = NewIframe("Adatlap.aspx?langid=" + g_Babel.GetLang(), 260,350);
		g_PopUpHandler.SetContent(g_Babel.GetWord('capPersonalForm'), iframe);
		g_PopUpHandler.SetSize(270, 400);
		g_PopUpHandler.CenterPopup();
		g_PopUpHandler.ShowPopup();
	}
	
	this.ShowLoginError = function()
	{
		var div = NewTag("div");
		var p = NewTag("p", div);
		NewTextTag(g_Babel.GetWord("msgLoginFailed"), p);
		var p = NewTag("p", div);
		p.style.textAlign = "right";
		var a = NewTag("a", p);
		a.href="javascript:void(0)";
		a.className = "UI_button_OK";
		var img = NewTag("img",a);
		SetImage(img, "design/BUTTON_ok.gif");
		AddEvent(a,"click", g_PopUpHandler.HidePopup);

		g_PopUpHandler.SetContent("Utvonalterv.hu", div);
		g_PopUpHandler.SetSize(270, 120);
		g_PopUpHandler.CenterPopup();
		g_PopUpHandler.ShowPopup();
	
	}
	
	this.Logout = function()
	{
		var input = GetBlock("idLoginCommand");
		if(input)
			input.value = "Logout";
		var form = GetBlock("idLoginForm");
		if(form)
			form.submit();
	}

	this.AddToFavourites = function()
	{
		AddBookmark("http://www.utvonalterv.hu", "Útvonalterv.hu");
	}


	this.HideUsefulDivOnMouseMove = function(pEvent)
	{
		var usefulbox = $("#usefulbox").get(0);
		if(usefulbox && usefulbox.style.display != "none")
		{
			var iMouseX = GetEventXNew(pEvent);
			var iMouseY = GetEventYNew(pEvent);
			var iDelta = 10;
			
			var iMinX = GetAbsLayerLeft(usefulbox);
			var iMinY = GetAbsLayerTop(usefulbox);
			var iMaxX = GetAbsLayerRight($("#idUsefulLinkImg").get(0));
			var iMaxY = GetAbsLayerBottom(usefulbox);
								
			
			if(iMouseX < iMinX-iDelta || iMouseX > iMaxX+iDelta || iMouseY < iMinY-iDelta || iMouseY > iMaxY+iDelta)
			{
				$(usefulbox).slideUp("fast");
			}
		}
	}

	// Constructor
	this.Init = function ()
	{
		g_Babel.AddListener(me.OnChangeLang);
	}

	me.Init ();
}
