var isIE = (navigator.appName.indexOf("Microsoft") > -1);
var d = document;
var str;
var albumType;

if (!levelArray)	var levelArray = new Array();


function init()
{
	if (levelArray.length == 0)	getLevel();
	
	if (levelArray.length > 0)	highlightTopNav();
	if (levelArray.length > 1)	genLeftNav();
	if (levelArray.length > 3)	highlightContentTab();
	if (levelArray.length > 0)	genBreadcrumbs();
	genTitle();
	setBtnOver();
	genContentTableStyle();
	if (d.getElementById('fontSize'))	getFontSize();
	
	/* Added by DC on 15/11/2010 starts */
	//listScroll.init('newsList', 'Scrollbar-Container', true);
	/* Added by DC on 15/11/2010 ends */
}

function getLevel()
{
	var url = location.pathname;
	levelArray = url.split('/');
	levelArray.shift();
	levelArray.shift();
	
	var obj = levelArray[levelArray.length-1];
	if (obj != 'index.html')
	{
		levelArray[levelArray.length-1] = obj.replace('.html', '');
	}
	else
	{
		levelArray.pop();
	}
}

function highlightTopNav()
{
	var obj = levelArray[0];
	if (!d.getElementById(obj))	return;
	
	var btn = d.getElementById(obj);
	btn.className += ' active';
}

function highlightLeftNav()
{
	if (!d.getElementById('leftNav'))	return;
	
	var obj = levelArray[1];
	var obj1 = levelArray[2];
	if (!d.getElementById(obj))	return;
	if (!d.getElementById(obj + '_menu'))	return;
	if (!d.getElementById(obj1))	return;
	
	var btn = d.getElementById(obj);
	btn.className += ' active';
	
	var menu = d.getElementById(obj + '_menu');
	menu.style.display = 'block';
	
	var btn1 = d.getElementById(obj1);
	btn1.className += ' active';
}

function highlightContentTab()
{
	var obj = levelArray[3];
	if (!d.getElementById(obj))	return;
	
	var btn = d.getElementById(obj);
	btn.className += ' active';
}

/*Change font size*/
//Specify affected tags. Add or remove from list:
var tgs = new Array('p', 'h1', 'h2');

//Specify spectrum of different font sizes:
var szs = new Array('84%', '90%', '100%');
var startSz = sz = szs.length;

function getFontSize()
{
	if (getCookie('fontSize') == null)
	{
		startSz = 1;
	}
	else
	{ 
		startSz = getCookie('fontSize');
		if (startSz == "NaN")	startSz = 1;
	}	
	changeFontSize(startSz, true);
}

function changeFontSize(inc, start)
{
	if (!d.getElementById) return;
	var cEl = null,sz = eval(startSz),i,j,cTags;
	
	sz = inc;
	
	cEl = d.getElementsByTagName('body')[0];
	cEl.style.fontSize = szs[ sz ];	
	
	//Highlight Btn
	var fontSizeLink = d.getElementById('fontSize').getElementsByTagName('a');
	for (var i=0; i<fontSizeLink.length; i++)
	{
		fontSizeLink[i].className = fontSizeLink[i].className.replace('active', '');
		fontSizeLink[i].clicked = false;
	}
	fontSizeLink[sz].className = fontSizeLink[sz].className += ' active';
	fontSizeLink[sz].clicked = true;
	
	setCookie("fontSize", sz, nd, cpath, cdomain);
}

var nd= new Date();
nd.setTime(nd.getTime()+(60*24*60*60*1000));
var cdomain = (location.domain) ? location.domain : null;
var cpath = "/";

