var ratio = 1.6;
var currentImage = 1;

var midHeight;
var bottomOffset;
var galModeTopVar;
var extNavGallerySelect;
var galleryMode = false;
// var descVisible = true;
var fillMode = 'fill';

var fadeDuration = 750;

function adjustSlide() {

	var hVar = $(window).height() - 12;
	var wVar = $(window).width() - 12;
	
	hVar = $(window).height();
	wVar = $(window).width();
	
	$('body').css('overflow','auto');
	
	if(hVar >= 600) {
		$('body').css('overflow','hidden');
	}

	$('#bigDiv').css({
		'height' : hVar + 'px',
		'width' : wVar + 'px'
	});
	
	hVarImg = $('#bigDiv').height();
	wVarImg = $('#bigDiv').width();
	
	if(fillMode == 'fill') {
	
		if( $('#bigDiv > img:nth-child(' + currentImage + ')').width() > $('#bigDiv > img:nth-child(' + currentImage + ')').height() ) {
			$('#bigDiv > img:nth-child(' + currentImage + ')').css({
				'height' : hVarImg + 'px',
				'width' : 'auto'
			});
			if($('#bigDiv > img:nth-child(' + currentImage + ')').width() < wVarImg) {
				$('#bigDiv > img:nth-child(' + currentImage + ')').css({
					'width' : wVarImg + 'px',
					'height' : 'auto'
				});
			}
		}
		else if( $('#bigDiv > img:nth-child(' + currentImage + ')').width() <= $('#bigDiv > img:nth-child(' + currentImage + ')').height() ) {
			$('#bigDiv > img:nth-child(' + currentImage + ')').css({
				'width' : wVarImg + 'px',
				'height' : 'auto'
			});
			if($('#bigDiv > img:nth-child(' + currentImage + ')').height() < hVarImg) {
				$('#bigDiv > img:nth-child(' + currentImage + ')').css({
					'height' : hVarImg + 'px',
					'width' : 'auto'
				});
			}
		}
		
	}
	
	else if(fillMode == 'fit') {
	
		if( $('#bigDiv > img:nth-child(' + currentImage + ')').width() > $('#bigDiv > img:nth-child(' + currentImage + ')').height()) {
			
			$('#bigDiv > img:nth-child(' + currentImage + ')').css({
				'height' : hVarImg + 'px',
				'width' : 'auto',
				'min-width' : '900px'
			});	
			
			if( $('#bigDiv > img:nth-child(' + currentImage + ')').width() <= 900 ) {
				$('#bigDiv > img:nth-child(' + currentImage + ')').css({
					'height' : 'auto',
					'width' : '900px'
				});
			}
		}
		
		/* Portrait */
		else if( $('#bigDiv > img:nth-child(' + currentImage + ')').height() >= $('#bigDiv > img:nth-child(' + currentImage + ')').width()) {
			
			if($('#bigDiv > img:nth-child(' + currentImage + ')').height() >= hVar) {
				$('#bigDiv > img:nth-child(' + currentImage + ')').css({
					'width' : '900px',
					'height' : 'auto'
				});
			}
			else {
				$('#bigDiv > img:nth-child(' + currentImage + ')').css({
					'height' : hVarImg + 'px',
					'width' : 'auto'
				});
			}
			
			if( $('#bigDiv > img:nth-child(' + currentImage + ')').width() <= 900 ) {
				$('#bigDiv > img:nth-child(' + currentImage + ')').css({
					'height' : 'auto',
					'width' : '900px'
				});
			}
			
		}
		
	};
	
	$('#bigDiv > img:nth-child(' + currentImage + ')').css({
		'margin-left' : (wVarImg / 2 - $('#bigDiv > img:nth-child(' + currentImage + ')').width() / 2) + 'px',
		'margin-top' : (hVarImg / 2 - $('#bigDiv > img:nth-child(' + currentImage + ')').height() / 2) + 'px'
	});
};

