$(document).ready(function() {
	navHover ();
	quoteRotator ();
	slideshow ();
	galleryNoAjax ();
	gallery ();
	imageSwitch ();
	expand ();
	mainNav ();	

});

function navHover () {   		
	$('#navMain li').hover(
		function () {  				   		   
			if (!$(this).hasClass('active')) { 						
				$(this).addClass('open').children('a:first').next('ul').slideDown(500);		
				$(this).prev('li.slide').slideDown(600);			
			};									
		},
		function () { 
			if (!$(this).hasClass('active')) { 		  
				$(this).removeClass('open').children('a:first').next('ul').slideUp(170);	
				$(this).prev('li.slide').slideUp(200);				
			};
					
	});  		 		
}   

//rotate the quotes
function quoteRotator () {		
	
		//recursive call
		$(window).load(function () {
			var targ = 'ul.quoteRotate li.active';
			
			fadeOut(targ);
			
		});
				
		//each function
		function fadeOut (targ) {
			$(targ).fadeOut(700, function (){
						
				var x = $(this).next('li');
									
				if(!x.length == 0) {
					targ = x;
				} else {
					targ = 'ul.quoteRotate li.active';
				}
												
				fadeIn (targ);
			});
		
		}
		
		function fadeIn (targ) {
			$(targ).animate({"opacity": "show"}, 400, function () {
				//create delay before pic change in ms eg 3000 = 3 secs
				$(targ).animate({opacity: 1.0}, 5000);
				//fade out image and replace
				fadeOut(targ);
			});
		}

}

function slideshow () {		
	
		//recursive call
		$(window).load(function () {
			var targ = 'ul.slideshow li.active';
			
			fadeOut(targ);
			
		});
				
		//each function
		function fadeOut (targ) {
			$(targ).fadeOut(900, function (){
						
				var x = $(this).next('li');
									
				if(!x.length == 0) {
					targ = x;
				} else {
					targ = 'ul.slideshow li.active';
				}
												
				fadeIn (targ);
			});
		
		}
		
		function fadeIn (targ) {
			$(targ).animate({"opacity": "show"}, 300, function () {
				//create delay before pic change in ms eg 3000 = 3 secs
				$(targ).animate({opacity: 1.0}, 3000);
				//fade out image and replace
				fadeOut(targ);
			});
		}

}

function galleryNoAjax () {		
	//what to do on thumb click	
	$('ul.thumbs li.noAjax a').click(function() {
		
		var imgSelect = $(this).attr('id');
						
		// thumbnail display	
		$('ul.thumbs li a').removeClass('active');
		$(this).addClass('active');
						
		//set img alt tag		
		var altName = $(this).children('img').attr('alt');						
		imgName = altName.replace(/_/g, " ").replace(/-/g, "/");
		
		//show hide/ fade the images
		$('ul.main li.active').removeClass('active').fadeOut(300, function(){			
			$('ul.main li#' + imgSelect).fadeIn(500).addClass('active');
		});		
						  
		return false;
	});

}

function gallery () {		
	//what to do on thumb click	
	$('ul.thumbs li.ajax a').click(function() {
		var ajaxLink = $(this).attr('href');

		ajaxGallery (ajaxLink);
			
			
		$('ul.thumbs li a').removeClass('active');
		$(this).addClass('active');
				
		var altName = $(this).children('img').attr('alt');						
		imgName = altName.replace(/_/g, " ").replace(/-/g, "/");
		
		$('h2.thumbName').replaceWith('<h2 class="thumbName"> </h2>');
		
		$('h2.thumbName').prepend(imgName);
		
		//people gallery textManagement		
		$('#people #text div').removeClass('active').addClass('hide');					
		$('#'+altName).addClass('active').removeClass('hide');			
		
				  
		return false;
	});

}
//Ajax load new gallery page
function ajaxGallery (ajaxLink) {
	$('#contentImage').load(ajaxLink);	
}

function imageSwitch () {
	$('ul.imgList a').live('click',function () {
		$(this).parent().addClass('active').siblings().removeClass('active');
					
		var imgSrc = $(this).attr('href');
		var imgName = $(this).children('img').attr('alt');
		$('#contentImage img').attr('src', imgSrc);

					
		return false;
	});	
}

//Expander for dashboard
function expand(){
	
	// expand	
	$('a.btnExpander').click(function(){	
		

		if ($(this).next('.expander').hasClass('closed')){			

			var targ = $(this).next('.expander');			
			opener(targ);			
				
		} else {	
			var targ = $(this).next('.expander');
			closer(targ);
			
						
	}
	return false;
	
	//function for this action
		function opener(targ){
			$(targ).addClass('open').removeClass('closed')						
				.animate({
				width: "300px",
				height: "330px",											
				}, 400, function() {
					$('#question').focus();	
				});
		
		}
				
		function closer(targ) {
			$(targ).removeClass('open').addClass('closed')			
				.animate({	
				width: "0px",
				height: "0px",				
								
				}, 150);
		
		}
	//end special functions
		
	});
			
}

// navigation function
function mainNav () {
	$('#mainNav li a').click(function(){
		//get href value of nav clicked
																				
		//add remove class for active states
		$('#mainNav li a').removeClass('selected');
		$(this).addClass('selected').parent().siblings();	
		//return false;
	});
	
	$('.text').attr({scrollTop:0,scrollLeft:0});
	$('#images').attr({scrollTop:0,scrollLeft:0});
	
	// Scroll initially if there's a hash (#something) in the url
	$('#mainNav').localScroll({
		target: '.text', //could be a selector or a jQuery object too.
		axis:'y',//the default is 'y'
		queue:true,		
		duration:1300
	}).localScroll({
		target: '#images', //could be a selector or a jQuery object too.
		axis:'y',//the default is 'y'
		queue:true,
		duration:1000
	});	
}
