//Function to get URL parameter
$.extend({
	getUrlVars: function(){
		var vars = [], hash;
		var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
		for(var i = 0; i < hashes.length; i++)
		{
			hash = hashes[i].split('=');
			vars.push(hash[0]);
			vars[hash[0]] = hash[1];
		}
		return vars;
	},
	getUrlVar: function(name){
		return $.getUrlVars()[name];
	}
});




$(document).ready(function() {

	/* NAVIGATION
	********************************************************/
	/* Add class last child to the last item of the navigation */
	$("div.navbar ul.nav li ul li:last-child").addClass("last-child");

	
	/* =TAB
	********************************************************/
	/* Add class last child to the last item of the tab */
	$("div.tabs ul.nav li ul li:last-child").addClass("last-child");
	
	/* Add class 'before-selected' to the item before the last of the tab */
	$("div.tabs ul.nav li a.selected").parent().prev().addClass("before-selected");

	
	/* =TABLE
	********************************************************/
	/* Add class last child to the last th of data table */
	$("table.data tr th:last-child").addClass("last-child");

	
	/* =GLOBAL TOOLS (ON HEADER)
	********************************************************/
	/*Adjust the width of the #tools div*/
	$("#tools").css('width', '160px');

	/* Add print preview icon */
	$("#tools ul").prepend('<li class="printer-friendly"><a href="#" title="Print-friendly preview"><span class="pop-up"><span>Printer-friendly preview</span></span></a></li>');

	/* Initialise email-to-friend lightbox */
	$("#tools ul li.email").colorbox({width:"580px", height:"430px", iframe:true, href:"/email-to-a-friend?SQ_DESIGN_NAME=blank_design"});
	
	/* BOOKMARK TOOL*/		
	//Add bookmark icon
	$("#tools ul").prepend('<li class="bookmark"><span class="pop-up" title="Bookmark this page"><span>Bookmark this page</span></span></li>'); 
	//Initialise bookmark jquery plugin
	$("#tools ul li.bookmark").jBrowserBookmark();	

	
	/* =BANNER CAROUSEL (RIGHT HAND COLUMN)
	********************************************************/
	//Add css to enable carousel only when javascript is enabled
	$(".banner-container img").css({'display' : 'none', 'position' : 'absolute', 'top' : '0', 'left' : '0'}); 
	
	var abc = $(".banner-container img").length;
	if (abc < 2) {
		//Hide the navigation items when the banner is only 1
		$("div.banner2 .nav").css({'display' : 'none'}); 
	}
	else {
		//Add the navigation items when the banner are more than 1
		$("div.banner2 .nav").css({'display' : 'inline-block'}); 
	}
	
	//Initialise carousel
	$(".slidetabs").tabs(".banner-container img", { 
		effect: 'fade',
		fadeOutSpeed: "slow",
		rotate: true
	}).slideshow({
		autoplay: true,
		interval: 3000
	});
	
	
	/* =JOB VACANCIES SHOW-HIDE (HOME PAGE)
	********************************************************/	
	$("div.job-vacancies ul").hide();
	$("div.job-vacancies div.job h2").click(function() {
		var checkElement = $(this).parent().next();
		
		if((checkElement.is('ul')) && (checkElement.is(':visible'))) {  //The menu item clicked has a expanded sub-menu underneath
			checkElement.slideUp('normal');  //Slide up the expanded sub-menu
			$(this).parent().toggleClass('opened'); //Toggle class collapsible to change the orientation of the arrow on the right
			return false;
		}
		
		if((checkElement.is('ul')) && (!checkElement.is(':visible')) && ($('li', checkElement).length)) {  //The menu item clicked has a hidden sub-menu underneath that is not empty
			checkElement.slideDown('normal');  //Slide down the hidden submenu
			$(this).parent().toggleClass('opened'); //Toggle class collapsible to change the orientation of the arrow on the right
			return false;
		}
	});
	
	
	/* =SITEMAP SHOW-HIDE
	********************************************************/	
	$("div#accordion div.wrapper").removeClass('expanded');
	$("div#accordion div.wrapper div.content").hide();
	$("div#accordion div.wrapper div.expander").click(function() {
		var checkElement = $(this).next();
		
		if((checkElement.is('div#accordion div.content')) && (checkElement.is(':visible'))) {  //The menu item clicked has a expanded sub-menu underneath
			checkElement.slideUp('fast');
			checkElement.queue(function () {
				$(this).parent().removeClass('expanded'); 
				$(this).dequeue();
			});
			return false;
		}
		
		if((checkElement.is('div#accordion div.content')) && (!checkElement.is(':visible')) && ($('ul', checkElement).length)) {  //The menu item clicked has a hidden sub-menu underneath that is not empty
			checkElement.slideDown('fast');
			checkElement.queue(function () {
				$(this).parent().addClass('expanded');
				$(this).dequeue();
			});
			return false;
		}
	});

	
	/* = FEEDBACKS LISTING
	********************************************************/	
	/*Limit the length of comments displayed on the feedback listing to 55 characters max*/
	$("table#feedback-listing td.comments").each(function() {
		var totalchar = $(this).text();
		if (totalchar.length > 50) {
			totalchar = totalchar.substring(0,50);
			$(this).text(totalchar);
			$(this).append(" ...");
		}
	});

	/*Automatically select ascending / descending order for sorting by Date or Page Name/URL*/
	$("span#sort-order").addClass("hidden");
	$("span#sort-by select").change(function () {		
		var sortby = $('span#sort-by select').val();
		if (sortby == '0') {
			if ($('span#sort-order select option[value="1"]').length != 0) {
				$('span#sort-order select option[value="0"]').remove();
			}
			else {
				$('span#sort-order select').append('<option value="1" selected="selected">Descending</option>');
				$('span#sort-order select option[value="0"]').remove();
			}
		}
		else {
			if ($('span#sort-order select option[value="0"]').length != 0) {
				$('span#sort-order select option[value="1"]').remove();
			}
			else {
				$('span#sort-order select').append('<option value="0" selected="selected">Ascending</option>');
				$('span#sort-order select option[value="1"]').remove();
			}
		}
	});
	
	
	/* = PAGINATION
	********************************************************/	
	/*Add the pagination nav container*/
	$('<div class="pagination-nav"></div>').insertAfter('div.pagination-items, table#feedback-listing');
	
	/*Initialize pagination*/
	$('div.news-pagination').pajinate({
		num_page_links_to_display : 3,
		items_per_page : 10	
	});
	
	$('div.feedback-pagination').pajinate({
		num_page_links_to_display : 5,
		items_per_page : 10	
	});
	
	$('div.search-results-pagination').pajinate({
		num_page_links_to_display : 3,
		items_per_page : 10	
	});
	
	$('div.job-vacancies-pagination').pajinate({
		num_page_links_to_display : 3,
		items_per_page : 10
	});	
	
	$('div.library-pagination').pajinate({
		num_page_links_to_display : 3,
		items_per_page : 10
	});	
	
	$('div.events-pagination').pajinate({
		num_page_links_to_display : 3,
		items_per_page : 10
	});	
	
	/*Remove pagination if there is only one page*/
	if ($('div.pagination-nav a.active_page').next().attr("class") == "ellipse more" && $('div.pagination-nav a.active_page').prev().attr("class") == "ellipse less") {
		$('div.pagination-nav').remove();
	}
	
	
	/* = SEARCH PAGE
	********************************************************/	
	/*Add class to search submit button*/
	$("div#search input[type='submit']").addClass("button");
	
	
	/* = EVENTS LISTING & DETAILS PAGE
	********************************************************/	
	//To erase "(all day)" text if the event only has starting date
	$('div.events-pagination span.date,  li.event-details-date span.data').each(function() {
		var eraseAllDay = $(this).text().replace('\(all day\)', '');
		$(this).text(eraseAllDay);
	});
	
	//To add clear:left css to the 1st, 3rd li.half
	$('div.section').each(function() {
		$(this).find('ul li.half:eq(0), ul li.half:eq(2), ul li.half:eq(4), ul li.half:eq(6)').css('clear', 'left');
	});

	/* = OPEN EXTERNAL LINK ON NEW WINDOW
	********************************************************/	
	$(".main-content a[href*='http://']:not([href*='"+location.hostname+"']),[href*='https://']:not([href*='"+location.hostname+"'])").not(":has('img')").attr("target","_blank");
	
});
