// Copyright (c) Gate17 System www.gate17.net by Marc Krisnanto. All rights reserved.

var App =
{
	// the main menu bar DIV element
	Menu : null
};


App.logout = function ()
{
	try { Server.get ("/command?a=qLogout"); } catch (err) { }
	var u = document.getElementById ("username");
	var g = document.getElementById ("logout");
	if (u)
		u.style.display = "none";
	if (g)
		g.style.display = "none";
};


App.setLogout = function ()
{
	var u = document.getElementById ("username");
	var g = document.getElementById ("logout");
	try
	{
		var x = Server.get ("/command?a=qGetUser");
		if (x)
		{
			if (u)
			{
				u.innerHTML = x;
				u.style.display = "inline";
			}
			if (g)
				g.style.display = "inline";
		}
	}
	catch (err)
	{
		if (u)
			u.style.display = "none";
		if (g)
			g.style.display = "none";
	}
};


G.setError = function (x)
{
	alert (x);
};


G.setInfo = function (x)
{
	window.status = x;
};


function htmlProcess (s)
{
	function tagparam (s)
	{
		var r = {};
		var param, value;
		var i = 0;
		s = s.trim ();
		while (true)
		{
			j = s.indexOf ("=", i);
			if (j < 0)
				break;
			param = s.substr (i, j - i).trim ();
			i = s.indexOf ('"', j + 1);
			if (i < 0)
				break;
			j = s.indexOf ('"', i + 1);
			if (j < 0)
				break;
			value = s.substr (i + 1, j - i - 1);
			i = j + 1;
			r [param] = value;
		}
		return r;
	}

	var r = [];
	var k = 0;

	s = s.replace (/<--*-->/g, '');

	var t = s.length;

	while (k < t)
	{
		var tag = -1, closer, len, title, rest, i, j, m;

		if ((i = s.indexOf ("<", k)) >= 0)
		{
			if (s.substr (i, 6) == "<link ")
			{
				tag = 1;
				len = 6;
				closer = "";
			}
			else if (s.substr (i, 7) == "<media ")
			{
				tag = 2;
				len = 7;
				closer = "";
			}
			else if (s.substr (i, 7) == "<imgof ")
			{
				tag = 3;
				len = 7;
				closer = "";
			}
			else if (s.substr (i, 8) == "<fileof ")
			{
				tag = 4;
				len = 8;
				closer = "</fileof>";
			}
			else if (s.substr (i, 8) == "<search ")
			{
				tag = 5;
				len = 8;
				closer = "";
			}
			else
			{
				if (k < i)
					r.push ([0, null, s.substr (k, i - k)]);
				r.push ([0, null, '<']);
				k = i + 1;
				continue;
			}
		}
		else
		{
			s = s.substr (k);
			if (s)
				r.push ([0, null, s]);
			break;
		}

		j = s.indexOf (">", i + len);
		if (j <= i)
			break;

		if (closer)
		{
			m = s.indexOf (closer, j + 1);
			if (m < 0)
				break;
		}
		else
		{
			m = j + 1;
		}

		if (k < i)
			r.push ([0, null, s.substr (k, i - k)]);

		title = s.substr (j + 1, m - j - 1).trim ();
		rest  = s.substr (i + len, j - i - len);

		r.push ([tag, title, rest]);
		k = m + closer.length;
	}

	s = "";
	for (var i = 0; i < r.length; i++)
	{
		var part  = r [i];
		var tag   = part [0];
		var title = part [1];
		var rest  = part [2];
		var param;
		var obj;

		switch (tag)
		{
			case 1: // <link oid="1" title="bla">
				param = tagparam (rest);
				if (param.oid == '0')
				{
					part = '[' + (param.title || '[LINK ?') + ']';
				}
				else
				{
					try
					{
						obj = Server.get ('/command?a=qGet&i=' + param.oid);
						obj = eval ('[' + obj + '][0]');
						part = '<a href="/view.htm?i=' + param.oid + '">' + (param.title || obj.name || obj.title) + '</a>';
					}
					catch (err)
					{
						part = '[OBJECT oid=' + param.oid + ']';
					}
				}
				break;

			case 2: // <media oid="1" ...>
				param = tagparam (rest);
				try
				{
					obj = Server.get ('/command?a=qGet&i=' + param.oid);
					obj = eval ('[' + obj + '][0]');
					if (obj.kind == 0)
						part = '<img src="/command?a=qGetFile&o=' + param.oid + '&i=0">';
					else
						part = '<a href="/command?a=qGetFile&o=' + param.oid + '&i=0">' +
									H(param.title || obj.title) + '</a>';
				}
				catch (err)
				{
					part = '[MEDIA oid=' + param.oid + ']';
				}
				break;

			case 3: // <imgof oid="123" idx="1" ...>
				param = tagparam (rest);
				part = '<img src="/command?a=qGetFile&i=' + param.oid + '&j=' + param.idx + '"';
				delete param.oid;
				delete param.idx;
				for (k in param)
					part += ' ' + k + '="' + param [k] + '"';
				part += '>';
				break;

			case 4: // <fileof oid="123" idx="0" ...>click here</fileof>
				param = tagparam (rest);
				part = '<a href="/command?a=qGetFile&r=1&i=' + param.oid + '&j=' + param.idx + '"';
				delete param.oid;
				delete param.idx;
				for (k in param)
					part += ' ' + k + '="' + param [k] + '"';
				part += '>' + title + '</a>';
				break;

			case 5: // <search query="foo" title="blah">
				param = tagparam (rest);
				part = '<a href="/query.htm?q=' + encodeURIComponent (param.query) + '"';
				title = param.title || param.query;
				delete param.query;
				delete param.title;
				for (k in param)
					part += ' ' + k + '="' + param [k] + '"';
				part += '>' + title + '</a>';
				break;

			default:
				part = rest;
				break;
		}
		s += part;
	}
	return s;
}



