var Arnava = Class.create(
{
	initialize: function()
	{
		this.makeNav();
		this.makeFlags();
		this.makeMenu();
		this.makeProfiles();
	},

	makeNav: function()
	{
		$$("#nav a.nav").each((function(a)
		{
			a.epigraph = a.title;
			a.title = "";
			a.imgs = [a.down("img")];

			if(a.previous() && a.previous().hasClassName("inter"))
			{
				a.imgs.push(a.previous());
			}

			if(a.next() && a.next().hasClassName("inter"))
			{
				a.imgs.push(a.next());
			}

			a.imgs.each(function(img)
			{
				img.off = img.src;
			});

			a.toOn = (function()
			{
				a.imgs.each(function(img)
				{
					img.src = img.src.replace(/off./, "on.");
				});

				this.updateEpigraph(a.epigraph);
			}).bind(this);

			a.toOff = (function()
			{
				a.imgs.each(function(img)
				{
					img.src = img.off;
				});

				$$(".nav-selected").each((function(ns)
				{
					this.updateEpigraph(ns.epigraph);
					updated = true;
				}).bind(this));
			}).bind(this);

			a.observe("click", function(e)
			{
			});

			a.observe("mouseenter", a.toOn);
			a.observe("mouseleave", a.toOff);
		}).bind(this));
		
		$("nav").observe("mouseleave", function(e)
		{
			$$(".slide").invoke("hide");
			$$(".welcome", ".news").invoke("show");
		});
	},

	makeFlags: function()
	{
		$$("#flags img").each(function(img)
		{
			var opacity = img.hasClassName("flag-selected") ? 0.9 : 0.7;

			img.setOpacity(opacity);

			img.observe("mouseenter", function()
			{
				img.setOpacity(1)
			});

			img.observe("mouseleave", function()
			{
				img.setOpacity(opacity)
			});
		});
	},

	makeMenu: function()
	{
		$$("#menu .item-items").each(function(items)
		{
			items.show = function()
			{
				items.getElementsBySelector(".item-item").invoke("appear");
			}

			items.hide = function()
			{
				items.getElementsBySelector(".item-item").invoke("hide");
			}

			items.observe("mouseenter", function()
			{
				items.show();
			});

			items.observe("mouseleave", function()
			{
				items.hide();
			});

			items.hide();
		});
	},

	makeProfiles: function()
	{
		$$(".profile-text").invoke("hide");
		$$(".profile-link").each(function(link)
		{
			link.observe("click", function(e)
			{
				var id = link.readAttribute("rel");

				$(id).getElementsBySelector(".profile-title").invoke("hide");
			
				e.stop();

				$j("#" + id).dialog(
				{
					modal: true,
					width: 800,
					height: 400,
					draggable: false,
					resizable: false
				});

				$j("#" + id).dialog('open');
			});
		});
	},

	updateEpigraph: function(txt)
	{
		$$(".welcome", ".news").invoke("hide");
		$$(".slide").invoke("show");

		$$(".epigraph").each(function(ep)
		{
			ep.update(txt.toUpperCase());
			ep.setOpacity(0);
			ep.appear();
		});
	}
});

document.observe("dom:loaded", function()
{
	var myArnava = new Arnava();
});

