$(document).ready(function() {

	var coreHidden = false;
	
	var hideCore = (function(){
		if (!coreHidden) {
			$('table#compareBenefits tr.core').hide();
			$('table#compareBenefits .spanner').each(function(n){ // Fix for IE
				$(this).attr('colSpan', ($(this).attr('colSpan')-1));
			});
			coreHidden = true;
		};
	});
	var showCore = (function(){
		if (coreHidden) {
			$('table#compareBenefits tr.core').show();
			$('table#compareBenefits .spanner').each(function(n){ // Fix for IE
				$(this).attr('colSpan', ($(this).attr('colSpan')+1));
			});
			coreHidden = false;
		};
	});
	
	/* Hide details */
	$('table#compareBenefits td span.detail').hide();
	hideCore();
	$('table#compareBenefits td a.expander.open').hide();
	$('table#compareBenefits td a.expander.closed').show().css({display: 'block'});
	
	/* Hook expander/contractors */
	$('table#compareBenefits td a.expander').each(function(n) {
		$(this).bind('click',function(evt) {
			var el = $(this);
			var d = el.parent().parent().find('span.detail');
			var isCore = el[0].className.match(/core/); // Find Core special case

			if (el[0].className.match(/open/)) {
				if (isCore) { hideCore(); }
				else { d.hide('fast'); };
			}
			else {
				if (isCore) { showCore(); }
				else { d.show('fast'); };
			};
			el.hide();
			el.siblings('a').show().css({display: 'block'});
		});
	});

	
	/* Hook expand/contract ALL */
	$('table#compareBenefits #expanderAll').bind('click',function(evt){
		$('table#compareBenefits td a.expander.closed').not('a.core').each(function(n){
			$(this).click();
		});
		$('table#compareBenefits td a.expander.closed.core').each(function(n){ // Must do after other rows
			$(this).click();
		});
	});
	$('table#compareBenefits #collapserAll').bind('click',function(evt){
		$('table#compareBenefits td a.expander.open').not('a.core').each(function(n){
			$(this).click();
		});
		$('table#compareBenefits td a.expander.open.core').each(function(n){ // Must do after other rows
			$(this).click();
		});
	});

});