$(document).ready( function() {
	
	//Open / Close feature
	
	//Initialization - Hide all the item within each topic
	$("li.cclose").each( function() {
		//Hide all the tags that are next to $(this) until div tag with class "openlink" or "closelink"
		$(this).find("div.detail-pane").hide();
		$(this).addClass("copen");
		$(this).removeClass("cclose");
	});
	
	//When the show/hide link is clicked.
	$("li.cclose > a, li.copen > a, ").click( function(event) {
		event.preventDefault();

		var objParent = $(this).parent();
		
		objParent.find("div.detail-pane").toggle();
		objParent.toggleClass("copen");
		objParent.toggleClass("cclose");
	});
	
	// "Faculty/Department Research/Consultancy Units" Section - Alphabetical List
	var arrFilter = ['^[a-e]','^[f-m]','^[n-z]'];
	var re = new RegExp(arrFilter[0], "i");

	$("div#tabs-content-2 >p >a").click( function(event) {
		event.preventDefault();
		
		//Reset all selected colors
		$(this).parent().find("a").css("color", "");
		
		var pos = $(this).prevAll("a").length;
		var re = new RegExp(arrFilter[pos], "i");
		
		$("div#tabs-content-2 >ul >li").each( function() {
			var strContent = $.trim($(this).text());
			if (re.test(strContent)) {
				$(this).show();
			} else {
				$(this).hide();
			}
		})
		//Set itself "active" color
		$(this).css("color", "#960");
	})
	
	//Set the first filter - "A-E" active by default
	$("div#tabs-content-2 >ul >li").each( function() {
		var strContent = $.trim($(this).text());
		if (re.test(strContent)) {
			$(this).show();
		} else {
			$(this).hide();
		}
	})
	//Set itself "active" color
	$("div#tabs-content-2 >p >a:first").css("color", "#960");
	
	
	//Alphabetical Order in "Faculty/Department Research/Consultancy Units" Section
	/*$("div#tabsHolder >div#tabs-content-3 >ul >li").each( function() {
		var strCentre = $(this).text().replace(/^[\s\t]+|[\s\t]+$/g, "");
		var arrInitial = ["The Centre for ", "Research Laboratory for ", "Research Centre for ", "Centre for The", "Centre for ", "Centre of The ", "Centre of ", "Institute of ", "Joint Centre for ", "Joint Center for ", "Joint Laboratory for ", "Joint Laboratory on ", "The Joint Laboratory for ", "The Joint Laboratory of ", " Joint Laboratory on ", "Joint Research Centre for ", "Research Centre for ",  "The "];
		
		
		for (i in arrInitial) {
			var re = new RegExp("^" + arrInitial[i], 'i');
			if (re.test(strCentre)) {
				strCentre = strCentre.replace(re, "") + ", " + arrInitial[i];
				
				if ($(this).find("a").length > 0) {
					$(this).find("a").text(strCentre);
				} else {
					$(this).text(strCentre);
				}
				break;
			}
		}
	})
	
	jQuery.fn.sort = function() {
		return this.pushStack([].sort.apply(this, arguments), []);
	}; 	
	
	function listsort(a, b) {
		return $(a).text().replace(/^[\s\t]+|[\s\t]+$/g, "").toLowerCase() > $(b).text().replace(/^[\s\t]+|[\s\t]+$/g, "").toLowerCase() ? 1 : -1;
	}
	
	$("div#tabsHolder >div#tabs-content-3 >ul >li").sort(listsort).appendTo('ul#tmptmp');*/
	
	// End of Alphabetical Order

	if (/research-study\.html$/.test(document.location.pathname)) {
		$("div.activestyle a").click(function() {
			setBlockHeight($("div#col-left >div.block-5f:first >h3, div#col-middle >div.block-5f:first >h3, div#col-right >div.block-5f:first >h3"));
			setBlockHeight($("div#col-left >div.block-5f:eq(1) >h3, div#col-middle >div.block-5f:eq(1) >h3, div#col-right >div.block-5f:eq(1) >h3"));
			setBlockHeight($("div.block-5f"), 10);
		})
	
		setBlockHeight($("div#col-left >div.block-5f:first >h3, div#col-middle >div.block-5f:first >h3, div#col-right >div.block-5f:first >h3"));
		setBlockHeight($("div#col-left >div.block-5f:eq(1) >h3, div#col-middle >div.block-5f:eq(1) >h3, div#col-right >div.block-5f:eq(1) >h3"));
		setBlockHeight($("div.block-5f"), 10);

		
	} else if (/excellence\.html$/.test(document.location.pathname)) {
		$("div.activestyle a").click(function() {
			for (i=0;i<$("div#fourfold-left > div.block").length;i++) {
				setBlockHeight($("div#fourfold-left > div.block:eq(" + i + ") >h3, div#fourfold-right > div.block:eq(" + i + ") >h3"));	
			}
			
			setBlockHeight($("div#fourfold-left > div.block, div#fourfold-right > div.block"), 15);
		})
	
		for (i=0;i<$("div#fourfold-left > div.block").length;i++) {
			setBlockHeight($("div#fourfold-left > div.block:eq(" + i + ") >h3, div#fourfold-right > div.block:eq(" + i + ") >h3"));	
		}
		setBlockHeight($("div#fourfold-left > div.block, div#fourfold-right > div.block"), 15);
		
	} else if (/laboratory\.html$/.test(document.location.pathname)) {
		$("div.activestyle a").click(function() {
			for (i=0;i<$("div#fourfold-left > div.block").length;i++) {
				setBlockHeight($("div#fourfold-left > div.block:eq(" + i + ") >h3, div#fourfold-right > div.block:eq(" + i + ") >h3"));	
				setBlockHeight($("div#fourfold-left > div.block:eq(" + i + "), div#fourfold-right > div.block:eq(" + i + ")"));	
			}
		})
	
		for (i=0;i<$("div#fourfold-left > div.block").length;i++) {
			setBlockHeight($("div#fourfold-left > div.block:eq(" + i + ") >h3, div#fourfold-right > div.block:eq(" + i + ") >h3"));	
			setBlockHeight($("div#fourfold-left > div.block:eq(" + i + "), div#fourfold-right > div.block:eq(" + i + ")"));	
		}

	} else	{
		$("div.activestyle a").click(function() {
			setBlockHeight($("div#fourfold-left > div.block, div#fourfold-right > div.block"));
			setBlockHeight($("div.block-5f"));
		})
	
		setBlockHeight($("div#fourfold-left > div.block, div#fourfold-right > div.block"));
		setBlockHeight($("div.block-5f"));
	}

})

