$(document).ready(function () {
	
	
	$('.toggle .toggler').click(function () {
		$(this).next().toggle('fast').parent().toggleClass('closed');
	});

// Search page script
	$('#professionals_search form').onload = function() {
		
	}

	var locations_html = $('#professionals_search #by_location ').html();
	var specialties_html = $('#professionals_search #by_specialty').html();
	var names_html = $('#professionals_search #by_name').html();
	var blank_html = '<li><select class="empty" disabled><option value=""></option></select></li>';
	var blank_sel = '<select class="empty" disabled><option value=""></option></select>';
	var name_sel = '<div class="empty"></div>';
	var all_locs = '<select class="empty" disabled><option value="">All locations</option></select>';
	var all_specs = '<select class="empty" disabled><option value="">All specialties</option></select>';

	var select = $('#professionals_search #select_criteria select');
	select.val($('options:first', select).val());
	$('#professionals_search #select_criteria select').removeAttr('disabled');
	$('#professionals_search #by_location').remove();
	$('#professionals_search #by_specialty').remove();
	$('#professionals_search #by_name').remove();
	$('#professionals_search #select_criteria').after(blank_html).after(blank_html);

	$('#professionals_search #select_criteria select').change(function() {
		var val = $(this).val();
		// alert($(this).parent().next().html());
		if (val == '') {
			$(this).parent().next().html(blank_sel).next().html(blank_sel);
		}
		if (val == 'by_location') {
			$(this).parent().next().html(locations_html).next().html(specialties_html);
		}
		else if (val == 'by_specialty') {
			$(this).parent().next().html(specialties_html).next().html(locations_html);
		}
		else if (val == 'by_name') {
			$(this).parent().next().html(name_sel).next().html(names_html);
		}
	});
	

	
// Search results script
	var blank_sel = '<select><option val=""></option></select>';
	var empty_sel = $('#professionals_results .empty');
	var loc_sel   = $('#professionals_results .by_location');
	var spec_sel  = $('#professionals_results .by_specialty');
	var name_sel  = $('#professionals_results .by_name');
	$('#professionals_results #select_criteria').removeAttr('disabled');
	$('#professionals_results .by_location').hide();
	$('#professionals_results .by_specialty').hide();	
	$('#professionals_results .by_name').hide();
	$('#professionals_results #select_criteria').change(function() {
		var val = $(this).val();
		if (val == '') {
			$('#professionals_results .expanded').hide();
			$('#professionals_results .expanded').attr("disabled", "disabled");
			$('#professionals_results .empty').show();
		}
		if (val == 'by_location') {
			$('#professionals_results .expanded').hide();
			$('#professionals_results .expanded').attr("disabled", "disabled");
			$('#professionals_results .by_location').removeAttr('disabled');
			$('#professionals_results .by_location').show();

		}
		else if (val == 'by_specialty') {
			$('#professionals_results .expanded').hide();
			$('#professionals_results .expanded').attr("disabled", "disabled");
			$('#professionals_results .by_specialty').removeAttr('disabled');
			$('#professionals_results .by_specialty').show();

		}
		else if (val == 'by_name') {
			$('#professionals_results .expanded').hide();
			$('#professionals_results .expanded').attr("disabled", "disabled");
			$('#professionals_results .by_name').removeAttr('disabled');
			$('#professionals_results .by_name').show();

		}
	});

// Narrow Search form submission - added by CE
	$('form#narrow_search').change(function() {
		$('form#narrow_search').submit();
	});


// Open download links in new window
	$('.download').click(function () {
		event.preventDefault();
		window.open(this.href, "window");
	});
	
	$('.research-image a').click(function () {
		event.preventDefault();
		window.open(this.href, "window");
	});
		
		
//
	if ( $('body#success_stories').length ) {
		$('div.success-stories-section').each (

			function () {
				var $dtArr = $(this).find("dt");
				var dtArrLen = $dtArr.length;
				var halfway = $(this).find("dt")[Math.round(dtArrLen / 2)];
				
				// make sure the halfway point breaks at a header
				if ($(halfway).hasClass('header')) {
					var pointToBreak = $(halfway).index();
				}
				else {
					var pointToBreak = $(halfway).nextAll("dt.header").index();			
				}
				
				$dtArr.slice(0, pointToBreak).wrapAll("<div class='column first'>");
				$dtArr.slice(pointToBreak, dtArrLen).wrapAll("<div class='column'>");
			}
	
		);
	}		
});

