	var exposePlayerApi;//global
	
	var siteBaseUrl="";//global, set in resource body
	
	//function displays area for video, then embeds partner player swfs
	function launchPlayer(){

		//show player area
		exposePlayerApi =	$('#video-area').expose({api: true,closeOnClick:false,opacity:0.86,color:'#000',onLoad:function(){/*$('#totalplayer').css('background-color','transparent');*/}}).load(); 
		
	
		//start: fix for ie7 z-index bug in jquery tools expose func., puts flash above all layers
		var wmodeSetting = "transparent";
		
		if($.browser.msie){
			var vers = jQuery.browser.version.substr(0,1);
			if(vers =="6" || vers=="7"){
				
				wmodeSetting = "window";//fix for ie6, ie7 z-index bug in jquery tools expose func., puts flash above all layers
			}
		}
		//end: fix for ie7
		
		
		//load SWFs
		
    	tpSetCommManagerID("commManagerDiv");

	    tpRegisterID("playerwidget");
	
	    var flashvars = {
	    ID:"playerwidget",
	    width:512,
	    height:288,
	    autoplay:true,
	    v: videoUrl //provided from script above
	    };
	
	    var params = {
	        bgColor: "0x000000",
	        scale: "noscale",
	        menu: "true",
	        salign: "tl",
	        allowFullScreen: "true",
	        allowScriptAccess: "always",
	        wmode: wmodeSetting
	    };
	    var attributes = {};
	
	    swfobject.embedSWF("http://www-tc.pbs.org/video_qa/media/swf/flvPlayer.swf", "flashplayer", "512", "328", "9.0.159","http://www-tc.pbs.org/video_qa/media/swf/expressInstall.swf", flashvars, params, attributes);


		var flashvars = {
		    instanceID: tpGetInstanceID(),
		    commManagerID: tpGetCommManagerID(),
		    registeredIDs: tpGetRegisteredIDs()
		};
		
		var params = {
		    wmode: wmodeSetting,
		    allowScriptAccess: "always"
		};
		
		var attributes = {};
		
		swfobject.embedSWF("http://www-tc.pbs.org/video_qa/media/swf/commManager.swf", "commManagerDiv", "1", "1", "9.0.159","http://www-tc.pbs.org/video_qa/media/swf/expressInstall.swf", flashvars, params, attributes);



		var isPlayingVar = true;
		var jsTriggerd = false;
	
		function setPlaying(eventData) {
		    if(jsTriggerd){
		        isPlayingVar = true;
		        jsTriggerd = (jsTriggerd) ? false : jsTriggerd;
		    } else {
		        isPlayingVar = (isPlayingVar) ? true : isPlayingVar;
		    }
		    if(!isPlayingVar && !jsTriggerd){
		        isPlayingVar = true;
		    }
		    return isPlayingVar;
		}
		function setNotPlaying(eventData) {
		    if(jsTriggerd){
		        isPlayingVar = false;
		    } else {
		        jsTriggerd = (jsTriggerd) ? true : jsTriggerd;
		        isPlayingVar = (isPlayingVar) ? false : isPlayingVar;
		    }
		    return isPlayingVar;
		}
		tpController.addEventListener("OnMediaPause", "setNotPlaying");
		tpController.addEventListener("OnMediaUnpause", "setPlaying");
		tpController.addEventListener("OnMediaPlay", "setPlaying");
		tpController.addEventListener("OnMediaStart", "setPlaying");
		tpController.addEventListener("OnMediaComplete", "setNotPlaying");
		
	
	
		//clipinfowidget is the ad container
	    tpRegisterID("clipinfowidget");
	    
	    var flashvars = {
	        bannerAlignment: "top",
	        bannerRegions: "Banner300x60",
	        commManagerID: tpGetCommManagerID(),
	        height: 60,
	        ID: "clipinfowidget",
	        instanceID: tpGetInstanceID(),
	        showAuthor: "false",
	        showBanners: "true",
	        showCopyright: "false",
	        showDescription: "false",
	        showMetadata: "false",
	        skinURL: "http://www-tc.pbs.org/video_qa/media/swf/skinCinematic.swf",
	        stylesheetURL: "http://www-tc.pbs.org/video_qa/media/css/verdana.css",
	        useBannerQueuing: "true",
	        
	        width: 300,
	        backgroundColor: "0x000000",
	        descriptionColor: "0x000000",
	        frameColor: "0x000000",
	        titleColor: "0x000000"
	    };
	    
	    var params = {
	        scale: "noscale",
	        wmode: wmodeSetting,
	        quality: "high",
	        salign: "tl",
	        allowFullScreen: "true",
	        menu: "true",
	        allowScriptAccess: "always"
	    };
	    var attributes = {};
	
	    swfobject.embedSWF("http://www-tc.pbs.org/video_qa/media/swf/info.swf", "clipinfowidget", "300", "60", "9.0.159","http://www-tc.pbs.org/video_qa/media/swf/expressInstall.swf", flashvars, params, attributes);

	
	}
	

	if (!Array.prototype.indexOf) {
  		Array.prototype.indexOf = function(elt /*, from*/) {
    		var len = this.length >>> 0;

    		var from = Number(arguments[1]) || 0;
    		from = (from < 0)
     			? Math.ceil(from)
         		: Math.floor(from);
    		if (from < 0)
      			from += len;

    		for (; from < len; from++) {
      			if (from in this && this[from] === elt)
        		return from;
    				}
    		return -1;
  		};
	}
	
	function buildNav() {

		$(".on").remove();
		$(".num").remove();
		for(j=0; j<imgArr.length; j++) {
			if(j == current) {
				$("#next").before("<span class=\"on\">" + eval(j+1) + "<\/span>");
			} else {
				$("#next").before("<a href=\"#\" class=\"num\">" + eval(j+1) + "<\/a>");
			}
		}
	}
	
	// Rebuild navigation upon click on previous or next arrows to advance slideshow
			
	function advanceNav() {
		if(imageArr.length == 1) {
			$("#prev").replaceWith("<span id=\"prev\">< PREV<\/span>");
			$("#next").replaceWith("<span id=\"next\">NEXT ><\/span>");
		} else if(current+1 == imageArr.length) {
			$("#next").replaceWith("<span id=\"next\">NEXT ><\/span>");
			$("#prev").replaceWith("<a href=\"#\" id=\"prev\">< PREV<\/a>");
		} else if(current+1 == 1) {
			$("#prev").replaceWith("<span id=\"prev\">< PREV<\/span>");
			$("#next").replaceWith("<a href=\"#\" id=\"next\">NEXT ><\/a>");
		} else {
			$("#prev").replaceWith("<a href=\"#\" id=\"prev\">< PREV<\/a>");
			$("#next").replaceWith("<a href=\"#\" id=\"next\">NEXT ><\/a>");
		}
	}
	
	// Checks to see where the current status of the slideshow is at and updates the navigation accordingly
			
	function checkNav() {
		buildNav();
		advanceNav();
		
		$("#overlay").css("height", imgObj[current].height + $("#overlay > p").height());
		
		navWidth = $(".nav").width();
		closeLeftPos = $("#slide-close").position().left;
		overlayWidth = $("#overlay").width();
		$(".nav").css("width", navWidth + "px");
		
		if(closeLeftPos < navWidth || navWidth > overlayWidth) {
			$("#slide-close").css("left", navWidth+20+"px");
		} else {
			$("#slide-close").css("left", "");
			$("#slide-close").css("right", "0px");
			closeLeftPosAfter = $("#slide-close").position().left;
			if(closeLeftPosAfter < navWidth) {
				$("#slide-close").css("left", navWidth+20+"px");
			}
		}
				
		$("a#next").click(function() {
			current++;
			$("#overlay").css("width", imgObj[current].width);
			$("#overlay > img").attr("src", imgObj[current].src);
			$("#overlay > p").html(capArr[current]);
			$("#overlay").css("height", imgObj[current].height + $("#overlay > p").height());
			checkNav();
			return false;
		});
				
		$("a#prev").click(function() {
			current--;
			$("#overlay").css("width", imgObj[current].width);
			$("#overlay > img").attr("src", imgObj[current].src);
			$("#overlay > p").html(capArr[current]);
			$("#overlay").css("height", imgObj[current].height + $("#overlay > p").height());
			checkNav();
			return false;
		});
				
		$("a.num").click(function() {
			currentImg = Number($(this).html());
			current = currentImg-1;
			$("#overlay").css("width", imgObj[current].width);
			$("#overlay > img").attr("src", imgObj[current].src);
			$("#overlay > p").html(capArr[current]);
			$("#overlay").css("height", imgObj[current].height + $("#overlay > p").height());
			checkNav();
			return false;
		});
		
		$("img[overlay]").css("width", imgObj[current].width+24);
	}

