//Casey Brooks Photography
//Site by Matt Coleman for Drexler (drxlr.com)

jQuery(function ($) {

	//Nav Fades
		$('h1 a, a.contact, a.email, a.bio, a.blog').hover( function() {
			$(this).fadeTo(100,.5);
		}, function() {
			$(this).fadeTo(200, 1);			
		});
		
		$('#story-nav li').fadeTo(0, .3);
		$('#story-nav li').hover( function() {
			$(this).fadeTo(50, 1);
		}, function() {
			$(this).fadeTo(150, .3);			
		});

	//Hide all imgs with title of thumb
		$("img.attachment-full[title*='thumb']").parent("li").remove();
		$("img.attachment-full[title*='Thumb']").parent("li").remove();

	// Set Initial Image Size
	    var viewportWidth = $(window).width();
		var viewportHeight = $(window).height(); 
		
		$('#story img').hide();
		//var imgHeight = (viewportHeight - 120);
		//$('#story img').height(imgHeight).width("auto");
		$('#story img').fadeIn();
	
	//Center Vertically
		$(window).resize(function(){
		  $('#content').css({
		   position:'absolute',
		   //left: ($(window).width() 
		   //  - $('.className').outerWidth())/2,
		   top: ($(window).height() 
		     - $('#content').outerHeight())/2
		  });		
		 });
		 // To initially run the function:
		 $(window).resize();
	
	
	//Thumb FX
		$('.thumbs li.active').fadeTo(0, .2);
		$('.thumbs li').not('.cat').hover( function() {
			$(this).fadeTo(50, .4);
			}, function() {
			$(this).fadeTo(150, 1);			
		});	
		
	//Prevent Image Tooltips from showing
		$('#story img').attr('title','');
		$('#story img').children().attr('title','');
		$('.attachment-post-thumbnail').attr('title','');
		$('.attachment-post-thumbnail').children().attr('title','');

	// We'll set the overflow here, so the site still works without JS
		$('#story').css({'width' : '9999px', 'overflow-x' : 'visible'});

	//Magical Resizing
		//$(window).resize(function(){
		   // var viewportWidth = $(window).width();
		   // var viewportHeight = $(window).height();

	//Dynamic Resize on Browser Height Change
	// Subtract 120 to make it fit real nice
		//var imgHeight = (viewportHeight - 120);
		//$('#story img').height(imgHeight).width("auto");
		//});



	//Let's make these suckas slide
				
				// Vars
    			var activeImg = 0;
				var TotalImgs = $('#story li').size();
				var storyWrap = $('#story-wrap');
				var crntImg = $('#current-img');
				
				//$('#story li').not('li:eq(0)').fadeTo(0, .2);
				
				//Hide Nav if only 1 image
				if (TotalImgs < 3) {
					$('#story-nav').hide();
				}
				
				//Setup Selectors
				var _Prev = $(".prev a");
				var _Next = $(".next a");
				
				
			    // IMAGE SLIDING
			    //NEXT --------------------------------
			    function goRight() {
			        if (activeImg < TotalImgs) {
			            var crntSlide = activeImg;
			            crntSlide++;
			            $(storyWrap).stop().scrollTo('#story li:eq('+crntSlide+')', 200, {offset: 0, axis: 'x', easing:'easeOutExpo', onAfter:function(){ 
			            // Callback 
			            activeImg = crntSlide;
						//$(crntImg).html(crntSlide+1);
			                }
			            });
			        } 
			        
			        if (activeImg == TotalImgs-1) {
			            //$('.next_hidr').hide();
			        }
			        
			    }

				//PREV --------------------------------
			    function goLeft() {
			        if (TotalImgs > 0) {
			            var crntSlide = activeImg;
			            crntSlide--;
			            $(storyWrap).stop().scrollTo('#story li:eq('+crntSlide+')', 200, {offset: 0, axis: 'x', easing:'easeOutExpo', onAfter:function(){ 
			            // Callback 
			            activeImg = crntSlide;
			            //$(crntImg).html(crntSlide+1);
			                }
						});   
					}
			    }

	    		//Call function on Click
	    		
		    		$(_Next).bind('click', function () { goRight(); return false; });
		    		$(_Prev).bind('click', function () { goLeft(); return false; });

				//Key Nav
					$(document.documentElement).keydown(function (event) {
					    var direction = null;
					    if (event.keyCode == 37) {
					       goLeft();
					    } else if (event.keyCode == 39) {
						   goRight();
					    }
					    if (direction != null) {
					      $(_Next).click();
					    }			    
					  });

			   // Image Clicks
			    $("#story li").click(function(){
			    // If we're at the last image, go back to the beginning
			    if (activeImg == TotalImgs-1) {$(storyWrap).scrollTo('#story li:eq(0)', 300, {offset: 0, axis: 'x', easing:'easeOutExpo', onAfter:function(){ 
			            // Callback 
			            crntSlide = 0;
			            activeImg = crntSlide;
			            $(crntImg).html(crntSlide+1);			                    
			        }
			        });
			    
			    } else {
			    	//Find which image was clicked.
			    	//We add 1, to compensate for index of 0
			        var clickedImg = $(this).index();
			        clickedImg = clickedImg++;
			        $(storyWrap).scrollTo('#story li:eq('+clickedImg+')', 300, {easing:'easeOutExpo', offset: 0, axis: 'x', onAfter:function(){ 
			            // Callback
			            activeImg = clickedImg;
			             $(crntImg).html(clickedImg+1);
			        	}
			        }); 
			    } // Else
			}); // End Image Click function


			    $('#story').stop(true,false).mousewheel( function(event, delta) {
			            //var dir = delta > 0 ? 'Up' : 'Down',
			            //    vel = Math.abs(delta);
			            
			            if ( delta < 0 ) { goRight(); } 
			            else { goLeft(); }
			            event.preventDefault();
			        });


//And...done.

});