function setCookie(name, value, expires, path, domain, secure)
{
  var curCookie = name + "=" + escape(value) +
		((expires) ? "; expires=" + expires.toGMTString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");

  d.cookie = curCookie;
}

function getCookie(name)
{
	if (d.cookie.indexOf(name) < 0)
	{		
		return null;
	}
	else
	{	
		var startStr = d.cookie.indexOf(name) + name.length + 1;
		var endStr = d.cookie.indexOf(";", startStr);
		if (endStr == -1)	endStr = d.cookie.length;
		return unescape(d.cookie.substring(startStr, endStr));
	}
}

function genBreadcrumbs()
{
	if (!d.getElementById('breadcrumbs'))	return;
	var breadcrumbs = d.getElementById('breadcrumbs');
	
	var div = createDiv('fontSize', '', 'Font Size');
	breadcrumbs.appendChild(div);
	var smallA = createA('javascript:changeFontSize(0);', '', 'A', '', 'small');
	div.appendChild(smallA);
	var midA = createA('javascript:changeFontSize(1);', '', 'A', '', 'mid');
	div.appendChild(midA);
	var largeA = createA('javascript:changeFontSize(2);', '', 'A', '', 'large');
	div.appendChild(largeA);
	
	//alert(d.getElementById(levelArray[0]));
	
	if (!d.getElementById(levelArray[0]))	return;
	var a = createA('/cidp/index.html', '_self', 'Home');
	breadcrumbs.appendChild(a);
	
	for (var i=0; i<levelArray.length; i++)
	{
		if (levelArray[i])
		{
			var obj = levelArray[i];
			if (!d.getElementById(levelArray[i]))	break;
			var btn = d.getElementById(levelArray[i]);
			var txt = (isIE)	?	btn.innerText	:	btn.textContent;
			var link = btn.href;
			
			breadcrumbs.innerHTML += ' > ';
			
			if (i < levelArray.length-1)
			{
				//var a = createA(getLink(i), '_self', txt);
				var a = createA(link, '_self', txt);
				breadcrumbs.appendChild(a);
			}
			else
			{
				breadcrumbs.innerHTML += txt;
			}
		}
	}
	
	/*function getLink(total)
	{
		var link = '/';
		
		for (var i=0; i<=total; i++)
		{
			link += levelArray[i] + '/';
		}
		
		return link;
	}*/
}

function genTitle()
{
	var h1 = d.getElementsByTagName('h1');
	
	for (var i=0; i<h1.length; i++)
	{
		var txt = (isIE)	?	h1[i].innerText	:	h1[i].textContent;
		txt = txt.replace('&', '#');
		var h1Swf = createDiv('h1Swf'+i, '', txt);
		var h = h1[i].offsetHeight;
		
		h1[i].innerHTML = '';
		h1[i].appendChild(h1Swf);
		
		//SWF Object
		var flashvars = {};
		flashvars.txt = txt;
		
		var params = {};
		params.allowscriptaccess = "sameDomain";
		params.wmode = "opaque";
		
		var attributes = {};
		
		swfobject.embedSWF('/cidp/swf/title.swf', 'h1Swf'+i, '462', h, '9.0.45', false, flashvars, params, attributes);
	}
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

var motion;
var goSpeed = 20;
var tempNum;
var frameBody;
var tempX;
var tempY;
var isIFrame = false;

function scrollStart(name, goNum, iFrame) {
	tempNum = goNum;
	if (navigator.appName.indexOf("Microsoft") > -1) {
		if (iFrame != null) {
    		frameBody  = document.frames(name).document.body;
    		isIFrame = true;
    	} else {
    		frameBody  = document.getElementById(name);
    	}
	} else {
		if (iFrame != null) {
    		frameBody  = document.all(name);
    	//	frameBody.contentWindow.scrollTo(0, 0);
    		isIFrame = true;
    	} else {
    		frameBody  = document.getElementById(name);
    	}
	}
	scrollMotion();
}

function scrollMotion(){
//	window.status = document.getElementById("contentIFrame").document.body.scrollHeight +"  :  "+ frameBody.offsetHeight;
	if (navigator.appName.indexOf("Microsoft") > -1) {
		frameBody.scrollTop += tempNum;
	} else {
		if (isIFrame) {
			tempX = frameBody.contentWindow.pageXOffset;
			tempY = frameBody.contentWindow.pageYOffset;
			tempY += tempNum;
			frameBody.contentWindow.scrollTo(tempX, tempY);
		} else {
			frameBody.scrollTop += tempNum;
		}
	}
	
	motion = setTimeout("scrollMotion()", goSpeed);
}

function scrollStop() {
	clearTimeout(motion);
}


var iconScroll = 
{
	init:	function (obj, R, L)
	{	
		this.container  = d.getElementById(obj);
		this.arrowR  = d.getElementById(R);
		this.arrowL  = d.getElementById(L);
		this.timeOutSpeed = 10;
		
		this.curIndex = 0;
		
		this.containerW = this.container.offsetWidth;
		this.objW = this.container.scrollWidth;
		this.total = Math.round(this.objW/this.containerW);
		
		//Duplicate Content for loop
		//var str = this.container.innerHTML;
		//this.container.innerHTML += str;
		
		//window.status = this.container.offsetWidth + '  :  ' + this.container.scrollWidth;
		
		this.arrowR.onclick = function()	{iconScroll.goScroll(1)};
		this.arrowL.onclick = function()	{iconScroll.goScroll(-1)};
	},
	
	goScroll:	function(num)
	{
		this.curIndex += num;
		if (this.curIndex > this.total-1)	this.curIndex = 0;
		if (this.curIndex < 0)	this.curIndex = this.total-1;
		
		//this.container.L = this.containerW*this.curIndex;
		this.container.L = Math.ceil(this.objW/this.total)*this.curIndex;
		this.container.speed = 10;
		
		clearTimeout(this.container.timeOut);
		iconScroll.motion(this.container);
	},
	
	motion:	function(container)
	{
		//container.objL = Number(container.scrollLeft.substring(0, (container.scrollLeft.toString().length-2)));
		container.objL = container.scrollLeft;

		container.goStep = (container.L - container.objL)/container.speed;
		if (container.goStep > 0)	container.goStep = Math.ceil(container.goStep);
		if (container.goStep < 0)	container.goStep = Math.floor(container.goStep);

		if (!(Math.abs(container.objL - container.L) < 1))
		{
			container.objL += container.goStep;
			container.timeOut = setTimeout(function(){iconScroll.motion(container);}, this.timeOutSpeed);
		}
		else
		{
			container.objL = container.L;
			clearTimeout(container.timeOut);
			//alert(container.objL + ' : ' +  container.L);
		}

		container.scrollLeft = container.objL;
		//window.status = container.scrollLeft + '  :  ' + container.L;
		//window.status = container.goStep;
	}
}

/*
var frameBody2;
var tempNum2;
var motion2;

function genScroll(name)
{
	frameBody2  = document.getElementById(name);
	
	//Duplicate Content for loop
	var str = frameBody2.innerHTML;
	frameBody2.innerHTML += str;
	
	iconW = frameBody2.offsetWidth;
	
	//window.status = frameBody2.scrollWidth;
	
	//setTimeout("autoScrollGo()", 1000);
}

function scrollMotion2()
{
	frameBody2.scrollLeft += tempNum2;
	
	if (frameBody2.scrollLeft > frameBody2.scrollWidth/2)
	{
		frameBody2.scrollLeft -= frameBody2.scrollWidth/2;
	}
	else if (frameBody2.scrollLeft <= 0)
	{
		frameBody2.scrollLeft = frameBody2.scrollWidth/2;
	}
	
	//window.status = frameBody2.scrollLeft + "  :  " + frameBody2.scrollWidth;
	
	motion2 = setTimeout("scrollMotion2()", (goSpeed));
}

function scrollStart2(name, goNum, iFrame)
{
	tempNum2 = goNum;
	scrollMotion2();
}

function scrollStop2()
{
	clearTimeout(motion2);
	//setTimeout("autoScrollGo()", 0);
}*/

function createImg(src, alt, w, h, link, t, className, id)
{
	var img = d.createElement('img');
	if (src)	img.setAttribute('src', src);
	if (alt)	img.setAttribute('alt', alt);
	if (w)	img.setAttribute('width', w);
	if (h)	img.setAttribute('height', h);
	if (className)	img.className = className;
	if (id)	img.setAttribute('id', id);
	
	if (link)
	{
		img.setAttribute('border', 0);
		
		var a = d.createElement('a');
		a.setAttribute('href', link);
		if (t && typeof(t) != 'undefined')	a.setAttribute('target', t);
		a.appendChild(img);
		return a;
	}
	else
	{	
		return img;
	}
}

function createA(link, t, txt, id, className)
{		
	var a = d.createElement('a');
	if (link)	a.setAttribute('href', link);
	if (t && typeof(t) != 'undefined')	a.setAttribute('target', t);
	
	//if (txt)	a.appendChild(d.createTextNode(txt));
	if (txt)	a.innerHTML = txt;
	if (id)	a.setAttribute('id', id);
	if (className)	a.className = className;
	
	return a;
}

function createDiv(id, className, txt)
{		
	var div = d.createElement('div');
	if (id)	div.setAttribute('id', id);
	if (className)	div.className = className;
	//if (txt)	div.appendChild(d.createTextNode(txt));
	if (txt)	div.innerHTML = txt;
	return div;
}

function createTag(tag, id, className, txt)
{		
	var div = d.createElement(tag);
	if (id)	div.setAttribute('id', id);
	if (className)	div.className = className;
	if (txt)	div.innerHTML = txt;
	return div;
}

function createInput(name, type, value, className)
{		
	var input = d.createElement('input');
	if (name)	input.setAttribute('name', name);
	if (type)	input.setAttribute('type', type);
	if (value)	input.setAttribute('value', value);
	if (className)	input.className = className;
	return input;
}


function startFading(id) {
//  var el = document.getElementById('fading_image_container').getElementsByTagName("div")[nextImage];
  var el = document.getElementById(id);

  el.style.visibility = 'visible';
  el.style.zIndex = 2;
  setOpacity(el, 0);
  fadeImage(el,0);
}

function fadeImage(el, currentOpacity) {

  currentOpacity += 10;

  if (currentOpacity > 100) {
    setOpacity(el, 100);
//    var prevEl = el.previousSibling ? el.previousSibling : el.parentNode.lastChild;
//    var prevEl = document.getElementById('fading_image_container').getElementsByTagName("div")[1];
//    prevEl.style.visibility = 'hidden';
    el.style.zIndex = 1;
  }
  else {
    setOpacity(el, currentOpacity);
    window.setTimeout(function() { fadeImage(el, currentOpacity); }, 20);
  }
}

function setOpacity(el, opacity) {

	opacity /= 100;

	el.style.opacity = opacity;
	el.style.MozOpacity = opacity;
	el.style.filter = "alpha(opacity=" + (opacity*100) + ")";

}

function setBtnOver()
{
	var img = d.getElementsByTagName('img');
	
	for (var i=0; i<img.length; i++)
	{
		if (img[i].className.indexOf('btnOver') < 0)	continue;
		if (img[i].src.indexOf('_over') >= 0)			img[i].clicked = true;
		
		img[i].onmouseover = function()
		{
			if (this.clicked)	return;
			var ext = this.src.substring(this.src.length-4, this.src.length);
			this.src = this.src.replace(ext, '_over' + ext);
		}
		
		img[i].onmouseout = function()
		{
			if (this.clicked)	return;
			var ext = this.src.substring(this.src.length-4, this.src.length);
			this.src = this.src.replace('_over' + ext, ext);
		}
	}
}

function genFooter()
{
	str = '';
	str += '<span class="leftFooter"><a href="http://www.google.com/custom?hl=en&client=google-coop-np&cof=FORID%3A13%3BAH%3Aleft%3BS%3Ahttp%3A%2F%2Fhkuspace.hku.hk%2Fcidp%2F%3BCX%3ACentre%2520for%2520International%2520Degree%2520Programmes%3BL%3Ahttp%3A%2F%2Fwww.hkuspace.hku.hk%2Fcidp%2Fimages%2Fcidplogo.jpg%3BLH%3A80%3BLP%3A1%3BLC%3A%23cc0000%3BKMBOC%3A%23336699%3BKMTC%3A%230000CC%3BKMTVC%3A%230000CC%3B&adkw=AELymgUh54J7jHjhoRCJWllgWBxK0mseH-JtsqQNm-gIZVXVTSjLGHEZ_A6V1n9QAK7yXEBxR-R7sE-L_FL3fvuHdUCa36q5nTcVWiIwVsl8j_BKjj1Osps&boostcse=0&q=&btnG=Search&cx=013978796946241707904%3Agsg3loafgqy" target="_blank" class="search">Site Search</a> | <a id="faq" href="http://hkuspace.hku.hk/cidpfaq/home/?__f=category&node=279" target="_blank">FAQ</a> | <a href="javascript:NewWindow(\'/cidp/contact.html\',\'Contact\',\'500\',\'400\',\'yes\',\'no\')">Contact Us</a> | <a href="/cidp/sitemap.html">Sitemap</a> | <a href="javascript:NewWindow(\'/cidp/privacy.html\',\'Privacy\',\'500\',\'500\',\'yes\',\'no\')">Privacy Policy</a> |  <a href="http://cidpaa.hkuspace.hku.hk/" target="blank">CIDP Alumni</a> |  <a href="javascript:NewWindow(\'/cidp/publishing.html\',\'Publishing\',\'500\',\'500\',\'yes\',\'no\')">Web Publishing Policy</a><span>Enquiry: 2910 7555</span></span>';
	str += '<span class="rightFooter">Copyright &copy; 2009 HKU SPACE. All Rights Reserved.</span>';
	
	init();
	
	d.write(str);
}

function genContentTopNav()
{
	str = '';
	str += '<a href="/cidp/index.html" id="logo"><img src="/cidp/images/logo.gif"></a>';
	str += '<a href="http://www.google.com/custom?hl=en&client=google-coop-np&cof=FORID%3A13%3BAH%3Aleft%3BS%3Ahttp%3A%2F%2Fhkuspace.hku.hk%2Fcidp%2F%3BCX%3ACentre%2520for%2520International%2520Degree%2520Programmes%3BL%3Ahttp%3A%2F%2Fwww.hkuspace.hku.hk%2Fcidp%2Fimages%2Fcidplogo.jpg%3BLH%3A80%3BLP%3A1%3BLC%3A%23cc0000%3BKMBOC%3A%23336699%3BKMTC%3A%230000CC%3BKMTVC%3A%230000CC%3B&adkw=AELymgUh54J7jHjhoRCJWllgWBxK0mseH-JtsqQNm-gIZVXVTSjLGHEZ_A6V1n9QAK7yXEBxR-R7sE-L_FL3fvuHdUCa36q5nTcVWiIwVsl8j_BKjj1Osps&boostcse=0&q=&btnG=Search&cx=013978796946241707904%3Agsg3loafgqy" target="_blank" id="search" id="search" class="topNavRollover"><span class="displace">Site Search</span></a>';
	str += '<a href="/cidp/about/cidp/aboutus.html" id="about" class="topNavRollover"><span class="displace">About CIDP</span></a>';
	str += '<a href="/cidp/news/info/info.html" id="news" class="topNavRollover"><span class="displace">News &amp; Events</span></a>';
	str += '<a href="/cidp/student/index.html" id="student" class="topNavRollover"><span class="displace">Student Life</span></a>';
	str += '<a href="/cidp/support/studying.html" id="support" class="topNavRollover"><span class="displace">Support Services</span></a>';
	str += '<a href="/cidp/how/procedure/online.html" id="how" class="topNavRollover"><span class="displace">How to Apply</span></a>';
	str += '<a href="/cidp/programmes/index.html" id="programmes" class="topNavRollover"><span class="displace">Programmes</span></a>';
	
	d.write(str);
}

function genContentTabNav()
{
	str = '';
	str += '<a href="struct.html" id="struct" class="tabRollover"><span class="displace">Programme Structure</span></a>';
	str += '<a href="entry.html" id="entry" class="tabRollover"><span class="displace">Entry Requirements</span></a>';
	str += '<a href="staff.html" id="staff" class="tabRollover"><span class="displace">Teaching Staff</span></a>';
	str += '<a href="partner.html" id="partner" class="tabRollover"><span class="displace">Partner\'s Message</span></a>';
	str += '<a href="overview.html" id="overview" class="tabRollover"><span class="displace">Programme Overview</span></a>';
	
	d.write(str);
}

function genLeftNavProgrammes()
{
	str = '';
	str += '<a href="/cidp/programmes/arts/graphic/overview.html" id="arts">Arts &amp; Design</a>';
	str += '<div id="arts_menu">';
	str += '		<a href="/cidp/programmes/arts/graphic/overview.html" id="graphic">Bachelor of Arts (Hons) Graphic Design</a>';
	str += '		<a href="/cidp/programmes/arts/interior/overview.html" id="interior">Bachelor of Arts (Hons) Interior Architecture and Design</a>';
	str += '</div>';
	str += '<a href="/cidp/programmes/business/accounting/overview.html" id="business">Business</a>';
	str += '<div id="business_menu">';
	str += '		<a href="/cidp/programmes/business/accounting/overview.html" id="accounting">Bachelor of Commerce (Accounting / Accounting &amp; Accounting Technologies)</a>';
	str += '		<a href="/cidp/programmes/business/business-banking/overview.html" id="business-banking">Bachelor of Science (Hons) Business / Banking & Finance</a>';
	str += '		<a href="/cidp/programmes/business/logistics/overview.html" id="logistics">Bachelor of Commerce (Logistics & Marketing)</a>';
	str += '		<a href="/cidp/programmes/business/marketing/overview.html" id="marketing">Bachelor of Commerce (Marketing & Advertising)</a>';
	str += '		<a href="/cidp/programmes/business/marketing-management/overview.html" id="marketing-management">Bachelor of Arts (Hons) Marketing & Management</a>';
	str += '</div>';
	str += '<a href="/cidp/programmes/it/networks/overview.html" id="it">Information Technology</a>';
	str += '<div id="it_menu">';
	str += '		<a href="/cidp/programmes/it/networks/overview.html" id="networks">Bachelor of Science (Hons) in Computer Networks</a>';
	str += '		<a href="/cidp/programmes/it/info-systems/overview.html" id="info-systems">Bachelor of Science in Computing and Information Systems</a>';
	str += '</div>';
	str += '<a href="/cidp/programmes/social/asian/overview.html" id="social">Social Cultral &amp; Media Studies</a>';
	str += '<div id="social_menu">';
	str += '		<a href="/cidp/programmes/social/asian/overview.html" id="asian">Bachelor of Arts (Asian Studies)</a>';
	str += '		<a href="/cidp/programmes/social/advertising/overview.html" id="advertising">Bachelor of Arts Communication, Advertising & Public Relations</a>';
	str += '		<a href="/cidp/programmes/social/communication/overview.html" id="communication">Bachelor of Arts (Communication Studies)</a>';
	str += '		<a href="/cidp/programmes/social/media/overview.html" id="media">Bachelor of Arts (Hons) Media and Cultural Studies</a>';
	str += '		<a href="/cidp/programmes/social/journalism/overview.html" id="journalism">Bachelor of Arts (Hons) Journalism, Media and Cultural Studies</a>';
	str += '		<a href="/cidp/programmes/social/publishing/overview.html" id="publishing">Bachelor of Arts (Hons) Publishing, Media and Cultural Studies</a>';
	str += '		<a href="/cidp/programmes/social/criminology/overview.html" id="criminology">Bachelor of Arts (Hons) Criminology</a>';
	str += '</div>';
	str += '<a href="/cidp/programmes/hosp/hotel/overview.html" id="hosp">Tourism &amp; Hospitality</a>';
	str += '<div id="hosp_menu">';
	str += '		<a href="/cidp/programmes/hosp/hotel/overview.html" id="hotel">Bachelor of Arts in Hotel & Hospitality Management</a>';
	str += '		<a href="/cidp/programmes/hosp/tourism/overview.html" id="tourism">Bachelor of Arts in Tourism Management</a>';
	str += '</div>';
	
	d.write(str);
}

function genLeftNav()
{
	if (!d.getElementById('leftNav'))	return;
	
	var leftNav = d.getElementById('leftNav');
	var obj = sectionArray[0];
	for (var i=0; i<obj.length; i++)
	{
		if (obj[i].id != levelArray[0])	continue;
		
		var obj1 = obj[i].subSection;
		for (var j=0; j<obj1.length; j++)
		{
			var str = (obj1[j].id == levelArray[1]) ? "active" : ""
			var a = createA(obj1[j].link, obj1[j].target, obj1[j].name, obj1[j].id, str);
			leftNav.appendChild(a);
			
			if (obj1[j].id != levelArray[1])	continue;
			
			if (obj1[j].subSection)
			{
				var obj2 = obj1[j].subSection;
				var div = createDiv(obj1[j].id + '_menu');
				for (var k=0; k<obj2.length; k++)
				{
					var str = (obj2[k].id == levelArray[2]) ? "active" : ""
					var a = createA(obj2[k].link, obj2[k].target, obj2[k].name, obj2[k].id, str);
					div.appendChild(a);
				}
				leftNav.appendChild(div);
			}
		}
	}
}

function genTools()
{
	str = '';
	str += '<div id="onlineApp"><a href="https://w3.hkuspace.hku.hk/eform/CIDP/OnlineForm.php" target="_blank">Online Application</a></div>';
	str += '<div id="download"><a href="../../../how/procedure/offline/form.html">Download Useful Forms</a></div>';
	str += '<div id="print"><a href="javascript:window.print();">Print Version</a></div>';
	
	d.write(str);
}

function genToolss()
{
	str = '';
	str += '<div id="onlineApp"><a href="https://w3.hkuspace.hku.hk/eform/CIDP/OnlineForm.php" target="_blank">Online Application</a></div>';
	str += '<div id="download"><a href="../../../how/procedure/offline/index.html" target="_blank">Download Useful Forms</a></div>';
	str += '<div id="print"><a href="javascript:window.print();">Print Version</a></div>';
	
	d.write(str);
}

function genContact()
{
	str = '';
	str += '<div id="contact">';
	str += '	<p><strong>Programme Enquires:</strong> 3762 0086</p>';
	str += '	<strong>Email:</strong> <a href="mailto:renee.chan@hkuspace.hku.hk">renee.chan@hkuspace.hku.hk</a>';
	str += '	<div></div>';
	str += '</div>';
	
	d.write(str);
}

function genContentTableStyle()
{
	var table = d.getElementsByTagName('table');
	//alert(table)
	//clearTableStyle(table);
	
	
	for (var j=0; j<table.length; j++)
	{
		if (!(table[j].className == 'contentTable' || table[j].className == 'linkTable'))	continue;
		
		var rows = table[j].rows;
		
		for (var i=0; i<rows.length; i++)
		{
			if (rows[i].className == 'total')	continue;
			if (i%2 == 0)			rows[i].className = 'even';
			if (i == rows.length-1)	rows[i].className += ' last';
			
			if (table[j].className == 'linkTable')
			{
				rows[i].onmouseover	= function() {this.className += ' highlight';}
				rows[i].onmouseout	= function() {this.className = this.className.replace('highlight', '');}
			}
		}
	}
}

function NewWindow(mypage, myname, w, h, scroll,resizable) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable='+resizable+','
	win = window.open(mypage, myname, winprops)
	win.self.focus()
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

function genSitemap()
{
	var sitemap = d.getElementById('sitemapWrapper');
	var obj = sectionArray[0];
	var siteDiv = createDiv('', 'sitemapContainer', '');
	sitemap.appendChild(siteDiv);
	
	for (var i=0; i<obj.length; i++)
	{
		
		var conDiv = createDiv('', '', '');
		siteDiv.appendChild(conDiv);
		
		var span = createTag('span', '', '');
		conDiv.appendChild(span);
		
		var level1a = createA(obj[i].link, obj[i].target, obj[i].name, '', '');
		span.appendChild(level1a);
		
		var level1ul = createTag('ul', '', '');
		conDiv.appendChild(level1ul);
		
		var obj1 = obj[i].subSection;
		
		if (obj1)
		{
			for (var j=0; j<obj1.length; j++)
			{
				var level1li = createTag('li', '', '');
				level1ul.appendChild(level1li);
				
				var level2a = createA(obj1[j].link, obj1[j].target, obj1[j].name, '', '');
				level1li.appendChild(level2a);
				
				if (obj1[j].subSection)
				{
					var level2ul = createTag('ul', '', '');
					level1li.appendChild(level2ul);
					
					var obj2 = obj1[j].subSection;
					for (var k=0; k<obj2.length; k++)
					{
						var level2li = createTag('li', '', '');
						level2ul.appendChild(level2li);
						
						var level3a = createA(obj2[k].link, obj2[k].target, obj2[k].name, '', '');
						level2li.appendChild(level3a);
					}
				}
			}
		}
	}
}

function GetParam(name)
{
	var start=location.search.indexOf("?"+name+"=");
	if (start<0) start=location.search.indexOf("&"+name+"=");
 	if (start<0) return '';
 	start += name.length+2;
 	var end=location.search.indexOf("&",start)-1;
 	if (end<0) end=location.search.length;
 	var result=location.search.substring(start,end);
 	var result='';
 	for(var i=start;i<=end;i++)
 	{
 		var c=location.search.charAt(i);
 		result=result+(c=='+'?' ':c);
 	}
 	//alert(unescape(result));
 	return unescape(result);
}

function genAlbum(target, year)
{
	albumType = target;
	var gallery = d.getElementById('galleryContainer');
	var obj = eval(albumType + "Array")[0];
	var count = 0;
	
	for (var i=0; i<obj.length; i++)
	{
		//alert(obj[i].id);
		if (year == obj[i].year)
		{
			if (!(count%4))
			{
				var galleryBg = createDiv('', 'galleryBg clearfix', '');
				gallery.appendChild(galleryBg);
				
				if (i<(obj.length-1))
				{
					gallery.appendChild(createDiv('', 'dash', ''));
				}
			}
		
			var thumbDiv = createDiv('', '', '');
			galleryBg.appendChild(thumbDiv);
			
			var a = createA('album.html?albumid='+obj[i].id, '', '', '', '');
			thumbDiv.appendChild(a);
			
			var img = createImg('/cidp/images/student/'+albumType+'/'+obj[i].id+'_thumb.jpg', '', 169, 126, '', '', '');
			a.appendChild(img);
			
			var p = createTag('p', '', '', obj[i].name);
			thumbDiv.appendChild(p);
			
			count ++;
		}
	}
}

//var albumType = GetParam("albumact");
var albumNum = GetParam("albumid");


function changePhoto(target)
{
	var obj = eval(albumType + "Array")[0];
	
	var mainImg = d.getElementById('mainImg');
	var photoCaption = d.getElementById('contentAlbum');
	
	//alert(GetParam("albumid").split('_')[1]-1);
	//if (!obj[(GetParam("albumid").split('_')[1]-1)]) return;
	
	
	var objA = null;
	
	for (var i=0; i<obj.length; i++)
	{
		if (obj[i].id == GetParam("albumid"))
		{
			objA = obj[i];
			break;
		}
	}
	var obj1 = objA.subSection;
	
//	var obj1 = obj[(GetParam("albumid").split('_')[1]-1)].subSection;
	
	var num = (target-1);
	mainImg.src = '/cidp/images/student/'+albumType+'/'+albumNum+'_'+obj1[num].id+'_lrg.jpg';
	photoCaption.innerHTML = obj1[num].name;
}

function genPhoto(target)
{
	if (GetParam("albumid") == '') return;
	
	albumType = target;
	var objNum = (GetParam("albumid").split('_')[1]-1);
	
	//alert(target);
	
	var obj = eval(albumType + "Array")[0];
	//var obj1 = obj[objNum].subSection;
	

	var objA = null;
	
	for (var i=0; i<obj.length; i++)
	{
		if (obj[i].id == GetParam("albumid"))
		{
			objA = obj[i];
			break;
		}
	}
	var obj1 = objA.subSection;
	
	var gallery = d.getElementById('galleryContainer');
	
	//var h5 = createTag('h5', 'titleAlbum', '', obj[objNum].name);
	var h5 = createTag('h5', 'titleAlbum', '', objA.name);
	gallery.appendChild(h5);
	
	var divMain = createDiv('mainImgContainer', '', '');
	gallery.appendChild(divMain);
	
	var img = createImg('/cidp/images/student/'+albumType+'/'+albumNum+'_'+obj1[0].id+'_lrg.jpg', '', 462, 347, '', '', '', 'mainImg');
	divMain.appendChild(img);
	
	var p = createTag('p', 'contentAlbum', '', '');
	gallery.appendChild(p);
	
	
	var thumbDiv = createDiv('thumbContainer', '', '');
	gallery.appendChild(thumbDiv);
	var thumb = d.getElementById('thumbContainer');
	
	for (var i=1; i<=obj1.length; i++)
	{
		var a = createA('javascript:changePhoto('+obj1[(i-1)].id.split('_')[1]+');', '', '', '', '');
		thumbDiv.appendChild(a);
		
		var thumbImg = createImg('/cidp/images/student/'+albumType+'/'+albumNum+'_photo_'+i+'.jpg', '', 60, 45, '');
		a.appendChild(thumbImg);
		//alert(a);
	}
	
	changePhoto(1);
		
	//	alert(obj[(GetParam("albumid").split('_')[1]-1)].subSection);
}

//alert(picnum);

function genYearPull()
{
	str = '';
	str += '<select id="yearPull" onchange="MM_jumpMenu(\'parent\',this,0)">';
	str += '	<option value="index.html">2011-2012</option>';
	str += '	<option value="2010-2011.html">2010-2011</option>';	str += '	<option value="2009-2010.html">2009-2010</option>';
	str += '	<option value="2008-2009.html">2008-2009</option>';
	str += '</select>';
	
	d.write(str);
}
		
		
		
function genYearMenu() {
	var sPath = window.location.pathname;
	var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
	//alert("sPage: "+sPage);
	var menu = document.getElementById('yearPull');
	var option = menu.getElementsByTagName('option');
	for (var i=0; i<option.length; i++)
	{
		option[i].value;
		if (sPage == option[i].value)
		{
			option[i].selected = true;
			//alert(option[i].select);
		}
	}
}

/* Added by DC on 15/11/2010 starts */

var listScroll = 
{
	scroller:	null,
	scrollbar:	null,

	init:	function (obj, scroll, autoStart)
	{	
		this.container  = d.getElementById(obj);
		this.div  = this.container.getElementsByTagName('div')[0];
		this.scrollbar  = d.getElementById(scroll);
		this.track = this.scrollbar.getElementsByTagName('div')[0];
		this.scroller = this.scrollbar.getElementsByTagName('div')[1];
		this.timeOutSpeed = 70;
		this.containerH = this.container.offsetHeight;
		this.trackH = this.track.offsetHeight - this.scroller.offsetHeight;
		this.id = 0;
		
		//Duplicate Content for loop
		var ul = this.container.getElementsByTagName('ul');
		for (var i=0; i<ul.length; i++)
		{
			ul[i].isScroll = ul[i].offsetHeight > this.containerH;
			if (!ul[i].isScroll)	continue;
			
			var str = ul[i].innerHTML;
			ul[i].innerHTML += str;
		}
		
		this.container.onmouseover	= function() {listScroll.stopScroll();};
		this.container.onmouseout	= function() {listScroll.autoScrollStart();};
		
		scroller  = new Scrolling.Scroller(this.container, this.container.offsetWidth, this.container.offsetHeight);
		scrollbar = new Scrolling.Scrollbar(this.scrollbar, scroller, new Scrolling.ScrollTween());
		
		if (autoStart)	this.autoScrollMode();
		//if (autoStart)	this.changeList(0);
	},
	
	autoScrollStart:	function()
	{
		this.stopScroll();
		var ul = this.container.getElementsByTagName('ul');
		if (!ul[this.id].isScroll)	return;
		this.timeOut2 = setTimeout(function(){listScroll.autoScroll(1);}, 1000);
	},
	
	autoScroll:	function(num)
	{
		this.num = num;
		
		this.stopScroll();
		this.motion(this.div);
	},
	
	goScroll:	function(num)
	{
		this.num = num;
		
		this.stopScroll();
		this.motion(this.div);
	},
	
	motion:	function(container)
	{
		var num = Number(container.style.top.replace('px', '')) - this.num;
		var endH = Math.round(this.objH/2);
		if (num < -endH)	num += endH;
		if (num > 0)		num -= endH;
		container.style.top = Math.round(num) + 'px';
		//window.status = container.style.top + '  :  ' + endH + '  :  ' + this.objH;
		
		//this.scroller.style.top = (container.scrollTop/((this.objH - this.containerH)/2 + this.containerH))*this.trackH + 'px';
		//this.scroller.style.top = Math.round((-Number(container.style.top.replace('px', ''))/(this.objH/2))*this.trackH) + 'px';
		this.scroller.style.top = Math.round((-Number(container.style.top.replace('px', ''))/(this.objH/2))*this.trackH) + 'px';
		
		this.timeOut = setTimeout(function() {listScroll.motion(container);}, this.timeOutSpeed);
	},
	
	stopScroll:	function()
	{
		clearTimeout(this.timeOut);
		clearTimeout(this.timeOut2);
	},
	
	autoScrollMode:	function(num)
	{
		var ul = this.container.getElementsByTagName('ul');
		if (!ul[this.id].isScroll)
		{
			this.stopScroll();
			return;
		}
		
		var li = ul[this.id].getElementsByTagName('li');
		for (var j=0; j<li.length; j++)
		{
			li[j].style.display = 'block';
		}
		
		//this.objH = this.container.scrollHeight;
		this.objH = this.div.offsetHeight;
		
		this.autoScrollStart();
	},
	
	scrollBarMode:	function(num)
	{
		var ul = this.container.getElementsByTagName('ul');
		var li = ul[this.id].getElementsByTagName('li');
		var total = li.length;
		var j = 0;
		while (j < total/2)
		{
			li[j].style.display = 'none';
			j ++;
		}
		this.stopScroll();
	}
}
/* Added by DC on 15/11/2010 ends */

