$(function() {
	// accordion on home
	$('.wrap').hide();	
	$('h3.section').click(function(){
		if( $(this).next().is(':hidden') ) {
			$('h3.section').removeClass('ui-state-active').next().slideUp();
			$(this).addClass('ui-state-active').next().slideDown();
		} else {
			$(this).removeClass('ui-state-active').next().slideUp();
		}
		return false;
	});
	
	// tabs in footer
	$('#footer .nav-sub').tabs();
	
	// main page cycle
	$('#promo').cycle({
	    timeout: 8000, 
	    effect: 'fade',
	    speed: 500
	});
	
	$('.promo-image').css('visibility', 'visible');	
	
	$('#promo .btn').hover(function(){
		$('#promo').cycle("pause");
	}, function(){
		$('#promo').cycle("resume");
	});
	
	
	// adds and removes search text
	$('#site-search input[type="text"]').focus(function(){
		$(this).filter(function(){
			return $(this).val() === '' || $(this).val() === 'Search Site';
		}).val('');
	}).blur(function(){
		$(this).filter(function(){
			return $(this).val() === '';
		}).val('Search Site');
	});
	
	$(".promo-image .btn").tooltip({
		effect: 'fade',
		fadeInSpeed: 100,
		fadeOutSpeed: 100,
		offset: [0,17],
		position: 'top center',
		relative: true		
	}).click(function(){
		return false;
	});
	
	// Add print icon
	$("#page-tools").append('<li><a id="print" href="#">Print This Page</a></li>');
	$("#print").click(function() {
	    window.print();
	    return false;
	});
	
	
	// Add hover effect to rows
	$(document).delegate('#facility-search-results li', 'hover', function() {
		$(this).toggleClass('highlighted')
				.click(function(){
					window.location = $(this).find('h3 a').attr('href');
				}).css("cursor","pointer");
	});
	
	// facility search & physician search
	// hide the submit button
	$('#facility_search input[type="submit"] , #physician_facility_search input[type="submit"]').hide();
	
	// when the state select changes,
	// post the results
	$('#facility_search #state').change(function(){
		getResults("/about/facilities.dot");
	});
		
	$('#physician_facility_search #state').change(function(){
		getResults("/physicians/index.dot");
	});
});

function getResults(url) {
	$('#facility-search-results').fadeOut('slow', function(){
		$('#facility-search').prepend('<img src="/design/images/template/loading.gif" />').fadeIn('fast', function(){
			$.ajax({
				type: "POST",
				url: url,
				data: "state=" + $('#state').val() + "&show_results=true",
				success: function(data){
					window.location.hash = '#' + '/' + $('#state').val();
					
					$('#facility-search img').fadeOut('fast', function(){
						$('#facility-search-results')
							.html(jQuery(data).find('#facility-search-results').html())
							.fadeIn('slow');
							$('#facility-search img').remove();
					});
				}
			});
		});
	});
}

// AddThis
var addthis_config = {
    // username: 		"username",
	ui_click:       true,
	ui_use_css: 	false
}