function cmdMainQuery ()
{
	var q = document.getElementById ("SearchText").value.trim ();
	if (q)
		document.location = '/query.htm?q=' + encodeURIComponent (q);
}


function cmdMainHome ()
{
	document.location = '/'
}


function cmdMainSitemap ()
{
	document.location = '/sitemap.htm';
}


function cmdMainFaq ()
{
	document.location = '/faq.htm';
}


function cmdMainContact ()
{
	document.location = '/contact.htm';
}


function cmdMainAbout ()
{
	document.location = '/about.htm';
}


function cmdMainJoin ()
{
	document.location = '/join.htm'
}


function cmdMainLogin ()
{
	document.location = '/login.htm'
}


function cmdTravelAbout ()
{
	//document.location = '/view.htm?a=qView&i=....';
	document.location = '/static/travel/about.htm';
}


function cmdTravelHow ()
{
	//document.location = '/view.htm?a=qView&i=....';
	document.location = '/static/travel/howget.htm';
}


function cmdTravelHotel ()
{
	document.location = '/static/travel/hotel.htm';
}


function cmdTravelGuestHouse ()
{
	document.location = '/static/travel/house.htm';
}


function cmdTravelAgent ()
{
	document.location = '/static/travel/agent.htm';
}


function cmdTravelTransportation ()
{
	document.location = '/static/travel/transport.htm';
}


function cmdExploreSearch ()
{
	document.location = '/search.htm';
}


function cmdExploreHeritage ()
{
	document.location = '/static/explore/sight/heritage.htm';
}

function cmdExploreNewSpot ()
{
	document.location = '/static/explore/sight/newspot.htm';
}

function cmdExploreMuseum ()
{
	document.location = '/static/explore/sight/museum.htm';
}

function cmdExploreChurch ()
{
	document.location = '/static/explore/sight/church.htm';
}

function cmdExploreTemple ()
{
	document.location = '/static/explore/sight/temple.htm';
}

function cmdExploreGarden ()
{
	document.location = '/static/explore/sight/garden.htm';
}

function cmdExploreFortress ()
{
	document.location = '/static/explore/sight/fortress.htm';
}

function cmdExploreIsland ()
{
	document.location = '/static/explore/sight/island.htm';
}

function cmdExploreExhibition ()
{
	document.location = '/static/explore/sight/exhibition.htm';
}

function cmdExploreOther ()
{
	document.location = '/static/explore/sight/other.htm';
}

function cmdExploreTourHighlight ()
{
	document.location = '/static/explore/tour/highlight.htm';
}


function cmdExploreTourWalking ()
{
	document.location = '/static/explore/tour/walking.htm';
}


function cmdExploreTourHistorical ()
{
	document.location = '/static/explore/tour/historical.htm';
}


function cmdExploreFoodGeneral ()
{
	document.location = '/static/explore/food.htm';
}


