/*
	Department of Chemistry
	University of Rochester
	Author:	John Bertola
	Date:	October 17, 2011
	Updated Javascript Functions
*/

function search_swap()
{
	$("#search div").eq(0).css("display", "block");
	$("#search a").eq(0).css("display", "none");
	$("#search_box").focus();
}

(function () {
	$("#sub_nav").css("top", "123px");
	$("#news, #events").css("top", "0px");
	$("#search div:first-child").css("display", "none");

	/* Navigation Container */
	
	(function () {
		var nav_element = $("#nav ul:first-child"),
			subnav_element = $("#sub_nav"),
			banner_element = $("#banner"),
			status_current = "up",
			timeout_id = null;
		
		function show_ul(ul_no) {
			var uls = nav_element.find("ul");
			uls.css("visibility", "hidden");
			highlight(ul_no);
			
			if (ul_no !== null)
				uls.eq(ul_no).css("visibility", "visible");
		}
		
		function highlight(link_no) {
			var num_links = nav_element.children("li").size();
			
			for (var i=0; i<num_links; i++)
				nav_element.children("li").eq(i).children("a").css("color", (i === link_no ? "#FFC400" : "#FFFFFF"));
		}
		
		function subnav_up() {
			if (status_current == "down")
			{
				status_current = "animating";
				show_ul(null);
				subnav_element.animate({top: "-=25"}, 240, "swing", function () {
					status_current = "up";
				});	
			}
		}
		
		function subnav_down(ul_no) {
			if (status_current == "up")
			{
				status_current = "animating";
				subnav_element.animate({top: "+=25"}, 240, "swing", function () {
					status_current = "down";
					show_ul(ul_no);
				});
			}
			else if (status_current == "down")
				show_ul(ul_no);
		}
		
		function set_timer() {timeout_id = setTimeout(subnav_up, 2000);}
		function clear_timer() {clearTimeout(timeout_id);}
		
		nav_element.delegate('a[id^="link"]', 'mouseover', function () {
			subnav_down($(this).parent().prevAll().size());
		});
		
		nav_element.hover(clear_timer, set_timer);
		subnav_element.hover(clear_timer, set_timer);
		banner_element.hover(clear_timer, set_timer);
	})();
	
	/* Scroller Container */
	
	(function () {
		var news_selected = null,
			scrolling = false,
			current_locs = [];
			
		current_locs["news"] = current_locs["events"] = 0;
		
		function show_news() {
			$("#news").css("z-index", 1100);
			$("#news_h").css("background-color", "#608BAF").css("color", "#001E36");
			$("#events").css("z-index", 1050);
			$("#events_h").css("background-color", "#CCCCCC").css("color", "#666666");
			
			news_selected = true;
		}
		
		function show_events() {
			$("#news").css("z-index", 1050);
			$("#news_h").css("background-color", "#CCCCCC").css("color", "#666666");
			$("#events").css("z-index", 1100);
			$("#events_h").css("background-color", "#608BAF").css("color", "#001E36");
			
			news_selected = false;
		}
		
		function scroll_up() {
			var id = news_selected ? "news" : "events",
				box = $("#"+id),
				p_tags = box.children("p"),
				scroll_amt;
			
			if (current_locs[id] > 0 && !scrolling)
			{
				current_locs[id]--;
				scroll_amt = "+=" + p_tags.eq(current_locs[id]).outerHeight(true);
				scrolling = true;
				
				box.animate({top: scroll_amt}, 400, "swing", function () {
					scrolling = false;
				});	
			}
		}
		
		function scroll_down() {
			var id = news_selected ? "news" : "events",
				box = $("#"+id),
				p_tags = box.children("p"),
				scroll_amt;
			
			if (current_locs[id] < (p_tags.size() - 1) && !scrolling)
			{
				scroll_amt = "-=" + p_tags.eq(current_locs[id]).outerHeight(true);
				current_locs[id]++;
				scrolling = true;
				
				box.animate({top: scroll_amt}, 400, "swing", function () {
					scrolling = false;
				});	
			}
		}
		
		$("#news_h").bind("click", show_news);
		$("#events_h").bind("click", show_events);
		$("#down").bind("click", scroll_down);
		$("#up").bind("click", scroll_up);
		
		show_events();
	})();
})();
