currentSet = 0;

// Slides captions up

function FixHeight(selected, transition)
{
	selected = parseInt(selected);
	var firstInSet = Math.floor(selected/2) + 1;
	var captionLength = $("#captions li").size();
	
	if (currentSet != firstInSet)
	{
		var padding = 28;
		var current = selected - (selected % 2);

		if (current != 0)
		{
			var offsetHeight = 0;
			do
			{
				current = current - 1;
				offsetHeight = offsetHeight + $("#item" + current).height() + padding;
			} while (current != 0)
			offsetHeight = '-' + offsetHeight + 'px';
			
			$('#captions ul').animate({
				top: offsetHeight
			}, 1500);
		}
		
		else
		{
			$('#captions ul').animate({
				top: 0
			}, 1500);
		}
		
		var height = 0;
		current = selected - (selected % 2);
	
		while ((current != captionLength) && (current != (selected - (selected % 2)) + 2))
		{
			height = height + $("#item" + current).height() + padding;
			current = current + 1;
		}
		
		if (height > 258)
			height = 258;
		height = (height - 8) + 'px';
		
		if (transition == 'transition')
		{
			$('#captions').animate({
					height: height
				}, 1500);
		}
		
		else
		{
			$('#captions').css("height", height);
			$('#captions .hidden').removeClass("hidden");
			$('#up-arrow a, #down-arrow a').remove();
		}
		
		// Setup arrows
		
		$('#up-arrow, #down-arrow').unbind();
		if ((selected == 0) && (captionLength > 2))
		{
			$('#up-arrow').attr("class", "up-arrow-off");
			$('#down-arrow').attr("class", "down-arrow-on");
			$('#down-arrow').click(function () { Arrow (selected, "down"); });
		}
		
		else if (((selected + 2) >= captionLength) && (captionLength > 2))
		{
			$('#up-arrow').attr("class", "up-arrow-on");
			$('#down-arrow').attr("class", "down-arrow-off");
			$('#up-arrow').click(function () { Arrow (selected, "up"); });
		}
		
		else
		{
			$('#up-arrow').attr("class", "up-arrow-on");
			$('#down-arrow').attr("class", "down-arrow-on");
			$('#up-arrow').click(function () { Arrow (selected, "up"); });
			$('#down-arrow').click(function () { Arrow (selected, "down"); });
		}
	}
	
	currentSet = firstInSet;
}

// Up/down arrow for captions

function Arrow (selected, direction)
{
	Pause();
	if (direction == 'up')
		selected = selected - (selected % 2) - 2;
	else
		selected = selected - (selected % 2) + 2;
	FixHeight(selected, 'transition');
}

// Pause slideshow

function Pause ()
{
	$('#images').cycle('pause'); 
	$('#pause').replaceWith('<li id="resume"><img src="index_files/images/resume.png" alt="Resume" width="23" height="33"></li>');
}

// Resume slideshow

function Resume ()
{
	$('#images').cycle('resume', true); 
	$('#resume').replaceWith('<li id="pause"><img src="index_files/images/pause.png" alt="Pause" width="23" height="33"></li>');
}

// Trigger for caption sliding from slideshow

function SliderCallback (currSlideElement, nextSlideElement)
{
	var id = $(nextSlideElement).attr("id").substring(5);
	FixHeight(id, 'transition');
}

$(document).ready(function()
{
	FixHeight(0, 'none');
	
	/* Dropdown menus */
	
	$("#top-menu li, #top-menu2 li").mouseover(function() {
		$(this).addClass("hover");
	}).mouseout(function() {
		$(this).removeClass("hover");
	});
	
	/* Making them keyboard accessible (based off of http://carroll.org.uk/sandbox/suckerfish/bones2.html */
	
	$("#top-menu a, #top-menu2 a").each(function() {
		$(this).focus(function() {
			$(this).parent().addClass("hover");
			$(this).parents("li").addClass("hover");
		});
		
		$(this).blur(function() {
			$(this).parent().removeClass("hover");
			$(this).parents("li").removeClass("hover");
		});
	});
	
	/* Lightbox */
	
	
	$('#photos li a, #video a').prettyPhoto({
		showTitle: false
	});
	
	/* Slideshow */
	
	$('#highlights ul li.active').removeClass('active');
	
	$('#feature-image').append('<ul id="controls"><li id="prev"><img src="index_files/images/previous.png" alt="Previous" width="32" height="33"></li><li id="pause"><img src="index_files/images/pause.png" alt="Pause" width="23" height="33"></li><li id="next"><img src="index_files/images/next.png" alt="Next" width="33" height="33"></li>');
	
	$('#images').cycle({ 
		fx:     'fade', 
		speedIn:  '500', 
		speedOut: '500',
		timeout:  '8000',
		prev:   '#prev',
		next: 	'#next',
		before: SliderCallback,
		pager:  '#highlights ul', 
		pagerAnchorBuilder: function(idx, slide) { 
			// return selector string for existing anchor 
			return '#highlights ul li:eq(' + idx + ') a'; 
		} 
	});
	
	$('#pause').live("click", Pause);
	$('#resume').live("click", Resume);
	
	/* News tabs */
	
	$("#in-the-news a").live("click", function(event)
	{
		$('#news .hidden').attr('class', 'show');
		$('#news .unhidden').attr('class', 'hidden');
		$('.show').attr('class', 'unhidden');
		
		$('#header').replaceWith('<div id="header"><p id="buzz"><a href="?news=1"><img src="index_files/images/campus_buzz_off.gif" alt="Campus Buzz" width="139" height="38"></a></p><h2 id="in-the-news"><img src="index_files/images/mason_in_the_news_on.gif" alt="Mason in the News" width="293" height="38"><a href="http://news.gmu.edu/articles/category/mason-in-the-news/feed" class="rss"><img src="index_files/images/rss_icon.gif" alt="RSS" width="13" height="14"></a></h2></div>');
		return false;
	});
	
	$("#buzz a").live("click", function(event)
	{
		$('#news .hidden').attr('class', 'show');
		$('#news .unhidden').attr('class', 'hidden');
		$('.show').attr('class', 'unhidden');
		
		$('#header').replaceWith('<div id="header"><h2 id="buzz"><img src="index_files/images/campus_buzz_on.gif" alt="Campus Buzz" width="158" height="38"><a href="http://news.gmu.edu/articles/category/latest-news/feed" class="rss"><img src="index_files/images/rss_icon.gif" alt="RSS" width="13" height="14"></a></h2><p id="in-the-news"><a href="?news=2"><img src="index_files/images/mason_in_the_news_off.gif" alt="Mason in the News" width="274" height="38"></a></p></div>');
		return false;
	});
});
