$(document).ready(function(){
	prepare_suggestions();
	prepare_overlabels();
});

function prepare_suggestions() {
  
	// Fade out the suggestions box when not active
	$("input").blur(function(){
		$('#suggestions').fadeOut();
		$('#specialty-list').fadeOut();                
	});
	
	              
  $('#inputStringSpecialty').click(function(){
  	$('#specialty-list').fadeIn();            
  });
   
	$('#specialty-list li').click(function() {
		$('#inputStringSpecialty').val($(this).html().replace("&amp;","&"));
		$('#selected-specialty-id').val($(this).attr('rel'));
    $('#specialty-list').fadeOut();            
	});

	$('#previous-physicians').click(lookup_by_specialty);

	$('#next-physicians').click(lookup_by_specialty);
            
	$("#inputStringZipCode").keypress(function(event) {
		if(event.which == 13 || event.which == 10) {     
			lookup_by_specialty;
		}
	});

	$("#inputStringSpecialty").keypress(function(event) {
		if(event.which == 13 || event.which == 10) {     
			lookup_by_specialty;
		}
	});

	$("#specialty").keypress(function(event) {
		if(event.which == 13 || event.which == 10) {     
			lookup_by_specialty;
		}
		});
}				

function lookup(inputString) {
    if(inputString.length == 0) {
        $('#suggestions').fadeOut(); // Hide the suggestions box
    } else {
        $.post(MyAjax.ajaxurl, {action: 'via_name_search', queryString: ""+inputString+""}, function(data) { // Do an AJAX call
            $('#suggestions').fadeIn(); // Show the suggestions box
            $('#suggestions').html(data); // Fill the suggestions box
            prepare_google_event_tracking('suggestions');
        });
    }
}
        
function lookup_by_specialty(direction) {
    var specialty = $("#selected-specialty-id").html();
    if (specialty.length == 0)
        specialty = $("input#inputStringSpecialty").val();            
    
		var zipcode = $("input#zipcode").val();
    var gender = $('input[name*=physician_gender]:checked').val();
    var show = $('#select-number-physicians').val();
    var index = $('#page-index').html();
//		var direction = $('#nav-direction').html();
    
    var inputString = 'specialty'+ specialty + '@zipcode' + zipcode + '@gender'+ gender +'@show'+ show +'@index'+ index +'@direction'+ direction ;
    if(inputString.length == 0) {
        $('#inputStringSpecialty').focus(); // Hide the suggestions box
    } else {
        $.post(MyAjax.ajaxurl, {action: 'via_specialty_search', queryString: ""+inputString+""}, function(data) { // Do an AJAX call
            $('#physician-search-results').fadeIn(); // Show the physician-search-results
            $('#physician-search-results').html(data); // Fill the physician-search-results
            // track search term in Google analytics
						try {
						  _gat._getTrackerByName()._trackEvent('physician_directory_specialty_search_term', $("input#inputStringSpecialty").val());
						} catch (e) {
						}
        });
    }
}
        
function clearInput(field_id, term_to_clear) {
  
  // Clear input if it matches default value
  if (document.getElementById(field_id).value == term_to_clear ) {
      document.getElementById(field_id).value = '';
      document.getElementById('search-submit').disabled = false;
  }
  
  // If the value is blank, then put back term, disable search in order to prevent searching for "Search our site"
  else if (document.getElementById(field_id).value == '' ) {
      document.getElementById(field_id).value = term_to_clear;
      document.getElementById('search-submit').disabled = true;
  }
} // end clearSearch()

function prepare_overlabels() {
	var labels = $('label.overlabel');
	labels.addClass('overlabel-applied');
	
	if (labels.lenght < 1) {
		return;
	}
	var inputs;

	labels.each(function() {
		var input = $('#' + $(this).attr('for'));
		var text = $(this).html();
		input.val(text);
		
		input.blur(function() {
			if ($(this).val() == '') {
				$(this).val(text)
			}
		});
		
		input.focus(function() {
			$(this).val('');
		});
		
	});
	
	
}
