$(document).ready(function() {

	if($('.tooltip').length > 0) {
		$('.tooltip').tooltip({ 
		    track: true, 
		    delay: 0,
		    showURL: false,
		    fade: 100
		});
	}
	
//	if($('#getstarted').length > 0) {
//		$('#getstarted').beaubox();
//	}

	if($('select#county_navigation').length > 0) {
		$('select#county_navigation').change(function() {
			window.location = '/search.php?county=' + $(this).attr('value');
		});
	}

	if($('div.site_overview_container').length > 0) {
		$('div.site_overview_container').equalHeights(true);
	}
	
	if($('div#county_contact_info').length > 0) {
		$('div#county_contact_info_clerk a').click(function() {
			if($('div#county_contact_info_extra').is(':hidden')) {
				$('div#county_contact_info_extra').slideDown('normal');
				$('div#county_contact_info_clerk a').html('show less');
			} else {
				$('div#county_contact_info_extra').slideUp('fast');
				$('div#county_contact_info_clerk a').html('show more');
			}
			return false;
		});
	}
	
	if($('#pdf-container').length > 0) {
		var headHeight = $('#site_header').height();
		var footerHeight = $('#footer').height();
	
		var height = $(document).height() - headHeight - footerHeight - 6;
		$('#pdf-container').height(height);
	}
	
	if($('input#search-name').length > 0) {
		$('input#search-name').focus();
	}
	
	if($('form#login_form input#username').length > 0) {
		$('form#login_form input#username').focus();
	}
	
	if($('form#purchase').length > 0) {
		// Hide the calculate button from users with Javascript
		$('form#purchase p#calculate').hide();
	
		$('form#purchase a#all').click(function() {
			$('input[type=checkbox]:enabled').each(function() {
				this.checked = true;
			});
			UpdateThePurchaseNumbers();
			return false;
		});
		$('form#purchase a#none').click(function() {
			$('input[type=checkbox]:enabled').each(function() {
				this.checked = false;
			});
			UpdateThePurchaseNumbers();
			return false;
		});

		$('form#purchase input:checkbox').click(function() {
			UpdateThePurchaseNumbers();
		});
	}
	
	if($('table#subscription_price').length > 0) {
		$('table#subscription_price tbody tr td a').each(function() {
			var href = $(this).attr('href');
			$(this).parent('td').parent('tr').click(function() {
				window.location = href;
			}).css('cursor', 'pointer');
		});
		$('table#subscription_price tbody tr td input[type="radio"]').each(function() {
			$(this).parent('td').parent('tr').click(function() {
				$(this).children('td').children('input').attr('checked', 'checked');
				$(this).siblings('tr').removeClass('selected');
				$(this).addClass('selected');
			}).css('cursor', 'pointer');
		});
	}
	
	if($('a#cancel_subscription').length > 0) {
		$('a#cancel_subscription').click(function() {
			var answer = confirm('Are you sure you want to cancel your subscription? You will not have access to instrument previews anymore.');
			return answer;
		});
	}
	
	$("table.sortable").tablesorter(); 

});

function UpdateThePurchaseNumbers() {
	var checked = $('form#purchase input:checkbox:checked').length;
	$('span.purchase_total').html(checked);
}


/*-------------------------------------------------------------------- 
 * JQuery Plugin: "EqualHeights"
 * by:	Scott Jehl, Todd Parker, Maggie Costello Wachs (http://www.filamentgroup.com)
 *
 * Copyright (c) 2008 Filament Group
 * Licensed under GPL (http://www.opensource.org/licenses/gpl-license.php)
 *
 * Description: Compares the heights or widths of the top-level children of a provided element 
 		and sets their min-height to the tallest height (or width to widest width). Sets in em units 
 		by default if pxToEm() method is available.
 * Dependencies: jQuery library, pxToEm method	(article: 
		http://www.filamentgroup.com/lab/retaining_scalable_interfaces_with_pixel_to_em_conversion/)							  
 * Usage Example: $(element).equalHeights();
  		Optional: to set min-height in px, pass a true argument: $(element).equalHeights(true);
 * Version: 2.0, 08.01.2008
--------------------------------------------------------------------*/

$.fn.equalHeights = function(px) {
	$(this).each(function(){
		var currentTallest = 0;
		$(this).children().each(function(i){
			if ($(this).height() > currentTallest) { currentTallest = $(this).height(); }
		});
	//	if (!px || !Number.prototype.pxToEm) currentTallest = currentTallest.pxToEm(); //use ems unless px is specified
		// for ie6, set height since min-height isn't supported
		if ($.browser.msie && $.browser.version == 6.0) { $(this).children().css({'height': currentTallest}); }
		$(this).children().css({'min-height': currentTallest}); 
	});
	return this;
};
