/* Do not touch. This is the relative path for
 * the dropdown list. Changing it at your own risk!
 */
var currentAddr = window.location.toString();
var relativePath = (currentAddr.match('/pages/')) ? '' : 'pages/';
var relativePath2 = (currentAddr.match('/UTWP/')) ? '' : 'UTWP/';
var relativeImgPath = (currentAddr.match('/pages/')) ? '../images/' : 'images/';

/* ---------- Settings ----------- */

//Default settings, for IE, safari, opera.
var navTop = 19; //Is the height for nav bar. It varies cross browsers.
var dropDownTop = 25; //Is the top padding for the dropdowns
var fixDropdownTop = 0; //in msie 8.0, there are some weirdness... need to hack the dropdown position
var fixDropdownLeft = 0; //left

if ($.browser.mozilla && $.browser.version.substring(0,3) == '1.9') { navTop = 21; dropDownTop = 27; }

//strange height in msie 8.0... dunno why
if ($.browser.msie && $.browser.version == '8.0') { fixDropdownTop = -14; fixDropdownLeft = -12;}


/* Dropdowns!
 * These are the dropdowns corresponding to each navigation links.
 * Please take care of the \n\ in EVERY end of line. It's very important
 * to include this 'hack'!!!
 *
 * Naming convention
 * id="dropdown-xxxxxxx" where xxxxx is the page's filename
 *
 * To disable a link, simply add the class "disabled" into the link.
 * Example: <a href="link.html" class="disabled">CELC Facilities</a>\n\
 *
 * To add a section, simply add the following line to the dropdown
 * <div class="section-line"></div>\n\
*/
var dropdownHTML = '\n\
<div id="dropdown-index" class="dropdown">\n\
</div>\n\
<div id="dropdown-about" class="dropdown">\n\
	<a href="'+relativePath+'Vision_Mission.html">Vision, Mission & Strategy</a>\n\
	<a href="'+relativePath+'about_introduction.html">Introduction to CELC</a>\n\
	<a href="'+relativePath+'about_contact.html">Contact Us</a>\n\
	<a href="'+relativePath+'about_celc_facilities.html">CELC Facilities</a>\n\
	</div>\n\
<div id="dropdown-staff" class="dropdown">\n\
	<a href="'+relativePath+'staff.html#management">Management Team</a>\n\
	<a href="'+relativePath+'staff.html#full-time">Teaching Staff</a>\n\
	<a href="'+relativePath+'staff.html#part-time">Part Time Teaching Staff</a>\n\
	<a href="'+relativePath+'staff.html#non-academic">Non Academic Staff</a>\n\
</div>\n\
<div id="dropdown-students" class="dropdown">\n\
	<a href="'+relativePath+'UGstudents.html">Undergraduate Students</a>\n\
	<a href="'+relativePath+'Gradstudents.html">Graduate Students</a>\n\
</div>\n\
<div id="dropdown-courses" class="dropdown">\n\
	<a href="'+relativePath+'courses_bec.html">Basic English Course</a>\n\
	<a href="'+relativePath+'courses_efap.html">English for Academic Purposes</a>\n\
	<a href="'+relativePath+'courses_foe_eg1413.html">Critical Thinking and Writing (FOE)</a>\n\
	<a href="'+relativePath+'courses_fos_sp1202.html">Communicating with the Academy (FOS)</a>\n\
	<a href="'+relativePath+'courses_fos_sp1203.html">Foundation in Effective Communicaton (FOS)</a>\n\
	<a href="'+relativePath+'courses_biz_es2002.html">Business Communication (BIZ)</a>\n\
	<a href="'+relativePath+'courses_soc_cs2301.html">Business and Technical Communication (SOC)</a>\n\
	<a href="'+relativePath+'courses_sde_es2007d.html">Professional Communication (SDE)</a>\n\
	<a href="'+relativePath+'courses_fos_es2007s.html">Professional Communication (FOS)</a>\n\
	<a href="'+relativePath+'courses_special_programmes.html">Graduate English Modules</a>\n\
	<a href="'+relativePath+'courses_gems.html">General Education Modules</a>\n\
	<a href="'+relativePath+'courses_SM2.html">Special Programmes</a>\n\
	<a href="'+relativePath+'utwp_index.html">University Town Writing Programme</a>\n\
</div>\n\
<div id="dropdown-publications" class="dropdown">\n\
	<a href="'+relativePath+'about_ELTWO.html">ELTWO</a>\n\
	<a href="'+relativePath+'publications_relt.html">Journal of RELT</a>\n\
	<a href="'+relativePath+'publications_staff_publications.html">Staff Publications</a>\n\
	<a href="'+relativePath+'publications_staff_seminars.html">Staff Seminars</a>\n\
</div>\n\
<div id="dropdown-resources" class="dropdown">\n\
	<a href="'+relativePath+'resources_SELF.html">SELF</a>\n\
</div>\n\
<div id="dropdown-employment" class="dropdown">\n\
	<a href="'+relativePath+'employment_full_time_lecturers.html">Full-Time Lecturers</a>\n\
	<a href="'+relativePath+'employment_part_time_tutors.html">Part-time Tutors</a>\n\
</div>\n\
';

