var ShowsGlobal = {
	init: function() {
		ShowsGlobal.nav.init();
		
		if (scheduleId == "") {
			jQuery("#view-schedule").hide()
		} 
		else {
			jQuery("#view-schedule").click(function() {
				if (!ShowsGlobal.schedule.loaded) {
					ShowsGlobal.schedule.load()
				}
				jQuery("#schedule").toggle();
			});
			jQuery("#schedule .close").click(function() {
				jQuery("#schedule").hide();
				return false
			});
			jQuery("#schedule .timezone").click(function() {
				jQuery("#schedule .active").removeClass("active");
				jQuery(this).addClass("active");
				ShowsGlobal.schedule.timezone = this.title;
				ShowsGlobal.schedule.load();
				return false
			})
		}
		jQuery("body").click(function(A) {
			if (A.target.id == "view-schedule" || jQuery(A.target).parents("#schedule").length > 0) {
				return
			}
			jQuery("#schedule").hide()
		});
		
		if (rating != "") {
			jQuery("body").append('<img id="rating" alt="rating" src="http://www.adultswim.com/shows/tools/img/rating-icons/' + rating + '.gif"/>')
		}
	},
	
	nav: {
		asMenuHidden: true,
		
		init: function() {
			jQuery(window).bind("resize", function(){
				if (!ShowsGlobal.nav.asMenuHidden) {
					ShowsGlobal.nav.resizeNav();
				}
			});
			
			if (jQuery('body').attr("id") == "landing") {
				ShowsGlobal.nav.landingMenu();
			}
			else {
				ShowsGlobal.nav.hideASMenu();
			}
	    },
		
	    hideASMenu: function() {
			ShowsGlobal.nav.asMenuHidden = true;
			
			jQuery('#video-nav').hide();
			jQuery('#schedule-nav').hide();
			jQuery('#boards-nav').hide();
			jQuery('#music-nav').hide();
			jQuery('#lists-nav').hide();
			jQuery('#shop-nav').hide();
			jQuery('#accountNav').hide();
			
			jQuery('#bearNav').append('<a id="main-menu" href="#">Main Menu</a>');
			
			jQuery('#asNav').css('width', 'auto')
			jQuery('#navLinks').css('width', 'auto')
			
			jQuery('#main-menu').click(function() {
				ShowsGlobal.nav.showASMenu();
				ShowsGlobal.nav.hideShowMenu();
			})
	    },
		
		showASMenu: function() {
	    	jQuery('#video-nav').show();
			jQuery('#schedule-nav').show();
			jQuery('#boards-nav').show();
			jQuery('#music-nav').show();
			jQuery('#lists-nav').show();
			jQuery('#shop-nav').show();
			jQuery('#accountNav').show();
			
			jQuery('#main-menu').remove();
	    },
		
	    hideShowMenu: function(){
			ShowsGlobal.nav.asMenuHidden = false;
			
	    	jQuery('#show-nav').hide();
			
			jQuery('#shows-navs').append('<div id="show-menu"><a href="#">Show Menu</a></div>');
			
			ShowsGlobal.nav.resizeNav();
			
			jQuery('#navLinks').css('width', '100%')
			
			jQuery('#show-menu').click(function() {
				ShowsGlobal.nav.showShowMenu();
				ShowsGlobal.nav.hideASMenu();
			})
	    },
		
		showShowMenu: function() {
	   		jQuery('#show-nav').show();
			
			jQuery('#show-menu').remove();
	    },
		
	    resizeNav: function() {
			var browserWidth = jQuery(window).width();
			var showMenuWidth = jQuery('#show-menu').outerWidth();
			
			var asNavWidth = browserWidth - showMenuWidth - 30;
			
			jQuery('#asNav').css('width', asNavWidth);
	    },
		
		landingMenu: function() {
			ShowsGlobal.nav.asMenuHidden = false;
			
			jQuery('#show-nav').hide();
			
			ShowsGlobal.nav.resizeNav();
			
			jQuery('#navLinks').css('width', '100%');
		}
	},
	
	schedule: {
		loaded: false,
		timezone: "EST",
		months: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
		days: ["SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY"],
		
		feedurl: function() {
			return "/adultswimdynsched/xmlServices/ScheduleServices?methodName=getAllShowings&showId=" + encodeURI(scheduleId) + "&name=" + encodeURI(scheduleName) + "&timezone=" + ShowsGlobal.schedule.timezone
		},
		
		load: function() {
			jQuery("#schedule .showtimes").empty().append("Loading Schedule...");
			
			jQuery.ajax({
				url: ShowsGlobal.schedule.feedurl(),
				dataType: "xml",
				async: true,
				complete: function(xml) {
					var showtimes = jQuery("<ul/>").appendTo(jQuery("#schedule .showtimes").empty());
					
					try {
						var showings = jQuery("show", xml.responseXML);
						
						if (showings.length < 1) {
							throw "no showings"
						}
						for (var i = 0; i < showings.length; i++) {
							var li = jQuery("<li/>").appendTo(showtimes);
							
							try {
								var year = new Date().getFullYear();
								var datetime = new Date(jQuery(showings[i]).attr("date") + " " + year + " " + jQuery(showings[i]).attr("time"));
								
								if (datetime.getTime() < new Date().getTime()) {
									datetime = new Date(jQuery(showings[i]).attr("date") + " " + ++year + " " + jQuery(showings[i]).attr("time"))
								}
								
								jQuery('<span class="weekday"/>').append(ShowsGlobal.schedule.days[datetime.getDay()] + ":").appendTo(li);
								li.append(ShowsGlobal.schedule.months[datetime.getMonth()] + "." + datetime.getDate());
								li.append("@");
								li.append(datetime.getHours() == 0 ? 12 : datetime.getHours() > 12 ? datetime.getHours() - 12 : datetime.getHours());
								li.append(":");
								li.append((datetime.getMinutes() < 10 ? "0": "") + datetime.getMinutes());
								li.append(datetime.getHours() < 12 ? "AM": "PM")
							} 
							catch(e) {
								li.append("TBD")
							}
						}
					} 
					catch(e) {
						jQuery("#schedule .timezones").empty();
						showtimes.html("Not airing this week.")
					}
					
					ShowsGlobal.schedule.loaded = true;
				}
			})
		}
	}
}

