// ****************************************************************************************************** //
// ***************************					 jQuery 				  ******************************* //
// ***************************   The Rockefeller University 2009.08.12    ******************************* //
// ****************************************************************************************************** //

$(document).ready(function(){
	
	
	//CENTENNIAL FUNCTIONS
	$('#dd1').change(function(){
		//alert($(this).val());	
		window.location = "100Stories.php?ra="+$(this).val();
	});
	$('#dd2').change(function(){
		//alert($(this).val());	
		window.location = "100Stories.php?r="+$(this).val();
	});
	$('#dd3').change(function(){
		//alert($(this).val());	
		window.location = "100Stories.php?d="+$(this).val();
	});


	// toggler
	$('#content a.toggler').each(function(){
	
		var div = $(this).attr('href').toString().split('#')[1];
		this.toggleDiv = $('#'+div);
		this.toggleDiv.hide();
		
		$(this).click(function(e){
			if( this.toggleDiv.is(':hidden') ) 
			{
				this.toggleDiv.show();
				var h = this.toggleDiv.height(); 	// keep heights equal
			}
			else 
			{
				this.toggleDiv.hide();
				var h = this.toggleDiv.height()*-1;	// keep heights equal
			}
			
			e.preventDefault();	
			
			// match heights with each show/hide
			$('#nav ul:first, #content').equalHeight(h);				
		});
	
	});
		
	
	// ************************************************************************************************* //
	// 							 OPEN NEW WINDOW W/ HREF AS URL 	      						    *** //
	// add attributes to class name in link i.e. <a href="test.php" class="popWin width120 scroll1"> *** //
	// ************************************************************************************************* //	
	$('a.popWin, area.popWin').click(function()
	{
	// window defaults
		var props = {
			width: 720,
			height: 500,
			location: 0,
			menubar: 0,
			resizable: 1,			
			scrollbars: 0,
			status: 0,
			toolbar: 0
		};
		var hrefClass = this.className.toString();
		var URL = $(this).attr('href');
		var winProps = '';
		
	// if values exist in link class for win properties, replace defaults w/ these
		for(var prop in props) 
		{
			if(hrefClass.match(prop)) {
				p = prop.toString()+'\\d+';
				props[prop] = hrefClass.match(p).toString().match(/\d+/);
			}
				winProps += prop.toString()+'='+props[prop].toString()+',';			
		}
		var newWin = window.open(URL,'RU',winProps);
		return false;
	});



	// ******************************************************************************************************** //
	//  						OPEN LARGER IMAGES DYNAMICALLY FROM HREF IN THUMBNAILS    						  //
	// 			Give the containing element id="dynamicPop" OR individual links class="dynamicPop" 			  //
	// ******************************************************************************************************** //
	$("#dynamicPop a, a.dynamicPop").click(function(evt)
	{
		evt.preventDefault();
	// get href of link (for src of full size image)
		var href = $(this).attr('href');
	// get position of link (to pos dynamic div)
		var pos = $(this).position();		
		
	// create div container
		var picDiv = $( document.createElement('div') );
		picDiv.attr('class','dynamicPopped draggable');
		picDiv.css('position','absolute');
		picDiv.css('top',pos.top-100);
		picDiv.css('left',pos.left-100);
		
	// create image
		var img = $( document.createElement('img') );
		img.attr('src',href);
		
	// create link+event handler to close dynamic div
		var a = $( document.createElement('a') );
		a.attr('href','#close');
		a.html('Close');
		a.bind('click', function(evt)
		{
			$(this).unbind('click');		
			$(this).parent().remove();
			evt.preventDefault();			
		});
		
	// append to div container and to document
		picDiv.append(a);
		picDiv.append($( document.createElement('br') ));
		picDiv.append(img);
		
		$('#content').append(picDiv);
	// make div draggable
		picDiv.draggable();
	});


	// ******************************************************************************************* //
	// ***************************   PRINTER FRIENDLY TOGGLE    ********************************** //
	// ******************************************************************************************* //
	$('#printerFriendly').click(function(event){
		window.print();
		event.preventDefault();
	});
	
	
	// *****************************************************************************
	// *****************  CLEAR DEFAULT TEXT ON INPUT FOCUS  ***********************
	// *****************************************************************************
	// cache results
	var defaultValueElements = $('input:text.defaultValue, textarea.defaultValue');
	
	defaultValueElements.each(function(){
	// set default value as prop. of element
			this.defaultVal = this.value;
	});
	defaultValueElements.focus(function(){
	// if value == default, clear default value & set color
			if($(this).val() == this.defaultVal)
			{
				$(this).val('');
				$(this).css('color','#000');
			}
	});
	defaultValueElements.blur(function(){
	// if value == blank or default reset back to default && change color
			if( $(this).val() == '' || $(this).val() == ' ' || $(this).val() == this.defaultVal)
			{
				$(this).css('color','#ccc')
				$(this).val(this.defaultVal);
			} else {
			// if value is not blank or default remove class 'defaultValue' && blur/focus event handlers
				$(this).removeClass('defaultValue'); 
				$(this).unbind('blur, focus');
			} 
	});


		
	// ***************************************************************************** //
	// ***********************  NAVIGATION  **************************************** //
	// *****************************************************************************	//
	// add arrow class to lis in #nav w/ sub menus
	$('#nav > ul li:has(ul)').addClass('arrow');	
	
	// add click events to toggle arrows & subs
	$('#nav > ul li:has(ul) a').click(function(event){
	
		// get height difference w/ each click to keep column heights matched
		var h = 0;
		
		if(event.target.href.charAt(event.target.href.length-1) == '#') 
		{
		// comment out following function to keep all clicked sub menus visible		
			$('#nav > ul li:siblings').each(function(){
				if(this.className == 'arrowActive' && this != event.target.parentNode) {
					h -= this.getElementsByTagName('ul')[0].offsetHeight;
					var c = this.className;
					var cn = c.replace(/arrowActive/, 'arrow');
					this.className = cn;
				}	
			});  	

		// toggle classes between arrow and arrowActive (this toggles the arrow position & visibility of sub nav)
			if($(this).parent().hasClass('arrow')) 
			{
				h += $(this).parent().find('ul').height();
				$(this).parent().removeClass('arrow').addClass('arrowActive');
				
				
			} 
			else if($(this).parent().hasClass('arrowActive')) 
			{
				h -= $(this).parent().find('ul').height();
				$(this).parent().removeClass('arrowActive').addClass('arrow');
			}
			
		// prevent default event
			event.preventDefault();
			
		// match heights with each menu expand/collapse
		$('#nav ul:first, #content').equalHeight(h);			
		}
	});

	// keep sub nav open between pages
	//
	// check if url has a 'sub=' to display
	if(location.href.match('sub=')){
	// split out open sub num X (sub=X)
		var regex = /sub=\d\d?/;
		var $open_sub = regex.exec(location.href.toString());
		$open_sub_num = $open_sub[0].split('=')[1];
		$open_sub_num = $open_sub_num - 1;
	
	// get list items and add arrowActive class to list item passed in url, if it exists
		var x = $('#nav > ul li:siblings');
		if(x[$open_sub_num]) 
		{
			x[$open_sub_num].className = 'arrowActive';	
			
			// set intitial heights to match
			$('#nav ul:first, #content').equalHeight(x[$open_sub_num].offsetHeight);
		}
	}
	
	// get index of open sub menu in nav
	$('#nav > ul ul a').click(function(event){
		var $sub_num = $(this).parents('li:eq(1)').prevAll('li').length;
	// start count at 1, not 0
		$sub_num = $sub_num + 1;		
	// get href of clicked link
		var $href = $(this).attr('href');
		
	// if href has "sub=" leave it alone
		if(!$href.match('\\sub='))
		{
		// check if "?" (parameter(s)) already exist(s) in href and use "&sub=", rather than "?sub=" if so
			var $sym = ($href.match('\\?')) ? '&' : '?';
		
		// if href has no "/" (folder) or '#' and the last character isn't '/', it is a link to same folder/dept. so keep sub open
			if ($href.lastIndexOf('/') == -1 && !$href.match('#') && $href.charAt($href.length-1) != '/') $href = $href+$sym+'sub='+$sub_num;
		// if href has "/" (not as last char.) check if folder/dept. in href is the same as folder/dept. in URL
			else if ($href.lastIndexOf('/') != -1 && $href.charAt($href.length-1) != '/') {
				var $href_parts = $href.split('/');
				
				var $url = location.href;
				var $url_parts = $url.split('/');
				
				if($href_parts[$href_parts.length-2] == $url_parts[$url_parts.length-2]) $href = $href+$sym+'sub='+$sub_num;
			}
			$(this).attr('href',$href);
		}
	});

	
	// set intitial heights to match
	$('#nav ul:first, #content').equalHeight();

	// ***************************************************************************** //
	// **************************  STRIPE TABLES *********************************** //
	// ***************************************************************************** //
	// stripe even rows on table(s)
	$('tr:even', $('table:not(.noStripe)')).each(function() {
		$(this).addClass('tableStripe');
	});



	// *****************************************************************************
	// ***********************  SCROLL ARROWS  *************************************
	// *****************************************************************************
	if( $('div.scrollFrame').length > 0 ) 
	{
		$('div.scrollFrame > a').click(function(evt){	
			var speed = 1000;
			var distance = 100;
			
			var scrollContent = $(this).prevAll('.scrollContent');
			var contentTop = scrollContent.css('top');
			var direction = $(this).attr('href');
			var contentHeight = scrollContent.height();	
			var newTop = 0;
			
			if(direction == '#up'){
				 newTop = parseInt(contentTop) + distance;
				 if(newTop >= 0)
						newTop = 0;
			}
			if(direction == '#down'){
				newTop = parseInt(contentTop) - distance;
				if(newTop < parseInt(contentHeight*-1))
					newTop = parseInt(contentHeight*-1);
			}
			
			scrollContent.animate({top: newTop}, speed, function(){
				scrollContent.css('top',newTop);
			});
		evt.preventDefault();
		});
	}

});


	
// *****************************************************************************
// **********************  MATCH COLUMN HEIGHTS  *******************************
// *****************************************************************************
$.fn.equalHeight = function(h) 
{	
	if(!h) var h = 0;
	var tallest = 0;
	
	$(this).each(function()
	{
		var thisHeight = $(this).height();
		if(thisHeight > tallest) tallest = thisHeight;

	});

	$(this).each(function()
	{
		$(this).height(eval(tallest+h));
	});
};	