var curNav = null;
/* start of document.ready. Adding event listeners */
$(document).ready(function() {
	
	//Layout adjustment
	$('#nav').css('height',navTop+'px');
	
	//fixing top-nav
	if ($.browser.msie) {
		$('#top-nav').css('padding-top','0px');
		$('#top-nav input').attr('size', '16');
	}
	if ($.browser.msie && $.browser.version == '8.0') {
		$('#top-nav').css('padding-top', '1px');
	}
	
	if ($.browser.opera) {
		$('#top-nav').css('padding-top','2px');
		$('#top-nav input').css('font-size','10px');
		$('#top-nav select').css('font-size','10px');
	}
	
	/* top search bar */
	//Press enter.. then go!
	$('#searchQuery, #searchMenu').keypress(function(e) {
		if (e.which == 13) {
			go();
		}
	});
	
	//Append the dropdown AFTER NAV.
	$('#nav').after(dropdownHTML);
	
	//Navigation dropdown thingy
	$('#nav a').mouseover(function() {
		curNav = $(this);
		curNav.css('background','url('+relativeImgPath+'nav-orange,jpg) repeat-x').css('color','#e4e4e4');
		curNav.css('background','#444').css('color','#fff');
		$('.dropdown').hide(); /* hide all other dropdowns first */
		filename = getFileName($(this).attr('href'));
		var top = $(this).offset().top + fixDropdownTop;
		var left = $(this).offset().left + fixDropdownLeft;
		$('#dropdown-'+filename).css('top', top+dropDownTop).css('left',left);

		//if has links then show.. quite stupid sia!
		if (typeof $('#dropdown-'+filename+' a').attr('href') != 'undefined') {
			$('#dropdown-'+filename).show();
		}
	});
	$('#nav a').mouseout(function() {
		resetNavBackground();
		filename = getFileName($(this).attr('href'));
		$('.dropdown').hide(); /* experimental */
	});
	$('.dropdown').mouseover(function() {
		resetNavBackground();
		//curNav.css('background','url('+relativeImgPath+'nav2.png) repeat-x').css('color','#000');
		curNav.css('background','#444').css('color','#fff');
		$('.dropdown').hide(); /* hide all other dropdowns */
		$(this).show();
	});
	$('.dropdown').mouseout(function() {
		resetNavBackground();
		$(this).hide();
	});
	$('.dropdown').click(function() {
		resetNavBackground();
		$('.dropdown').hide(); /* hide all if one of the dropdown is clicked */
	});
	$('#banner').mouseover(function() {
		$('.dropdown').hide();
	});
	
	//Prepend a "raquo" in front of every link! I love raquo!
	$('.dropdown a').prepend('&raquo;&nbsp;&nbsp;');

	
	//Added disabled!
	$('.dropdown a.disabled').click(function() {
		return false;
	});
	
	$('#banner').dblclick(function() {
		//$(this).slideUp();
		$('.dropdown').hide();
	});
});

function resetNavBackground() {
	$('#nav a').css('background','url(../images/nav-orange.jpg) repeat-x').css('color','#FFF');
};

/* Extract filename (xxxx) from either "http://address/pages/xxxx.html" or "http://address/xxxx.html" */
function getFileName(addr){
	filename = addr;
	if (addr.match('/')) filename = addr.substring(addr.indexOf('/')+1); //getting the filename
	filename = filename.substring(0, filename.indexOf('.')); //remove .html
	return filename;
}

/* function go, used by top search bar */
function go() {
	var searchMenu = $("#searchMenu").val();
	var searchQuery = $("#searchQuery").val();
	
	if (searchQuery == "") return false;
	
 	switch(searchMenu){
		case '0':
		window.location="http://www.google.com/search?q="+searchQuery+"&sitesearch=nus.edu.sg/celc";
		break;
		case '1':
		window.location="http://www.google.com/search?q="+searchQuery+"&sitesearch=nus.edu.sg";
		break;
		case '2':
		window.location="http://www.google.com/search?q="+searchQuery;
		break;
		case '3':
	window.location="https://aces01.nus.edu.sg/vocInternetStaffSearchAPP/jsp/departmentstaffindex.jsp?SearchValue="+searchQuery+"&Searchbtn=Search&SearchCriteria=NAME";
		break;
		case '4':
		window.location="http://www.nus.edu.sg/search/";
		break;
	}
	return false;
}