$(document).ready(function(){

	function imageSlide(elem, intdelay, eq){
    		
    		var slideSpeed = 2000;
    		
    		var slideDelay = 4000;
    		
    		var imgEase = 'swing';
    		
    		var pdelay = slideDelay/2 - 2000;
    		
    		var phidedelay = pdelay + 1500;
    		
    		var eLength = $(elem).size() - 1;
    		
    		var imgSize = 566;

			var Eq = eq;
			
			$('body').attr('eLength', eLength);
			
			$('body').attr('eq', Eq)
			
    		$(elem).eq(Eq).delay(intdelay).animate({width: imgSize, left: 0}, slideSpeed, imgEase, function(){
    		    
    		    $(this).children().css('float', 'right');
    		        		    		
    			$(this).delay(slideDelay).animate({width: 0, left: 0}, slideSpeed, imgEase, function(){
    		    	
    		    	$(this).children().css('float', 'left');
    		    	    		    	    		    	
    				$(this).css({left: imgSize});
    				
    				$(this).clearQueue();
    		
    			});
    			
    			if( Eq == eLength ) {
    			
    				var eQ = 0;
    			
    			} else {
    			
    				var eQ = Eq+1;
    			 
    			}
    							
    			imageSlide(elem, slideDelay, eQ);			    		
    	    		
    		});
    		    		
    	}

		imageSlide('.slide', 0, 0);
		
		var imgEase = 'swing';
		
		$('ul#main-nav li').click(function(){
				
			var href = $(this).find('a').attr('href');
				
			window.location.href = href;
				
		});
		
		var nggImgWidth = $('.ngg-gallery-thumbnail-box img').width();
		
		$('.ngg-gallery-thumbnail-box, .ngg-gallery-thumbnail').css({width: nggImgWidth});
		
		var nggWidth = $('.ngg-gallery-thumbnail-box').outerWidth()+20;
		
		var nggAmount = $('.ngg-gallery-thumbnail-box').size();
		
		var spanWidth = nggWidth*nggAmount;
		
		$('div#thumbnails div#thumbCtnr').css({width: spanWidth});
		
		$('.slideshowlink').remove();
		
		$('span#next').mousedown(function(){
		
			$('body').data('mouse', 'down');
		
			function slideLeft(){
				
				$('div#thumbCtnr').animate({left: '-=40'}, 40, imgEase, function(){
				
					if($('body').data('mouse') == 'down' && $('div#thumbCtnr').position().left >= -spanWidth+700) {
				
						slideLeft();
						
					}
									
				});
				
			}
			
			if($('body').data('mouse') == 'down' && $('div#thumbCtnr').position().left >= -spanWidth+700) {
				
				slideLeft();
				
			}
		
		}).mouseup(function(){
		
			$('body').data('mouse', 'up');
		
		});
		
		$('span#prev').mousedown(function(){
		
			$('body').data('mouse', 'down');
		
			function slideRight(){
			
				$('div#thumbCtnr').animate({left: '+=40'}, 40, imgEase, function(){
				
					if($('body').data('mouse') == 'down' && $('div#thumbCtnr').position().left <= 0) {
				
						slideRight();
						
					}
				
				});
				
			}
			
			if($('body').data('mouse') == 'down' && $('div#thumbCtnr').position().left <= 0 ) {
				
				slideRight();
				
			}
		
		}).mouseup(function(){
		
			$('body').data('mouse', 'up');
		
		});
		
		
		setTimeout(function(){
		
			$('.ngg-gallery-thumbnail-box .ngg-gallery-thumbnail a').eq(0).trigger('click');
		
		}, 300)
	
		var tSize = $('.ngg-gallery-thumbnail-box').size();

		$('.ngg-gallery-thumbnail-box .ngg-gallery-thumbnail a').each(function(index){
		
			$(this).bind('click' ,function(){
						
				$('.ngg-gallery-thumbnail img').removeClass('current');		
				
				var startIndex = index;
				
				$('.ngg-gallery-thumbnail img').eq(startIndex).addClass('current');
				
				$('body').data('index', startIndex);
			
				var img = $(this).attr('href');
				
				var title = '';
					
				var title = $(this).find('img').attr('title');
				
				if(title == ''){
					
				} else {
											
						$('p#caption').remove();
						
						$('#largeCtnr').after('<p id="caption">' + title + '</p>');
					
					}
				
				$('#largeCtnr').children().remove();
				
				$('#largeCtnr').append('<img src="' + img + '" alt="" />');
			
				return false;
				
			});
		
		});		
		
		$('span#nextImg').click(function(){
			
			$('body').data('fadein', false);
			
			var tSize = $('.ngg-gallery-thumbnail-box').size()-1;
			
			$('.ngg-gallery-thumbnail img').removeClass('current');	
			
			var startIndex = $('body').data('index');
						
			if(startIndex == tSize){
			
				var startIndex = -1;
			
			}
			
			$('.ngg-gallery-thumbnail img').eq(startIndex+1).addClass('current');
			
			var nextImg = startIndex+1;
			
			var prevImg = startIndex-1;
			
			$('body').data('index', nextImg);
				
				var img = null;
			
				var img = $('.ngg-gallery-thumbnail-box').eq(nextImg).find('a').attr('href');
				
				var title = '';
				
				var title = $('.ngg-gallery-thumbnail-box').eq(nextImg).find('a').find('img').attr('title');
				
				if(title == ''){
					
				} else {
						
						
						$('p#caption').remove();
						
						$('#largeCtnr').after('<p id="caption">' + title + '</p>');
					
					}
				
				$('#largeCtnr').children().remove();
				
				$('#largeCtnr').append('<img src="' + img + '" alt="" />');
			
				return false;
		
		});
		
		$('span#prevImg').click(function(){
			
			$('body').data('fadein', false);
				
				var tSize = $('.ngg-gallery-thumbnail-box').size();
				
				$('.ngg-gallery-thumbnail img').removeClass('current');	
				
				var startIndex = $('body').data('index');
				
				if(startIndex == 0){
				
					var startIndex = tSize;
				
				}
								
				var nextImg = startIndex+1;
				
				var prevImg = startIndex-1;
									
				var title = '';
				
				var title = $('.ngg-gallery-thumbnail-box').eq(prevImg).find('a').find('img').attr('title');
				
				if(title == ''){
					
				} else {
						
						$('p#caption').remove();
						
						$('#largeCtnr').after('<p id="caption">' + title + '</p>');
					
					}
				
				$('.ngg-gallery-thumbnail img').eq(prevImg).addClass('current');
				
				$('body').data('index', prevImg);
					
					var img = null;
				
					var img = $('.ngg-gallery-thumbnail-box').eq(prevImg).find('a').attr('href');
					
					$('#largeCtnr').children().remove();
					
					$('#largeCtnr').append('<img src="' + img + '" alt="" />');
																			
				return false;
		
		});
		
		$('.ngg-gallery-thumbnail a').each(function(){
		
			$(this).hover(function(){
		
				$(this).css('position', 'relative').parent().parent().css({zIndex: 999, position: 'relative'}).stop(true, true).animate({width: 180, height: 145}, 1000, imgEase);
			
				$(this).find('img').css('position', 'relative').stop(true, true).animate({marginLeft:-20, width: '150%', height: '150%'}, 1000, imgEase);
			
			}, function(){
				
				$(this).parent().parent().css('z-index', '1').stop(true).animate({width: 120, height: 95}, 1000, imgEase);
				
				$(this).find('img').stop(true).animate({marginLeft:5, width: 100, height: 75}, 1000, imgEase);
			
			});	
		
		});
		
		$('span#nextImg').hover(function(){
		
			$(this).stop(true, true).animate({right: '-=6px'}, 500, imgEase);
		
			$(this).find('span').stop(true, true).animate({opacity: 1}, 500, imgEase);
		
		}, function(){
		
			$(this).stop(true).animate({right: 30}, 500, imgEase);
		
			$(this).find('span').stop(true).animate({opacity: 0}, 500, imgEase);
		
		});
		
		$('span#prevImg').hover(function(){
		
			$(this).stop(true, true).animate({right: '+=6px'}, 500, imgEase);
		
			$(this).find('span').stop(true, true).animate({opacity: 1}, 500, imgEase);
		
		}, function(){
		
			$(this).stop(true).animate({right: 70}, 500, imgEase);
		
			$(this).find('span').stop(true).animate({opacity: 0}, 500, imgEase);
		
		});
		
		$('#newsCtnr a.newsEventLink').hover(function(){
		
			$(this).find('div.articleCtnr h3 span').stop(true, true).animate({right: 20}, 500, imgEase);
		
		}, function(){
		
			$(this).find('div.articleCtnr h3 span').stop(true).animate({right: -65}, 500, imgEase);
		
		});
		
		$('#eventCtnr a.newsEventLink').hover(function(){
		
			$(this).find('div.articleCtnr h3 span').stop(true, true).animate({right: 20}, 500, imgEase);
		
		}, function(){
		
			$(this).find('div.articleCtnr h3 span').stop(true).animate({right: -65}, 500, imgEase);
		
		});

		
		function newsClick(){
		
			$('span.news').parent().click(function(){
			
				var href = $(this).attr('href');
				
				var hrefTrim = href.slice(0,-1);
				
				var h = hrefTrim + ' #newsCtnr';
							
				$('#newsCtnr').load(h, function(){
				
					$('#newsCtnr #newsCtnr').children().unwrap();
				
					$('#newsCtnr a.newsEventLink').hover(function(){
			
						$(this).find('div.articleCtnr h3 span').stop(true, true).animate({right: 20}, 500, imgEase);
			
					}, function(){
			
						$(this).find('div.articleCtnr h3 span').stop(true).animate({right: -65}, 500, imgEase);
			
					});
				
					newsClick();
					
					Cufon.replace('h2', {
	
						hover: true
	
					});
				
				});
			
				return false;
			
			});
		
		}
		
		newsClick();
				
		function eventsClick(){
		
			$('span.events').parent().click(function(){
		
				var href = $(this).attr('href');
			
				var hrefTrim = href.slice(0,-1);
				
				var h = hrefTrim + ' #eventCtnr';
		
				$('#eventCtnr').load(h, function(){
			
					$('#eventCtnr a.newsEventLink').hover(function(){
		
						$(this).find('div.articleCtnr h3 span').stop(true, true).animate({right: 20}, 500, imgEase);
		
					}, function(){
		
						$(this).find('div.articleCtnr h3 span').stop(true).animate({right: -65}, 500, imgEase);
		
					});
			
					eventsClick();
			
					Cufon.replace('h2', {
	
						hover: true
	
					});
			
				});
		
				return false;
		
			});	
		
		}

		eventsClick();
		
		$('ul#social li').hover(function(){
		
			$(this).stop(true, true).animate({opacity: 1}, 500, imgEase).find('a img').stop(true, true).animate({width: '+=3', height: '+=3', marginTop: '-=1.5', marginLeft: '-=1.5', marginRight: '-=1.5'}, 500, imgEase);
		
		}, function(){
		
			$(this).stop(true).animate({opacity: .7}, 500, imgEase).find('a img').stop(true).animate({width: 30, height: 30, marginTop: 0, marginLeft: 0, marginRight: 0}, 500, imgEase);
		
		});
		
		$('#span#fRight').css({opacity: .1});

		//Adds class to body based on browser being used
		var userAgent = navigator.userAgent.toLowerCase();
		$.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase());
		// Is this a version of IE?
		if($.browser.msie){
		$('body').addClass('IE');
		// Add the version number
		$('body').addClass('IE' + $.browser.version.substring(0,1));
		
		}
		// Is this a version of Chrome?
		if($.browser.chrome){
		$('body').addClass('Chrome');
		//Add the version number
		userAgent = userAgent.substring(userAgent.indexOf('chrome/') +7);
		userAgent = userAgent.substring(0,1);
		$('body').addClass('Chrome' + userAgent);
		// If it is chrome then jQuery thinks it's safari so we have to tell it it isn't
		$.browser.safari = false;
		}
		// Is this a version of Safari?
		if($.browser.safari){
		$('body').addClass('Safari');
		// Add the version number
		userAgent = userAgent.substring(userAgent.indexOf('version/') +8);
		userAgent = userAgent.substring(0,1);
		$('body').addClass('Safari' + userAgent);
		}
		// Is this a version of Mozilla?
		if($.browser.mozilla){
		//Is it Firefox?
		if(navigator.userAgent.toLowerCase().indexOf('firefox') != -1){
		$('body').addClass('Firefox');
		// Add the version number
		userAgent = userAgent.substring(userAgent.indexOf('firefox/') +8);
		userAgent = userAgent.substring(0,1);
		$('body').addClass('Firefox' + userAgent);
		}
		// If not then it must be another Mozilla
		else{
		$('body').addClass('Mozilla');
		}
		}
		// Is this a version of Opera?
		if($.browser.opera){
		$('body').addClass('Opera');
		}

	Cufon.replace('ul li a', {
	
		hover: true
	
	});
	
	Cufon.replace('div#mapLink a');
	
	Cufon.replace('h2', {
	
		hover: true
	
	});
 
});


//END Document
