/* 

		Designed and Developed by Jordan T. Lowe
		Beckman Institute For Advanced Science and Technology 
		University of Illinois at Urbana-Champaign

		This is where the magic happens...
		Take her out for a night on the town.
		If you play you're cards right, she
		might just show you all of her functions.
                 
		LOVED in July
        
		make her pretty:  
		http://jsbeautifier.org/  

*/



$(document).ready(function () {


	/**
		* Quicklook Drawer Functionality
		*
		* Opens and closes the quicklook drawer
		*/
	$(function () {
		var openSpeed  = 600;
		var closeSpeed = openSpeed * (2 / 3); //close faster than open
		var easingType = "easeInOutCubic";

		$(".quicklook-btn > a").toggle(
			function () {
				var drawerHeight = $("#quicklook-outer").outerHeight(true);

				//set drawer marginTop to negative value of drawer height
				$("#quicklook-outer").css({ marginTop: "-" + drawerHeight + "px" });
				//set drawer to "display:block"
				$("#quicklook-outer").show();
				//slide dwon drawer by animating top margin to 0px
				$("#quicklook-outer").animate({ marginTop: "0px" }, openSpeed, easingType);
				//Change button icon
				$(".quicklook-btn > a").css({ backgroundPosition: "center -30px", opacity: "1.0" });
			},
			function () {
				var drawerHeight = $("#quicklook-outer").outerHeight(true);

				//set drawer marginTop to negative value of drawer height
				$("#quicklook-outer").animate({ marginTop: "-" + drawerHeight + "px" }, closeSpeed, easingType,
					function () {
						//set marginTop back to default of 0px
						$(this).css({ marginTop: "0px" });
						//set drawer to "display:none" for accessibility
						$("#quicklook-outer").css({ display: "none" });
					}
				);

				$(".quicklook-btn > a").removeAttr('style');
			}
		);

	});





	/**
		* Research List Group Interactivity
		*/
	$(".research-list .title").click(
		function () {
			var slideSpeed     = 400;
			var parent         = $(this).parent();
			var pubList        = parent.find('ul');
			var pubListItems   = pubList.children();
			var pubListHeight  = 0;
			var childCount     = 0;
			var scrollSpeed    = 500;
			var easingType     = "easeInOutCubic";

			//Slow the list "height animation" for longer lists
			if (pubListItems.length > 15) {
				slideSpeed  += 200;
				scrollSpeed += 100;
			}

			if (parent.hasClass("showing") == false) {
				$(".research .showing ul").animate({ height: "0px" }, slideSpeed);
				$(".research .showing .state-indicator").css({ "background-position": "center 6px" });
				$(".research .showing").removeClass("showing");
				
				//set height of ul, since initially 0px
				pubListItems.each(function () {
					pubListHeight += $(this).outerHeight();
				});

				//animate the height to show the list
				pubList.animate({ height: pubListHeight }, slideSpeed, function () {
					//add showing class
					parent.addClass("showing");
					//scroll page to list
					$("html, body").animate({
						scrollTop: $(pubList).offset().top - 50
					}, scrollSpeed, easingType);
				});

				//change state-message in title bar
				$(this).find(".state-message").html("collapse &ndash;");
				$(this).find(".state-indicator").css({ "background-position": "center -25px" });
			}
			else {
				pubList.animate({ height: "0px" }, slideSpeed, function () {
					parent.removeClass("showing");
				});

				$(this).find(".state-message").html("click to expand +");
				$(this).find(".state-indicator").css({ "background-position": "center 6px" });
			}

		});





	/**
		* Tech Transfer Gallery Interactivity
		*/	
	$(function () {

		$(".techtransfer .navigation .button").click(function () {

			var index       = $(this).index() + 1; //get the button's possion
			var currentImg  = $(".techtransfer .gallery .showing");
			var currentBtn  = $(".techtransfer .gallery .on");
			var imgToShow   = $(".techtransfer .gallery .images").find("img:nth-child(" + index + ")");
			var thisBtn     = $(this);

			if (!thisBtn.hasClass("on")) { //if not already showing image 
				currentImg.fadeOut("slow", 0, function () {
					currentImg.removeClass("showing");
				});

				//remove on status for current images corresponding button
				currentBtn.removeClass("on");

				imgToShow.fadeIn().addClass("showing");

				//set on status for new image's corresponding button
				thisBtn.addClass("on");
			}

		});

	});





	/**
		* Email Address Link
		*
		* Outputs an email address given in specific format
		*/	

	//Output address as a link
	$(function () {
		var spt  = $('.mailme-link');
		spt.each(function () {
			var at   = / at /;
			var dot  = / dot /;
			var addr = $(spt).text().replace(at, "@").replace(dot, ".");
			$(spt).after('<a href="mailto:' + addr + '" title="Send an email">' + addr + '</a>');
			$(spt).remove();
		});
	});

	//Output address as a link
	$(function () {
		var spt  = $('.mailme-display');
		spt.each(function() {
			var at   = / at /;
			var dot  = / dot /;
			var addr = $(this).text().replace(at, "@").replace(dot, ".");
			$(this).html(addr);
		});
	});





	/**
		* Scroll to CV Section Anchor
		*/	

	$(".cv-nav a").click(function () {
		var href        = $(this).attr('href');
		var scrollTo    = $(href).offset().top  - $(".cv-nav-outer").outerHeight() - 50; //leave room for .cv_nav 
		var scrollSpeed = 500 + (scrollTo * .15); //slow down for longer scrolls
		var easingType  = "easeInOutCubic";

		$('html,body').stop();  //stop scrolling so we can begin scrolling imediately
		$('html,body').animate( {scrollTop: scrollTo}, scrollSpeed, easingType );

		return false; //stop default <a> click behavior
	});





	/**
		* CV Navigation Top Fixation
		*/	

	$(function () {

		//if element exists, else no need to watch event
		if( $(".cv-nav-outer").length > 0 ) {
			
			var cvNavTop          = $(".cv-nav-outer").offset().top - 12; //-12px for .cv_nav top positioning
			var cvNavOuterHeight  = $(".cv-nav-outer").outerHeight(true) + 50; //total height of .cv_nav_outter
			var mainTopPad        = $("#main").css("padding-top");  //gets original padding-top to be used below

			$(window).scroll(function() {  //when window is scrolled
					var scrolled = $(window).scrollTop(); //how far has it scrolled

					//if scrolled past top of .cv_nav_outer
					if(scrolled >= cvNavTop){  
						//give it class to apply style
						$(".cv-nav-outer").addClass("cv-nav-outer-fixed"); 
						$(".cv-nav").addClass("cv-nav-fixed");  //give it class to apply style

						//set padding-top of #main to cv_nav height so content doesn't shift up.
						$("#main").css( {"padding-top" : cvNavOuterHeight} ); 
					}
					else if(scrolled < cvNavTop) {
						$(".cv-nav-outer").removeClass("cv-nav-outer-fixed");
						$(".cv-nav").removeClass("cv-nav-fixed");

						//reset padding-top back to original
						$("#main").css({"padding-top" : mainTopPad}); 
						
						//resets back to initial value
						cvNavTop = $(".cv-nav-outer").offset().top - 12; 
					}

			});

		}
		
	});




});