function adjustHeights() {

	midHeight = $(window).height() - $('.top').outerHeight() - $('.bottom').outerHeight() - 110;
	galModeTopVar = midHeight + $('.top').outerHeight() + 50;
	
	$('.middle').css({
		'height' : midHeight + 'px',
		'margin-top' : '30px'
	});
	
	$('.middle #right').css({
		'height' : midHeight + 'px'
	});
	
	$(function()
	{
		$('.flexcroll').jScrollPane(
			{
				showArrows: true,
				verticalGutter: 0,
				verticalDragMinHeight: 20,
				verticalDragMaxHeight: 100
			}
		);
	});
	
	$(function()
	{
		$('.modalContent').jScrollPane(
			{
				showArrows: true,
				verticalGutter: 0,
				verticalDragMinHeight: 20,
				verticalDragMaxHeight: 100
			}
		);
	});
	
	if( $('.middle #right').css('min-height') < $('.middle #right').css('height') ) {
		$('.middle .jspArrowDown').css({
			'margin-top' : midHeight - 24 + 'px'
		});
	}
	
	else if ($('.middle #right').css('min-height') == $('.middle #right').css('height')){
		$('.middle .jspArrowDown').css({
			'margin-top' : 411 + 'px'
		});
	}
	
};

function topnav() {

	$('.bottomNav').hide(); // ADDED RB TO HIDE MENU ON BOTTOM BUILD

	$('ul.mainList li').hover(
		function(){
			// REMOVED SO ALWAYS OPENED
			//if( $(this).children('a').attr('id') != $('body').attr('id') || $(this).children('a').attr('id') == 'homes' ) {

				var testNews = $(this).children('a').attr('id');

				if (testNews == 'news') { 
					$(this).children('ul.contentNav, #newComContainer').slideDown(250);
				} else {
					$(this).children('ul.contentNav').slideDown(250);
				}
			// REMOVED SO ALWAYS OPENED
			//}
		},
		function(){
			$(this).children('ul.contentNav,#newComContainer').hide();
		}
	);

	$('.expGallery').toggle(expGallery1,expGallery2);
	
	$('.galSelect, .proj').unbind('click').toggle(
		function(){
			if(galleryMode == true) {
				$('#galMenu').slideDown(250);
			};
		},
		function() {
			if(galleryMode == true) {
				$('#galMenu').slideUp(250);
			};			
		}
	);
	
	$('#galMenu li').click(function() {
		$('#galMenu li').removeClass('active');
		$(this).addClass('active');
		$('.galSelect').unbind('click').toggle(function(){$('#galMenu').slideDown(250);},function() {$('#galMenu').slideUp(250);});
	});
	
	$('.expcol').toggle(expColFirst,expColSecond);
	
		function expColFirst() {
			hideGalSelect();
			setTimeout(function(){
				$('.bottom').css({
					'overflow' : 'hidden'
				})
				$('.galMode').animate({
					'left' : '800px'
				},500);
			},250);
			$(this).addClass('expcol_active');
		};
		
		function expColSecond() {
			// descVisible = true;
			$('.galMode').animate({
				'left' : '0'
			},500,function(){
				$('.bottom').css({
					'overflow' : 'visible'
				});
			});
			$(this).removeClass('expcol_active');
		};
	
	$('#galNav #galPrev').click(slideBackward);
	$('#galNav #galNext').click(slideForward);
	$('#galNav #galIndex').hover(
		function() {
			$('#galNums').show();
			if( $('#galMenu').is(':visible') ) {
				$('.galSelect').click();
			}
		},
		function() {
			$('#galNums').hide();
		}
	);
	
};

function externalGalLoader(loadGalVar){
	$('.expGallery').unbind('click').toggle(expGallery2,expGallery1);
}

function expGallery1(loadGalVar) {
	galleryMode = true;
	$('ul.mainList').stop(true,true).fadeOut(500);
	$('.middle').stop(true,true).animate({'opacity':'0'},500,function(){
		$('.middle').css('visibility','hidden');
	});
	hideGalSelect();
	if(extNavGallerySelect != null) {
		loadGal(extNavGallerySelect);
		extNavGallerySelect = null;
	};
	if(currentGallery == 'home') {
		loadGal('home','all');
	}
	else if(currentGallery == 'cvv') {
		loadGal('cvv','all');
	}
	else if(currentGallery == 'oow') {
		loadGal('oow','all');
	}
	else if(currentGallery == 'occ') {
		loadGal('occ','all');
	}
	else if(currentGallery == 'osv') {
		loadGal('osv','all');
	}
	else if(currentGallery == 'dch') {
		loadGal('dch','all');
	}
};

