(function($){
	
	/*--------------------------------------------------------------------------------------------
		Vars
	--------------------------------------------------------------------------------------------*/

	var wrapper;
	var col_side;
	var col_main;
	var fade_speed = 500;
	var thumb_speed = 300;
	var nav_drop_speed = 500;
	
	
	/*--------------------------------------------------------------------------------------------
		hasScrollBar
	--------------------------------------------------------------------------------------------*/
	$.fn.hasScrollBar = function() {
        return this.get(0).scrollHeight > this.height();
    }
    
    
	/*--------------------------------------------------------------------------------------------
		Setup Gallery
	--------------------------------------------------------------------------------------------*/
	function setup_gallery()
	{
		$('#gallery').each(function(){
		
			var gallery = $(this);
			var mask = gallery.children('.mask');
			var photos = mask.children('.photos');
			var photo = photos.children('.photo');
			var thumbs = mask.children('.thumbs');
			var controls = gallery.children('.controls');
			
			
			// thumbs need to be hidden
			thumbs.css({
				'display'	:	'block'
			});
			
			thumbs.css({
				'margin-bottom'	:	- thumbs.height()
			});
				
				
			// setup gallery width / height
			$(window).resize(function(){
				
				gallery.css({
					'width'		:	col_main.width(),
					'height'	:	$(window).height() - 100 - 40
				});
				
				
				if(thumbs.children('ul').height() > gallery.height())
				{
					thumbs.css({
						'height'	:	gallery.height()
					});
				}
				
				
				
				if(!thumbs.hasClass('active'))
				{
					thumbs.css({
						'margin-bottom'	:	- thumbs.height()
					});
				}
				
				
				photos.find('img').each(function(){
					
					var img = $(this);
					
					// asume image is portrait
					img.css({
						'width'		:	'auto',
						'height'	:	col_main.height()
					});
					
					
					// if image is wider than col_main, then the image is landscape
					if(img.width() > col_main.width())
					{
						// image is landscape
						img.css({
							'width'		:	col_main.width(),
							'height'	:	'auto'
						});
					}
					
				});

			
			}).trigger('resize');
			
			
			// thumb a click
			thumbs.find('a').click(function(){
				
				var rel = $(this).parent('li').attr('rel');
				var href = $(this).attr('href');
				
				change_slide(rel, href);
				
				return false;
			});
			
			
			// trigger first image link
			thumbs.find('a').first().trigger('click');
			
			
			// controls
			controls.find('li.thumbnails a').click(function(){
				
				if(thumbs.hasClass('active'))
				{
					hide_thumbs();
				}
				else
				{
					show_thumbs();
				}

			});
			
			function show_thumbs()
			{
				thumbs.animate({'margin-bottom' : 0}, thumb_speed);
				thumbs.addClass('active');
			}
			
			function hide_thumbs()
			{
				thumbs.animate({'margin-bottom' : -thumbs.height()}, thumb_speed);
				thumbs.removeClass('active');
			}
			
			
			// controls: prev
			controls.find('li.prev a').click(function(){
				
				var prev = thumbs.find('li.active').prev().length != 0 ? thumbs.find('li.active').prev() : false;
				
				if(prev)
				{
					prev.children('a').trigger('click');
				}
			});
			
			
			// controls: next
			controls.find('li.next a').click(function(){
				
				var next = thumbs.find('li.active').next().length != 0 ? thumbs.find('li.active').next() : false;
				
				if(next)
				{
					next.children('a').trigger('click');
				}
			});
			
			
			function change_slide(rel, href)
			{	
				
				thumbs.find('li.active').removeClass('active');
				thumbs.find('li[rel="'+rel+'"]').addClass('active');
				
				
				// always hide the thumbs
				hide_thumbs();
				
				
				// set prev / next
				var next = thumbs.find('li[rel="'+rel+'"]').next().length != 0 ? thumbs.find('li[rel="'+rel+'"]').next() : false;

				if(!next)
				{
					controls.find('li.next').addClass('disabled').animate({'opacity':0.2}, 200);
				}
				else
				{
					controls.find('li.next').removeClass('disabled').animate({'opacity':1}, 200);
				}
				
				
				// set prev / next
				var prev = thumbs.find('li[rel="'+rel+'"]').prev().length != 0 ? thumbs.find('li[rel="'+rel+'"]').prev() : false;
				
				if(!prev)
				{
					controls.find('li.prev').addClass('disabled').animate({'opacity':0.2}, 200);
				}
				else
				{
					controls.find('li.prev').removeClass('disabled').animate({'opacity':1}, 200);
				}
				
				
				// get active photo (if it exists)
				var active = photos.find('.photo.active').length != 0 ? photos.find('.photo.active') : false;
				
				
				// fade out current photo
				if(active)
				{
					active.removeClass('active').animate({'opacity' : 0}, fade_speed);
				}
				
				
				// create photo if it doesn't exist
				if(photos.find('.photo[rel="'+rel+'"]').length == 0)
				{
					gallery.addClass('loading');
					
					var div = $('<div class="photo active" rel="'+rel+'"></div>');
					var img = new Image();
					
					$(img).load(function(){
						
						div.append(this);
						photos.append(div);
						gallery.removeClass('loading');
						div.delay(fade_speed).animate({'opacity' : 1}, fade_speed);
						
						// trigger window resize to resize the new image
						$(window).trigger('resize');
						
					}).attr('src', href);

				}
				else
				{
					// if there is no active photo (on page load), don't delay the loading in proccess
					if(active)
					{
						photos.find('.photo[rel="'+rel+'"]').addClass('active').delay(fade_speed).animate({'opacity' : 1}, fade_speed);
					}
					else
					{
						photos.find('.photo[rel="'+rel+'"]').addClass('active').animate({'opacity' : 1}, fade_speed);
					} 
					
				}
				
			}
			
			
			var t;
			var cursor_y = 0;
			var scrolling = false;
			
			
			// scroll the thumbs depending on the mouse y position
			thumbs.mousemove(function(e){
				
				cursor_y = e.pageY - thumbs.offset().top;
				//console.log(cursor_y);
				//var pageCoords = "( " + e.pageX + ", " + e.pageY + " )";
				
				if(cursor_y < 120)
				{
					// scroll up
					//console.log('scroll up');
					if(!scrolling)
					{
						t = setInterval(function(){my_scroll_up();}, 25);
						scrolling = true;
					}
					
				}
				else if(cursor_y > thumbs.height() - 120)
				{
					// scroll up
					//console.log('scroll down');
					if(!scrolling)
					{
						t = setInterval(function(){my_scroll_down();}, 25);
						scrolling = true;
					}
				}
				else
				{
					// in the middle!
					clearInterval(t);
					scrolling = false;
				}
				
			});
			
			
			/*thumbs.mouseout(function(){
				clearInterval(t);
				scrolling = false;
			});*/
			
			
			function my_scroll_up()
			{
				console.log('scrolling up!');
				var current = thumbs.scrollTop();
				
				thumbs.scrollTop(current - 10);
				
				/*if(cursor_y >= 240)
				{
					clearInterval(t);
					scrolling = false;
				}*/
			}
			
			function my_scroll_down()
			{
				console.log('scrolling down!');
				var current = thumbs.scrollTop();
				
				thumbs.scrollTop(current + 10);
				
				/*if(cursor_y <= thumbs.height() - 240)
				{
					clearInterval(t);
					scrolling = false;
				}*/
			}
			
		});
		
	}



	/*--------------------------------------------------------------------------------------------
		Setup Gallery
	--------------------------------------------------------------------------------------------*/
	function setup_col_widths()
	{
		
		$(window).resize(function(){
		
			col_main.css({
				'width'	:	wrapper.width() - col_side.outerWidth()
			});
		
		}).trigger('resize');
	}
	
	
	
	/*--------------------------------------------------------------------------------------------
		Setup Side Col
	--------------------------------------------------------------------------------------------*/
	function setup_side_cols()
	{
		col_side.children('ul').children('li').each(function(){
			
			var li = $(this);
			
			if(li.find('ul').length > 0)
			{
				li.find('ul').wrap('<div class="mask" />');
				li.children('a').click(function(){
					
					// temporarily remove right padding (if the body gets a scrollbar, it will push the main content down the page!!!)
					wrapper.css({'padding-right' : 0 });
					
					li.children('.mask').animate({'height' : 'toggle'}, nav_drop_speed, function(){
						
						// now put the padding back and re think the layout!
						wrapper.css({'padding-right' : '50px' });
						$(window).trigger('resize');
						
					});
					
					return false;
				});
			}
			
		});


	}
	
	
	
	/*--------------------------------------------------------------------------------------------
		setup_fancybox
	--------------------------------------------------------------------------------------------*/
	
	function setup_fancybox()
	{
		col_main.find('a').each(function(){
			
			if($(this).attr('href').indexOf('vimeo.com') > -1)
			{
				// we have a vimeo!
				$(this).click(function(){
		             $.fancybox({
		              'overlayColor'		: '#000',
		              'autoScale'   		: false,
		              'width'               : 680,
		              'height'              : 495,
		              'href'                : this.href.replace(new RegExp("([0-9])","i"),'moogaloop.swf?clip_id=$1'),
		              'type'                : 'swf',    // <--add a comma here
		              });
		             return false;
		
		        });
			}
			else if($(this).attr('href').indexOf('youtube.com') > -1)
			{
				// we have a youtube!
				$(this).click(function(){
		             $.fancybox({
		              'overlayColor'		: '#000',
		              'autoScale'   		: false,
		              'width'               : 680,
		              'height'              : 495,
		              'href'                : this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
		              'type'                : 'swf',    // <--add a comma here
		              'swf'                 : {'allowfullscreen':'true'} // <-- flashvars here
		              });
		             return false;
		
		        });
			}
			
			/*
			if($(this).attr('href'))
			
			// fancybox
			$("a.fancybox_video").click(function(){
	             $.fancybox({
	              'overlayColor'		: '#000',
	              'autoScale'   		: false,
	              'width'               : 680,
	              'height'              : 495,
	              'href'                : this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
	              'type'                : 'swf',    // <--add a comma here
	              'swf'                 : {'allowfullscreen':'true'} // <-- flashvars here
	              });
	             return false;
	
	        });*/
		
		});
		
	}
	
	
	
	/*--------------------------------------------------------------------------------------------
		Document Ready
	--------------------------------------------------------------------------------------------*/
		
	$(document).ready(function()
	{
		// setup vars
		wrapper = $('.wrapper');
		col_side = wrapper.children('.col_side');
		col_main = wrapper.children('.col_main');
		
		setup_col_widths();
		setup_gallery();
		setup_side_cols();
		setup_fancybox();
	});
	
	
	/*--------------------------------------------------------------------------------------------
		Window load
	--------------------------------------------------------------------------------------------*/
	$(window).load(function()
	{
		$(window).trigger('resize');
	});

	
	
})(jQuery);



