
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

//Set the Height of the block(s) to the max of them so that they are in the same height.
function setBlockHeight(objSelectedItems) {
	var iMax = 0;
	for (j=0; j<objSelectedItems.length; j++) {
		var objItem = objSelectedItems[j];
		$(objItem).css("height", "");
		if ($(objItem).height() > iMax) {
			iMax = $(objItem).height();
		}
	}
	//If there are 2 parameters, the 2nd one is the additional pixel added on the max height
	if (setBlockHeight.arguments.length > 1) {
		iMax += setBlockHeight.arguments[1];
	}
	objSelectedItems.css("height",iMax + "px");

	//For IE7, the bottom of the text is cut off if the height is defined. Remedy :  set padding-bottom to 1px.
	var iPaddingBottom = objSelectedItems.css("padding-bottom");
	if (iPaddingBottom == "" || iPaddingBottom == "0px") {
		objSelectedItems.css("padding-bottom","1px");
	}

}

$(document).ready( function() {

	//Header
	
	//Check whether it is in Simplified Chinese or Traditional Chinese
	if(document.location.hostname == "translate.itsc.cuhk.edu.hk") {
		$("a#linkSC").attr("id", "linkTC").html("&#x7E41;&#x9AD4;");  //&#x7E41;&#x9AD4; represents 繁體
	}
	
	//Traditional Chinese button
	$("a#linkEng[href='#']").click(function(event) {
		event.preventDefault();
		if(document.location.hostname == "translate.itsc.cuhk.edu.hk") {
			document.location.href = document.location.href.replace(/\/chinese\//, "/english/").replace("translate.itsc.cuhk.edu.hk/uniTS/", "");
		} else {
			document.location.href = document.location.href.replace(/\/chinese\//, "/english/");
		}
	});
	//Simplified Chinese button
	$("a#linkTC[href='#']").click(function(event) {
		event.preventDefault();
		if(document.location.hostname == "translate.itsc.cuhk.edu.hk") {
			document.location.href = document.location.href.replace("translate.itsc.cuhk.edu.hk/uniTS/", "");
		} else {
			document.location.href = document.location.href.replace(/\/english\//, "/chinese/");
		}
	});
	
	//Traditional Chinese button
	$("a#linkSC[href='#']").click(function(event) {
		event.preventDefault();
		if(/\/english\//.test(document.location.href)) {
			document.location.href = document.location.href.replace(/\/english\//, "/chinese/").replace(/^(http:\/\/)/, "$1translate.itsc.cuhk.edu.hk/uniTS/");
		} else {			
			document.location.href = document.location.href.replace(/^(http:\/\/)/, "$1translate.itsc.cuhk.edu.hk/uniTS/");
		}
	});
	
	//MyCUHK
	$("a#linkMyCUHK").click(function() {
		//alert('Secure connection (HTTPS) not supported.');
	});
	
	//Search Box
	$("div#search form").submit( function() {
		//return false;
	});
	

	//Left Navigation
	var pathname;
	if(document.location.hostname == "translate.itsc.cuhk.edu.hk") {
		pathname = "h" + "ttp://translate.itsc.cuhk.edu.hk" + document.location.pathname;
	} else {
		pathname = document.location.pathname;
	}
	
	$("div.leftnavi >ul >li >a").css('font-weight','bold');
	$("div.leftnavi >ul >li >a[href = '" + pathname + "']").addClass('current');
	$("div.leftnavi >ul >li:has(ul>li a[href='" + pathname + "']) >a").addClass('current');
	$("#inner-nav >div#nav-title >a[href = '" + pathname + "']").addClass('current');
	//IE6
	$("#inner-nav >div#nav-title >a[href = '" + pathname + "']").css('color', '#ffcc33');

	$("div#menu ul.toplevel li a[href='" + pathname + "']").parent().addClass("current");

	//Shortcut
	var objShortcutFadeOutId;
	$("img#short-cut-button").click(
		function(event) {
			if ($("div#short-cut-pane-inner:visible").length >0) {
				$("div#short-cut-pane-inner").fadeOut("slow");
				if(document.location.hostname == "translate.itsc.cuhk.edu.hk") {
					$(this).attr("src", "/chinese/images/btn_shortcuts_in_xgb.gif");
				} else {
					$(this).attr("src", "/chinese/images/shortcut-in_xb5.gif");
				}
			} else {
				$("div#short-cut-pane-inner").fadeIn("slow");
				if(document.location.hostname == "translate.itsc.cuhk.edu.hk") {
					$(this).attr("src", "/chinese/images/btn_shortcuts_out_xgb.gif");
				} else {
					$(this).attr("src", "/chinese/images/shortcut-out_xgb.gif");
				}
			}
		}
	)
	//Shortcut - End
	//Social Bookmarking
	$("div.tools img[id]").css('cursor','pointer');
	
	
	//Twitter Bookmarking
	$("img#bookmark-twitter").click(function() {
		window.open('http://twitter.com/home?status='+encodeURIComponent(document.title)+'+'+encodeURIComponent(document.location.href),'_blank','width=930,height=470,left=50,top=50,toolbar=no,menubar=no,location=no,scrollbars=yes,status=yes,resizable=yes');

		bookmarkingLogging('Twitter');
	})
	
	//Facebook Bookmarking
	$("img#bookmark-facebook").click(function() {
		window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(document.location.href)+'&t='+encodeURIComponent(document.title),'_blank','width=930,height=470,left=50,top=50,toolbar=no,menubar=no,location=no,scrollbars=yes,status=yes,resizable=yes');

		bookmarkingLogging('Facebook');
	})
	
	//Google Bookmarking
	$("img#bookmark-google").click(function() {
		window.open('http://www.google.com/bookmarks/mark?op=edit&bkmk='+encodeURIComponent(document.location.href)+'&title='+encodeURIComponent(document.title)	,'_blank','width=930,height=470,left=50,top=50,toolbar=no,menubar=no,location=no,scrollbars=yes,status=yes,resizable=yes');

		bookmarkingLogging('Google');
	})
	
	//Baidu Bookmarking
	$("img#bookmark-baidu").click(function() {
		window.open('http://cang.baidu.com/do/add?it='+encodeURIComponent(document.title.substring(0,76))+'&iu='+encodeURIComponent(location.href)+'&fr=ien#nw=1','_blank','scrollbars=no,width=600,height=450,left=75,top=20,status=no,resizable=yes');
		
		bookmarkingLogging('Baidu');
		
	})
	
	//QQ Bookmarking
	$("img#bookmark-qq").click(function() {
		window.open('http://shuqian.qq.com/post?from=3&title='+encodeURIComponent(document.title)+'&uri='+encodeURIComponent(document.location.href)+'&jumpback=2&noui=1','favit','width=930,height=470,left=50,top=50,toolbar=no,menubar=no,location=no,scrollbars=yes,status=yes,resizable=yes');

		bookmarkingLogging('QQ');
	})
	
	function bookmarkingLogging(strProvider) {
		var strTitle = document.title;
		var strUrl = document.location.href;
		
        $.get('http://mmlab.itsc.cuhk.edu.hk/iso/social-bookmarking-stat.aspx', 
			{'title': strTitle, 'provider':strProvider, 'url':strUrl}, 
			function(data) {
				if (/true/i.test(data.status)) { //If Status == "true"
					//alert(data.reason);
                } else {
					//alert(data.reason);
                }
            }, 'jsonp')
	}
	
	// Change CSS Setting using class-name
	$("*[class*='set-']").each( function() {
		var arrClass = $(this).attr("class").replace(/\s+/g, " ").split(" ");
		
		for (i in arrClass) {
			if (/^set-/.test(arrClass[i])) {
				var arrAction = arrClass[i].split("-");
				//split e.g. "set-width-130px" into ['set', 'width', '130px']
				if (arrAction.length == 3) {
					if (arrAction[1] == "color") {
						$(this).css(arrAction[1], "#" + arrAction[2]);
					} else if (arrAction[1] == "textalign") {
						$(this).css("text-align", arrAction[2]);
					} else if (arrAction[1] == "paddingbottom") {
						$(this).css("padding-bottom", arrAction[2]);
					} else {
						$(this).css(arrAction[1], arrAction[2]);
					}
				}
				
			}
		}
	})

	//Set the purple border of div#inner-main
	$(window).load( function() {
							   
		if ($("div#inner-main").height() < $("div#right-container").height()) {
			var oDiv = $("<div></div>");
			var iHeightDiff = $("div#right-container").height() - $("div#inner-main").height() + 10;
			oDiv.css("height", iHeightDiff);
			oDiv.insertAfter("div.tools");
		}
	})


})

