// jQuery document ready
(function($) {

	/***** Drop down menu *****/

	initNav = function() {

		

		

		var $navEl = $("ul#nav");

		var activeClass = "selected";

		// add first and last classes

		$navEl.find("ul").each(function(i) {

			var ct = $(this).children().size();

			if (ct == 1) {

				$("li", this).addClass("single");

			} else {

				$("li:first-child", this).addClass("first");

				$("li:last-child", this).addClass("last");

			}

		});

		// animate menu

		$navEl.superfish({

			animation : {height:"show"},

			speed : 300,

			alpha: 2,

			autoArrows: false,

			dropShadows: false

		});

		// find selected elements and add "selected" class

		navChainSelect($navEl, activeClass);

	}

	/***** navChainSelect *****/
	// adds a "select" class to the current selected page element
	// matches the url to the link within the nav
	
	navChainSelect = function(navEl, className) {
		var rootFiles = ['', 'index.htm', 'index.html', 'index.php', 'default.asp', 'default.aspx'];
		var loc = document.location.pathname+document.location.search;
		loc = loc.replace("intranet/","");
		if (loc.indexOf("/") == 0) loc = loc.substring(1);
		$('li', navEl).each(function(i) {
			var $el = $('>a', this);
			var href = $el.attr('href');
			if (href == loc || href == document.location.href || ( $.inArray(href, rootFiles)>-1 && $.inArray(loc, rootFiles)>-1 ) ) {
				$el.addClass(className);
				$(this).parents().children('a').addClass(className);
				return false;
			}
		});
	}

	/***** Expandables *****/

	initExpandables = function() {

		heightArray = new Array();

		$(".expandables .body").each(function(i) {

		  theHeight = $(this).height();

		  heightArray[i] = theHeight;

		});

		//hide the all of the element with class body

		$(".expandables .body").hide();

		//toggle the component with class body

		$(".expandables .head")

			//.append('<span class="expand-link">Click to expand</span>')

			.css({cursor: "pointer"})

			.click(function(e){

				if ($(".expandables").hasClass('toggle')) {

					$(".expandables .body").stop().hide();

				}

				var $el = $(this).next(".body");

				$el.css({height: heightArray[$(".head").index(this)]});

				$el.slideToggle(400);

				return false;

			}

		).find('.expand-link').text('Click to expand');

	}

		

	

	/***** Accordion *****/

	initAccordion = function() {

		// Get content height for smoother animation

		heightArray = new Array();

		$('.accordion .body').each(function(i) {

		  theHeight = $(this).height();

		  heightArray[i] = theHeight;

		});

		// accordion action

		$('.accordion .head')

			//.append('<span class="expand-link">Click to expand</span>')

			.css({cursor: "pointer"})

			.click(function() {

				$('.accordion .head').removeClass('selected');

				$('.accordion .body').slideUp('normal');

				if ($(this).next().is(':hidden') == true) {

					$(this).next('.body').css({height: heightArray[$('.head').index(this)]});

					$(this).addClass('selected');

					$(this).next().slideDown('normal');

				} 

		}).find('.expand-link').text('Click to expand');

		//hide the all of the element with class body

		$(".accordion .body").hide();

	}

	

	

	$(document).ready(function(){
		initNav();
		initExpandables();
		initAccordion();

		// forgotten details
		$("#reminder").click(function(event) {
			event.preventDefault();
			fnForgotform(false);
		});
		function fnForgotform(boSubmit) {
			obData = (boSubmit)?$("#fmforgot").serialize():{};
			$.post("lib/php/forgot.php",obData,function(sResult){
				aResult = sResult.split("<_tbx_>");
				$("#loginpanel").html(aResult[0]);
				if ($.trim(aResult[1])!="") {
					// display error
					$("#loginerror").html("<div class=\"alert\">"+aResult[1]+"</div>");
				}
				$("#getdetails").click(function(event) {
					event.preventDefault();
					fnForgotform(true);
				});
			});
		};
	});

	

})(jQuery);