function cmdExploreFoodRestaurant ()
{
	document.location = '/static/explore/food/restaurant.htm';
}


function cmdExploreFoodSnack ()
{
	document.location = '/static/explore/food/snack.htm';
}


function cmdExploreEntertainment ()
{
	document.location = '/static/explore/entertainment.htm';
}


function cmdExploreShopping ()
{
	document.location = '/static/explore/shopping.htm';
}


function cmdExploreMap ()
{
	document.location = '/static/explore/map.htm';
}


function cmdEventCalendar ()
{
	document.location = '/static/event/calendar.htm';
}


function cmdEventMain ()
{
	document.location = '/static/event/main.htm';
}


function cmdMiceFacilityGeneral ()
{
	document.location = '/static/mice/facility.htm';
}


function cmdMiceFacilityCenter ()
{
	document.location = '/static/mice/facility/center.htm';
}


function cmdMiceFacilityHotel ()
{
	document.location = '/static/mice/facility/hotel.htm';
}


function cmdMiceFacilityOther ()
{
	document.location = '/static/mice/facility/others.htm';
}


function cmdMiceCalendar ()
{
	document.location = '/static/mice/calendar.htm';
}


function cmdInfoPress ()
{
	document.location = '/static/info/press.htm';
}


function cmdInfoCenter ()
{
	document.location = '/static/info/center.htm';
}


function cmdInfoMgto ()
{
	document.location = '/static/info/mgto.htm';
}


function cmdInfoContact ()
{
	document.location = '/static/info/contact.htm';
}


function cmdInfoHotline ()
{
	document.location = '/static/info/hotline.htm';
}


function cmdInfoOthers ()
{
	document.location = '/static/info/others.htm';
}


function cmdOtherForum ()
{
	document.location = '/static/others/forum.htm';
}


function cmdOtherGallery ()
{
	document.location = '/static/others/gallery.htm';
}


function cmdOtherEcard ()
{
	document.location = '/static/others/ecard.htm';
}


function cmdOtherWallpaper ()
{
	document.location = '/static/others/wallpaper.htm';
}


function cmdOtherVideo ()
{
	document.location = '/static/others/video.htm';
}



function itemToggle (e)
{
	var x = e.className == 'ItemTitleOff';
	e.className = x ? 'ItemTitleOn' : 'ItemTitleOff';
	if (isIE)
		e.nextSibling.style.display = x ? "block" : "none";
	else
		e.nextSibling.nextSibling.style.display = x ? "block" : "none";
}



