$(document).ready( function() {
/* Browser testing - Not Ideal, but necessary */
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
		var ieversion=new Number(RegExp.$1); // capture x.x portion and store as a number
		$('body').addClass('ie');
		if (ieversion>=8) {$('body').addClass('ie8');} else if (ieversion>=7) {$('body').addClass('ie7');} else if (ieversion>=6) {$('body').addClass('ie6');}
	}

/* Controlling the Primary Navigation Dropdown menus */	
	$('nav.second_nav ul li:last-child').addClass('last-child');
	$('.donateLink').click( function(event) {
		event.preventDefault();
		$('#sidebarDonate').submit();
	});
	
	$('#primary_nav li.level1').hover(
		function () {
			$(this).find('ul.level2, li.level2').show();
		},
		function () {
			$(this).children('ul.level2, li.level2').hide();
		}
	);
	
	$('#primary_nav li.level2').hover(
		function () {
			var popOut = $(this);
			var hiddenLI = $(this).find('ul.level3');
			popOut.toggleClass('navHover');
			hiddenLI
					.show()
					.find('li.level3')
						.show()
						.end()
					.css({
						'top'	:	function() { 
										var height = 0;
										popOut.prevAll().each(function() {
											height += $(this).height();
											height += 6;
										});
										if ($('body').hasClass('ie') && !$('body').hasClass('ie8') && (popOut.prevAll().length > 0)) { 
											height += 6;
										}
										return height;
									},
						'left'	:	$(this).width()
					});
		},
		function () {
			$(this).toggleClass('navHover').children('ul.level3, li.level3').hide();
		}
	);
	$('#primary_nav li.level3').hover(
		function () {
			var popOut = $(this);
			var hiddenLI = $(this).find('ul.level4');
			popOut.toggleClass('navHover');
			hiddenLI
					.show()
					.find('li.level4')
						.show()
						.end()
					.css({
						'top'	:	function() { 
										var height = 0;
										popOut.prevAll().each(function() {
											height += $(this).height();
											height += 6;
										});
										if ($('body').hasClass('ie') && !$('body').hasClass('ie8') && (popOut.prevAll().length > 0)) { height += 6;}
										return height;
									},
						'left'	:	$(this).width()
					});
			
		},
		function () {
			$(this).toggleClass('navHover').children('ul.level4, li.level4').hide();
		}
	);

/* Layout Fixes - Ideally not supposed to use JS for this, but in a hurry */	
	$('.ie6 #content, .ie7 #content').css('padding-left', 185);
	$('.ie6#homepage #content, .ie7#homepage #content').css('padding-left', 165);
	$('#main, #sidebar, #content').css('height', 'auto');
		
	if ( $('#sidebar>.wrap').height() > $('#content').height() )
		$('#sidebar').css('position', 'static');

/* Layer popup for Adult Classes page */
	$('#adult_classes #workshops .shopTitle a').click(function() {
		var current = $(this).parent().siblings('.workshopInfo');
		if (current.length > 0) {event.preventDefault();}
		if (current.hasClass('active')) {current.hide().removeClass('active');} else {
			$('#adult_classes #workshops .workshopInfo').removeClass('active').hide();
			current.show().addClass('active');
		}
	});
	$('#adult_classes #workshops .workshopInfo span').click(function() {$('#adult_classes #workshops .shopTitle a').click();});

/* Initiate lightbox for Photo Gallery pictures */
	$('a[rel^="prettyPhoto"]').prettyPhoto({ default_width: 680, default_height: 680 });

/* Slideshow function for homepage */
	(function($) {
		$.fn.alcSlideShow = function(options) {
			var defaults = 	{
								delay		: 5,
								fadeTime	: 3,
								slideNum	: 0 	/* The number slide to start on */
							};
			options = $.extend(defaults, options);
			
			var slides = $(this).find('.slides');
			var numSlides = slides.length;
			slides.not(':first').hide();
				
			setTimeout( function() {changeSlide();}, options.delay * 1000 )
			
			function changeSlide () {	
				slides
					.eq( options.slideNum )
					.fadeOut( options.fadeTime * 1000, function() { 
						setTimeout( function() {changeSlide();}, options.delay * 1000 );
					 });
				if ( options.slideNum == ( numSlides - 1 ) ) {
					options.slideNum = 0;
				} else {options.slideNum++;}
				slides.eq( options.slideNum ).fadeIn( options.fadeTime * 1000 );
			}	
		};
	})(jQuery);
	
	$('#slideshow').alcSlideShow();
	
});