$(document).ready( function() {
	//Tabs
	//For  /research/excellence.html
	var research_array = ['network-coding', 'historical-anthropology-chinese-society', 'circulating-foetal-nucleic-acids', 'chinese-medicine', 'plant-agricultural-biotechnology', 'information-technology'];
	//For  /research/research-study.html
	var area_array = ['chinese-studies', 'biomedical-sciences', 'information-sciences', 'economics-and-finance', 'geo-earth-sciences'];
	//For  /research/laboratory.html
	var statekey_array = ['oncology', 'agrobiotechnology', 'phytochemistry', 'synthetic-chemistry'];
	//For  /research/institutes.html
	var institute_array = ['research-institues', 'faculty-department-consultancy', 'joint-research-units'];
	//Get if there is a parameter named "college" in the querystring.
	var select_research = $.query.get("research");
	var select_area = $.query.get("area");
	var select_statekey = $.query.get("statekeylab");
	var select_institute = $.query.get("institute");
	var selectedTabIndex = -1;
	
	
	//Search /research/excellence.html
	for(i in research_array) {
		if (select_research == research_array[i]) {
			selectedTabIndex = i;
			break;
		}
	}
	
	//Search /research/research-study.html
	if (selectedTabIndex == -1) {
		for(i in area_array) {
			if (select_area == area_array[i]) {
				selectedTabIndex = i;
				break;
			}
		}
	}
	
	//Search /research/research-study.html
	if (selectedTabIndex == -1) {
		for(i in statekey_array) {
			if (select_statekey == statekey_array[i]) {
				selectedTabIndex = i;
				break;
			}
		}
	}
	
	// if this page is /research/institutes.html
	if (/institutes.html$/.test(document.location.pathname)) {
	
		//Set Tabs width
		$("ul#tabs li a:eq(0)").css("width","23%").css("text-align", "center");
		$("ul#tabs li a:eq(1)").css("width","47%").css("text-align", "center");
		$("ul#tabs li a:eq(2)").css("width","23%").css("text-align", "center");
	
		selectedTabIndex = 0;
		for(i in institute_array) {
			if (select_institute == institute_array[i]) {
				selectedTabIndex = i;
				break;
			}
		}
	}
	
	if(selectedTabIndex == -1) {
		$("div#main-page").show();
		$("div#tabsHolder").hide();
	} else {
		$("div#main-page").hide();
		$("div#tabsHolder").show();

		//Highlight selected Tabs
		$("ul#tabs li:eq(" + selectedTabIndex + ")").addClass("current");
		//Tabs Content not in selectedTab Index are hidden
		$("div#tabsHolder > div:not(:eq(" + selectedTabIndex + "))").hide(); 
	}

	//For laboratory.html - Area under <div class="block"> is clickable to the target url
	if (/laboratory\.html$/.test(document.location.pathname)) {

		$("div.block").hover(function() {$(this).find("h3>a").css("color", "#960")}, function() {$(this).find("h3>a").css("color", "")})
	
		$("div.block").css("cursor","pointer").click( function() {
			document.location.href=$(this).find("a:first").attr("href");
		});
		
		$("div.block a").click( function(event) {
			event.preventDefault();
		});
	}

	
})