function putCurrentPageAtEnd(items,pageId){
	//puts the current page at the end of the related resource list
	var currentPageInRelItems = items.find('.' + pageId);
	var parentCategoryList = currentPageInRelItems.parent();
	currentPageInRelItems.remove();
	parentCategoryList.append(currentPageInRelItems);
}

$(window).ready(function(){
		//TODO: determine which of the code below should go in separate js files for each resource type
		
		$('#print-pg').click(function(){
			window.print();
			return false;
		});
		
		//do this first on page load
		var pageId = $('body').attr('id');
		putCurrentPageAtEnd($("div.scrollable").find('.items'),pageId);
		
		// initialize scrollable 
		$("div.scrollable").scrollable({
			size: 1,
			items: '.items',  
			hoverClass: 'hover',
			onBeforeSeek:function(el){
				var items = this.getItemWrap();
				
				
				
				if (!items.data("full")) {
					var scrollId = items.attr("id");
					var path = scrollId.replace('-','/');
					
					$.get(siteBaseUrl + "inc/rel-topics/frag/" + path + ".html", {}, function(data, textStatus){
						
						items.append(data);
						items.data("full", true);
						
						//once extra rel resources loaded, move current page back again
						putCurrentPageAtEnd(items,pageId);
						
						
					});
					
				}
				
				return true;
			}
		});
	
		$("ul.items li").live("click",function(e){
			
			var linkObj = $(this).find('a');
			
			
			
			if ($.browser.msie) {
				
				if (e.button == 0) {//if left mouse click
					
					if (linkObj.attr('target') == "_blank") {
						window.open(linkObj.attr("href"));
						
					}
					else {
						location = linkObj.attr("href");
					}
				}
				
			}else{
				
				if (e.which == 1) {//if left mouse click
				
					if (linkObj.attr('target') == "_blank") {
						window.open(linkObj.attr("href"));
					}
					else {
						location = linkObj.attr("href");
					}
				}
			}
			
			return false;
		})
		
			
		$('#size-tool').fontSizer('#resource-col',{s:".85em",m:"1em",l:"1.2em"});
		
		// Overlay (with expose) function
		$('.ia-box').find(".launch").overlay({
			target:"#ia-overlay",
			close:"#ia-close",
			start:{
				top: 10,
				left: 10,
				width: 0,
				absolute: false,
				closeOnClick:false
			},
			expose: {
				color: '#000000',
				finish: {top: 'center'},
				closeOnClick:false
			}
		});
		
		//handle program and short video
			
		$('#vid-close').click(function(){
			$('.vid-link').show();
			$('#prog-vid-launch').show();
			$('#video-area').find('hr').show();
			$('#totalplayer').hide();
			$('#vid-close-ie').remove();
			$('#flashcontainer').html('<div id="flashplayer"><p class="notice">To view the full experience of this website, please download and install <a href="http://get.adobe.com/flashplayer/" target="_blank">Adobe Flash Player 9</a>.</p></div>');
			$('#commManagerDiv').replaceWith('<div class="commManager" id="commManagerDiv"></div>');
			$('#monetization').replaceWith('<div id="monetization"><div class="clipInfo" id="clipinfowidget"></div></div>');
			//video-short
			$('.vid-box').show();
			
			exposePlayerApi.close();
			return false;
		});
		$('.vid-link').add('#prog-vid-launch a:not(.local-listing)') //launches for program video
		.add('.vid-btn').add('.vid-box a:first').add('.vid-box .box-btn').add('.vid-box a img') //launches for video short
		.click(function() { 
			$('.vid-link').hide();
			$('#prog-vid-launch').hide();
			$('#video-area').find('hr').hide();
			$('#totalplayer').show();
			
			//video-short
			$('.vid-box').hide();
			
	        // open and launch player
	        launchPlayer();
			
			//fix ie7 z-index bug where overlay goes over close button, add duplicate button above it.
			if($.browser.msie){
				if(jQuery.browser.version.substr(0,1)=="7" || jQuery.browser.version.substr(0,1)=="6"){
					var vidCloseBtn = $('#vid-close');
					closeTopPos = vidCloseBtn.offset().top;
					var vidCloseIe = vidCloseBtn.clone(true);
					
					vidCloseIe.attr('id','vid-close-ie');
					
					$('#main').prepend(vidCloseIe);
					
					vidCloseIe.css('top',closeTopPos + 'px');
					
				}
			}
			
			return false;
	    }); 
		
	
	
	/* Dynamic slideshow functionality: takes all images embedded in the body of the page and dynamically generates a slideshow, with navigation, upon clicking to enlarge of any image on the page */
	
	current = 0;
	imgObj = new Array();
	imageArr = new Array();
	capArr = new Array();
	imgArr = $(".img-box").find("a[rel]");	
	
	$(".img-box").find("a[rel]").parent().each(function(i) {
		capArr[i] = $(this).contents().not("a").clone();
	});
		
	for(i=0; i<imgArr.length; i++) {
		imageArr[i] = $(imgArr[i]).attr("href");
		imgObj[i] = new Image();
		imgObj[i].src = imageArr[i];
	}
	
	$(".img-box").find("a[rel]").overlay({
		close:"#slide-close",
		expose: {
			color: '#000000',
			finish: {top: 'center'}
		},
		onLoad: function() { 
			checkNav();
    	}
	});					
	$(".img-box").find("a[rel]").click(function() {
		enlImg = $(this).attr("href");
		current = imageArr.indexOf(enlImg);		
		$("#overlay").css("width", imgObj[current].width);
		$("#overlay > img").attr("src", imgObj[current].src);
		$("#overlay > p").html(capArr[current]);
		return false;
	});		
			
	navArr = $(".num");
	
	// Build navigation initially
	
	//add hover to anchor link
	$('#pbs-share').find('a').hover(
		function(){ $('.pbs_bookmarking_links_div').show(); },
		function(){ $('.pbs_bookmarking_links_div').hide(); }
	);
	
	
	//make inline photos behave like enlarge link
	$('.img-box').each(function(){
		var enlargeBtn = $(this).find('a') 
		$(this).find('img').click(function(){
			enlargeBtn.click();
		});
	});
		
	// Audio answer for ATE postair pages
	
	$(".audio-ans").click(function() {
		audioFile = $(this).attr("href");
				
		QTaud = "<object id=\"audioplayer\" classid=\"clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B\" width=\"230\" height=\"16\" codebase=\"http://www.apple.com/qtactivex/qtplugin.cab\"><param name=\"src\" value=\"" + audioFile + "\" \/><param name=\"AUTOPLAY\" value=\"true\" \/><param name=\"CONTROLLER\" value=\"true\" \/><param name=\"cache\" value=\"true\" \/><param name=\"bgcolor\" value=\"#FFFFFF\" \/><embed id=\"audioplayer\" src=\"" + audioFile + "\" width=\"170\" height=\"16\" alt=\"\" cache=\"TRUE\" autoplay=\"true\" controller=\"true\" bgcolor=\"#FFFFFF\" pluginspage=\"http://www.apple.com/quicktime/download/\"><\/embed><\/object>";
				
		$(this).parent().replaceWith(QTaud);
				return false;
	});
	
	//create any twiddles
	$('#accordion').twiddler({head:"h2"});


	//program video
	$('#prog-vid-launch a:first').addPlayArrow({addHoverEls:"#prog-vid-launch h2 a",useOnClass:true});
	
});