function expGallery2() {
	galleryMode = false;
	$('.middle').css('visibility','visible').stop(true,true).animate({'opacity':'1'},500);	
	$('ul.mainList').stop(true,true).fadeIn(500);
	hideGalSelect();
	if($('.galMode').css('left') == '800px') {
		$('.expcol').click();
	};
	setupButtonDisabler();
}

function hideGalSelect() {
	if( $('#galMenu').is(':visible') ) {
		$('.galSelect').click();
	}
};

function contentnav() {

	function navToggle1() {
		$('ul.contentNav li ul:visible').each(function(){
			$(this).parent().children('a').click();
		});
		$(this).parent().children('ul').slideDown(250);
		$(this).parent().addClass('active');
	}
	
	function navToggle2() {
		$(this).parent().children('ul').slideUp(250,function(){$(this).css('display','none')});
		$(this).parent().removeClass('active');
	}
	
	$('ul.contentNav li').each(function(){
		if($(this).children('ul').size() != 0) {
			$(this).children('a').toggle(navToggle1,navToggle2);
		}
	});
	
	function navToggle3() {
		$('ul.bottomNav li ul:visible').each(function(){
			$(this).parent().children('a').click();
		});
		$(this).parent().children('ul').slideDown(250);
		$(this).parent().addClass('active');
	}
	
	function navToggle4() {
		$(this).parent().children('ul').slideUp(250,function(){$(this).css('display','none')});
		$(this).parent().removeClass('active');
	}
	
	$('ul.bottomNav li').each(function(){
		if($(this).children('ul').size() != 0) {
			$(this).children('a').toggle(navToggle3,navToggle4);
		}
	});

	$('ul.contentSliderContent').css('opacity','0'); // ARCHIVED NEWS -- HIDE AT FIRST
	$('.contentSliderTrigger').toggle(
		function(){
			var numChildren = $(this).next('.contentSliderContent').children('li').size();
			$(this).next('.contentSliderContent').slideDown(numChildren * 15);
		},
		function(){
			var numChildren = $(this).next('.contentSliderContent').children('li').size();
			$(this).next('.contentSliderContent').slideUp(numChildren * 15);
		}
	);
	
};

function thumbGallery() {

	function thumbClick(clickIndex) {
		$('.expGallery').click();
			loadGal('cvv',clickIndex);
		};

	var galHTML = $('.photoGallery').html();
	$(galHTML).filter('img').each(function(){
		// Do this to each image...
	});
	
	$('.thumb').each(function(){
		$(this).append('<div class="galPlus"></div>');
	});
	
	$('.thumb').click(function(){
		thumbClick( $(this).index('.thumb') );
	});
	
	$('div.vtdesc').each(function(){
		var descHeight = $(this).outerHeight();
		
		$(this).parent('div').children('.vtPlusSign').css({
			
			'position' : 'absolute',
			'right' : '2px',
			'bottom' : '2px'
		});
		
		if(descHeight > 30) {
			descHeight = 27;
		}
		
		$(this).css({
			'margin-top' : -1 * descHeight + 'px'
		});
	
	});

};

function extNavGallery(navVar) {
	extNavGallerySelect = navVar;
	$('.expGallery').click();
}

function modalHeights(){
		
	$('.modalWindow').css({
		'left': $('.middle').offset().left + 'px',
		'height' : $('.middle').outerHeight() + 70 + 'px'
	})
	
	$('.modalContent').css({
		'height' : $('.middle').outerHeight() + 30 + 'px'
	})
	
	$('.modalContainer').css({
		'height' : $('.top').outerHeight() + $('.middle').outerHeight() + $('.bottom').outerHeight() + 110 + 'px'
	})
	
}

