$(document).ready( function() {
	//Tabs
	var selectedTabIndex = 0;
	
	//Highlight selected Tabs
	$("ul#tabs.alphatab li:eq(" + selectedTabIndex + ")").addClass("current");
	//Tabs Content not in selectedTab Index are hidden
	$("div#tabsHolder > div.tab-content:not(:eq(" + selectedTabIndex + "))").hide(); 
	
	//
	if (/undergraduate-programmes\.html$/.test(document.location.pathname)) {
		//strip off "<a> tag"
		$("a#by-alpha").replaceWith($("a#by-alpha").html());
	}
	
	if (/\/bachelor-degree\//.test(document.location.pathname)) {
		//strip off "<a> tag"
		$("a#by-faculty").replaceWith($("a#by-faculty").html());
	}
	
	if (/undergraduate-programmes\.html$/.test(document.location.pathname)) {
		//Hide all Footnote
		$("div#tabsHolder div.simple-table-footnote div.simple-table-footnote-item").hide();
	
		//show relevant footnote on the list
		$("div#tabsHolder > div.tab-content:eq(" + selectedTabIndex + ")  li sup a").each( function() {
			var tmpValue = $(this).text();
			$("div#tabsHolder div.simple-table-footnote div.simple-table-footnote-item:has(a[name=footnote-" + tmpValue + "])").show();
		});
	}
	
	
	//Setting width on Tabs
	$("ul#tabs.alphatab li a").css("width","31%").css("text-align", "center");
	$("ul#tabs.alphatab li a").css("height","20px");
	$("ul#tabs.alphatab").css("height","35px");
	
	
	$("ul#tabs.facultytab li a:eq(0)").css("width","40px").css("text-align", "center");
	$("ul#tabs.facultytab li a:eq(1)").css("width","80px").css("text-align", "center");
	$("ul#tabs.facultytab li a:eq(2)").css("width","60px").css("text-align", "center");
	$("ul#tabs.facultytab li a:eq(3)").css("width","60px").css("text-align", "center");
	$("ul#tabs.facultytab li a:eq(4)").css("width","40px").css("text-align", "center");
	$("ul#tabs.facultytab li a:eq(5)").css("width","50px").css("text-align", "center");
	$("ul#tabs.facultytab li a:eq(6)").css("width","40px").css("text-align", "center");
	$("ul#tabs.facultytab li a:eq(7)").css("width","40px").css("text-align", "center");

	$("ul#tabs >li >a[class!=current]").click(function(event) {
	
		//Check if the href has no leading pound sign "#"
		if ( /^#/.test($(this).attr("href")) == false) {
			return;
		}
		
		event.preventDefault();

		var strIDName = $(this).attr("href");
		var objTabsHolder = $(this).closest("div.tabsHolder")
		
		//Change Active Tab
		objTabsHolder.find("ul#tabs li.current").removeClass("current");
		$(this).parent().addClass("current");
		
		
		//Change Tab Content
		objTabsHolder.find("div.tab-content").hide();
		objTabsHolder.find("div" + strIDName).show();
		objTabsHolder.find("div#tabs-content-footnote").show();

		//Hide all Footnote
		objTabsHolder.find("div.simple-table-footnote div.simple-table-footnote-item").hide();
		
		//show relevant footnote on the list
		objTabsHolder.find("div" + strIDName + " li sup a").each( function() {
			var tmpValue = $(this).text();
			objTabsHolder.find("div.simple-table-footnote div.simple-table-footnote-item:has(a[name=footnote-" + tmpValue + "])").show();
		});
		
	});

	//Student Comments	
	$("div.activestyle a").click(function() {
		var pos1 = $("div.student-comments-item:visible:first").parent().prevAll("span").length;
		$("div.student-comments-item").show();
		setBlockHeight($("div.student-comments-item-body"));
		$("div.student-comments-item:gt(" + pos1 + "), div.student-comments-item:lt(" + pos1 + ")").hide();
	})

	//Randomly pick one of the story as default
	var defaultCommentsIndex = Math.floor(Math.random()*$('div.student-comments-item').length);
	setBlockHeight($("div.student-comments-item-body"));
	$("div.student-comments-item").hide();
	$("div.student-comments-item:eq(" + defaultCommentsIndex + ")").show();
	
	//Previous  photo
	$("div.student-comments-item div.prev-button").click(function(event) {
		event.preventDefault();
		var objWrapper = $(this).closest("div.student-comments-item");
		objWrapper.toggle();
		if (objWrapper.prev("div.student-comments-item").length > 0) {
			//objWrapper.prev("div.student-comments-item").toggle();
			objWrapper.prev("div.student-comments-item").show();
			var objPrev = objWrapper.prev("div.student-comments-item").find("div.student-comments-item-body");
			objPrev.html(objPrev.html());
			
		} else {
			//objWrapper.parent().find("div.student-comments-item:last").toggle();
			objWrapper.parent().find("div.student-comments-item:last").show();
			var objPrev = objWrapper.parent().find("div.student-comments-item:last").find("div.student-comments-item-body");
			objPrev.html(objPrev.html());
		}
	})
	
	//Next  photo
	$("div.student-comments-item div.next-button").click(function(event) {
		event.preventDefault();
		var objWrapper = $(this).closest("div.student-comments-item");
		
		objWrapper.toggle();
		if (objWrapper.next("div.student-comments-item").length > 0) {
			objWrapper.next("div.student-comments-item").show();
			var objNext = objWrapper.next("div.student-comments-item").find("div.student-comments-item-body");
			objNext.html(objNext.html());
		} else {
			objWrapper.parent().find("div.student-comments-item:first").show();
			var objNext = objWrapper.parent().find("div.student-comments-item:first").find("div.student-comments-item-body");
			objNext.html(objNext.html());
		}
	})
	
	
	$("div.activestyle a").click(function() {
		var pos = $(this).parent().prevAll("span").length;
		if (pos == 0) { //Small Font
			$('div#nav-title').addClass("left-navi-top-bg");
			$('div#nav-title').removeClass("left-navi-top-bg2");
		} else  { //Medium Font and Large Font
			$('div#nav-title').addClass("left-navi-top-bg2");
			$('div#nav-title').removeClass("left-navi-top-bg");
		}
	})
	
	var cookie = readCookie("style");	
	var title = cookie ? cookie : getPreferredStyleSheet();
	$("div.activestyle a[onclick*=\"'" + title + "'\"]").each(function() {
		var pos = $(this).parent().prevAll("span").length;
		if (pos == 0) { //Small Font
			$('div#nav-title').addClass("left-navi-top-bg");
			$('div#nav-title').removeClass("left-navi-top-bg2");
		} else  { //Medium Font and Large Font
			$('div#nav-title').addClass("left-navi-top-bg2");
			$('div#nav-title').removeClass("left-navi-top-bg");
		}
	});


	if (/study-in-hong-kong\.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, div#fourfold-right > div.block"));
			}
		})
	
		//for (i=0;i<$("div#fourfold-left > div.block").length;i++) {
			setBlockHeight($("div#fourfold-left > div.block, div#fourfold-right > div.block"));
		//}
	
	} else if(/summer-programmes\.html$/.test(document.location.pathname)) {

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

		setBlockHeight($("div#fourfold-left > div.block >h3, div#fourfold-right > div.block >h3"));
		setBlockHeight($("div#fourfold-left > div.block, div#fourfold-right > div.block"));
		
	} else if(/enrichment-programmes\.html$/.test(document.location.pathname)) {
		$("div#fourfold-left > div.block, div#fourfold-right > div.block").css("height", "500px");

		$("div.activestyle a").click(function() {
			setBlockHeight($("div#fourfold-left > div.block:eq(0) >h3, div#fourfold-right > div.block:eq(0) >h3"));
			setBlockHeight($("div#fourfold-left > div.block:eq(1) >h3, div#fourfold-right > div.block:eq(1) >h3"));
			setBlockHeight($("div#fourfold-left > div.block, div#fourfold-right > div.block"));
		})

		setBlockHeight($("div#fourfold-left > div.block:eq(0) >h3, div#fourfold-right > div.block:eq(0) >h3"));
		setBlockHeight($("div#fourfold-left > div.block:eq(1) >h3, div#fourfold-right > div.block:eq(1) >h3"));
		setBlockHeight($("div#fourfold-left > div.block, div#fourfold-right > div.block"));

	} else if (/general-education\.html$/.test(document.location.pathname)) {

		$("div.activestyle a").click(function() {
			setBlockHeight($("div#fourfold-right > div.block >h3, div#fourfold-left > div.block >h3,"));
			setBlockHeight($("div#fourfold-right > div.block >div.main >h3:first, div#fourfold-left > div.block >div.main >h3:first,"));
			$("div#fourfold-right > div.block").css("height", $("div#fourfold-left > div.block:first").height() + $("div#fourfold-left > div.block:eq(1)").height());

})

		setBlockHeight($("div#fourfold-right > div.block >h3, div#fourfold-left > div.block >h3,"));
		setBlockHeight($("div#fourfold-right > div.block >div.main >h3:first, div#fourfold-left > div.block >div.main >h3:first,"));
		$("div#fourfold-right > div.block").css("height", $("div#fourfold-left > div.block:first").height() + $("div#fourfold-left > div.block:eq(1)").height());
	
	} else if(/admission\.html$/.test(document.location.pathname)) {

		$("div.activestyle a").click(function() {
			setBlockHeight($("div#col-left > div.block-5f >h3, div#col-middle > div.block-5f >h3, div#col-right > div.block-5f >h3,"));
			setBlockHeight($("div#col-left > div.block-5f >div.main >h4:first, div#col-middle > div.block-5f >div.main >h4:first, div#col-right > div.block-5f >div.main >h4:first,"));
			setBlockHeight($("div#col-middle > div.block-5f:gt(0), div#col-right > div.block-5f:gt(0)"));
			setBlockHeight($("div#col-middle > div.block-5f:first, div#col-right > div.block-5f:first"));
			$("div#col-left > div.block-5f").css("height", $("div#col-middle > div.block-5f:first").height() + $("div#col-middle > div.block-5f:eq(1)").height() + 18);
		})

		setBlockHeight($("div#col-left > div.block-5f >h3, div#col-middle > div.block-5f >h3, div#col-right > div.block-5f >h3,"));
		setBlockHeight($("div#col-left > div.block-5f >div.main >h4:first, div#col-middle > div.block-5f >div.main >h4:first, div#col-right > div.block-5f >div.main >h4:first,"));
		setBlockHeight($("div#col-middle > div.block-5f:gt(0), div#col-right > div.block-5f:gt(0)"));
		setBlockHeight($("div#col-middle > div.block-5f:first, div#col-right > div.block-5f:first"));
		$("div#col-left > div.block-5f").css("height", $("div#col-middle > div.block-5f:first").height() + $("div#col-middle > div.block-5f:eq(1)").height() + 18);
		
	}
	
})

