﻿if (typeof ($afn) == "undefined" || !$afn) { var $afn = {}; }

$(document).ready(function ()
{
	$("#TopLinksContainer").addClass($topMenuStyle);
	$.getJSONP($jsonPath + "/TopMenu.json", { _ver: $siteVersion });
});

$afn.ProcessTopMenu = function (jsonData)
{
	var $tc = $("#TopLinksContainer");
	// Home button
	$("<div></div>")
		.addClass("homeButton")
		.attr("title", "Home")
		.click(function ()
		{
			location.href = "/";
		})
		.appendTo($tc);

	// Drop menu
	var $ul = $("<ul></ul>").addClass("sf-menu");
	for (var i = 0; i < jsonData.length; i++)
	{
		var mi = jsonData[i];

		var $li = $("<li></li>")
			.append($("<a></a>")
				.css("white-space", "nowrap")
				.cond(mi["Url"].length == 0,
				function ()
				{
					this.attr("href", "#");
				},
				function ()
				{
					this.attr("href", mi["Url"]);
				})
				.cond(mi["Image"].length == 0,
				function ()
				{
					this.html(mi["Text"]);
				},
				function ()
				{
					this.append(mi["Text"])
						.append($("<img/>")
							.attr("src", mi["Image"])
							.css(
							{
								"margin-left": 1,
								border: 0
							}));
				}))
			.iff(mi.Items.length > 0)
			.append(function ()
			{
				var $iul = $("<ul></ul>");

				for (var j = 0; j < mi.Items.length; j++)
				{
					var imi = mi.Items[j];

					$("<li></li>")
					.append($("<a></a>")
						.css("white-space", "nowrap")
						.cond(imi["Url"].length == 0,
						function ()
						{
							this.attr("href", "#");
						},
						function ()
						{
							this.attr("href", imi["Url"]);
						})
						.cond(imi["Image"].length == 0,
						function ()
						{
							this.html(imi["Text"]);
						},
						function ()
						{
							this.append(imi["Text"])
								.append($("<img/>")
									.attr("src", imi["Image"])
									.css(
									{
										"margin-left": 1,
										border: 0
									}));
						}))
					.appendTo($iul);
				}

				return $iul;
			})
			.end();

		$ul.append($li);
	}

	$ul.supersubs(
		{
			minWidth: 12,
			maxWidth: 27,
			extraWidth: 1
		})
		.superfish(
		{
			autoArrows: false,
			dropShadows: false,
			delay: 400
		})
		.appendTo($tc);
};
