/*
	Department of Chemistry
	University of Rochester
	Author:	John Bertola
	Date:	May 15, 2008
	Javascript Functions
*/

/* Preps the navbar and scrollers */
function initialize()
{
	document.getElementById("sub_nav").style.top = "123px";
	document.getElementById("news").style.top = "0px";
	document.getElementById("events").style.top = "0px";
	document.getElementById("search").getElementsByTagName("div")[0].style.display = "none";
}

function search_swap()
{
	document.getElementById("search").getElementsByTagName("div")[0].style.display = "block";
	document.getElementById("search").getElementsByTagName("a")[0].style.display = "none";
	document.getElementById("search_box").focus();
}

/* Global variables for navigation bar */
var down_status = false;	//keeps track of whether the subnavbar is set to be up or down  
var up_request = false;	//true before delay + animation, false after subnavbar is fully up
var current_down = false;	//keeps track of whether the subnavbar is actually fully up or down
var subnav_hidden = true;	//keeps track of whether a subnav group is showing
var stay_down = false;
var NUM_LINKS = 8;			//the number of links in the primary navbar
var DELAY = 500;			//time in msec after which the subnavbar can retract
var nav_selection = 0;		//the last link that was hovered over with the mouse

/* BEGIN Functions for navbar */

/* Changes the color of a primary link */
function highlight(id)
{
	for (i=1; i<=NUM_LINKS; i++)
	{
		var name = "link" + i;
		if (i == id)
			document.getElementById(name).style.color = "#FFC400";
		else
			document.getElementById(name).style.color = "#FFFFFF";
	}
			
}

/* Changes the color of all primary links to white */
function unhighlight()
{
	for (i=1; i<=NUM_LINKS; i++)
	{
		var name = "link" + i;
		document.getElementById(name).style.color = "#FFFFFF";
	}
}

/* Used to show the sub-navbar */
function subnav_down()
{
	down_status = true;
	object = document.getElementById('sub_nav');
	position = parseInt(object.style.top);
	if (position == 123 || position == 122)
		animate_down();
}

/* Animates the appearance of the sub-navbar */
function animate_down()
{
	object = document.getElementById('sub_nav');
	position = parseInt(object.style.top);
	if (position <= 132)
		object.style.top = (position+3)+"px";
	else if (position <= 143)
		object.style.top = (position+2)+"px";
	else
		object.style.top = (position+1)+"px";
	
	if (parseInt(document.getElementById("sub_nav").style.top) <= 147)
		setTimeout(animate_down, 20);
	else
		current_down = true;
		
	if (!down_status)
		subnav_up();
}

/* Used to hide the sub-navbar */
function subnav_up()
{
	down_status = false;
	stay_down = false;
	object = document.getElementById('sub_nav');
	position = parseInt(object.style.top);
	if (position == 148 && !up_request)
	{
		up_request = true;
		setTimeout(check_up, DELAY);	
	}
}

/* Animates the hiding of the sub-navbar */
function animate_up()
{
	object = document.getElementById('sub_nav');
	position = parseInt(object.style.top);
	if (position >= 136)
		object.style.top = (position-3)+"px";
	else if (position >= 128)
		object.style.top = (position-2)+"px";
	else
		object.style.top = (position-1)+"px";
		
	if (parseInt(document.getElementById("sub_nav").style.top) >= 124)
		setTimeout(animate_up, 25);
	else
	{
		up_request = false;
		current_down = false;
	}
	
	if (down_status || !subnav_hidden)
		subnav_down();
}

/* Prevents the sub-navbar from hiding */
function subnav_stay()
{
	stay_down = true;
}

/* Combines all of the necessary functions to make the sub-navbar hide */
function check_up()
{
	if (!down_status && !stay_down && position == 148)
	{ 
		unhighlight();
		hide_all();
		animate_up();
	}
	else
		up_request = false;
}

/* Hides all sub-navbar links */
function hide_all()
{
	objects = document.getElementById("nav_cont").getElementsByTagName("ul");
	for (i=0; i<objects.length; i++)
		objects[i].style.display = "none";
	subnav_hidden = true;
}

/* Shows a group of sub-navbar links */
function show_nav(id)
{
	nav_selection = id;
	if (!current_down)
		setTimeout(show_nav_helper, 200);
	else
		show_nav_helper();
	subnav_hidden = false;
}

function show_nav_helper()
{
	hide_all();
	highlight(nav_selection);
	var nav = "nav" + nav_selection;
	object = document.getElementById(nav);
	object.style.display = "block";
}

