if (typeof ($afn) == "undefined" || !$afn) { var $afn = {}; }

$(document).ready(function ()
{
	$.getScript("/scripts/TopMenu.js?_ver=" + $siteVersion);
	$.getScript("/scripts/BottomMenu.js?_ver=" + $siteVersion);
	$.getScript("/scripts/SocialMediaButtons.js?_ver=" + $siteVersion);

	$("#WeatherContainer").flash(
	{
		swf: "swfs/AFWeather.swf",
		width: 169,
		height: 35
	});

	$(".swf_holder").flash(
	{
		swf: "swfs/ITPreloader.swf",
		width: 400,
		height: 375,
		allowScriptAccess: "always"
	});

	$("#flaChannelButtons").flash(
	{
		swf: "swfs/channel_buttons.swf",
		width: 164,
		height: 286,
		allowScriptAccess: "always",
		flashvars:
		{
			Channel: $.Params.Values["ref"]
		}
	});

	$("#flaLinkTab").flash(
	{
		swf: "swfs/corner_links.swf",
		width: 160,
		height: 290,
		allowScriptAccess: "always"
	});

	$("#AFNConnectContainer").attr("title", "Manage my Decoder")
	.append(
		$("<img/>").attr("src", "/images/authorize.png").css("border", "0"))
	.click(function ()
	{
		//window.open("/AFNConnect.mspx");
		window.open("https://afnconnect.myafn.dodmedia.osd.mil");
	});

	$("#DMAContainer").attr("title", "AFN is a Broadcast Service of the Defense Media Activity")
	.append(
		$("<table></table>").attr("cellpadding", "0")
			.append($("<tr></tr>")
				.append($("<td></td>").html("AFN is a broadcast service<br />of the Defense Media Activity"))
				.append($("<td></td>")
					.append($("<img/>").attr("src", "/images/DMA_logo_small.png").css("border", "0")))))
	.click(function ()
	{
		location.href = "http://www.dma.mil";
	});

	if ($qs.getChannelRef() == null)
	{
		$.getJSON("PromoB.axd", { _ver: $siteVersion }, function (jsonData)
		{
			if (jsonData != null && jsonData != undefined)
			{
				var $divPromoB = $("#divPromoB");

				switch (jsonData["ImageType"])
				{
					case ".swf":
						$divPromoB.flash(
						{
							swf: jsonData["ImageName"],
							width: 287,
							height: 282,
							allowScriptAccess: "always"
						});
						break;

					default:
						$divPromoB.append($("<img />")
						.width(287)
						.height(282)
						.attr("src", jsonData["ImageName"]));
						break;
				}
			}
		});
	}
	else
	{
		$.getJSON("ChannelTabs.axd", { _ver: $siteVersion, c: $qs.getChannelRef() }, function (jsonData)
		{
			var $divPromoB = $("#divPromoB");
			var tzAbbrev = jsonData["Abbrev"];
			var tabsData = jsonData["TabsData"];
			var daysOfWeek = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"];

			var $tabsContainer = $("<div></div>");
			$divPromoB.append($tabsContainer);
			var $tabsList = $("<ul></ul>");
			$tabsContainer.append($tabsList);
			for (var i = 0; i < daysOfWeek.length; i++)
			{
				var day = daysOfWeek[i];
				var tabData = tabsData[i];
				$tabsList.append($("<li/>").append($("<a/>").attr("href", "#" + day).html(day)));

				var $tab = $("<div></div>").attr("id", day);
				for (var j = 0; j < tabData.length; j++)
				{
					var dayData = tabData[j];
					var airDate = JSON.parseDate(dayData["AirDate"]);
					var $info = $("<div></div>").addClass("info").appendTo($tab);
					$info.append(
						$("<div></div>")
						.css({ float: "left" })
						.html(airDate.format("hh:mm tt") + " " + tzAbbrev))
					.append(
						(j == 0) ? $("<div></div>").css({ float: "right" }).html(airDate.format("M/d")) : "")
					.append($("<br/>"))
					.append(
						$("<div></div>")
						.css({ float: "left" })
						.append(
							$("<strong></strong>")
							.append(dayData["Title"])
							.append((dayData["EpisodeTitle"] != null) ? (" - " + dayData["EpisodeTitle"]) : "")));
				}
				$tabsContainer.append($tab);
			}

			$tabsContainer.tabs();

			$divPromoB.append($("<img/>")
			.attr("src", "images/week_" + $qs.getChannelRef() + "Bottom.gif")
			.width(296)
			.height(137));
		});
	}

	$.getJSONP($jsonPath + "/Bulletins.json", null);
});

$afn.ProcessBulletinsData = function (jsonData)
{
	var $bc = $("#BulletinContainer");
	$(jsonData).each(function (i, b)
	{
		$("<div></div>")
		.append((b.Title + " - " + b.Description).substring(0, 100).toUpperCase())
		.append(" ")
		.append($("<a></a>")
			.attr("href", "#")
			.data("bulletin", b)
			.css(
			{
				color: "silver",
				"text-decoration": "none"
			})
			.html("&lt;more&gt;")
			.click(function ()
			{
				var bulletin = $(this).data("bulletin");

				$("<div></div>")
				.data("bulletinId", bulletin.Id)
				.attr("title", bulletin.Title + " - " + bulletin.Description)
				.html(bulletin.Content)
				.dialog(
				{
					modal: true,
					width: 480,
					minWidth: 480,
					minHeight: 250,
					maxHeight: 640,
					buttons:
					{
						CLOSE: function ()
						{
							$(this).dialog("close");
						},
						PRINT: function ()
						{
							window.open("PrintBulletin.ashx?id=" + $(this).data("bulletinId"));
							$(this).dialog("close");
						}
					}
				});
				return false;
			}))
		.appendTo($bc);
	});
};
