(function($) { // This forces the $ to refer to jQuery inside the following block. It's a clever way of allowing Airbag's JavaScript, written in jQuery, to still work even though the rest of the site, including the autocomplete, is using Prototype.
var readMedia = {
	init : function() {
		var c = readMedia;
		var p = c.Project;

		$(document).ready(function() {
			// List function(s) to fire onload here
			p.tagIt();

			p.cruft();
			p.feedSlides();
			p.question();
			p.readMenu();
			p.valueNav();
			p.videoSlides();
			if (typeof DD_belatedPNG != "undefined") {
				DD_belatedPNG.fix(c.Config.sPNGSel);
			}
		});

		p.loadSIFR();
	}

	,Config : {
		sHTMLtag : "can-has-js"
		,sImgPath : "/images/"
		,sPNGSel : "#rm-supra, #rm-outer, #rm-inner, #rm, #mast img, .home-feature h2, .home-feature a, #landing-nav .group, #value-nav li.on, #value-nav li a, #value-nav li em, .feed, .feed .slides, .feed .nav, .cta, .cta .wrap, .cta form, .cta fieldset, blockquote.tout, blockquote.tout img, #ur, #question, #question div.wrap, #rm-nav a.top, #rm-nav div, #rm-nav div a"
		,sSwfPath : "/swf/"
	}

	/*
		CLIENT-SPECIFIC FUNCTIONS
	*/
	,Project : {
		tagIt : function() {
			var c = readMedia;
			$("html").addClass(c.Config.sHTMLtag);
		}

		,cruft : function() {
			var c = readMedia;

			$("#page").wrap('<div id="rm-supra"><div id="rm-outer"><div id="rm-inner"><div id="rm"></div></div></div></div>');

			// Blockquote...um, quotes.
			$("blockquote.tout p:first").prepend('<img src="' + c.Config.sImgPath + 'quotes.png" class="q" alt="" />');
			$("blockquote.tout p:last").append('<img src="' + c.Config.sImgPath + 'quotes-end.png" class="qe" alt="" />');
		}

		,feedSlides : function() {
			var oSlides = $(".feed ul.slide");
			$(".feed ul.slide li:first-child").addClass("first");

			if (oSlides.length > 1) {
                // // Fix the height
                // var limit = 0;
                // oSlides.each(function() {
                //  var o = $(this);
                //  var iHeight = o.height();
                //  if (iHeight > limit) limit = iHeight;
                // });
                // $(".feed .slides").height((limit / 12) + "em");
                // if ($.browser.safari) {  // Because this makes sense.
                //  $("body.sect-home .feed .slides").height(((limit + 12) / 12) + "em");
                // }

				// Build the slide navigation
				var tmp = 1;
				$(".feed").append('<ul class="nav group"><li class="prev"><a href="#"><b>Previous</b></a></li></ul>');
				oSlides.each(function() {
					var sSlug = "feed-slide-" + tmp;

					$(this).attr("id", sSlug);
					$(".feed ul.nav").append('<li class="step"><a href="#' + sSlug + '"><b>Slide ' + 1 + '</b></a>')

					$(this).hide();

					if (tmp == 1) {
						$(this).show();
					}

					tmp++;
				});
				$(".feed ul.nav").append('<li class="next"><a href="#"><b>Next</b></a></li>');
				$(".feed li.step").eq(0).addClass("current");

				// Attach event handlers to the nav
				$(".feed ul.nav li.step a").click(function() {
					if (!$(this).parent().hasClass("current")) {
						var sSlug = $(this).attr("href").split("#")[1];	// Had to use split() to work around an IE6 bug
						$(".feed ul.slide").hide();
						$(".feed ul[id=" + sSlug + "]").fadeIn(600);

						// Change the display of the bullets
						$(this).parent().siblings().removeClass("current");
						$(this).parent().addClass("current");
					}

					return false;
				});

				// Previous/next functionality
				$(".feed ul.nav li[class!=step] a").click(function() {
					var sDir = ($(this).parent().attr("class") == "next") ? 1 : -1;
					var oCurrent = $(".feed ul.slide:visible");

					// Are we moving forward?
					if (sDir > 0) {
						var oNextSlide = oCurrent.next();
						if (!oNextSlide.html()) {
							oNextSlide = $(".feed ul.slide:first");
						}
					} else {
						var oNextSlide = oCurrent.prev();
						if (!oNextSlide.html()) {
							oNextSlide = $(".feed ul.slide:last");
						}
					}

					var sSlug = oNextSlide.attr("id");
					if (sSlug) {
						oCurrent.hide();
						oNextSlide.fadeIn(600);

						$(".feed ul.nav li.step").removeClass("current");
						$(".feed ul.nav li.step a[href$=#" + sSlug + "]").each(function() {
							$(this).parent().addClass("current");
						});
					}
					return false;
				});
			} else {
				// If there aren't any slides, insert the closing shim
				$(".feed").append('<div class="nav"></div>');
			}
		}

		,loadSIFR : function() {
			var c = readMedia;

			/*
				Register the fonts
			*/
			var whitneySemi = {
				src: c.Config.sSwfPath + 'whitney-semi.swf'
			}

			var whitneySemiItal = {
				src: c.Config.sSwfPath + 'whitney-semiital.swf'
			}

			var whitneyMedium = {
				src: c.Config.sSwfPath + 'whitney-medium.swf'
			}

			/*
				Activate the fonts
			*/
			sIFR.activate(whitneySemi);
			sIFR.activate(whitneySemiItal);
			sIFR.activate(whitneyMedium);

			/*
				Hi, we're the replacements
			*/
			sIFR.replace(whitneySemi, {
				selector: 'body:not(.sect-release) #main h1, body.sect-release #main .title h1',
				wmode: 'transparent',
				css: [
					'.sIFR-root { background-color: #FFFFFF; color: #8BA32A; font-size: 30px; letter-spacing: -0.5; leading: -4 }'
				]
				,tuneHeight : "-4px"
			});

			sIFR.replace(whitneySemi, {
				selector: '#landing-splash .primary h2',
				wmode: 'transparent',
				css: [
					'.sIFR-root { background-color: #FFFFFF; color: #8BA32A; font-size: 24px; letter-spacing: -0.5; leading: 10 }'
				]
				,tuneHeight : "-4px"
			});

			sIFR.replace(whitneySemiItal, {
				selector: '.epicenter .lede h2, body.sect-home .overview h2.title',
				wmode: 'transparent',
				css: [
					'.sIFR-root { background-color: #FFFFFF; color: #0090DC; font-size: 24px; letter-spacing: -0.5; leading: -5; }'
				]
			});

			sIFR.replace(whitneyMedium, {
				selector: '.features .title',
				wmode: 'transparent',
				css: [
					'.sIFR-root { background-color: #FFFFFF; color: #8BA32A; font-size: 24px; letter-spacing: -0.5; }'
				]
			});

			sIFR.replace(whitneyMedium, {
				selector: 'body.sect-schools #main .primary h2',
				wmode: 'transparent',
				css: [
					'.sIFR-root { background-color: #FFFFFF; color: #D82D0B; font-size: 24px; letter-spacing: -0.5; }'
				]
			});

			sIFR.replace(whitneyMedium, {
				selector: 'body.sect-state #main .primary h2',
				wmode: 'transparent',
				css: [
					'.sIFR-root { background-color: #FFFFFF; color: #4BBC95; font-size: 24px; letter-spacing: -0.5; }'
				]
			});

			sIFR.replace(whitneyMedium, {
				selector: 'body.sect-localgov #main .primary h2',
				wmode: 'transparent',
				css: [
					'.sIFR-root { background-color: #FFFFFF; color: #62BEE8; font-size: 24px; letter-spacing: -0.5; }'
				]
			});

			sIFR.replace(whitneyMedium, {
				selector: '.article .title h2',
				wmode: 'transparent',
				css: [
					'.sIFR-root { background-color: #FFFFFF; color: #1784CA; font-size: 26px; letter-spacing: -0.5; }'
				]
			});

			sIFR.replace(whitneySemi, {
				selector: '#value-nav h1',
				wmode: 'transparent',
				css: [
					'.sIFR-root { background-color: #FFFFFF; font-size: 30px; letter-spacing: -0.5; color: #0484CB; }'
				]
			});

			var landingTitle = "background-color: #FFFFFF; font-size: 36px; letter-spacing: -0.5;";

			sIFR.replace(whitneySemi, {
				selector: 'body.sect-localgov #landing-nav h1',
				wmode: 'transparent',
				css: [
					'.sIFR-root { ' + landingTitle + ' color: #62BEE8; }'
				]
				,tuneHeight: "-10px"
			});

			sIFR.replace(whitneySemi, {
				selector: 'body.sect-schools #landing-nav h1',
				wmode: 'transparent',
				css: [
					'.sIFR-root { ' + landingTitle + ' color: #D82D0B; }'
				]
				,tuneHeight: "-10px"
			});

			sIFR.replace(whitneySemi, {
				selector: 'body.sect-state #landing-nav h1',
				wmode: 'transparent',
				css: [
					'.sIFR-root { ' + landingTitle + ' color: #4BBC95; }'
				]
				,tuneHeight: "-4px"
			});

			sIFR.replace(whitneySemi, {
				selector: '#landing-nav h1',
				wmode: 'transparent',
				css: [
					'.sIFR-root { ' + landingTitle + ' color: #333333; }'
				]
				,tuneHeight: "-10px"
			});

			sIFR.replace(whitneyMedium, {
				selector: '#landing-nav h2, body.sect-home .feed .title',
				wmode: 'transparent',
				css: [
					'.sIFR-root { background-color: #FFFFFF; color: #333333; font-size: 18px; letter-spacing: -0.2; }'
				]
			});

			sIFR.replace(whitneyMedium, {
				selector: 'body.type-value #main .primary h2, #more-vids h2',
				wmode: 'transparent',
				css: [
					'.sIFR-root { background-color: #FFFFFF; color: #8BA32A; font-size: 18px; letter-spacing: -0.2; }'
				]
			});
		}

		,question : function() {
			if ($("#question").length > 0) {
				// Insert the "close" button
				$("#question h2").append("<i></i>");

				// Attach show/hide functionality to the link & close icon
				$("a[href=#question], #question h2 i").click(function() {
					if ($("body").hasClass("question")) {
						$("body").removeClass("question");
					} else {
						$("body").addClass("question");
					}
					return false;
				});

				// Hide the modal window if the escape key is pressed
				document.onkeydown = function(e) {
					var keycode = (e == null) ? event.keyCode : e.which;

					if(keycode == 27){ // escape, close box
						if ($("body").hasClass("question")) {
							$("body").removeClass("question");
						}
					}
				}
			}
		}

		,readMenu : function() {
			if ($("#rm-nav").length > 0) {
				$("#rm-nav a.top").click(function() {
					var oParent = $(this).parent();
					if (oParent.hasClass("open")) {
						oParent.removeClass("open");
					} else {
						oParent.addClass("open");
					}

					return false;
				});
			}

			$(document).mousedown(function(event) {
				if ($(event.target).parents("#rm-nav").length == 0) {
					$("#rm-nav li.top").removeClass("open");
				}
			});
		}

		,valueNav : function() {
			$("#value-nav a").append("<em></em>");
			$("#value-nav li.on").each(function() {
				var iThis = $("#value-nav li").index(this);
				var oUl = $(this).parents("ul");
				oUl.attr("class", "");
				oUl.addClass("item-" + (iThis + 1));
			});
		}

		,videoSlides : function() {
			var oSlides = $("#videos ul.slide");
			var sCap = '<i class="cap"></i>';

			// IE patch converts object -> embed
			if (document.all && !window.opera) {
				$("dd.video object").each(function() {
					var oMov = $(this);
					var iW = oMov.attr("width");
					var iH = oMov.attr("height");
					var sType = (oMov.attr("type")) ? oMov.attr("type") : "application/x-shockwave-flash";
					var sURL = oMov.attr("data");

					var sHTML = '<embed src="' + sURL + '" type="' + sType + '" width="' + iW + '" height="' + iH + '" allowfullscreen="true" allowscriptaccess="always"></embed>';
					oMov.replaceWith(sHTML);
				});
			}

			if (oSlides.length > 1) {
				$("#videos .slides").wrapInner('<div class="wrap"></div>')

				// Build the slide navigation
				var tmp = 1;
				$("#videos").append('<ul class="nav"><li class="prev"><a href="#">Previous</a></li></ul>');
				oSlides.each(function() {
					var sSlug = "vid-slide-" + tmp;

					$(this).attr("id", sSlug);
					$("#videos ul.nav").append('<li class="step"><a href="#' + sSlug + '">Slide ' + 1 + '</a>')

					$(this).hide();

					if (tmp == 1) {
						$(this).show();
					}

					tmp++;
				});
				$("#videos ul.nav").append('<li class="next"><a href="#">Next</a></li>');
				$("#videos li.step:first, #videos li.vid:first").addClass("current");

				// Mark the first video as "Now Playing"
				$("#videos li.vid:first").append(sCap);
				$("#videos li.vid:first dt").append("<i>Now Playing</i>")

				// Attach event handlers to the nav
				$("#videos ul.nav li.step a").click(function() {
					if (!$(this).parent().hasClass("current")) {
						var sSlug = $(this).attr("href").substring(1);

						$("#videos ul.slide").hide();
						$("#videos ul[id=" + sSlug + "]").fadeIn(600);

						// Change the display of the bullets
						$(this).parent().siblings().removeClass("current");
						$(this).parent().addClass("current");
					}

					return false;
				});

				// Previous/next functionality
				$("#videos ul.nav li[class!=step] a").click(function() {
					var sDir = ($(this).parent().attr("class") == "next") ? 1 : -1;
					var oCurrent = $("#videos ul.slide:visible");

					// Are we moving forward?
					if (sDir > 0) {
						var oNextSlide = oCurrent.next();
						if (!oNextSlide.html()) {
							oNextSlide = $("#videos ul.slide:first");
						}
					} else {
						var oNextSlide = oCurrent.prev();
						if (!oNextSlide.html()) {
							oNextSlide = $("#videos ul.slide:last");
						}
					}

					var sSlug = oNextSlide.attr("id");
					if (sSlug) {
						oCurrent.hide();
						oNextSlide.fadeIn(600);

						$("#videos ul.nav li.step").removeClass("current");
						$("#videos ul.nav li.step a[href=#" + sSlug + "]").each(function() {
							$(this).parent().addClass("current");
						});
					}
					return false;
				});
			} else {
				// If there aren't any slides, insert the closing shim
				$("#videos").append('<div class="nav"></div>');
			}

			var oVideos = $("#videos li.vid");

			if (oVideos.length > 0) {
				$("#videos").prepend('<div class="main"></div>');
				$("#videos .main").html($("#videos li.vid:first").find("dd.video").html());

				oVideos.click(function() {
					oVideos.removeClass("current");
					oVideos.find("i").remove();
					$(this).addClass("current");

					$(this).find("dt").append("<i>Now Playing</i>");
					$(this).append(sCap);

					$("#videos .main").html($(this).find("dd.video").html()).fadeIn();
				});
			}
		}
	}
};

readMedia.init();
})(jQuery); // End the special jQuery block.
