jQuery(document).ready(function() {
	
	jQuery(window).load(function() {

		jQuery('#portfolio').each(function(){
			
			var portnav = jQuery('#portfolio_navigation');
			
			portnav.css({'display':'none','opacity':'0'});
			
			jQuery('#header').mouseenter(function() {
				var timeout = $(this).data("timeout");
				if(timeout) clearTimeout(timeout);
				portnav.css('display','block').stop().fadeTo('slow', 1);
			}).mouseleave(function() {
				jQuery(this).data("timeout", setTimeout($.proxy(function() {
					portnav.stop().fadeTo('slow', 0);
				}, this), 2000));
			});
			
			jQuery('html, body').css({'width':'100%','overflow':'hidden'});
			jQuery('#header').append('<a id="prev" href="javascript:void(0);">Previous Image</a><a id="next" href="javascript:void(0);">Next Image</a>');
			jQuery('#gallery img').css({'cursor':'pointer'});
			
			$speed	= 1500;
			$ease	= 'easeInOutCubic';
				
			gallery_center();
			gallery_click_slide();
			
			jQuery(window).live('resize', function() {
				gallery_center()
				gallery_click_slide();
			});
			
			function gallery_center() {
				var $left = jQuery('#gallery .active img').position().left;
				var $offset = Math.round(( jQuery(window).width() - jQuery('#gallery .active img').width() ) / 2);
				var $position = -$left+$offset;
				//var $position = $position > 0 ? 0 : $position;
				jQuery('#gallery').css({ left: $position });
			};
			
			function gallery_size() {
				var $total_image_width = 0;
				jQuery('#gallery img').each(function() {
					$total_image_width += jQuery(this).width();
				});
				$total_image_width += 500;
				jQuery('#gallery').width($total_image_width);
			};
			
			
			function gallery_click_slide() {
				
				jQuery('#gallery li').live('click', function(){
					gallery_slide('next', 'false', this, 'self');
					return false;
				});
			};
			
			jQuery('#prev').live('click', function(){
				gallery_slide('prev', 'false', this, 'other');
				return false;
			});
			jQuery('#next').live('click', function(){
				gallery_slide('next', 'false', this, 'other');
				return false;
			});
			jQuery(document).live('keydown', function(e){
				if (e.keyCode == 37 || e.keyCode == 38) { 
					gallery_slide('prev', 'false', this, 'other');
					return false;
				};
				if (e.keyCode == 39 || e.keyCode == 40) { 
					gallery_slide('next', 'false', this, 'other');
					return false;
				};
			});
			jQuery('body').live('mousewheel', function(event, delta) {
				if (delta > 0) {
					gallery_slide('prev', 'true', this, 'other');
				} else {
					gallery_slide('next', 'true', this, 'other');
				};
				return false;
			});
		
			function gallery_slide(direction, mouse_wheel, selector, location) {
				
				$active_class = 'active'; 
				
				if (location == 'self') {
					
					if(!jQuery(selector).hasClass($active_class)) {
						var $left = jQuery(selector).position().left;
						var $offset = Math.round(( jQuery(window).width() - jQuery(selector).width() ) / 2);
						
						gallery_animate();
						
						if(!jQuery(selector).hasClass($active_class)){
							jQuery('#gallery li').removeClass($active_class);
							jQuery(selector).addClass($active_class);
						};
						
						gallery_active_animate();
						
					} else {
						gallery_go();
					};	
					
				} else {
					gallery_go();
				};
				
				function gallery_go() {
				
					if(direction == 'prev') {
						var $check_location = 'first';
						var $move_location = 'last';
					};
					if(direction == 'next') {
						var $check_location = 'last';
						var $move_location = 'first';
					};
					
					if(!jQuery('#gallery .active').hasClass($check_location)) {
						
						if(direction == 'prev') {
							$left = jQuery('#gallery .'+$active_class).prev().position().left;
							$offset = Math.round(( jQuery(window).width() - jQuery('#gallery .'+$active_class).prev().width() ) / 2);
						};
						if(direction == 'next') {
							$left = jQuery('#gallery .'+$active_class).next().position().left;
							$offset = Math.round(( jQuery(window).width() - jQuery('#gallery .'+$active_class).next().width() ) / 2);
						};
						
						gallery_animate();
						
						jQuery('#gallery .'+$active_class).addClass('remove');
						jQuery('#gallery li').removeClass($active_class);
						if(direction == 'prev') {
							jQuery('#gallery .remove').prev().addClass($active_class);
						};
						if(direction == 'next') {
							jQuery('#gallery .remove').next().addClass($active_class);
						};
						jQuery('#gallery li').removeClass('remove');
						
						gallery_active_animate();
						
					} else if(mouse_wheel !== 'true') {
					
						$left = jQuery('#gallery .'+$move_location).position().left;
						$offset = Math.round(( jQuery(window).width() - jQuery('#gallery .'+$move_location).width() ) / 2);
						
						gallery_animate();
						
						jQuery('#gallery li').removeClass($active_class);
						jQuery('#gallery .'+$move_location).addClass($active_class);
						
						gallery_active_animate();
					};
				};
				
				function gallery_animate() {
					//jQuery('#gallery li').stop().fadeTo($speed, .05);
				};
				
				function gallery_active_animate() {
					//jQuery('#gallery .active').stop().fadeTo($speed, 1);
					var $position = -$left+$offset;
					//var $position = $position > 0 ? 0 : $position;
					jQuery('#gallery').stop().animate({ left: $position }, $speed, $ease);
				};
				
			};
			
		});
	});
});