/* END Functions for navbar */

/* BEGIN Functions for scrollers */

/* Global variables for news/events scroller */
var scrolling = false;  	//set to true if the scroller is animating, false otherwise
var news_selected = true;  //keeps track of whether the news or events scroller is being displayed
var current_news = 0;  		//the location of the news scroller (zero is the first news item)
var current_event = 0;		//the location of the events scroller (zero is the first news item)
var new_location = 0;		//prior to animating, this variable is set to the "top" value of the relevant scroller
var box;					//used to hold scroller objects (div tag elements)

/* Shows the news scroller */
function show_news()
{
	document.getElementById("news").style.zIndex = "1100";
	document.getElementById("news_h").style.backgroundColor = "#608BAF";
	document.getElementById("news_h").style.color = "#001E36";
	document.getElementById("events").style.zIndex = "1050";
	document.getElementById("events_h").style.backgroundColor = "#CCCCCC";
	document.getElementById("events_h").style.color = "#666666";
	news_selected = true;
}

/* Shows the events scroller */
function show_events()
{
	document.getElementById("news").style.zIndex = "1050";
	document.getElementById("news_h").style.backgroundColor = "#CCCCCC";
	document.getElementById("news_h").style.color = "#666666";
	document.getElementById("events").style.zIndex = "1100";
	document.getElementById("events_h").style.backgroundColor = "#608BAF";
	document.getElementById("events_h").style.color = "#001E36";
	news_selected = false;
}

/* Makes the relevant scroller move down to display the next item at the top */
function scroll_down()
{
	if (news_selected)
	{
		id = "news";
		p_tags = document.getElementById(id).getElementsByTagName("p");
		if (current_news < (p_tags.length-1) && !scrolling)
		{
			box = document.getElementById(id);
			position = parseInt(document.getElementById(id).style.top);
			scroll_amt = p_tags[current_news].offsetHeight - 1;
			new_location = position - scroll_amt;
			current_news = current_news + 1;
			scrolling = true;
			animate_next_item();
		}
	}
	else
	{
		id = "events";
		p_tags = document.getElementById(id).getElementsByTagName("p");
		if (current_event < (p_tags.length-1) && !scrolling)
		{
			box = document.getElementById(id);
			position = parseInt(document.getElementById(id).style.top);
			scroll_amt = p_tags[current_event].offsetHeight - 1;
			new_location = position - scroll_amt;
			current_event = current_event + 1;
			scrolling = true;
			animate_next_item();
		}
	}
}

/* Animates the scroller, making it display the next item at the top */
function animate_next_item()
{
	position = parseInt(box.style.top);
	rom = position - new_location;
	if (rom >= 15)
		box.style.top = (position-6)+"px";
	else if (rom >= 6)
		box.style.top = (position-2)+"px";
	else
		box.style.top = (position-1)+"px";
		
	if (rom > 0)
		setTimeout(animate_next_item, 25);
	else
		scrolling = false;
}

/* Makes the relevant scroller move up to display the previous item at the top */
function scroll_up()
{
	if (news_selected)
	{
		id = "news";
		p_tags = document.getElementById(id).getElementsByTagName("p");
		if (current_news > 0 && !scrolling)
		{
			box = document.getElementById(id);
			current_news = current_news - 1;
			position = parseInt(document.getElementById(id).style.top);
			scroll_amt = p_tags[current_news].offsetHeight - 1;
			new_location = position + scroll_amt;
			scrolling = true;
			animate_prev_item();
		}
	}
	else
	{
		id = "events";
		p_tags = document.getElementById(id).getElementsByTagName("p");
		if (current_event > 0 && !scrolling)
		{
			box = document.getElementById(id);
			current_event = current_event - 1;
			position = parseInt(document.getElementById(id).style.top);
			scroll_amt = p_tags[current_event].offsetHeight - 1;
			new_location = position + scroll_amt;
			scrolling = true;
			animate_prev_item();
		}
	}
}

/* Animates the scroller, making it display the previous item at the top */
function animate_prev_item()
{
	position = parseInt(box.style.top);
	rom = -1 * (position - new_location);
	if (rom >= 15)
		box.style.top = (position+6)+"px";
	else if (rom >= 6)
		box.style.top = (position+2)+"px";
	else
		box.style.top = (position+1)+"px";
		
	if (rom > 0)
		setTimeout(animate_prev_item, 25);
	else
		scrolling = false;
}

/* END Functions for Scrollers */
