(function($) {

	/***** 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;
		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 || ( $.inArray(href, rootFiles)>-1 && $.inArray(loc, rootFiles)>-1 ) ) {
				$el.addClass(className);
				$(this).parents().children('a').addClass(className);
				return false;
			}
		});
	};

	/***** 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: 8,
			autoArrows: false,
			dropShadows: false
		});
		
		// find selected elements and add "selected" class
		navChainSelect($navEl, activeClass);
	};
	

	// Show/Hide UL elements
	initShowHide = function() {
		var $el = $('ul.showhide');
		$el.find('ul').hide();
		$('a',$el).click(function() {
			//$('ul', this).slideToggle();
			$(this).parent().find('ul').slideToggle();
			return false;
		});
	};
	
	initGallery = function() {
		// Initialize Galleriffic Gallery
		if(jQuery().galleriffic) {
		
			$('#galleryimage').css('background-image','none');
			$('#gallery #thumbs').galleriffic({
				numThumbs: 100,
				preloadAhead: 5,
				imageContainerSel: '#slideshow',
				controlsContainerSel: '#controls',
				loadingContainerSel: '#loading',
				enableTopPager: false,
				enableBottomPager: false,
				syncTransitions: true
			});
			
			// Scroll gallery
			$('#gallery #thumbs a').click(function(e) {
				$('html, body').animate({ scrollTop: 0 }, 500);
			});
		
		}
	};

	// jQuery document ready
	$(document).ready(function(){
		initNav();
		initShowHide();
		initGallery(); 
	});
	
})(jQuery);

