$(document).ready(function () {
	if ($('html').hasClass('oldie')) {
		$('input').focus(function () {
			var ph = $(this).attr('placeholder');
			if (ph && $(this).val() == ph) {
				$(this).val('');
			}
			$(this).addClass('-focus');
		});
		$('input').blur(function () {
			var ph = $(this).attr('placeholder');
			if (ph && $(this).val() == '') {
				$(this).val(ph);
			}
			$(this).removeClass('-focus');
		});
		$('input[placeholder]').each(function () {
			if ($(this).val() == '') {
				$(this).val($(this).attr('placeholder'));
			}
		});
		$('input:focus').focus();
		$('li:last-child').addClass('-last-child');
		var inav = $('#inner-nav td');
		inav.first().addClass('-first-child');
		inav.last().addClass('-last-child');
	}
	$('input.autoselect').focus(function () {
		$(this).select();
		$(this).data('focus_selected', '1');
	});
	$('input.autoselect').mouseup(function (e) {
		if ($(this).data('focus_selected')) {
			e.preventDefault();
			$(this).data('focus_selected', null);
		}
	});
	$('input.autoselect').blur(function () {
		$(this).val($(this).val());
	});
	
	// utilities
	$('.pressable,.date-items li').click(function (e) {
		var l = $(this).find('a');
		if (l.size()) {
			e.preventDefault();
			window.location.href = l.attr('href');
			return;
		}
	});

    // backdrop
	var bd = $('#backdrop');
	if (bd.size()) {
		var bd_img, bd_orig;
		var bd_up_only = false;
		var bd_center_v = false;
		
		function resize_backdrop() {
	        var vp = [bd.width(), bd.height()];
	        var vp_rat = vp[0]/vp[1];
	        var dims = bd_orig;
	        var rat = dims[0]/dims[1];
	        var w=vp[0], h=vp[1], l='auto', t='auto';
	        if (vp_rat < rat)
	        {
	        	if (bd_up_only && h < bd_orig[1]) {
	        		h = bd_orig[1];
	        	}
	            // match height
	            w = h*rat;
	        }
	        else if (vp_rat > rat)
	        {
	        	if (bd_up_only && w < bd_orig[0]) {
	        		w = bd_orig[0];
	        	}
	            // match width
	            h = w/rat;
	        }
	        l = ((vp[0]-w)/2)+'px';
	        t = ((vp[1]-h)/2)+'px';
	        if (!bd_center_v) {
	            t = 0+'px';
	        }
	        bd_img.css({
	            "width":w+'px',
	            "height":h+'px',
	            "left":l,
	            "top":t
	        });
		}
		
		function show_backdrop(w, h) {
			bd_orig = [bd_img.width(), bd_img.height()];
			resize_backdrop();
			$(window).resize(resize_backdrop);
			bd_img.animate({'opacity':1}, 1000);
		}
		
		// we want to fade the backdrop in after both the Image's and the
		// window's onload events have fired (in webkit I've observed the
		// onload for the Image firing before the dimensions of the image
		// are available)
		var bd_load = 2;
		
		$(window).load(function () {
			bd_load = bd_load - 1;
			if (bd_load == 0) {
				show_backdrop();
			}
		});
		
		// preload backdrop image
		var pl = $('<img/>');
		pl.css('opacity', 0);
		pl.load(function () {
			bd_img = pl;
			bd_load = bd_load - 1;
			if (bd_load == 0) {
				show_backdrop();
			}
		});
		pl.attr('src', bd.data('src'));
		bd.append(pl);
	}
	
	// home boxes
	var hbs = $('.home-box');
	if (hbs.size()) {
		var hb_container = $('.home-boxes');
		var hb_slides; // defined after clones created
		var hb_cur = -1;
		var hb_next_cur = 0;
		var hb_speed = 600;
		var hb_easing = 'swing';
		
		/**
		 * Animations:
		 */
		
		function reveal_panel(which) {
			var clones = [];
			hb_container.clearQueue('hb_reveal').queue('hb_reveal', function (next) {
				// halt all slide animations
				hb_slides.stop(true);
				// hide all cover slides in this panel immediately
				which.find('.slide').hide();
				// fade in other cover slides
				var to_show = which.data('slides').size();
				which.data('slides').each(function () {
					// create a clone on top of the other slides and fade it in
					var my_clone = $(this).data('clone').css({'opacity':0,'display':'block'}).appendTo($(this).parent());
					//var my_clone = $(this).css({'opacity':0,'display':'block'}).appendTo($(this).parent());
					my_clone.animate({'opacity':1}, hb_speed, hb_easing, function () {
						to_show = to_show - 1;
						if (to_show == 0) {
							// all (clone) cover slides are opaque
							next();
						}
					});
				});
			}).queue('hb_reveal', function (next) {
				// make original slides top-most
				which.data('slides').css({'opacity':1,'display':'block'}).each(function () {
					$(this).appendTo($(this).parent());
				});
				// hide other slides
				hb_slides.not(which.data('slides')).hide();
				next();
			}).dequeue('hb_reveal');
		}
		
		function reveal_all(instant) {
			if (typeof instant != 'undefined') {
				// this is only called on document load
				hbs.find('a').stop(true).css({'bottom':'-50px'});
				hbs.data('slides').stop(true).css({'opacity':0,'display':'none'});
				return;
			}
			hb_container.clearQueue('hb_reveal').queue('hb_reveal', function (next) {
				// halt all animations
				hb_slides.stop(true);
				// hide them all
				var to_hide = hb_slides.size();
				hb_slides.animate({'opacity':0}, hb_speed, hb_easing, function () {
					to_hide = to_hide - 1;
					if (to_hide == 0) {
						next();
					}
				});
			}).queue('hb_reveal', function (next) {
				hb_cur = -1;
				// clean up (hide all)
				hb_slides.hide();
				next();
			}).dequeue('hb_reveal');
		}
		
		function focus_panel(which) {
			if (which.data('focusing')) {
				return;
			}
			which.data('focusing', true);
			// show "Learn More"
			which.find('a').stop(true).animate({'bottom':0}, hb_speed/2, hb_easing, function () {
				which.data('focusing', false);
				which.data('blurring', false);
			});
		}
		
		function blur_panel(which) {
			if (which.data('blurring')) {
				return;
			}
			which.data('blurring', true);
			// hide "Learn More"
			which.find('a').stop(true).animate({'bottom':'-50px'}, hb_speed/2, hb_easing, function () {
				which.data('blurring', false);
				which.data('focusing', false);
			});
		}
		
		function blur_all() {
			// only called after preloading, prior to revealing the panels
			hbs.find('a').css({'bottom':'-50px'});
		}
		
		/**
		 * Events (called after preloading panels/slides)
		 */
		
		function setup_hb_events() {
			// ready for interaction
			reveal_all(true);
			hbs.hover(function () {
				reveal_panel($(this));
				focus_panel($(this));
			}, function () {
				blur_panel($(this));
			});
			hb_container.hover(function () {
				// nothing
			}, function () {
				reveal_all();
			});
			hbs.click(function (e) {
				e.preventDefault();
				window.location.href = $(this).find('a').attr('href');
			});
		}
		
		/**
		 * Preparations/preloading:
		 * 
		 * We preload all the "slides" (images that cover the other three
		 * panels when you hover over one) and then reveal the panels in their
		 * "blurred" state (no "Learn More" link).
		 * 
		 * When you hover over a panel, the other three are obscured by that
		 * panel's slides. z-index proved to be an issue (I think a stack -- as
		 * in a push/pop array -- of z-indexes would have to be maintained
		 * to make that work). So we move each slide into the panel it will be
		 * obscuring, and when we need to make a particular slide visible, we
		 * move it to the end of its parent (appendChild) so that it's on top
		 * of any other slides that might be (semi-) visible.
		 * 
		 * When you move from panel 1 to panel 2, consider panel 4. First it
		 * was obscured by panel 1's slide; now panel 2's slide is fading in
		 * on top. If we let the animation go to completion, all is well. But
		 * if we move back to panel 1 before the animation completes we go
		 * from:
		 * 
		 * 		(Bottom -> Top)
		 * 		Panel 4 -> Slide 1 (opaque) -> Slide 2 (semi-opaque)
		 * 
		 * to:
		 * 
		 * 		Panel 4 -> Slide 2 (semi-opaque) -> Slide 1 (invisible)
		 * 
		 * So you see a distracting flash of Panel 4 showing through before
		 * Slide 1 fully fades in.
		 * 
		 * To solve this problem, we create a clone of each slide, and we only
		 * ever fade-in the clones. So instead we have:
		 * 
		 * 		Panel 4 -> Slide 1 (opaque) -> Clone 2 (semi-opaque)
		 * 
		 * which becomes:
		 * 
		 * 		Panel 4 -> Slide 1 (opaque) -> Clone 2 (semi-opaque)
		 * 			    -> Clone 1 (invisible)
		 * 
		 * You can move between panels as much as you want, but you'll always
		 * have the opaque Slide 1 as a backstop until one of the fading
		 * animations completes. This ends up being good-enough.
		 * 
		 * After an animation completes, we move the (always opaque) actual
		 * slide to the top (covering its clone) and then hide all other slides
		 * and clones.
		 * 
		 * (Initially, I solved this by creating a new clone each time a slide
		 * needed to be faded in and then deleting all clones when an animation
		 * finally completed, but unsurprisingly this proved too slow,
		 * especially in IE. That would have been the most "accurate"
		 * rendering of what's going on.)
		 */
		
		hbs.css({'visibility':'hidden'});
		var to_load = hbs.size();
		hbs.each(function () {
			var self = $(this);
			var others = hbs.not(self);
			var slides = self.find('img.slide').not('.moved');
			self.data('slides', slides);
			var slides_to_load = slides.size();
			slides.each(function (i) {
				// preload:
				var pl = $('<img/>');
				pl.load(function () {
					slides_to_load = slides_to_load - 1;
					if (slides_to_load == 0) {
						to_load = to_load - 1;
						if (to_load == 0) {
							// hide all "Learn more" links initially
							blur_all();
							if ($('html').hasClass('oldie')) {
								// fading in causes rendering issues in IE
								hbs.css({'visibility':'visible'});
								setup_hb_events();
							} else {
								hbs.css({'opacity':0,'visibility':'visible'});
								hbs.animate({'opacity':1}, hb_speed/2, setup_hb_events);
							}
						}
					}
				});
				pl.attr('src', $(this).attr('src'));
				// position:
				var new_parent = others.eq(i);
				$(this).addClass('moved').appendTo(new_parent);
				// create a clone for fading
				$(this).data('clone', $(this).clone().addClass('clone').appendTo(new_parent));
			});
		});
		// save a master set of slides and their clones
		hb_slides = hb_container.find('.slide');
	}
	
	// home news
	var hn_container = $('.home-news');
	if (hn_container.size()) {
		var hn_groups = hn_container.find('.slice');
		var hn_groups_animating = false;
		hn_groups.find('h3').click(function (e) {
			e.preventDefault();
			if (hn_groups_animating) {
				return;
			}
			hn_groups_animating = true;
			var hn_cur = hn_groups.filter('.active').find('.stories');
			var hn_new = $(this).closest('.slice').find('.stories');
			if (hn_cur.is(hn_new)) {
				return;
			}
			var hn_height = hn_new.children('.w').height();
			hn_cur.children('.w').animate({'top':'-'+hn_height+'px'}, function () {
				hn_cur.closest('.slice').removeClass('active');
				hn_new.children('.w').css('top', '-'+hn_height+'px');
				hn_new.closest('.slice').addClass('active');
				hn_reset_pager();
				hn_new.children('.w').animate({'top':0}, function () {
					hn_groups_animating = false;
				});
			});
		});
		var hn_all_buttons = hn_container.find('.pager a');
		var hn_view_width = hn_container.find('.stories>.w').width();
		var hn_page_animating = false;
		
		function hn_reset_pager() {
			var hn_cur = hn_groups.filter('.active');
			var hn_buttons = hn_cur.find('.pager a');
			var stories_ul = hn_cur.find('ul');
			var stories = stories_ul.children('li');
			var cur_ml = parseInt(stories_ul.css('left'));
			var p = Math.round(Math.abs(cur_ml/hn_view_width));
			var max_p = Math.ceil(stories.size()/3);if (p == 0) {
				hn_buttons.filter('.prev').addClass('disabled');
			} else {
				hn_buttons.filter('.prev').removeClass('disabled');
			}
			if (p == max_p - 1) {
				hn_buttons.filter('.next').addClass('disabled');
			} else {
				hn_buttons.filter('.next').removeClass('disabled');
			}
		}
		
		hn_all_buttons.click(function (e) {
			e.preventDefault();
			if ($(this).hasClass('disabled') || hn_page_animating) {
				return;
			}
			var hn_cur = hn_groups.filter('.active');
			var hn_buttons = hn_cur.find('.pager a');
			var stories_ul = hn_cur.find('ul');
			var stories = stories_ul.children('li');
			var cur_ml = parseInt(stories_ul.css('left'));
			var p = Math.round(Math.abs(cur_ml/hn_view_width));
			var max_p = Math.ceil(stories.size()/3);
			var next_p;
			if ($(this).hasClass('prev')) {
				next_p = p - 1;
			} else {
				next_p = p + 1;
			}
			if (next_p < 0) {
				next_p = 0;
			}
			if (next_p >= max_p) {
				next_p = max_p-1;
			}
			if (next_p != p) {
				hn_page_animating = true;
				var target_ml = next_p * (hn_view_width);
				stories_ul.animate({'left':(-1*target_ml)+'px'}, function () {
					p = next_p;
					if (p == 0) {
						hn_buttons.filter('.prev').addClass('disabled');
					} else {
						hn_buttons.filter('.prev').removeClass('disabled');
					}
					if (p == max_p - 1) {
						hn_buttons.filter('.next').addClass('disabled');
					} else {
						hn_buttons.filter('.next').removeClass('disabled');
					}
					hn_page_animating = false;
				});
			}
		});
		hn_all_buttons.mouseup(function (e) {
			$(this).blur();
		});
		
		hn_container.find('li').click(function (e) {
			e.preventDefault();
			var link = $(this).find('a');
			window.location.href = link.attr('href');
		});
		
		hn_reset_pager();
		
		// home news slideshow
		var hn_slides = hn_container.find('img');
		var hn_slide_width = 244;
		var hn_slide_height = 158;
		var hn_slide_rotate_pause = 4800;
		var hn_slide_rotate_time = 1200;
		var hn_slide_rotate_timer;
		var hn_slide_rotating = true;
		var hn_slide_animating = false;
		var hn_slide_cur = 0;
		var hn_slides_num = hn_slides.size();
		
		function hn_show_slide(which) {
			if (which >= hn_slides_num) {
				which = 0;
			} else if (which < 0) {
				which = hn_slides_num-1;
			}
			if (hn_slide_animating || which == hn_slide_cur) {
				return;
			}
			hn_slide_animating = true;
			var next = hn_slides.eq(which);
			var cur = hn_slides.eq(hn_slide_cur);
			// fading animation
			next.css({'opacity':0.0}).addClass('next');
			next.animate({'opacity':1.0}, hn_slide_rotate_time, function () {
				cur.removeClass('active');
				next.addClass('active').removeClass('next');
				hn_slide_cur = which;
				hn_slide_animating = false;
			});
			// sliding animation:
			/*next.css({'top':(-1*hn_slide_height)+'px'});
			next.animate({'top':0}, hn_slide_rotate_time);
			cur.animate({'top':hn_slide_height+'px'}, hn_slide_rotate_time, function () {
				cur.removeClass('active');
				next.addClass('active');
				hn_slide_cur = which;
				hn_slide_animating = false;
			});*/
		}
		
		function hn_slide_rotate() {
			hn_show_slide(hn_slide_cur+1);
		}

	    hn_slide_rotate_timer = window.setInterval(hn_slide_rotate, hn_slide_rotate_pause);
	}
	
	// landing page slideshow
	var show = $('.landing-slideshow');
	if (show.size()) { (function () {
		var container = show.find('.slides');
	    var slides = container.find('.slide');
	    var slide_width = 984;
	    var rotate_pause = 7000;
	    var rotate_time = 1000;
	    var rotate_timer;
	    var rotating = true;
	    var animating = false;
	    var cur = 0;
	    var num = slides.size();
	    
	    // clone first slide so we can rotate to it without rewinding
	    container.css('width', ((num+2)*slide_width)+'px');
	    $(slides.get(0)).clone().appendTo(container);
	    // clone last slide for the same reason
	    $(slides.get(num-1)).clone().prependTo(container);
	    // reposition so first slide is still showing
	    container.css({'margin-left':(-1*slide_width)+'px'});
	    
	    function show_slide(which, by_rotate)
	    {
	        if (cur == which || animating)
	        {
	            return;
	        }
	        if (which < 0)
	        {
	            which = num-1;
	        }
	        if (which >= num)
	        {
	            which = 0;
	        }
	        if (rotate_timer)
	        {
	            window.clearTimeout(rotate_timer);
	            rotate_timer = false;
	        }
	        var target_l = -1*which*slide_width - slide_width;
	        if (by_rotate && which == 0)
	        {
	            target_l = -1*num*slide_width - slide_width;
	        }
	        animating = true;
	        container.animate({marginLeft:target_l+"px"}, rotate_time, function () {
	            cur = which;
	            if (by_rotate && which == 0)
	            {
	                container.css({"margin-left":(-1*slide_width)+'px'});
	            }
	            if (rotating)
	            {
	                rotate_timer = window.setTimeout(rotate, rotate_pause);
	            }
	            show.find('.controls .option').removeClass('active').eq(which).addClass('active');
	            animating = false;
	        });
	    }
	
	    
	    function rotate()
	    {
	        if (!rotating || animating)
	        {
	            return; // and don't reschedule
	        }
	        var next = cur+1;
	        if (next >= num)
	        {
	            next = 0;
	        }
	        show_slide(next, true);
	    }
	    
	    // controls
	    show.find('.controls a').click(function (e) {
	        e.preventDefault();
	        var num = $(this).closest('.option').prevAll().size();
	        show_slide(num, false);
	    });
	    
	    // pause on hover
	    show.hover(function () {
	        rotating = false;
	        if (rotate_timer)
	        {
	            window.clearTimeout(rotate_timer);
	            rotate_timer = false;
	        }
	    },
	    function () {
	        rotating = true;
	        if (!rotate_timer)
	        {
	            rotate_timer = window.setTimeout(rotate, rotate_pause/2);
	        }
	    });
	    
	    // start rotating
	    rotate_timer = window.setTimeout(rotate, rotate_pause);
	})() }
    
    // virtual tour
    var vt = $('.virtual-tour');
    if (vt.size() && !$('body').hasClass('admin-content'))
    {
        var viewer = vt.find('.viewer');
        var slides = $('.virtual-tour-slide');
        var thumbs = vt.find('.thumb');
        var links = thumbs.find('a');
        var transitioning = false;
        var rotating = true;
        var cur;
        
        slides.hide().appendTo(viewer);
        
        var main = $('#main');
        
        // rotate slides
        var rotate_timer;
        var rotate_pause = 7000;
        function rotate_slides()
        {
            if (rotating == false || transitioning)
            {
                return;
            }
            if (cur == slides.size()-1)
            {
                show_vt_slide(0);
            }
            else
            {
                show_vt_slide(cur+1);
            }
        }
        
        // move first slide into position
        function show_vt_slide(num)
        {
            if (transitioning || cur == num)
            {
                return;
            }
            if (typeof cur != 'undefined')
            {
                if (num >= slides.size())
                {
                    num = 0;
                }
                else if (num < 0)
                {
                    num = slides.size()-1;
                }
            }
            transitioning = true;
            links.eq(cur).removeClass('active');
            window.clearTimeout(rotate_timer);
            var slide = slides.eq(num);
            slide.css({opacity:0});
            var animating = true;
            var loading = false;
            if (!slide.data('loaded'))
            {
                loading = true;
                var pl_src = slide.find('img').attr('src');
                var pl_img = $('<img>');
                pl_img.load(function () {
                    loading = false;
                    slide.data('loaded', true);
                    if (animating && typeof cur != 'undefined')
                    {
                        return;
                    }
                    slide.animate({opacity:1}, function () {
                        cur = num;
                        transitioning = false;
                        rotate_timer = window.setTimeout(rotate_slides, rotate_pause);
                        links.eq(cur).addClass('active');
                    });
                });
                pl_img.attr('src', pl_src);
            }
            if (typeof cur == 'undefined')
            {
                slide.show();
                animating = false;
                return;
            }
            slides.eq(cur).animate({opacity:0}, function () {
                animating = false;
                slide.show();
                $(this).hide();
                if (loading == false)
                {
                    slide.animate({opacity:1}, function () {
                        cur = num;
                        transitioning = false;
                        rotate_timer = window.setTimeout(rotate_slides, rotate_pause);
                        links.eq(cur).addClass('active');
                    });
                }
            });
        }
        // preload slides
        thumbs.each(function () {
            var src = $(this).data('src');
            var thumb_src = $(this).data('thumb');
            var thumb = $(this);

            var to_load = 2;
            
            var pl_img = $('<img>');
            var pl_thumb = $('<img>');
            
            pl_img.load(function () {
                to_load--;
                if (to_load == 0)
                {
                    pl_thumb.animate({"opacity":1});
                }
            });
            
            pl_thumb.css({'opacity':0});
            thumb.find('a').append(pl_thumb);
            pl_thumb.load(function () {
                to_load--;
                if (to_load == 0)
                {
                    pl_thumb.animate({"opacity":1});
                }
            });

            pl_img.attr('src', src);
            pl_thumb.attr('src', thumb_src);
        });
        links.click(function (e) {
            e.preventDefault();
            var num = $(this).closest('.thumb').prevAll().size();
            rotating = false;
            show_vt_slide(num);
        });
        vt.find('.nav a').click(function (e) {
            e.preventDefault();
            rotating = false;
            if ($(this).attr('href') == '#next')
            {
                show_vt_slide(cur+1);
            }
            else
            {
                show_vt_slide(cur-1);
            }
        });
        thumbs.hover(function () {
        	thumbs.not($(this)).stop(true).animate({'opacity':0.75}, 300);
        }, function () {
        	thumbs.not($(this)).stop(true).animate({'opacity':1}, 300);
        });
        show_vt_slide(0);
        rotate_timer = window.setTimeout(rotate_slides, rotate_pause);
    }
    
    // "Did You Know?"
    var dyk = $('.did-you-know');
    if (dyk.size()) {
        var $messages = dyk.find('.text p');
        $messages.hide();
        var messages = $messages.get();
        messages.sort(function () { return 0.5 - Math.random(); });
        $(messages[0]).show();
        var cur_message = 0;
        var message_animating = false;
        var message_timer = false;
        var message_pause = 8000;
        var message_anim = 1200;
        function rotate_message()
        {
            if (message_animating)
            {
                return;
            }
            message_animating = true;
            var old_message = cur_message;
            cur_message = old_message + 1;
            if (cur_message >= messages.length)
            {
                cur_message = 0;
            }
            $(messages[old_message]).fadeOut(message_anim/2, function () {
                $(messages[cur_message]).fadeIn(message_anim/2, function () { message_animating = false; });
            });
        }
        dyk.find('.text').hover(function () {
            if (message_timer)
            {
                window.clearInterval(message_timer);
                message_timer = false;
            }
        }, function () {
        	if (!message_timer) {
                message_timer = window.setInterval(rotate_message, message_pause);
        	}
        });
        if (!message_timer) {
            message_timer = window.setInterval(rotate_message, message_pause);
        }
    }
    
    /**
     * Research faculty
     */
    var fm = $('.faculty-member');
    if (fm.size()) {
    	var sn = fm.find('.member-subnav');
    	var sn_lis = sn.find('li');
    	var sections = fm.find('.details-section');
    	var showing = 0;
    	
    	function fm_show(num) {
    		if (num == showing) {
    			return;
    		}
    		var sn_li = sn_lis.eq(num);
    		if (!sn_li.size()) {
    			return;
    		}
    		var section = sections.eq(num);
    		if (!section.size()) {
    			return;
    		}
    		sections.removeClass('open');
    		section.addClass('open');
    		sn_lis.removeClass('active');
    		sn_li.addClass('active');
    		showing = num;
    		if (showing != 0) {
    			fm.addClass('showing-sub');
    		} else {
    			fm.removeClass('showing-sub');
    		}
    	}
    	
    	sn.find('a').click(function (e) {
    		e.preventDefault();
    		var target = $(this).closest('li').prevAll().size();
    		if (target == showing) {
    			return;
    		}
    		fm_show(target);
    	});
    	
    	fm.find('.header .photo').click(function (e) {
    		fm_show(0);
    	});
    }
    
    /** staff **/
    var sl = $('.staff-list');
    if (sl.size()) {
    	sl.delegate('li', 'click', function (e) {
    		e.preventDefault();
    		window.location.href = $(this).find('a').attr('href');
    	});
    }
    
    /** outcomes **/
    var oc = $('.app\\/outcomes');
    if (oc.size()) {
    	oc.find('select').bind('change', function () {
    		if ($(this).val() == '') {
    			return;
    		}
    		if ($(this).attr('id') == 'program_field') {
    			// disable the year
    			oc.find('select[name="year"]').attr('disabled', 'disabled');
    		}
    		$(this).closest('form').submit();
    	});
    }
    
    /** clinical research projects **/
    var projects = $('.app\\/research\\/projects');
    if (projects.size()) {
    	// form
    	$('.app\\/research\\/projects .finder select').change(function (e) {
    		if ($(this).val() == '') {
    			return;
    		}
    		$(this).closest('form').submit();
    	});
    	// results:
    	var project_tabs = $('.app\\/research\\/projects .results .tabs a');
        var project_content = $('.app\\/research\\/projects .results .tab-content .tab');
        function open_project_info(e)
        {
            var tab;
            if (typeof e == 'string')
            {
                tab = project_tabs.filter('a[href='+e+']');
            }
            else
            {
                e.preventDefault();
                tab = $(this);
            }
            var project = tab.closest('.project');
            project.find('.tabs li').removeClass('active');
            tab.parent('li').addClass('active');
            var clicked = $(tab.attr('href'));
            project.find('.tab').removeClass('active');
            clicked.addClass('active');
        }
        project_tabs.live('click', open_project_info);
        // archive
        $('.app\\/research\\/projects .archive .show a').click(function (e) {
        	e.preventDefault();
        	$(this).closest('.archive').addClass('archive-open');
        });
    }
    
    /** research weekly seminars **/
    var seminars = $('.app\\/research\\/seminars');
    if (seminars.size()) {
        // archive
        $('.app\\/research\\/seminars .archive .show a').click(function (e) {
        	e.preventDefault();
        	$(this).closest('.archive').addClass('archive-open');
        });
    }
    
    /** careers **/
    var careers = $('.app\\/careers\\/list');
    if (careers.size()) {
    	// section headers
    	function toggle_section(elem) {
    		if (elem.hasClass('open')) {
    			elem.removeClass('open');
    			elem.find('a.toggle').text('+');
    		} else {
    			elem.addClass('open');
    			elem.find('a.toggle').text('–');
    		}
    	}
    	careers.find('h3.section-name').click(function (e) {
			e.stopPropagation();
			toggle_section($(this).closest('.section'));
    	});
    	careers.find('a.toggle').click(function (e) {
    		e.preventDefault();
    	});
    	// job details
    	function toggle_job(elem) {
    		if (elem.hasClass('open')) {
    			elem.removeClass('open');
    			elem.find('a.more').text('details');
    		} else {
    			elem.addClass('open');
    			elem.find('a.more').text('close');
    		}
    	}
    	careers.find('li.job .info h4').click(function (e) {
    		e.stopPropagation();
    		toggle_job($(this).closest('li.job'));
    	});
    	careers.find('li.job a.more').click(function (e) {
			e.preventDefault();
    	});
    }
    
    /** hide/reveal upcoming events **/
    var nl = $('.app\\/news\\/news-list');
    if (nl.size()) {
    	var upcoming = nl.find('li.event.upcoming');
    	if (upcoming.size() > 2) {
    		var to_hide = upcoming.slice(0, -2);
    		to_hide.hide();
			var show_message = 'Show '+to_hide.size()+' more future events';
			var hide_message = 'Hide above events';
    		var revealer = $('<li class="reveal"><a href="#reveal">'+show_message+'</a></li>');
    		revealer.insertAfter(to_hide.last());
    		revealer.click(function (e) {
				e.preventDefault();
				var link = $(this).find('a');
				if (link.text() == show_message) {
					link.text(hide_message);
					to_hide.show();
				} else {
					link.text(show_message);
					to_hide.hide();
				}
    		});
    	}
    }
    
    /** donation form **/
    var df = $('.app\\/donate');
    if (df.size()) {
    	var cb = df.find('.recognition-group input[type="checkbox"]');
    	function toggle_recognition_fields(select) {
    		cb.each(function () {
        		var others = $(this).closest('.field').siblings();
    			if ($(this).filter(':checked').size()) {
    				others.show();
    				if (typeof select != 'undefined' && select) {
        				others.first().find('input').select();
    				}
    			} else {
    				others.hide();
    			}
    		});
    	}
    	cb.bind('click change keyup', function (e) {
			var not_this = cb.not($(this));
    		if ($(this).filter(':checked').size()) {
    			not_this.removeAttr('checked');
    		}
    		toggle_recognition_fields(true);
    	});
    	toggle_recognition_fields(false);
    }
    
    /** directions **/
    var directions = $('.location-directions');
    if (directions.size()) {
    	// variant headers
    	function toggle_dir(elem) {
    		if (elem.hasClass('open')) {
    			elem.removeClass('open');
    			elem.find('a.toggle').text('+');
    		} else {
    			elem.addClass('open');
    			elem.find('a.toggle').text('–');
    		}
    	}
    	directions.find('h5.dir-name').click(function (e) {
			e.stopPropagation();
			toggle_dir($(this).closest('.dir'));
    	});
    	directions.find('a.toggle').click(function (e) {
    		e.preventDefault();
    	});
    }
});
