$(document).ready( function() {
	//Tabs
	//$("#tabs").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(); 
	
	//Change the name of order - Traditional Chinese : 依筆劃順序, Simplified Chinese : 依拼音順序
	if (document.location.hostname == "translate.itsc.cuhk.edu.hk") {
		$("a#by-alpha").text("依拼音順序");
	}

	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)) {
		jQuery.fn.sort = function() {
			return this.pushStack([].sort.apply(this, arguments), []);
		}; 	
		
		$("div#tabsHolder >div#tabs-content-1 >ul#full-list >li").sort(sortChinese).appendTo('ul#generated-list');
	
		$("ul#generated-list >li").each(function() {
			//Check if it is in Simp. Chinese version. If it is in Simp. Chinese Version, compare the pinyin or else if it is in Trad. Chinese, compare the stroke
			if(document.location.hostname == "translate.itsc.cuhk.edu.hk") {
				var pinyin = find_pinyin($.trim($(this).text()).substring(0,1));
				$(this).addClass("pinyin-" + pinyin);
			} else {
				var stroke = find_stroke($.trim($(this).text()).substring(0,1));
				$(this).addClass("stroke-" + stroke);
			}
		});

	}
	
	
	//Setting width on Tabs
	if (document.location.hostname == "translate.itsc.cuhk.edu.hk") {
		$("ul#tabs.alphatab li:has(a[class^='stroke'])").remove();	
	} else {
		$("ul#tabs.alphatab li:has(a.pinyin)").remove();	
	}
	
	$("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","45px").css("text-align", "center");
	$("ul#tabs.facultytab li a:eq(1)").css("width","55px").css("text-align", "center");
	$("ul#tabs.facultytab li a:eq(2)").css("width","55px").css("text-align", "center");
	$("ul#tabs.facultytab li a:eq(3)").css("width","55px").css("text-align", "center");
	$("ul#tabs.facultytab li a:eq(4)").css("width","55px").css("text-align", "center");
	$("ul#tabs.facultytab li a:eq(5)").css("width","45px").css("text-align", "center");
	$("ul#tabs.facultytab li a:eq(6)").css("width","45px").css("text-align", "center");
	$("ul#tabs.facultytab li a:eq(7)").css("width","65px").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();
		
		//Change Active Tab
		$("ul#tabs li.current").removeClass("current");
		$(this).parent().addClass("current");

		if(document.location.hostname == "translate.itsc.cuhk.edu.hk") {
			//Get the text as regular expression for filtering
			var re =new RegExp("^[" + $(this).text().replace(/\s+/,"") + "]", "i");
			var pinyin = "";
		
			$("ul#generated-list >li").each( function() {
				//Pattern of class is pinyin-[pinyin] which [pinyin] denote the pinyin of first character
				var strPinyin = $(this).attr("class");
				var iStroke = 0;
			
				if (strPinyin.split("-").length == 2) {
					strPinyin = strPinyin.split("-")[1];
				}
			
				if (re.test(strPinyin)) {
					$(this).show();
				} else {
					$(this).hide();
				}
			})

		} else {

			var arrClass=$(this).attr("class").split("-");
			var fromStroke = 0;
			var toStroke = 99;
		
			if (arrClass.length == 3) {
				if (/^\d+$/.test(arrClass[1])) {
					fromStroke = arrClass[1]*1;
				}
				if (/^\d+$/.test(arrClass[2])) {
					toStroke = arrClass[2]*1;
				}
			}


			$("ul#generated-list >li").each(function(event) {
			
				//Pattern of class is stroke-[num] which [num] denote the number of stroke of the first character
				var strStroke = $(this).attr("class");
				var iStroke = 0;
			
				if (strStroke.split("-").length == 2) {
					iStroke = strStroke.split("-")[1];
				}
			
				if (iStroke >= fromStroke && iStroke <= toStroke) {
					$(this).show();
				} else {
					$(this).hide();
				}
			
			});

		}
		
		
	});
	
	//Initialization by clicking the first tab
	$("div#tabsHolder >ul#tabs >li >a:visible:first").click();

	//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();
			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();
			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").toggle();
			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").toggle();
			var objNext = objWrapper.parent().find("div.student-comments-item:first").find("div.student-comments-item-body");
			objNext.html(objNext.html());
		}
	})
	
	
	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"));
		//}
		
		//Special Simplified Chinese Translation. (2010/3/26)
		//In Simplied Chinese Version, translate the following phrase
		// 質素 => 素質
		if (document.location.hostname == "translate.itsc.cuhk.edu.hk") {
			// 質素 => 素質
			$("p:contains('質素')").each( function() {
				$(this).text($(this).text().replace(/質素/g, "素質"));
			})
		}
		
	
	} 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:gt(0) >h3, div#fourfold-right > div.block:gt(0) >h3"));
			setBlockHeight($("div#fourfold-left > div.block, div#fourfold-right > div.block"));
		})

		setBlockHeight($("div#fourfold-left > div.block:gt(0) >h3, div#fourfold-right > div.block:gt(0) >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);
		
	}
	
})

