/* misc js */

// for testing bios:
//$.jqwidont.auto(false);// disable automatic widow-killer

$(document).ready(function() {
	setup_search_field();
	//check_local_links();
	kill_content_widows();
 	setup_studys();
	setup_expandable_content();
  stripe_tables();
	write_bio_test();// remove for production
});

function write_bio_test(){
	// only run if a 'node' path which is a bio page
	if( location.href.search(/node/) > 0){
		$(".nodata").hide();// hide empty fields
		$(".nodata").css("background-color","yellow");
		write_bio_toggle();

	}
}

// adds a togle button to show/hide empty bios
function write_bio_toggle(){
 toggleDiv = '<div id="toggle-switch" style="position:absolute; top:0; right:20px; background:red; display:block; padding:4px 9px;"><a href="#" style="color:#fff;">show/hide empty fields</a></div>';
	$('body').append(toggleDiv);
	$("#toggle-switch a").click(function(){
		toggle_bio_fields();
	});
}

function toggle_bio_fields(){
	$(".nodata").toggle();
	return false;
} 


/* misc js */

// controls and animation for study listing pages
function setup_studys(){
	 // setup page
	$(".study .hideable").hide();
	$(".study .studytitle").css('cursor', 'pointer');
	$(".study .studytitle h3").after( '<a  class="see-details">see details</a>' );
	
	// handle clicks
	$(".study .studytitle").click( function(){
	  $(this).parent().find(".hideable").toggle("fast"); 
	  $(this).parent().toggleClass('expanded');
	} );
	
	 
}

function setup_expandable_content(){
	 // setup page
	$(".expandable-content .hideable").hide();
	$(".expandable-content .expandable-p").css('cursor', 'pointer');
	$(".expandable-content .expandable-p p").after( '<a  class="read-more">read more</a>' );
	
	// handle clicks
	$(".expandable-content .expandable-p").click( function(){
	  $(this).parent().find(".hideable").toggle("fast"); 
	  $(this).parent().toggleClass('expanded');
	} );
	
	 
}
 



// search field functionality


function setup_search_field(){
	// setup inactive state
	search_set_inactive();
	
	// setup event handlers
	$("#search-input").blur(function(){
		// when blur happens							 
		search_set_inactive();						 
	});
	
	$("#search-input").focus(function(){
		// when user selects search box							 
		search_set_active();						 
	});
}

function search_set_inactive(){
	// add active stylings
	$("#search-input").addClass('blurred');
	
	// set default text if empty
	if( $("#search-input").val() == '' ){
		$("#search-input").val('search');
	}
	
	
	
	
}

function search_set_active(){
	// remove inactive stylings
	$("#search-input").removeClass('blurred');
	
	// hide default text if there
	if( $("#search-input").val() == 'search' ){
		$("#search-input").val('');
	}
	
	
}


function stripe_tables(){
	$("ul.striped li:even").addClass("stripe-even");
	$("ul.striped li:odd").addClass("stripe-odd");
	//$("table.striped tr.td-odd td").wrapInner('<div class="stripe-inner inner"></div>');
	//$("table.striped tr.td-even td").wrapInner('<div class="stripe-inner inner"></div>');
} 


function check_local_links(){
	$("div#center-col-inner a").each(function(){
		$(this).addClass("link-internal");
	});
	
	$("div#center-col-inner a[href^='"+'http://'+"']").each(function(){
		$(this).removeClass('link-internal');
		$(this).addClass('link-external');
	});
	
}


function kill_content_widows(){
	//$('#center-col-inner td').widont();
	//$('#center-col-inner p').widont();
}