/* ---------------------------------------------------------- */
/* Fix superscript links                                                                            */
/* ---------------------------------------------------------- */
$(document).ready(function() {

	$('a .super, a .super1, a .textsmall, a .reflink').each( function(){
		$(this).removeClass('super');
		$(this).removeClass('super1');
		$(this).removeClass('textsmall');
		$(this).removeClass('reflink');
		$(this).parent('a').addClass('super');
	});
});

/* ---------------------------------------------------------- */
/* Resize panel heights to match largest                                                 */
/* ---------------------------------------------------------- */
var resizeTimer = null;
$(document).ready(function() {
    if (resizeTimer) clearTimeout(resizeTimer);
    resizeTimer = setTimeout(doResize, 100);
});

$(window).bind('resize', function() {
	if (resizeTimer) clearTimeout(resizeTimer);
	resizeTimer = setTimeout(doResize, 100);
	// if ($.browser.msie && $.browser.version == 6.0) window.location.reload(true);
});

var maxHeight = 0;  
  
function doResize() {
	setPanelHeights('#content .panels');
};

function setPanelHeights(row) {
	row = $(row);
	row.each(function(){
		$(this).addClass('temp');
		setPanelHeight('.temp > .panel > .header > .content');  
		setPanelHeight('.temp > .panel > .body > .content');  
		setPanelHeight('.temp > .panel > .footer > .content');
		$(this).removeClass('temp')
	});
}

function setPanelHeight(col) { 
	maxHeight = 0;
	
	col = $(col); 
	
	if (!($.browser.msie && $.browser.version == 6.0)) {
		col.css({'min-height': 0}); 	
		col.css({'height': 'auto'}); 	
	}

	col.each(function() {          
		if ($(this).height() > maxHeight) { maxHeight = $(this).height(); }  
	});  
	
	if ($.browser.msie && $.browser.version == 6.0) { col.css({'height': maxHeight}); }
	col.css({'min-height': maxHeight}); 	
}

/* ---------------------------------------------------------- */
/* Navigation list handlers                                                                       */
/* ---------------------------------------------------------- */
$(document).ready(function() {

	// CRUK global navigation jump menu
	$("#cruk-nav-form #cruk-nav-submit").hide();
	
	var jumpURL = $("#cruk-nav-form #cruk-nav-sites").selectedValues();
	$("#cruk-nav-form").attr("action", jumpURL);
	$("#cruk-nav-form").attr("method", "post");
			
	$("#cruk-nav-form #cruk-nav-sites").change(function() { 
		jumpURL = $(this).selectedValues();
		if (jumpURL != '') {
			window.location.href = jumpURL;
		}
	});

	// General content navigation lists
	var jumpURL = $("#navigation-list #navigationURL, #navigation-list #navigationDropdown, .search-form #navigationDropdown, .searchform #navigationDropdown").selectedValues() + "?script=true";
	$("#navigation-list").attr("action", jumpURL);
	$("#navigation-list").attr("method", "post");
			
	$("#navigation-list #navigationURL, #navigation-list #navigationDropdown, .search-form #navigationDropdown, .searchform #navigationDropdown").change(function() { 
		jumpURL = $(this).selectedValues() + "?script=true";
		if (jumpURL != '') {
			$("#navigation-list").attr("action", jumpURL);
		}
	});
			
	$("#navigation-list #navigationURL, #navigation-list #navigationDropdown, .search-form #navigationDropdown, .searchform #navigationDropdown").dblclick(function() { 
		jumpURL = $(this).selectedValues() + "?script=true";
		if (jumpURL != '') {
			window.location.href = jumpURL;
		}
	});

	// Cancerhelp cancer type search
	var jumpURL1 = "search-results/index.htm?AdvancedSearchFormType=qanda_cancer_type_search_form&isFormSubmitted=true&cancertype=" + $("#cancertypeform #cancertype").selectedValues() + "&script=true";
	$("#cancertypeform").attr("action", jumpURL1);
	$("#cancertypeform").attr("method", "post");
			
	$("#cancertypeform #cancertype").change(function() { 
		jumpURL1 = "search-results/index.htm?AdvancedSearchFormType=qanda_cancer_type_search_form&isFormSubmitted=true&cancertype=" + $(this).selectedValues() + "&script=true";
		if (jumpURL1 != '') {
			$("#cancertypeform").attr("action", jumpURL1);
		}
	});
			
	$("#cancertypeform #cancertype").dblclick(function() { 
		jumpURL1 = "search-results/index.htm?AdvancedSearchFormType=qanda_cancer_type_search_form&isFormSubmitted=true&cancertype=" + $(this).selectedValues() + "&script=true";
		if (jumpURL1 != '') {
			window.location.href = jumpURL1;
		}
	});

	// Cancerhelp cancer subject search
	var jumpURL2 = "search-results/index.htm?AdvancedSearchFormType=qanda_cancer_subject_search_form&isFormSubmitted=true&cancersubject=" + $("#cancersubjectform #cancersubject").selectedValues() + "&script=true";
	$("#cancersubjectform").attr("action", jumpURL2);
	$("#cancersubjectform").attr("method", "post");
			
	$("#cancersubjectform #cancersubject").change(function() { 
		jumpURL2 = "search-results/index.htm?AdvancedSearchFormType=qanda_cancer_subject_search_form&isFormSubmitted=true&cancersubject=" + $(this).selectedValues() + "&script=true";
		if (jumpURL2 != '') {
			$("#cancersubjectform").attr("action", jumpURL2);
		}
	});
			
	$("#cancersubjectform #cancersubject").dblclick(function() { 
		jumpURL2 = "search-results/index.htm?AdvancedSearchFormType=qanda_cancer_subject_search_form&isFormSubmitted=true&cancersubject=" + $(this).selectedValues() + "&script=true";
		if (jumpURL2 != '') {
			window.location.href = jumpURL2;
		}
	});

});