function modal() {

	function modalClick(urlToNav,linkClass){
		
		var articleURL = urlToNav;
		
		$.ajax({
			url: articleURL,
			success: function(data) {
		
				// This is with the Arrows
				$('body').append('<div class="modalContainer"></div><div class="modalWindow"><ul class="topheader"><li class="close"><a href="#">&nbsp;</a></li><li class="next"><a href="#">&nbsp;</a></li><li class="prev"><a href="#">&nbsp;</a></li><li class="logo"></li></ul><div class="modalContent">' + data + '</div></div>');


				$('.modalContainer').fadeIn(250,function(){
					
					setTimeout(adjustAll,50);
					setModalBehaviors(articleURL);
					
					if($('.modalContent .caption') && $('.modalContent .label')) {
						$('.modalContent .caption').insertAfter($('.modalContent .label'));
						$('.modalContent img').insertAfter($('.modalContent .label'));
					}
				
					$('.modalWindow').fadeIn(250,function(){
						
						setTimeout(adjustAll,50);
						
						$('.modalContainer').click(function(){closeModal();});
						
						$('ul.topheader li.close').click(function(){closeModal();});
						
						$('a.newModal').unbind('click').click(
							function(e){
								e.preventDefault();
								modalClick($(this).attr("href"));
							}
						);
										
					});
				});
				
			}
		});

	}

	function closeModal(){
		$('.modalContainer').fadeOut(250,function(){$(this).remove();});
		$('.modalWindow').fadeOut(250,function(){$(this).remove();});
	}

	$('.modalTrigger').click(function(){
		var activeModal = $('#modalWindow' + $(this).attr('id').slice(-1));
		$('.modalContainer').fadeIn(250,function(){
			activeModal.fadeIn(250);
		});
	});
	
	//$('.newsList li a').each(function(){$(this).addClass('newModal');});
	//$('.landscape a, .landscapeL a').each(function(){$(this).addClass('newModal');});

	$('a.newModal').click(
		function(e){
			e.preventDefault();
			var linkClass = $(this).attr('class');
			modalClick($(this).attr("href"),linkClass);
		}
	);
	
	function setModalBehaviors(newClick) {
	
		$('ul.topheader li.prev').click(function(){
			$('#status').html(newClick);
		});

		$('ul.topheader li.next').click(function(){
			$('#status').html(newClick);
		});

	}
}

function testBrowser() {
	var userAgent = navigator.userAgent.toLowerCase();
	$.browser = {
		version: (userAgent.match( /.+(?:rv|it|ra|ie|me)[\/: ]([\d.]+)/ ) || [])[1],
		chrome: /chrome/.test( userAgent ),
		safari: /webkit/.test( userAgent ) && !/chrome/.test( userAgent ),
		opera: /opera/.test( userAgent ),
		msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),
		mozilla: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent )
	};
	
	// to test:
	// if($.browser.chrome) { alert('You're using chrome'); };
	// if($.browser.safari) { alert('You're using safari'); };
	// if($.browser.opera) { alert('You're using opera'); };
	// if($.browser.msie) { alert('You're using IE, you tool'); };
	// if($.browser.mozilla) { alert('You're using firefox'); };
	
};

function fancyBox() {

	$(".fancy a").fancybox({
		'padding'  	: '0',
		'margin'  	: '0',
		'overlayOpacity' : '.8',
		'overlayColor'	: 'black',
		'transitionIn'  : 'elastic',
		'transitionOut' : 'none',
		'titlePosition' : 'over',
		'titleFormat'   : function(title, currentArray, currentIndex, currentOpts) {
                        //return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
                        return '<span id="fancybox-title-over">' + (title.length ? ' &nbsp; ' + title : '') + '</span>';
                }
        });

}

function adjustAll() {
	adjustSlide();
	adjustHeights();
	modalHeights();
}

$(document).ready(function() {
	adjustHeights();
	topnav();
	contentnav();
	testBrowser();
	modal();
	modalHeights();
	
// iOS Hover Event Class Fix
if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
$('ul.mainList li').click(function(){ 
				var testNews = $(this).children('a').attr('id');

				if (testNews == 'news') { 
					$(this).children('ul.contentNav, #newComContainer').slideDown(250);
				} else {
					$(this).children('ul.contentNav').slideDown(250);
				}
			// REMOVED SO ALWAYS OPENED
			//}
		},
		function(){
			$(this).children('ul.contentNav,#newComContainer').hide();
		});
}


	setTimeout(function(){$('.galMode').css('overflow','visible')},500);
	if( $('.photoGallery').size() != 0 || $('vtGallery').size != 0){
		thumbGallery();
	};
	
	$('a').click(function(){
		setTimeout(adjustAll,150);
	});
	if($('#30years')) {
		$('#30years').children('li').children('a').each(function(){
			$(this).children('.article').insertAfter($(this).children('.date'));
		});
	};
});

$(window).resize(function() {
	adjustAll();
});

