$(document).ready(function(){
	
	check_width();
	$(window).resize(check_width);
		   
	$('.title').hide();
	
	//append .overlays
	$('.thumbnails li').append('<div class="overlay" style="background-color:black;"></div>').find('img').css('opacity',.6);
	
	var destination = '';
	
	// now do everything from perspective of .overlay
	$('.overlay').css('opacity',0).hover(function() { // on hover
			destination = $(this).parent().find('a').attr('href');
			$(this).addClass('hover');
			window.status= "Go to " + destination;
			
			var $this_img = $(this).parent().find('img');
			var offset = $this_img.offset();
	
			$this_img.parent().css({
				'z-index': 1000
			});
			if( ((offset.top+400) > $(window).height()) && $this_img.parent().hasClass('anch-right') ){
				$this_img.css({
					'position': 'absolute',
					'right':0,
					'bottom':0,
					'z-index': 1000
				}).stop().animate({
					width:600,
					height:400,
					opacity:1
				},function(){
					$this_img.parent().find('.title').fadeIn('fast'); // when done animating, fadeIn title
				});//end animate and callback function
			}else if ((offset.top+400) > $(window).height()){
				$this_img.css({
					'position': 'absolute',
					'left':0,
					'bottom':0,
					'z-index': 1000
				}).stop().animate({
					width:600,
					height:400,
					opacity:1
				},function(){
					$this_img.parent().find('.title').fadeIn('fast'); // when done animating, fadeIn title
				});//end animate and callback function
			} else{
				$this_img.css({
					'z-index': 1000
				}).stop().animate({
					width:600,
					height:400,
					opacity:1
				},function(){
					$this_img.parent().find('.title').fadeIn('fast'); // when done animating, fadeIn title
				});//end animate and callback function
			}
			
		}, function() { //on un-hover
			$(this).removeClass('hover').prev().stop().animate({
				width:180,
				height:120,
				opacity:.4
			},function(){
				$(this).css('z-index',1); // return img to 1 z-index
				$(this).parent().css('z-index',0); // return li to 0 z-index
			}).parent().find('.title').fadeOut(); // fade out title while animating
		}
	)//end hover
	.click(function(){
		window.location = destination;
	});//end click
	
	//$('.overlay:first').trigger('mouseover'); // for testing!

	$('#logo a').hover(function(){
		$(this).data('current',$(this).css('background-color')).stop().animate({ backgroundColor: "#d48036" }, "fast")
	},function(){
		$(this).stop().animate({backgroundColor: $(this).data('current') }, "fast")
	});
		
	$('.info-panel').addClass('collapsed');  
	$('.info-panel h2').click(function(){
		var panel = $(this).parent();
		if (panel.hasClass('collapsed')){
			$(this).next().slideDown().end().parent().removeClass('collapsed');
			panel.siblings('div.info-panel').not('.collapsed').addClass('collapsed').find('.panel-content').slideUp();
		}else{
			$(this).next().slideUp().end().parent().addClass('collapsed');
		}
		
	}).wrapInner('<a href="#"></a>').children().bind('mouseover focus',function(){
			$(this).addClass('hover');
		}).bind('mouseout blur',function(){
			$(this).removeClass('hover');
	});
	
	$('label').addClass('pos-abs');
	$('input,textarea').focus(function(){
			$(this).prev('label').addClass('neg-offset')
	}).blur(function(){
		if(!$(this).val()){
			$(this).prev('label').removeClass('neg-offset');
		}
	});
	
});

var check_width = function(){
			
	if( $(window).width() < 1258){
		$('.thumbnails li').removeClass('anch-right');
		$('.thumbnails li:nth-child(3n)').addClass('anch-right'); // add .anch-right to 3rd/last thumb
		$('body').removeClass('wide').css('min-width','1000px'); //layout adds up to 990
	}else{ // if width is greater than 1258
		$('.thumbnails li').removeClass('anch-right');
		$('.thumbnails li:nth-child(4n-1), .thumbnails li:nth-child(4n)').addClass('anch-right');//add .anch-right to last two thumb li
		$('body').addClass('wide').css('min-width','1200px');//layout adds up to 1190
	}
};