/* ---------------------------------------------------------- */
/* Text resize utility                                                                                */
/* ---------------------------------------------------------- */
$(document).ready(function(){
	var COOKIE_NAME = "chtextsize";
	var COOKIE_OPTS = { path: '/', expires: 30 };
	var TEXT_SIZES = ['smaller', 'small', 'medium', 'large', 'larger'];
	var TEXT_MIN = 1; // Small
	var TEXT_MAX = TEXT_SIZES.length - 1; // Larger
	
	var text_size = 2;

	// Set initial size
	if ( $.cookie(COOKIE_NAME) ) {
		text_size = $.cookie(COOKIE_NAME);
		if ( text_size < TEXT_MIN || text_size > TEXT_MAX ) text_size = 2;
		$('body').addClass(TEXT_SIZES[text_size]);	
	} else {
		text_size = 2;
		$('body').addClass(TEXT_SIZES[text_size]);	
		$.cookie(COOKIE_NAME, text_size, COOKIE_OPTS);
	}
	
	// Increase font size
	$(".increasefont").click(function(){
		if (text_size < TEXT_MAX) {
			$('body').removeClass(TEXT_SIZES[text_size]);
			text_size++;
			$('body').addClass(TEXT_SIZES[text_size]);	
			$.cookie(COOKIE_NAME, text_size, COOKIE_OPTS);
			doResize();

			if ($.browser.msie && $.browser.version == 6.0) window.location.reload(true);
		}
		return false;
	});
	
	// Decrease font size
	$(".decreasefont").click(function(){
		if (text_size > TEXT_MIN) {
			$('body').removeClass(TEXT_SIZES[text_size]);
			text_size--;
			$('body').addClass(TEXT_SIZES[text_size]);	
			$.cookie(COOKIE_NAME, text_size, COOKIE_OPTS);
			doResize();

			if ($.browser.msie && $.browser.version == 6.0) window.location.reload(true);
		}
		return false;
	});

});

/* ---------------------------------------------------------- */
/* Show/hide LH Nav navigators                                                              */
/* ---------------------------------------------------------- */
$(document).ready( function() {

	$("li.search-facet").each( function() {
		$(this).children("ol").children("li:gt(4)").addClass("hidden");
		$(this).children("div.showall").removeClass("hidden");
		$(this).children("div.showall").children("a").text("Show all");
	});
	
	$("li.search-facet a").click( function() {
		if ( $(this).parent().hasClass("closed") ) {
			$(this).parent().removeClass("closed");
		} else {
			$(this).parent().addClass("closed");
		}
	});

	$("li.search-facet div.showall a").click( function() {
		if ( $(this).text() == "Show all") {
			$(this).text("Show fewer");
			$(this).parent().siblings("ol").children("li").removeClass("hidden");		
		} else {
			$(this).text("Show all");
			$(this).parent().siblings("ol").children("li:gt(4)").addClass("hidden");
		}
	});
	
});

/* ---------------------------------------------------------- */
/* Show/hide "more" sections of forms                                                    */
/* ---------------------------------------------------------- */
$(document).ready( function() {
	
	$("#more1, #more2").hide();
	
	$(".show-more1 a").click( function() {
		var opts1 = $(".show-more1 a").text();
		
		if ( opts1 == "+ More options" ) {
			opts1 = "- Fewer options";
		} else {
			opts1 = "+ More options";
		}

		$("#more1").toggle("slow");		
		$(".show-more1 a").text(opts1);
	});
	
	$(".show-more2 a").click( function() {
		var opts2 = $(".show-more2 a").text();
		
		if ( opts2 == "+ More options" ) {
			opts2 = "- Fewer options";
		} else {
			opts2 = "+ More options";
		}
		
		$("#more2").toggle("slow");
		$(".show-more2 a").text(opts2);
	});
});