(function($){  
 	$.fn.twiddler = function(options) {  
   
	   	var defaults = {
			head:".twiddle-head",
	    	body:".twiddle-body",
	    	onClass:"on"
			
	   	};  
		
	   	var options = $.extend(defaults, options);  
 		
	   	return this.each(function() {
			
			var group = $(this);
			var heads = group.find(options.head);
			
		   	heads.each(function() {
				
				var twiddle = $(this);
				twiddle.next(options.body).hide()
				twiddle.click(function(event) {
					
					if (twiddle.next(options.body).is(":hidden")) {
						twiddle.next(options.body).slideDown("normal");
						twiddle.toggleClass(options.onClass);
					} else {
						twiddle.next(options.body).slideUp("normal");
						twiddle.toggleClass(options.onClass);
					}
				});	   
			});  
		   
		   
		   
		   
		});  
  	};  
})(jQuery);



(function($){  
 	$.fn.fontSizer = function(target,options) {  
   
	   	var defaults = {  
	    	s:".85em",
	    	m:"1em",
			l:"1.2em"
	   	};  
	   	var options = $.extend(defaults, options);  
 		
	   	return this.each(function() {
			
			var sizes = new Array(options.s,options.m,options.l);
			
			var sizerObj = $(this);
			
		   	for (var x = 1; x <= 3; x++) {
				var sizeEl = sizerObj.find('a:nth-child(' + x + ')');
				if(x==1) sizeEl.addClass("active");
				sizeEl.data("size",sizes[x-1]);
				
				sizeEl.click(function(){
					$(target).css("font-size",$(this).data("size"));
					sizerObj.find('a').removeClass('active');
					$(this).addClass("active");
					return false;
				});
			}
		   
		});  
  	};  
})(jQuery);
