/* ---------------------------------------------------------- */
/* 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);
});

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

function setPanelHeights(row) {
	row = $(row);
	row.each(function(){
		if (!$(this).hasClass('no-resize')) $(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}); 	
}

/* ---------------------------------------------------------- */
/* Handle CH homepage toggle cancer-list                      */
/* ---------------------------------------------------------- */
$(document).ready(function() {
	$('.cancer-list.left li').slice(3).hide();
	$('.cancer-list.right li').slice(3).hide();
	
	$('.cancer-list~.more').show().html('<a href="#">More cancer types [+]</a>');
	
	$('.cancer-list~.more').click( function() {

		$(this).closest('.panels').toggleClass('no-resize');
		$('.no-resize .content').removeAttr('style');

		if ($(this).closest('.panels').hasClass('no-resize')) {
			$('.cancer-list.left  li').slice(3).show();
			$('.cancer-list.right li').slice(3).show();
			$('.cancer-list.left  li').eq(2).height('3em');
			$('.cancer-list.right li').eq(2).height('3em');
			$(this).html('<a href="#">Close box [-]</a>');
		} else {
			$('.cancer-list.left li').slice(3).hide();
			$('.cancer-list.right li').slice(3).hide();
			$('.cancer-list.left li').eq(2).height('auto');
			$('.cancer-list.right li').eq(2).height('auto');
			$(this).html('<a href="#">More cancer types [+]</a>');
		}
		
		doResize();	
	});
});

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

	// CRUK global navigation jump menu
	$("#cruk-nav-form #cruk-nav-submit").hide();
	
	var jumpURL;
	$("#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
	$("#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;
		}
	});


		var jumpURL1 = "search-results/?AdvancedSearchFormType=qanda_cancer_type_subject_search_form&isFormSubmitted=true&cancertype=" +  "&cancersubject=" +"&script=true";
		$("#cancertypeformMod").attr("action", jumpURL1);
		$("#cancertypeformMod").attr("method", "post");	

		$("#cancertypeformMod #cancertypeMod").change(function() { 
			jumpURL1 = "search-results/?AdvancedSearchFormType=qanda_cancer_type_subject_search_form&isFormSubmitted=true&cancertype=" + document.getElementById('cancertypeMod').value +  "&cancersubject="  + document.getElementById('cancersubjectMod').value + "&script=true";
			//jumpURL1 = "search-results/?AdvancedSearchFormType=qanda_cancer_type_subject_search_form&isFormSubmitted=true&cancertype=" + $(this).selectedValues() + "&script=true";
			if (jumpURL1 != '') {
				$("#cancertypeformMod").attr("action", jumpURL1);
			}
		});
				
		$("#cancertypeformMod #cancertypeMod").dblclick(function() { 
			jumpURL1 = "search-results/?AdvancedSearchFormType=qanda_cancer_type_subject_search_form&isFormSubmitted=true&cancertype=" + $(this).selectedValues() + "&script=true";
			if (jumpURL1 != '') {
				window.location.href = jumpURL1;
			}
		});
	
		// Cancerhelp cancer subject search
						
		$("#cancertypeformMod #cancersubjectMod").change(function() { 
			jumpURL1 = "search-results/?AdvancedSearchFormType=qanda_cancer_type_subject_search_form&isFormSubmitted=true&cancersubject=" + document.getElementById('cancersubjectMod').value +  "&cancertype="  + document.getElementById('cancertypeMod').value + "&script=true";
			if (jumpURL1 != '') {
				$("#cancertypeformMod").attr("action", jumpURL1);
			}
		});
				
		$("#cancertypeformMod #cancersubjectMod").dblclick(function() { 
			jumpURL2 = "search-results/?AdvancedSearchFormType=qanda_cancer_type_subject_search_form&isFormSubmitted=true&cancersubject=" + $(this).selectedValues() + "&script=true";
			if (jumpURL2 != '') {
				window.location.href = jumpURL2;
			}
		});


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

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

});

/* ---------------------------------------------------------- */
/* 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() {
	if ($.browser.msie && $.browser.version == 6.0) {
		$(".show-more1, .show-more2").hide();
	} else {
		$("#more1, #more2").hide();
	
		$(".show-more1 a").click( function() {
			var opts1 = $(".show-more1 a").text();
				
			if ( opts1.indexOf("More") != -1 ) {
				opts1 = "- Fewer options";
			} else {
				opts1 = "+ More options";
			}

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


/* ---------------------------------------------------------- */
/* Researcher tabs                                            */
/* ---------------------------------------------------------- */
$(document).ready( function(){

 	$('#tab1').show();
 	$('#tab2').hide();
	$("#tab1-link").attr("href","");
	$("#tab2-link").attr("href","");

 	$('#tab1-link').click( function(){
 	
		$('#tab2').hide();
		
 		$('#tab1').show();
 		$('#tab1-link').css('backgroundColor', '#ffffff');
		$('#tab1-link h2').css('color', '#1d4296');
		$('#tab2-link').css('backgroundColor', '#1d4296');
		$('#tab2-link h2').css('color', '#ffffff');
		
 		return false;
 	});

 	$('#tab2-link').click( function(){
 	
 		$('#tab1').hide();
 		
 		$('#tab2').show(); 
 		$('#tab1-link').css('backgroundColor', '#1d4296');
		$('#tab1-link h2').css('color', '#ffffff');
		$('#tab2-link').css('backgroundColor', '#ffffff');
		$('#tab2-link h2').css('color', '#1d4296');

		return false; 	
 	});
 });


