// ****************************************************************************************************** //
// ***************************					 jQuery 				  ******************************* //
// ***************************   The Rockefeller University 2009.11.10   ******************************* //
// ****************************************************************************************************** //

$(document).ready(function(){
	
	// ************************************************************************************************* //
	// 							 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(e){
		e.preventDefault();
		$(this).popWin();
	});



	// ******************************************************************************************************** //
	//  						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 classes to nav lis containing uls to show arrow & hide subnavs
	$('#nav > ul li:has(ul)').addClass('arrow');
	
	// check for class="subX" on nav from mod_rewrite
	var regex = /sub\d\d?/;
	var navClasses = $('#nav').attr('class');
	
	if( navClasses != undefined && navClasses.match(regex) )
	{
		var $open_sub = regex.exec(navClasses);
		var $open_sub_num = $open_sub.toString().replace('sub','');
		$open_sub_num = $open_sub_num - 1;

		$('#nav > ul > li:eq('+$open_sub_num+'):has(ul)').addClass('arrowActive');			
	};
	
	// for nav item(s) with subnav, but no page to link to, use '#' as href & uncomment below to enable
	/*	
	$('#nav > ul li:has(ul) a[href=#]').click(function(e){	
		$(this).parent().siblings().each(function(){
			if(	$(this).hasClass('arrowActive') ) $(this).removeClass('arrowActive').addClass('arrow');
		});
		$(this).parent().addClass('arrowActive');
		e.preventDefault();
	}); 
	*/
	
	// 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();
		});
	};


	// *****************************************************************************
	// *************************  ROUNDED CORNERS  *********************************
	// *****************************************************************************	
	// if div#content has class 'hasRounded' 
	// find elements & add rounded corners to elements w/ class 'rounded'
	if( $('#content').hasClass('.hasRounded') )
	{
		var $hasRounded = $('#content.hasRounded');
		$hasRounded.find('.rounded').niftycube();
		$hasRounded.find('.roundedT').niftycube('top');
		$hasRounded.find('.roundedB').niftycube('bottom');
		$hasRounded.find('.roundedL').niftycube('left');
		$hasRounded.find('.roundedR').niftycube('right');
	};
	
});

// start closure
(function($) {	
	// *****************************************************************************
	// **********************  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));
		});
	};	
	
	
	// ************************************************************************************************* //
	// 							 OPEN NEW WINDOW W/ HREF AS URL 	      						    *** //
	// add attributes to class name in link i.e. <a href="test.php" class="popWin width120 scroll1"> *** //
	// ************************************************************************************************* //		
	$.fn.popWin = function(url)
	{
	// window defaults
		var props = {
			width: 720,
			height: 500,
			location: 0,
			menubar: 0,
			resizable: 1,			
			scrollbars: 0,
			status: 0,
			toolbar: 0
		};
		var hrefClass = ( $(this).attr('class') !== undefined ) ? $(this).attr('class') : '';
		var URL = (url) ? 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 $(this);
	};
})(jQuery); // end of closure

