var currentSlide = 0;
var sliderRound = 0;
function homeSlide(n){
	currentSlide++;
	if(currentSlide >= n){ currentSlide = 0;}
	if(currentSlide==0){ sliderRound++; }
	// Stop after 2 rounds.
	if (sliderRound<2){
		$("#homeslideshow").trigger("goto", [currentSlide] );
		setTimeout("homeSlide(" + n + ")",11000);
	}
}

$(document).ready(
	function() {

		// Home Bottom-Center Hover
		$('#homeslideshow p').hoverIntent(function(){
			$(this).addClass('hovered').animate({marginTop: '100px'}, 200 );
		},
		function(){
			$('#homeslideshow p').removeClass('hovered').animate({marginTop: '166px'}, 100 );
		});

		// Home Bottom-Center Slider
		$('#homeslideshow:not(.noslide)').each(function(){
			$.easing.slider = function (x, t, b, c, d, s) {s = .8; if (s == undefined) s = 1.70158; if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b; return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;};
			var navcount = $('#homeslideshow li').size();
			var navposition = 480 - navcount * 10;

			$(this).before('<ul style="left:' + navposition + 'px" id="proceed"></ul>');
			$('#homeslideshow li').each(function(i){
				$('#proceed').append('<li class="p'+ i +'"><a href="#" onclick="$(\'#homeslideshow\').trigger( \'goto\', [ ' + i + ' ] ); return false;">' + i + '</a></li>');
			});
			$('#proceed li.p0').addClass('active');
	
			$('#homeslideshow:not(.noslide)').serialScroll({items:'li', offset:0, start:0, duration:1200, force:false, stop:true, lock:false, cycle:false, easing:'slider', jump: false,
				onBefore:function( e, elem, $pane, $items, pos ){
					if($('#homeslideshow p.hovered').size() > 0){
						return false;
					} else {
						$('#proceed li').removeClass('active');
						$('#proceed li.p' + pos).addClass('active');
					}
				}
			});
			setTimeout("homeSlide(" + navcount + ")",11000);

		});

		// Smoothly scroll to anchor links using ScrollTo.
		$.localScroll();

		// Programs Dropdown Nav in Header
		$("ul#programsnav").removeClass("nojs");
		$("ul#programsnav li a:first").click(function(){return false;});
		$("ul#programsnav").hoverIntent(
			function () {
				$("ul#programsnav ul").slideDown();
			},
			function () {
				$("ul#programsnav ul").slideUp("fast");
			}
		);

		// Searchbox Flyout
		$("ul.footernav li a[href='/search.aspx']").addClass('togglesearch').parent('li').append('<div style="display:none" id="searchbox"><form id="search" method="get" action="/search.aspx"><input type="text" name="query" class="textbox" /><a id="searchbutton" href="#">Go</a><br /><a id="closesearch" class="togglesearch" href="#">Close</a></form></div>');
		$("a.togglesearch").click(function(){
			var sb = $('#searchbox');
			if (sb.is(':visible')){
				sb.fadeOut('fast');
			} else {
				sb.fadeIn('fast');
			}
			return false;
		});
		$("#searchbutton").click(function() {$('form#search').submit();});
		
		// News & Events Toggler
		$("ul#newsevents h3 a").click(function () {
			var ph3 = $(this).parent("h3");
			var pli = ph3.parent("li");
			if(pli.hasClass("active") == false){
				$("ul#newsevents li").removeClass("active");
				$("ul#newsevents li ul").hide();
				pli.addClass("active");
				ph3.next("ul").slideDown();
			}
			return false;
		});
		
		// Homepage launchpad navigation
		$("ul#homelaunchnav a").hover(
			function () {
				$(this).animate({paddingRight:"20px"},50).css("background-position","right center");
			},
			function () {
				$("ul#homelaunchnav a").css("background-position","-9999px 0");
				$(this).animate({paddingRight:"0px"},50, "linear");
			}
		);
		
		// Homepage Student Timeline
		$("ul.timeline li").hover(
			function () {
				var timelineincrement = -100;
				if ($(this).parent("ul").hasClass("inttimeline")){
					// The timeline in the sidebar is 132px tall instead of 100px.
					timelineincrement = -132;
				}
				if ($(this).parent("ul").hasClass("timeline1") || $(this).parent("ul").hasClass("timeline2")){
					// The timeline in the sidebar is 132px tall instead of 100px.
					timelineincrement = -50;
				}
				var bgpos = $(this).children("a.tlink").text() * parseInt(timelineincrement);
				bgpos = "0px " + bgpos.toString() + "px";
				$(this).parent("ul").css("background-position", bgpos);
				if ($("ul.timeline li p").is(':visible')){
					$(this).children("p").css("z-index", "20");
					$(this).children("p").fadeIn('fast');
				} else {
					$(this).children("p").slideDown();
				}
			},
			function () {
				$(this).children("p").css("z-index", "10");
				$(this).parent("ul").css("background-position", "0 0");
				$(this).children("p").fadeOut('slow');
			}
		);
	
		// Add Logo Overlay to content...
		$("p.intro:first img", $("#content")).each(function(){
			$('#content').prepend('<div id="logooverlay"></div>');
		});
		
		// Open links with rel="external" in new window - like ye ole' target="_blank"
		$('a[rel="external"]').click(function() {window.open( $(this).attr('href') ); return false;});

		// Zebra-stripe data tables
		$("#content table tbody").each(function() {
			$(this).removeClass("odd even");
			$("tr:odd", this).addClass("odd");
			$("tr:even", this).addClass("even");
		});

	}
);