/* ---------------------------------------------------------- */
/* F&R Centres forms                                          */
/* ---------------------------------------------------------- */
$(document).ready(function() {

	// Set up date picker widget
	if ($("#appdeadline").length) {
		$("#appdeadline").datepicker({
			changeMonth: true,
			changeYear: true,
			dateFormat: "dd/mm/yy"
		});
	}
	
	// Hide all conditional content
	$("#crukCentre-field, #directFunded-field, #grantFunded-field, #othercontracttype-field, #supervisor-field, #salarydetails-field, #othereligibility-field").hide();

	// Show conditional content
	if ($("#option1").is(":checked")) $("#crukCentre-field").show();
	if ($("#option2").is(":checked")) $("#directFunded-field").show();
	if ($("#option3").is(":checked")) $("#grantFunded-field").show();
	if ($("#contractType").val() == "Other") { $("#othercontracttype-field").show(); }
	if ($("#supasgrantholderno").is(":checked")) { $("#supervisor-field").show(); }
	if (($("#salaryRange").val() == "") || ($("#salaryRange").val() == "Competitive") || ($("#salaryRange").val() == "No salary (unpaid voluntary work)")) { 
		$("#salarydetails-field").hide();
	} else {	
		$("#salarydetails-field").show();
	}
	if ($("#applicantEligibility").val() == "Other") { $("#othereligibility-field").show(); }

	
	// Toggle content that is conditional on Institution Type selections
	$('#institutionType-field').trilemma();
	$("#option1").change(function() {	
		$("#crukCentre-field").toggle();
	});
	
	$("#option2").change(function() {
		$("#directFunded-field").toggle();
	});
	
	$("#option3").change(function() {
		$("#grantFunded-field").toggle();
	});
	
	$("#contractType").change(function() {
		if ($(this).val() == "Other") {
			$("#othercontracttype-field").show();
		} else {
			$("#othercontracttype-field").hide();
		}
	});
	
	$("input:radio[name=supasgrantholder]").change(function() {
		if ($(this).val() == "no") {
			$("#supervisor-field").show();
		} else {
			$("#supervisor-field").hide();
		}
	});
	
	$("#salaryRange").change(function() {
		if (($(this).val() == "") || ($(this).val() == "Competitive") || ($(this).val() == "No salary (unpaid voluntary work)")) {
			$("#salarydetails-field").hide();
		} else {
			$("#salarydetails-field").show();
		}
	});
	
	$("#applicantEligibility").change(function() {
		if ($(this).val() == "Other") {
			$("#othereligibility-field").show();
		} else {
			$("#othereligibility-field").hide();
		}
	});

	// LH nav search filter on results page
	$("#filter-tabs").show();
	$("#browse-tab").hide();
	$("#search-tab").show();
	$("#show-search-tab").addClass("selected");
	
	$("#show-browse-tab a").click(function(){
		$("#show-browse-tab").addClass("selected");
		$("#show-search-tab").removeClass("selected");
		$("#search-tab").hide();
		$("#browse-tab").show();
	});
	
	$("#show-search-tab a").click(function(){
		$("#show-search-tab").addClass("selected");
		$("#show-browse-tab").removeClass("selected");
		$("#browse-tab").hide();
		$("#search-tab").show();
	});

	// Limit Institution type checkboxes to two selected (uses jquery.trilemma plugin)

	$("#browse-tab ul ul").hide();
});

// Fix job board submission form error messages
//$(document).ready( function(){
//	$("#search-funded .row .errormsg").each(function(){
//		var html = $(this).html();
//		$(this).hide().parent().attr("title", html).addClass("has-error");		
//	});
	
//	$(".has-error").tooltip();
//});

// Fix job board search results filter dropdowns
$(document).ready( function(){
	var params = {};
	
	if (location.search) {
	    var parts = location.search.substring(1).split('&');
	
	    for (var i = 0; i < parts.length; i++) {
	        var nv = parts[i].split('=');
	        if (!nv[0]) continue;
	        params[nv[0]] = nv[1] || false;
	    }
	}
		
	if (params.orderby) {
		$("#orderby option").removeAttr("selected");
		$("#orderby option[value=" + params.orderby + "]").attr("selected","selected");
	}
	
	if (params.showresults) {
		$("#showresults option").removeAttr("selected");
		$("#showresults option[value=" + params.showresults + "]").attr("selected","selected");
	}
});

