var Show = {
	feeds: {},
	configuration: {
		load: function() {
			$.ajax({
				url: "feeds/conf.js",
				dataType: "json",
				async: false,
				success: function(A) {
					$.extend(Show.configuration, A.configuration);
					$.extend(Show.feeds, A)
				}
			})
		}
	},
	init: function() {
		this.configuration.load();
		document.title = Show.configuration.showName;
		if (Show.configuration.ids.scheduleId == undefined) {
			$("#lnkSchedule").hide()
		} else {
			$("#lnkSchedule").click(function() {
				if (!Show.schedule.loaded) {
					Show.schedule.load()
				}
				$("#schedule").toggle();
				Tracker.track(Show.configuration.showName + " - Schedule", "Shows")
			});
			$("#schedule .close").click(function() {
				$("#schedule").hide();
				return false
			});
			$("#schedule .btn_timezone").click(function() {
				$("#schedule .active").removeClass("active");
				$(this).addClass("active");
				Show.schedule.timezone = this.title;
				Show.schedule.load();
				return false
			})
		}
		$("body").click(function(A) {
			if (A.target.id == "lnkSchedule" || $(A.target).parents("#schedule").length > 0) {
				return
			}
			$("#schedule").hide()
		});
		if (Show.configuration.ratingIcon) {
			$("body").append('<img id="rating" alt="rating" src="' + Show.configuration.ratingIcon + '"/>')
		}
	},
	schedule: {
		loaded: false,
		load: function() {
			$("#schedule .showtimes").empty().append("Loading Schedule...");
			$.ajax({
				url: Show.schedule.feedurl(),
				dataType: "xml",
				async: true,
				complete: function(C) {
					var A = $("<ul/>").appendTo($("#schedule .showtimes").empty());
					try {
						var F = $("show", C.responseXML);
						if (F.length < 1) {
							throw "no showings"
						}
						for (var D = 0; D < F.length; D++) {
							var I = $("<li/>").appendTo(A);
							try {
								var H = new Date().getFullYear();
								var G = new Date($(F[D]).attr("date") + " " + H + " " + $(F[D]).attr("time"));
								if (G.getTime() < new Date().getTime()) {
									G = new Date($(F[D]).attr("date") + " " + ++H + " " + $(F[D]).attr("time"))
								}
								$('<span class="weekday"/>').append(Show.schedule.days[G.getDay()] + ":").appendTo(I);
								I.append(Show.schedule.months[G.getMonth()] + "." + G.getDate());
								I.append("@");
								I.append(G.getHours() == 0 ? 12 : G.getHours() > 12 ? G.getHours() - 12 : G.getHours());
								I.append(":");
								I.append((G.getMinutes() < 10 ? "0": "") + G.getMinutes());
								I.append(G.getHours() < 12 ? "AM": "PM")
							} catch(B) {
								I.append("TBD")
							}
						}
					} catch(E) {
						$("#schedule .timezones").empty();
						A.html("Not airing this week.")
					}
					Show.schedule.loaded = true
				}
			})
		},
		feedurl: function() {
			return "/adultswimdynsched/xmlServices/ScheduleServices?methodName=getAllShowings&showId=" + encodeURI(Show.configuration.ids.scheduleId) + "&name=" + encodeURI(Show.configuration.ids.scheduleName) + "&timezone=" + Show.schedule.timezone
		},
		timezone: "EST",
		months: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
		days: ["SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY"]
	}
};