var Omniture = {
	track: function(show, page) {
		s.referrer = "";
		s.pageName = "Shows : " + show + " - " + page;
		s.server = "Shows";
		s.channel = "Shows : " + show;
		s.t();
	}
}

var Nielsen = {
	track: function() {
		var d = new Image(1, 1);
		d.src = ["//secure-us.imrworldwide.com/cgi-bin/m?ci=us-204044h&cg=0&cc=1&si=", escape(window.location.href), "&rp=", escape(document.referrer), "&c0=usergen,1&rnd=", (new Date()).getTime()].join('');
	}
}

var Utilities = {
	log: function(value) {
		try {
			console.log(value);
		}
		catch (e) {
			
		}
	}
}

var AdFactory = {
    adurl: "/adultswimdynamic/js.ng/site=toonswim&toonswim_pos=${width}x${height}_${or}&toonswim_rollup=shows&toonswimstatus=test",
    parser: new RegExp(/document\.write\('(.*)'\);/),
    loadAd: function(A, C, B){
        try {
            B += "?" + videoId;
        } 
        catch (D) {
        }
        return '<iframe frameborder="no" scrolling="no" style="border:none;width:' + A + "px;height:" + C + 'px" src="' + B + '" />'
    },
    errorAd: function(A, B){
        return '<div style="background:#C2C2C2;color:#FFF;line-height:' + B + "px;width:" + A + 'px;text-align:center;">adspace</div>'
    },
    loadBillboard: function(A){
        A(AdFactory.loadAd(300, 250, "/shows/tools/includes/billboard.html"))
    },
    loadSponsorshipBadge: function(A){
        A(AdFactory.loadAd(125, 125, "/shows/tools/includes/sponsorshipbadge.html"))
    },
    loadLeaderboard: function(A){
        A(AdFactory.loadAd(728, 90, "/shows/tools/includes/leaderboard.html"))
    }
};

jQuery(document).ready(function() {
	ShowsGlobal.init();
})

