Drupal.behaviors.live_filter = function() {

  $('.view-hsf-fellow-finder .views-exposed-widget input.form-submit').hide();
  
  $('.view-hsf-fellow-finder .views-exposed-widget select').change(function() {
    $(".view-hsf-fellow-finder form").submit();
  });
}

$(function () {

	$("a.new-window").newwindow();	

	var page_container = $("#container");
	var page_left_side_bar = $("#sidebar-left");
	var page_right_side_bar = $("#sidebar-right");
	var page_header = $("#ihs-header");
	var page_center_content = $("#content-content");
	var page_node = page_center_content.find("div.node:first");
	var page_content_bottom = $("#content-bottom");
	var page_content_top = $("#content-top");

	// For themeing reasons, stick the right side bar after the header element
	var main_page_header = page_container.find(".ihs-page > .ihs-header");
	var main_page_body = page_container.find(".ihs-page > .ihs-body");
		
	if (page_right_side_bar.length && main_page_header.length) {
		page_right_side_bar.insertAfter(main_page_header);
		main_page_body.addClass("ihs-body-sidebar");				
	}
	
	if (page_content_bottom.length && main_page_body.length) {
		main_page_header.after(main_page_body);
		main_page_body.append(page_content_bottom);
	}
	
	// Stick the top block inside the center column
	if (page_content_top.length > 0) {
		main_page_body.prepend(page_content_top);
		page_content_top.show();
	}
	
	// Strip out white space from peoplename lists
	//$(".view-people-list ul li").htmlClean();
	
	// If we're on the front page and we have the carousel library loaded,
	// connect the relevant elements
	var promoted_carousel = $("#promoted-nodes-block");
	
	if (promoted_carousel.length > 0 && promoted_carousel.jcarousel) {
	
    promoted_carousel.jcarousel({
      scroll: 1,
      auto: 8,
      wrap: "last",
      buttonNextHTML: null,
      buttonPrevHTML: null,
      itemVisibleInCallback: function (carousel, elm, index, action) {
				
    		$(".jcarousel-control a").removeClass("active-carousel-link");
				$(".jcarousel-control a:eq(" + (index - 1) + ")").addClass("active-carousel-link");
      },
      initCallback: function (carousel) {

		    carousel.clip.hover(function() {
		        carousel.stopAuto();
		    }, function() {
		        carousel.startAuto();
		    });

		    $('.jcarousel-control a').bind('click', function() {
					carousel.startAuto(0); 
	    		$(".jcarousel-control a").removeClass("active-carousel-link");
	    		$(this).addClass("active-carousel-link");
	        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
	        return false;
		    });
		    
		    $("#promoted-nodes-block li").click(function () {
		    
		    	window.location.href = $(this).find("h4 > a").attr("href");
		    	return false;
		    });
      }
    });
    
    $('.jcarousel-control a:first').addClass("active-carousel-link");
	}	
	

	// The left hand navigation menu should not show 5th or lower
	// level navigation elements, so hide / remove them here
	var fourth_level_menu = $("#container #sidebar-left ul.menu ul.menu ul.menu ul.menu ul.menu");

	if (fourth_level_menu.length > 0) {
		
		fourth_level_menu.parent("li.active-trail").addClass("active-leaf");		
		fourth_level_menu.remove();		
	}	
	
	$("#sidebar-left ul.menu li.active-trail")
		.find("a:first")
		.addClass("active-trail-link");

	$("#sidebar-left ul.menu a.active")
		.parent("li")
		.addClass("active-leaf");

	$("#sidebar-left a.active-trail-link:last")
		.closest("li")
			.addClass("active-leaf");

	$("#header #logo-title").click(function () {
		window.location.href = Drupal.settings.basePath;
		return false;
	});
	
	$(".ihs-top-button-link").click(function () {	
		window.scrollTo(0, 0);
		return false;
	});
	
	// Emulate hover events on pager items
	$(".item-list .pager li").live("mouseover", function () {	
		$(this).addClass("hover");
	}).live("mouseout", function () {
		$(this).removeClass("hover");
	});
		
	// If this is the search results page, we need to clone the search form and
	// duplicate it at the bottom of the page

	if ($(".search-form").length && main_page_body.length) {
		$(".box h2").remove();
		main_page_body.append($(".search-form").clone());	
	}
	
	// Implement flyout menus for root level menus for IE7 and better browsers
	$("#block-menu-primary-links > div.content > ul.menu > li")
		.click(function () {
			
			if ($(this).is(".ihs-flyout-parent")) {
				
				window.location.href = $(this).children("a").attr("href");
				return false;			
			}		
		})
		.each(function () {
		
			if ( ! $(this).hasClass("active-trail")) {
				
				var that = this;
				var child_menu = $(this).find("ul:first");
			
				child_menu
					.addClass("ihs-flyout")
					.hide()
					.click(function (event) {

						$(this).find("li").click(function () {
							return false;
						});

						return false;
					})
					.find("a")
						.click(function () {						
							window.location.href = $(this).attr("href");
							return false;
						});
	
				$(this).hover(function () {
	
					$(this).addClass("ihs-flyout-parent");	
					
					if ( ! is_ie6()) {
					
						child_menu
							.css("left", page_left_side_bar.offset().left + page_left_side_bar.width() - 10)
							.css("top", $(that).offset().top)
							.show();						
					}			
	
				}, function () {
				
					$(this).removeClass("ihs-flyout-parent");			
					
					if ( ! is_ie6()) {
					
						child_menu.hide();				
					}
				});
			}
		});
		
	// If this is the contact page, make it so changing the FAQ
	// select option redirects the page
	$("select#ihs-contact-faq").change(function () {
		
		if ($(this).val()) {
			window.location.href = $(this).val();
		}	
	});
		
	// Event funtionality specific to the program finders
	var ish_update_program_finder = function () {
	
		var program_finder = $(".view-program-finder");
	
		if (program_finder.length)
		{
			var program_finder_form = program_finder.find("form");
		
			var program_finder_submit = program_finder.find("input[type='submit']");
	
			var clear_all_button = $("<span class='view-program-finder-clear-all'>Clear all</span>")
				.click(function () {
					program_finder_form
						.find("input[type='checkbox']")
							.attr("checked", false)
							.attr("disabled", true)
							.end()
						.find("input[type='text']")
							.val("")
							.end()
						.submit();
				});
			
			program_finder_form
				.find(".views-exposed-widget-container-edit-search-terms .views-widget")
					.append(program_finder_submit)
					.append(clear_all_button);
	
			program_finder_form.find(".views-exposed-widget:last").remove();	
	
			program_finder
				.find("input[type='checkbox']")					
					.click(function () {						
						program_finder_form.submit();
						program_finder
							.find("input[type='checkbox']")
							.attr("disabled", true);
					});
		
			// Override the default Drupal views response method, so that
			// we can capture when the form has been updated, and reattach
			// our custom events		
			Drupal.Views.Ajax.ajaxViewResponse = function(target, response) {
			
			  var $view = $(target);
			
			  // Check the 'display' for data.
			  if (response.status && response.display) {
			    var $newView = $(response.display);
			    $view.replaceWith($newView);
			    $view = $newView;
			    Drupal.attachBehaviors($view.parent());
			  }
			
			  if (response.messages) {
			    // Show any messages (but first remove old ones, if there are any).
			    $view.find('.views-messages').remove().end().prepend(response.messages);
			  }
			  			  
			  
			  ish_update_program_finder();
			};		
		}
	};	
	
	ish_update_program_finder();
	
	function is_ie6() {
		
		return ($.browser.msie && $.browser.version.substr(0, 1) < 7);	
	
	}	
});