App.init = function ()
{
	// Since no command need to be disabled, we don't need to create
	// G.Command ("cmdMainHome", cmdMainHome), etc. Simply use the functions
	// directly.

	// Generate the main menu
	App.Menu = document.getElementById ("Menu");

		var r, m, i, p, q;

		r = G.MenuItemRoot (App.Menu, 'Utama');
			m = G.MenuPopup (r);
				G.MenuItem (m, 'Halaman Muka'      , ".", cmdMainHome);
				G.MenuItem (m, 'Peta Situs'        , ".", cmdMainSitemap);
				G.MenuItem (m, 'Tanya Jawab'       , ".", cmdMainFaq);
				G.MenuItem (m, 'Hubungi Kami'      , ".", cmdMainContact);
				G.MenuItem (m, 'Mengenai Kami'     , ".", cmdMainAbout);
				G.MenuItem (m, 'Registrasi'        , ".", cmdMainJoin);
				G.MenuItem (m, 'Login'             , ".", cmdMainLogin);

		r = G.MenuItemRoot (App.Menu, 'Perjalanan');
			m = G.MenuPopup (r);
				G.MenuItem (m, 'Mengenai Macau'    , ".", cmdTravelAbout);
				G.MenuItem (m, 'Bagaimana ke Macau', ".", cmdTravelHow);
				p = G.MenuItem (m, 'Akomodasi ...'     , ".");
				q = G.MenuPopup (p);
					G.MenuItem (q, 'Hotel'         , ".", cmdTravelHotel);
					G.MenuItem (q, 'Guest house'   , ".", cmdTravelGuestHouse);
				G.MenuItem (m, 'Biro Perjalanan'   , ".", cmdTravelAgent);
				G.MenuItem (m, 'Transportasi Lokal', ".", cmdTravelTransportation);

		r = G.MenuItemRoot (App.Menu, 'Eksplorasi');
			m = G.MenuPopup (r);
				    G.MenuItem (m, 'Cari Obyek'       , ".", cmdExploreSearch);
				p = G.MenuItem (m, 'Tujuan Wisata ...', ".");
				q = G.MenuPopup (p);
					G.MenuItem (q, 'Warisan Sejarah', ".", cmdExploreHeritage);
					G.MenuItem (q, 'Tempat Baru'    , ".", cmdExploreNewSpot);
					G.MenuItem (q, 'Museum'         , ".", cmdExploreMuseum);
					G.MenuItem (q, 'Gereja'         , ".", cmdExploreChurch);
					G.MenuItem (q, 'Kuil'           , ".", cmdExploreTemple);
					G.MenuItem (q, 'Taman'          , ".", cmdExploreGarden);
					G.MenuItem (q, 'Benteng'        , ".", cmdExploreFortress);
					G.MenuItem (q, 'Kepulauan Luar' , ".", cmdExploreIsland);
					G.MenuItem (q, 'Eksibisi'       , ".", cmdExploreExhibition);
					G.MenuItem (q, 'Lainnya'        , ".", cmdExploreOther);
				p = G.MenuItem (m, 'Rekomendasi Tur ...' , ".");
				q = G.MenuPopup (p);
					G.MenuItem (q, 'Highlight'   , ".", cmdExploreTourHighlight);
					G.MenuItem (q, 'Jalan'       , ".", cmdExploreTourWalking);
					G.MenuItem (q, 'Sejarah'     , ".", cmdExploreTourHistorical);

				p = G.MenuItem (m, 'Kuliner ...' , ".");
				q = G.MenuPopup (p);
					G.MenuItem (q, 'Informasi Umum', ".", cmdExploreFoodGeneral);
					G.MenuItem (q, 'Restoran'      , ".", cmdExploreFoodRestaurant);
					G.MenuItem (q, 'Snack'         , ".", cmdExploreFoodSnack);

				G.MenuItem (m, 'Hiburan'         , ".", cmdExploreEntertainment);
				G.MenuItem (m, 'Belanja'         , ".", cmdExploreShopping);
				G.MenuItem (m, 'Peta Wisata'     , ".", cmdExploreMap);

		r = G.MenuItemRoot (App.Menu, 'Acara');
			m = G.MenuPopup (r);
				G.MenuItem (m, 'Kalender'   , ".", cmdEventCalendar);
				G.MenuItem (m, 'Acara Utama', ".", cmdEventMain);

		r = G.MenuItemRoot (App.Menu, 'Pertemuan');
			m = G.MenuPopup (r);
				p = G.MenuItem (m, 'Fasilitas ...' , ".");
				q = G.MenuPopup (p);
					G.MenuItem (q, 'Informasi Umum'                   , ".", cmdMiceFacilityGeneral);
					G.MenuItem (q, 'Pusat Konferensi dan Eksibisi'    , ".", cmdMiceFacilityCenter);
					G.MenuItem (q, 'Hotel dengan Fasilitas Konferensi', ".", cmdMiceFacilityHotel);
					G.MenuItem (q, 'Fasilitas Lainnya'                , ".", cmdMiceFacilityOther);
				G.MenuItem (m, 'Kalender'                             , ".", cmdMiceCalendar);

		r = G.MenuItemRoot (App.Menu, 'Informasi');
			m = G.MenuPopup (r);
				G.MenuItem (m, 'Konferensi Pers' , ".", cmdInfoPress);
				G.MenuItem (m, 'Pusat Informasi' , ".", cmdInfoCenter);
				G.MenuItem (m, 'Perwakilan MGTO' , ".", cmdInfoMgto);
				G.MenuItem (m, 'Kontak Wisatawan', ".", cmdInfoContact);
				G.MenuItem (m, 'Hotline'         , ".", cmdInfoHotline);
				G.MenuItem (m, 'Lainnya'         , ".", cmdInfoOthers);

		r = G.MenuItemRoot (App.Menu, 'Lain-lain');
			m = G.MenuPopup (r);
				G.MenuItem (m, 'Forum'    , ".", cmdOtherForum);
				G.MenuItem (m, 'Galeri'   , ".", cmdOtherGallery);
				G.MenuItem (m, 'ECard'    , ".", cmdOtherEcard);
				G.MenuItem (m, 'Wallpaper', ".", cmdOtherWallpaper);
				G.MenuItem (m, 'Video'    , ".", cmdOtherVideo);

	App.setLogout ();
};

