$(document).ready(function() {
	navHover ();
	
	galleryNoAjax ();
	gallery ();
	imageSwitch ();
});

function navHover () { 	
	
	$('#navMain li').hover(	
		
		function () { 									   		   
			if (!$(this).hasClass('active')) { 						
				$(this).addClass('open').children('a:first').next('ul').slideDown(500);
				var header = $(this).prev('li.slide').attr('id');				
				if (header) {					
					var temp = '#' + header.substring('4');		
					/* console.log(temp); */			
					$(temp).addClass('active');	
				}		
				$(this).prev('li.slide').slideDown(600);											
			};	
											
		},
		function () { 
				
			if (!$(this).hasClass('active')) { 		  
				$(this).removeClass('open').children('a:first').next('ul').slideUp(170);
				var header = $(this).prev('li.slide').attr('id');				
				if (header) {					
					var temp = '#' + header.substring('4');					
					$(temp).removeClass('active');	
				}	
				$(this).prev('li.slide').slideUp(200);	
				
				
			};	
							
	});
	 
			
}   

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

}
*/

function slideshow() {
    var $active = $('.slideshow li.active');

    if ( $active.length == 0 ) $active = $('.slideshow li:last');

    var $next =  $active.next().length ? $active.next()
        : $('.slideshow li:first');

    $active.addClass('lastActive');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1500, function() {
            $active.removeClass('active lastActive');
        });
}

$(function() {
    setInterval( "slideshow()", 5000 );
});


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');

		var fileType = imgSrc.substr(imgSrc.lastIndexOf('.') + 1);
		//if not jpg, then
		if(fileType != 'jpg'){				
			//show video wrapper
			if($('#videoWrap').hasClass('hide')){
				$('#videoWrap').removeClass('hide');
			}
			if($('#contentImage img')){
				$('#contentImage img').addClass('hide');
			}						
			//ajax video code into page	
			$('#videoWrap').load(imgSrc);
			
			
		} else {
			//hide video wrapper		
			if(!$('#videoWrap').hasClass('hide')){
				$('#videoWrap').addClass('hide');
				if($('#videoWrap embed')){
					$('#videoWrap embed').remove();
				}
				if($('#videoWrap object')){
					$('#videoWrap object').remove();
				}

				
			}
			//show image
			if($('#contentImage img').hasClass('hide')){
				$('#contentImage img').removeClass('hide');
			}
			
			$('#contentImage img').attr('src', imgSrc);
		}
		
					
		return false;
	});	
}